@@ -13,7 +13,7 @@ proc handler() {.noconv.} =
1313 quit (0 )
1414
1515
16- proc nmqttSub (host= " 127.0.0.1" , port: int = 1883 , ssl:bool = false , clientid= " " , username= " " , password= " " , topic: string , qos= 0 , keepalive= 60 , willtopic= " " , willmsg= " " , willqos= 0 , willretain= false , verbose= false ) {.async .} =
16+ proc nmqttSub (host= " 127.0.0.1" , port: int = 1883 , ssl:bool = false , clientid= " " , username= " " , password= " " , topic: string , qos= 0 , keepalive= 60 , removeretained = false , willtopic= " " , willmsg= " " , willqos= 0 , willretain= false , verbose= false ) {.async .} =
1717 # # CLI tool for subscribe
1818 if clientid != " " :
1919 ctx.clientId = clientid
@@ -33,6 +33,10 @@ proc nmqttSub(host="127.0.0.1", port: int=1883, ssl:bool=false, clientid="", use
3333
3434 await ctx.start ()
3535
36+ # Remove retained messages
37+ if removeretained:
38+ waitFor ctx.publish (topic, " " , 0 , true )
39+
3640 proc on_data (topic, msg: string ) =
3741 echo topic, " : " , msg
3842
@@ -59,19 +63,20 @@ $options
5963 doc= " Subscribe to a topic on a MQTT-broker." ,
6064 cmdName= " nmqtt_sub" ,
6165 help= {
62- " host" : " IP-address of the broker. Defaults to 127.0.0.1" ,
63- " port" : " network port to connect too. Defaults to 1883." ,
64- " ssl" : " enable ssl. Auto-enabled on port 8883." ,
65- " clientid" : " your connection ID. Defaults to nmqtt_pub_ appended with processID." ,
66- " username" : " provide a username" ,
67- " password" : " provide a password" ,
68- " topic" : " MQTT topic to publish to." ,
69- " qos" : " quality of service level to use for all messages. Defaults to 0." ,
70- " keepalive" :" keep alive in seconds for this client. Defaults to 60." ,
71- " willtopic" :" set the will's topic" ,
72- " willmsg" : " set the will's message" ,
73- " willqos" : " set the will's quality of service" ,
74- " willretain" :" set to retain the will message"
66+ " host" : " IP-address of the broker. Defaults to 127.0.0.1" ,
67+ " port" : " network port to connect too. Defaults to 1883." ,
68+ " ssl" : " enable ssl. Auto-enabled on port 8883." ,
69+ " clientid" : " your connection ID. Defaults to nmqtt_pub_ appended with processID." ,
70+ " username" : " provide a username" ,
71+ " password" : " provide a password" ,
72+ " topic" : " MQTT topic to publish to." ,
73+ " qos" : " quality of service level to use for all messages. Defaults to 0." ,
74+ " keepalive" : " keep alive in seconds for this client. Defaults to 60." ,
75+ " removeretained" : " clear any retained messages on the topic" ,
76+ " willtopic" : " set the will's topic" ,
77+ " willmsg" : " set the will's message" ,
78+ " willqos" : " set the will's quality of service" ,
79+ " willretain" : " set to retain the will message"
7580 },
7681 short= {
7782 " password" : 'P' ,
@@ -80,7 +85,8 @@ $options
8085 " willtopic" : '\0 ' ,
8186 " willmsg" : '\0 ' ,
8287 " willqos" : '\0 ' ,
83- " willretain" : '\0 '
88+ " willretain" : '\0 ' ,
89+ " removeretained" : '\0 '
8490 },
8591 usage= topLvlUse
8692 )
0 commit comments