diff --git a/src/communication.jl b/src/communication.jl index 1160915..18acde8 100644 --- a/src/communication.jl +++ b/src/communication.jl @@ -599,15 +599,8 @@ function sendReceiveMqttMsg(mqttInstance::mqttClientInstance_v2, receivechannel: )::NamedTuple where {T<:Any} timepass = nothing - attempts = 0 + attempts = 1 while attempts <= maxattempt - attempts += 1 - if attempts > 1 - println("\n attempts $attempts/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())") - pprintln(outgoingMsg) - println(" attempts $attempts/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") - end - sendMqttMsg(mqttInstance, outgoingMsg) starttime = Dates.now() @@ -632,6 +625,12 @@ function sendReceiveMqttMsg(mqttInstance::mqttClientInstance_v2, receivechannel: end sleep(1) end + if attempts > 1 + println("\n attempts $attempts/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + pprintln(outgoingMsg) + println(" attempts $attempts/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") + end + attempts += 1 end return (success=false, diff --git a/src/util.jl b/src/util.jl index 5e67e11..9f2024c 100644 --- a/src/util.jl +++ b/src/util.jl @@ -771,35 +771,30 @@ end """ - detect_keyword(keywords::AbstractVector{String}, text::String) -> Vector{Union{Nothing, String}} + detect_keyword(keywords::AbstractVector{String}, text::String) -> Dict{String, Integer} -Detects if multiple keywords exist in the text in different case variations (lowercase, uppercase first letter, or all uppercase). +Detects and counts occurrences of multiple keywords in the text in different case variations (lowercase, uppercase first letter, or all uppercase). # Arguments: - `keywords::AbstractVector{String}` Vector of keywords to search for - `text::String` The text to search in # Returns: -- `Vector{Union{Nothing, String}}` Returns a vector containing the matched keyword variations if found, otherwise nothing for each keyword +- `Dict{String, Integer}` Returns a dictionary mapping each keyword to its count in the text (0 if not found) # Examples: ```jldoctest julia> detect_keyword(["test", "error", "case"], "This is a Test case with ERRORS case") - 2-element Vector{Union{Nothing, String}}: - "Test" - "ERRORS" - nothing + Dict{String, Integer}("test" => 1, "error" => 1, "case" => 2) julia> detect_keyword(["warning", "missing"], "Warning: data is complete") - 2-element Vector{Union{Nothing, String}}: - "Warning" - nothing - ``` + Dict{String, Integer}("warning" => 1, "missing" => 0) + # Signature """ -function detect_keyword(keywords::T, text::String)::Union{Nothing, Dict} where {T<:AbstractVector} - kw = Dict{String, Any}() +function detect_keyword(keywords::T, text::String)::Dict{String, Integer} where {T<:AbstractVector} + kw = Dict{String, Integer}() splittext = string.(split(text, " ")) # use for loop and detect_keyword function to get the exact variation of each keyword in the text then push to kw list for keyword in keywords @@ -808,11 +803,13 @@ function detect_keyword(keywords::T, text::String)::Union{Nothing, Dict} where { if total != 0 kw[keyword] = total else - kw[keyword] = nothing + kw[keyword] = 0 end end return kw end + + """ detect_keyword(keyword::String, text::String) -> Union{Nothing, String}