comment here

This commit is contained in:
2023-03-17 15:47:31 +07:00
parent 7060adacaf
commit abad8d502d

View File

@@ -204,7 +204,7 @@ end
""" """
function mqttReconnect!(client::mqttClient) function mqttReconnect!(client::mqttClient)
if client.connectStatus == false if client.connectStatus == false
Mosquitto.disconnect(client) # disconnect to release port before attemping new connection Mosquitto.disconnect(client.clientInstance) # disconnect to release port before attemping new connection
if client.userName === nothing # check if the member needs username/password if client.userName === nothing # check if the member needs username/password
result = Mosquitto.connect(client.clientInstance, client.broker, client.port) result = Mosquitto.connect(client.clientInstance, client.broker, client.port)
@@ -239,16 +239,26 @@ end
""" """
mqtt communication loop, call this function manually mqtt communication loop, call this function manually
""" """
function mqttRun(client::mqttClient) function mqttRun(client::mqttClient, userMsgHandler::Function)
mqttLoop!(client) mqttLoop!(client)
mqttCheckConnection!(client) mqttCheckConnection!(client)
mqttReconnect!(client) mqttReconnect!(client)
mqttSubOnConnect!(client) mqttSubOnConnect!(client)
mqttOnmessage!(client, userMsgHandler)
end end
# What to do if there is a message """
function mqttOnmessage!(client::mqttClient, msgHandler::Function) userMsgHandler. A user defined function in user space.
# Example
function mqttMsgHander(topic::String, payload)
# this function access workSpace by global variable and do something with msg
end
"""
function mqttOnmessage!(client::mqttClient, userMsgHandler::Function)
nmessages = Base.n_avail(get_messages_channel()) nmessages = Base.n_avail(get_messages_channel())
nmessages == 0 && return 0 nmessages == 0 && return 0
@@ -260,7 +270,7 @@ function mqttOnmessage!(client::mqttClient, msgHandler::Function)
_ = take!(get_messages_channel()) # discard new arrival package _ = take!(get_messages_channel()) # discard new arrival package
end end
elseif timepass > 1 && client.retainedMsgCleared == false elseif timepass > 1 && client.retainedMsgCleared == false
workDict[:retainedMsgCleared] = true client.retainedMsgCleared = true
println("retained mqtt messages cleared") println("retained mqtt messages cleared")
else else
for i = 1:nmessages for i = 1:nmessages
@@ -270,12 +280,14 @@ function mqttOnmessage!(client::mqttClient, msgHandler::Function)
payload = vecToDict(pkg.payload) # payload in Dict format payload = vecToDict(pkg.payload) # payload in Dict format
# Do something with the message # Do something with the message
msgHandler(topic, payload) println("call userMsgHandler")
userMsgHandler(topic, payload) # user defined function in user space. How to handle msg
end end
end end
end end
function publish(client::mqttClient, msg::Dict, pubtopic::String="") function publish(client::mqttClient, msg::Dict, pubtopic::String="")
Mosquitto.publish(client.clientInstance, pubtopic, JSON3.write(msg)) Mosquitto.publish(client.clientInstance, pubtopic, JSON3.write(msg))
end end
@@ -284,24 +296,6 @@ end
""" """
request function check msg for additional communication info in addition to publish() request function check msg for additional communication info in addition to publish()
""" """
# function request(client::mqttClient, msg::Dict; pubtopic::String="")
# if pubtopic != ""
# Mosquitto.publish(client.clientInstance, pubtopic, JSON3.write(msg))
# elseif msg[:msgMeta][:sendto] != ""
# Mosquitto.publish(client.clientInstance, msg[:msgMeta][:sendto], JSON3.write(msg))
# else
# if length(client.pubtopic) != 0
# for (k, topic) in pairs(client.pubtopic)
# println("pub to $k")
# Mosquitto.publish(client.clientInstance, topic, JSON3.write(msg))
# end
# else
# error("publish topic not defined")
# end
# end
# end
function request(client::mqttClient, msg::Dict; pubtopic::String="") function request(client::mqttClient, msg::Dict; pubtopic::String="")
pubto = Vector{String}() pubto = Vector{String}()
if pubtopic != "" if pubtopic != ""