lpcmanager

Fix special SmatehCloud topic subscription not happening despite option being selected.
from mqtt import MQTTClient, MQTTLibrary
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
from runtime.spawn_subprocess import Popen, call, PIPE
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()
def _runtime_{location}_mqtt_start():
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
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"/>
""")
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