lpcmanager

Parents 48b1c9ba7a26
Children e4eda7d98c03
MQTT file transfer: limit receiving one file at a time and report more feedback to IDE PLC log.
  • +35 -3
    LPCMQTT.py
  • --- a/LPCMQTT.py Fri Feb 21 14:21:52 2025 +0100
    +++ b/LPCMQTT.py Fri Feb 21 16:34:47 2025 +0100
    @@ -23,16 +23,48 @@
    import sys
    import common_functions
    +import threading
    from runtime.spawn_subprocess import Popen, call, PIPE
    +from runtime.loglevels import LogLevelsDict
    +from runtime import GetPLCObjectSingleton
    LPCTopic = "/smarteh/"+common_functions.getLPCSerialFromMAC()+"/command"
    def _{location}_topic_callback(topic, payload):
    - proc = Popen([os.path.join(os.path.dirname(common_functions.__file__), 'mqtt_command.sh')], stdin=PIPE)
    - proc.stdin.write(payload)
    - proc.stdin.close()
    + GetPLCObjectSingleton().LogMessage(
    + LogLevelsDict["INFO"],
    + "LPC MQTT {name} rcv: "+topic)
    +
    + lock = threading.RLock()
    +
    + with lock:
    + proc = Popen([os.path.join(os.path.dirname(common_functions.__file__), 'mqtt_command.sh')], stdin=PIPE, stdout=PIPE)
    + proc.stdin.write(payload)
    + proc.stdin.close()
    + proc.stdin = None
    +
    + def WaitForCommandEnd():
    + with lock:
    + stdoutdata, stderrdata = proc.communicate()
    + returncode = proc.returncode
    + if returncode == 0:
    + GetPLCObjectSingleton().LogMessage(
    + LogLevelsDict["INFO"],
    + "LPC MQTT {name} command success: "+stdoutdata)
    + else:
    + GetPLCObjectSingleton().LogMessage(
    + LogLevelsDict["WARNING"],
    + "LPC MQTT {name} command error: rc="+str(returncode)+
    + " stdout: "+stdoutdata+
    + " stderr: "+stderrdata)
    +
    + stdout_thread = threading.Thread(target=WaitForCommandEnd)
    + stdout_thread.start()
    def _runtime_{location}_mqtt_start():
    + GetPLCObjectSingleton().LogMessage(
    + LogLevelsDict["INFO"],
    + "LPC MQTT {name} sub: "+LPCTopic)
    MQTT_subscribe("{name}", LPCTopic, _{location}_topic_callback)
    return