lpcmanager

SVGHMI: disable SuppressBrowserOutput by default, since it's incompatible with Python 2 runtime.
from mqtt import MQTTClient, MQTTLibrary, mqtt_client_gen
import os
import re
orig_CTNGenerate_C = MQTTClient.CTNGenerate_C
def CTNGenerate_C(self, buildpath, locations):
res = orig_CTNGenerate_C(self, buildpath, locations)
is_SmartehCloud = self.GetParamsAttributes(u"MQTTClient.AuthType")["value"] == u"SmartehCloud"
if not is_SmartehCloud:
return res
location_str = "_".join(map(str, self.GetCurrentLocation()))
name_str = self.BaseParams.getName()
runtimefile_path = os.path.join(buildpath, "runtime_%s_mqtt_.py" % location_str)
runtimefile = open(runtimefile_path, 'w')
runtimefile.write("""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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"
_{location}_lock = threading.Lock()
def _{location}_topic_callback(topic, payload):
GetPLCObjectSingleton().LogMessage(
LogLevelsDict["INFO"],
"LPC MQTT {name} rcv: "+topic)
_{location}_lock.acquire()
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():
stdoutdata, stderrdata = proc.communicate()
returncode = proc.returncode
_{location}_lock.release()
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
def _runtime_{location}_mqtt_stop():
return
""".format(location = location_str,
name = name_str))
runtimefile.close()
return res + (("runtime_%s_mqtt.py" % location_str, open(runtimefile_path, "rb")),)
MQTTClient.CTNGenerate_C = CTNGenerate_C
mqtt_client_gen.authParams["SmartehCloud"] = [
("ProjectID", None)]
prev_SanitizeTopic = mqtt_client_gen.MQTTClientModel.SanitizeTopic
def SanitizeTopic(self, config, Topic):
ProjectID = config.get("ProjectID", "")
if ProjectID:
return "/"+ProjectID+"/"+Topic
return Topic
mqtt_client_gen.MQTTClientModel.SanitizeTopic = SanitizeTopic
orig___init__ = MQTTClient.__init__
def __init__(self):
if self.new_config:
self.SetParamsAttribute(u'MQTTClient.AuthType', u'SmartehCloud')
orig___init__(self)
MQTTClient.__init__ = __init__
xsd_frags = re.split(r'(choice[^>]*>)', MQTTClient.XSD, 1, re.DOTALL)
xsd_frags.insert(-1, """
<xsd:element name="SmartehCloud">
<xsd:complexType>
<xsd:attribute name="ProjectID" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
""")
MQTTClient.XSD = "".join(xsd_frags)
orig_GetConfig = MQTTClient.GetConfig
def GetConfig(self):
res = orig_GetConfig(self)
if res["AuthType"] == "SmartehCloud":
res.update({
"AuthType": "x509",
"Verify": True,
"KeyStore": "/media/data/keystore/cloud/KeyStore.pem",
"TrustStore": "/media/data/keystore/cloud/TrustStore.pem"})
return res
MQTTClient.GetConfig = GetConfig