use channel
This commit is contained in:
@@ -241,12 +241,12 @@ end
|
|||||||
"""
|
"""
|
||||||
mqtt communication loop, call this function manually
|
mqtt communication loop, call this function manually
|
||||||
"""
|
"""
|
||||||
function mqttRun(client::mqttClient)
|
function mqttRun(client::mqttClient, storeResult::Channel)
|
||||||
mqttLoop!(client)
|
mqttLoop!(client)
|
||||||
mqttCheckConnection!(client)
|
mqttCheckConnection!(client)
|
||||||
mqttReconnect!(client)
|
mqttReconnect!(client)
|
||||||
mqttSubOnConnect!(client)
|
mqttSubOnConnect!(client)
|
||||||
return mqttOnmessage!(client)
|
mqttOnmessage!(client, storeResult)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -260,28 +260,26 @@ end
|
|||||||
end
|
end
|
||||||
|
|
||||||
"""
|
"""
|
||||||
function mqttOnmessage!(client::mqttClient)
|
function mqttOnmessage!(client::mqttClient, storeResult::Channel)
|
||||||
nmessages = Base.n_avail(get_messages_channel())
|
nmessages = Base.n_avail(get_messages_channel())
|
||||||
nmessages == 0 && return nothing
|
nmessages == 0 && return nothing
|
||||||
|
|
||||||
timepass = (Dates.now() - client.boottime).value / 1000.0
|
if client.retainedMsgCleared == false
|
||||||
|
|
||||||
if timepass <= 1 && client.retainedMsgCleared == false
|
|
||||||
# discard all retained message
|
# discard all retained message
|
||||||
for i = 1:nmessages
|
for i = 1:nmessages
|
||||||
_ = take!(get_messages_channel()) # discard new arrival package
|
_ = take!(get_messages_channel()) # discard new arrival package
|
||||||
end
|
end
|
||||||
elseif timepass > 1 && client.retainedMsgCleared == false
|
|
||||||
client.retainedMsgCleared = true
|
client.retainedMsgCleared = true
|
||||||
println("retained mqtt messages cleared")
|
println("retained mqtt messages cleared")
|
||||||
else
|
elseif client.retainedMsgCleared
|
||||||
for i = 1:nmessages
|
for i = 1:nmessages
|
||||||
pkg = take!(get_messages_channel()) # take new arrival package
|
pkg = take!(get_messages_channel()) # take new arrival package
|
||||||
# sptoptic = split(topic, "/")
|
# sptoptic = split(topic, "/")
|
||||||
payload = vecToDict(pkg.payload) # payload in Dict format
|
payload = vecToDict(pkg.payload) # payload in Dict format
|
||||||
|
put!(storeResult, (topic=pkg.topic, payload=payload))
|
||||||
return pkg.topic, payload
|
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
error("undefined condition")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user