From 42378714a048e1e75f40742e07976f0cb72ce0ec Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Tue, 10 Jun 2025 09:31:00 +0700 Subject: [PATCH 1/4] mark new version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 09b7cf2..df3a90b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "GeneralUtils" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" authors = ["tonaerospace "] -version = "0.2.4" +version = "0.3.0" [deps] CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" From 93aa0ee1ac9433a6a8883b3829476e7a6bf3c93f Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Tue, 10 Jun 2025 10:16:31 +0700 Subject: [PATCH 2/4] update --- src/communication.jl | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/communication.jl b/src/communication.jl index 6c56468..cf69b98 100644 --- a/src/communication.jl +++ b/src/communication.jl @@ -154,7 +154,7 @@ function mqttClientInstance_v2( keepaliveChannel::Channel, # used for checkMqttConnection(). user needs to specify because it has to be accessible by user-defined onMsgCallback() onMsgCallback::Function ; - clientId::Union{String,Nothing}=nothing, + clientId::String="NA", mqttBrokerPort::Integer=1883, keepaliveTopic::String= "/keepalive/$(uuid4snakecase())", keepaliveCheckInterval::Integer=30, @@ -170,7 +170,7 @@ function mqttClientInstance_v2( end MQTTClient.subscribe(client, keepaliveTopic, onMsgCallback, qos=qos) - keepaliveTopic = clientId === nothing ? keepaliveTopic : "/keepalive/$clientId" + keepaliveTopic = clientId == "NA" ? keepaliveTopic : "/keepalive/$clientId" instance = mqttClientInstance_v2( mqttBrokerAddress, @@ -416,7 +416,7 @@ end ----- """ function checkMqttConnection!(mqttInstance::T; - keepaliveCheckInterval::Union{Integer, Nothing}=nothing)::Bool where {T<:mqttClientInstance} + keepaliveCheckInterval::Union{Integer, Nothing}=nothing)::Union{Bool, Nothing} where {T<:mqttClientInstance} interval = keepaliveCheckInterval !== nothing ? keepaliveCheckInterval : mqttInstance.keepaliveCheckInterval @@ -429,6 +429,7 @@ function checkMqttConnection!(mqttInstance::T; end if intervaldiff > interval + connectionStatusStart = isMqttConnectionAlive(mqttInstance) # a flag to note whether the connection status has changed from false to true while true mqttConnStatus = isMqttConnectionAlive(mqttInstance) if mqttConnStatus == false @@ -438,16 +439,20 @@ function checkMqttConnection!(mqttInstance::T; mqttInstance.client, mqttInstance.connection = MakeConnection(mqttInstance.mqttBrokerAddress, mqttInstance.mqttBrokerPort) - connect(mqttInstance.client, mqttInstance.connection) - for topic in mqttInstance.subtopic - subscribe(mqttInstance.client, topic, mqttInstance.onMsgCallback, qos=mqttInstance.qos) + try + connect(mqttInstance.client, mqttInstance.connection) + for topic in mqttInstance.subtopic + subscribe(mqttInstance.client, topic, mqttInstance.onMsgCallback, qos=mqttInstance.qos) + end + MQTTClient.subscribe(mqttInstance.client, mqttInstance.keepalivetopic, mqttInstance.onMsgCallback, qos=mqttInstance.qos) + catch + println("Failed to reconnect MQTT broker at $(mqttInstance.mqttBrokerAddress):$(mqttInstance.mqttBrokerPort) $(Dates.now())") end - MQTTClient.subscribe(mqttInstance.client, mqttInstance.keepalivetopic, mqttInstance.onMsgCallback, qos=mqttInstance.qos) - - else mqttInstance.lastTimeMqttConnCheck = Dates.now() - println("Reconnected to MQTT broker at $(mqttInstance.mqttBrokerAddress):$(mqttInstance.mqttBrokerPort)") + if connectionStatusStart != mqttConnStatus + println("Reconnected to MQTT broker at $(mqttInstance.mqttBrokerAddress):$(mqttInstance.mqttBrokerPort) $(Dates.now())") + end return mqttConnStatus end end From fc3edd7b8f5ba7d4e405b1932c92747e6848e568 Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Tue, 10 Jun 2025 10:29:57 +0700 Subject: [PATCH 3/4] update --- src/communication.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/communication.jl b/src/communication.jl index cf69b98..a4e41f1 100644 --- a/src/communication.jl +++ b/src/communication.jl @@ -589,7 +589,6 @@ function sendReceiveMqttMsg(outgoingMsg::Dict{Symbol, T}; put!(mqttMsgReceiveChannel[:ch1], incomingMqttMsg) elseif occursin("keepalive", topic) put!(keepaliveChannel, incomingMqttMsg) - println("keepalive received ", incomingMqttMsg) else println("undefined condition ", @__FILE__, " ", @__LINE__) end From 5b4c1c1471d29e755b5d745ff17cf4d66183e37e Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Tue, 10 Jun 2025 10:38:51 +0700 Subject: [PATCH 4/4] update --- src/communication.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/communication.jl b/src/communication.jl index a4e41f1..e5a3257 100644 --- a/src/communication.jl +++ b/src/communication.jl @@ -573,7 +573,7 @@ julia> success, error, response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) # Signature """ function sendReceiveMqttMsg(outgoingMsg::Dict{Symbol, T}; - connectiontimeout::Integer=60, responsetimeout::Integer=60, responsemaxattempt::Integer=1)::NamedTuple where {T<:Any} + connectiontimeout::Integer=600, responsetimeout::Integer=60, responsemaxattempt::Integer=1)::NamedTuple where {T<:Any} senderId = outgoingMsg[:msgMeta][:senderId] mqttMsgReceiveTopic = "/GeneralUtils_sendReceiveMqttMsg/$senderId" mqttMsgReceiveChannel = (ch1=Channel(8),)