lpcmanager

MQTT SmartehCloud authentication and command support - WIP
py2compat
17 months ago, Edouard Tisserant
5ceee8f3e4d4
Parents 3a57baf957ed
Children 96018843360a
MQTT SmartehCloud authentication and command support - WIP
--- a/LPCExtension.py Mon Oct 21 16:18:05 2024 +0200
+++ b/LPCExtension.py Tue Jan 21 09:25:40 2025 +0100
@@ -22,7 +22,8 @@
product = GetLPCProduct()
if product in SOM28_modules + SOM6_modules:
features.libraries += [('Python', 'py_ext.PythonLibrary', True),
- ('RTC', SimplePOULibraryFactory(_poulibpath("RTC")), True)]
+ ('RTC', SimplePOULibraryFactory(_poulibpath("RTC")), True),
+ ('MQTT', 'mqtt.MQTTLibrary', True)]
if product in WX_GOT_modules + SVG_GOT_modules:
features.libraries += [('GOT', SimplePOULibraryFactory(_poulibpath("GOT")), True)]
if product in SOM6_modules:
@@ -45,7 +46,7 @@
('lpchmi', _('Smarteh HMI'), _('Create customized HMI'), 'lpchmi.LPCHMI'),
('bacnet', _('Bacnet support'), _('Map located variables over Bacnet'), 'LPCBACnet.RootClass'),
('modbus', _('Modbus'), _('Map located variables over Modbus'), 'LPCModbus.RootClass'),
- ('mqtt', _('MQTT client'), _('Map MQTT topics as located variables'), 'mqtt.MQTTClient'),
+ ('mqtt', _('MQTT client'), _('Map MQTT topics as located variables'), 'LPCMQTT.MQTTClient'),
('LPCBus', _('LPC bus'), _('Support for Smarteh modules'), 'LPCBus.LPCBus'),
('CanOpen', _('CANOpen'), _('Support for CANopen'), 'LPCCanFestival.LPCCanOpen')]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCMQTT.py Tue Jan 21 09:25:40 2025 +0100
@@ -0,0 +1,74 @@
+
+from mqtt import MQTTClient, MQTTLibrary
+import os
+import re
+
+orig_CTNGenerate_C = MQTTClient.CTNGenerate_C
+def CTNGenerate_C(self, buildpath, locations):
+ 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):
+ print("got data:", 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)
+ print("mqtt register", LPCTopic)
+ return
+
+def _runtime_{location}_mqtt_stop():
+ print("mqtt stop")
+ return
+
+""".format(location = location_str,
+ name = name_str))
+
+ runtimefile.close()
+
+ return orig_CTNGenerate_C(self, buildpath, locations) + (
+ ("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"/>
+""")
+print(xsd_frags)
+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