author | Edouard Tisserant <edouard@beremiz.fr> |
Thu, 05 Sep 2024 15:55:46 +0200 | |
changeset 4011 | fdbb3c4ebbf6 |
parent 4010 | 3779356cca8f |
child 4012 | 6337c9c2c379 |
permissions | -rw-r--r-- |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
1 |
# mqtt/client.py |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
2 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
3 |
from __future__ import absolute_import |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
4 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
5 |
import os |
4005
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
6 |
import re |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
7 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
8 |
from editors.ConfTreeNodeEditor import ConfTreeNodeEditor |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
9 |
from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
10 |
from .mqtt_client_gen import MQTTClientPanel, MQTTClientModel, MQTT_IEC_types, authParams |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
11 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
12 |
import util.paths as paths |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
13 |
|
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
14 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
15 |
# assumes that "build" directory was created in paho.mqtt.c source directory, |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
16 |
# and cmake build was invoked from this directory |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
17 |
PahoMqttCLibraryPath = paths.ThirdPartyPath("paho.mqtt.c", "build", "src") |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
18 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
19 |
PahoMqttCIncludePaths = [ |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
20 |
paths.ThirdPartyPath("paho.mqtt.c", "build"), # VersionInfo.h |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
21 |
paths.ThirdPartyPath("paho.mqtt.c", "src") |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
22 |
] |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
23 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
24 |
class MQTTClientEditor(ConfTreeNodeEditor): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
25 |
CONFNODEEDITOR_TABS = [ |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
26 |
(_("MQTT Client"), "CreateMQTTClient_UI")] |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
27 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
28 |
def Log(self, msg): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
29 |
self.Controler.GetCTRoot().logger.write(msg) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
30 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
31 |
def CreateMQTTClient_UI(self, parent): |
4010
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
32 |
return MQTTClientPanel( |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
33 |
parent, |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
34 |
self.Controler.GetModelData(), |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
35 |
self.Log, |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
36 |
self.Controler.GetTypes) |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
37 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
38 |
class MQTTClient(object): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
39 |
XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
40 |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
41 |
<xsd:element name="MQTTClient"> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
42 |
<xsd:complexType> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
43 |
<xsd:sequence> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
44 |
<xsd:element name="AuthType" minOccurs="0"> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
45 |
<xsd:complexType> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
46 |
<xsd:choice minOccurs="0"> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
47 |
<xsd:element name="x509"> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
48 |
<xsd:complexType> |
4005
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
49 |
<xsd:attribute name="Client_certificate" type="xsd:string" use="optional" default="KeyStore.pem"/> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
50 |
<xsd:attribute name="Broker_certificate" type="xsd:string" use="optional" default="TrustStore.pem"/> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
51 |
<xsd:attribute name="Verify_hostname" type="xsd:boolean" use="optional" default="true"/> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
52 |
</xsd:complexType> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
53 |
</xsd:element> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
54 |
<xsd:element name="PSK"> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
55 |
<xsd:complexType> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
56 |
<xsd:attribute name="Secret" type="xsd:string" use="optional" default=""/> |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
57 |
<xsd:attribute name="ID" type="xsd:string" use="optional" default=""/> |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
58 |
</xsd:complexType> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
59 |
</xsd:element> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
60 |
<xsd:element name="UserPassword"> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
61 |
<xsd:complexType> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
62 |
<xsd:attribute name="User" type="xsd:string" use="optional"/> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
63 |
<xsd:attribute name="Password" type="xsd:string" use="optional"/> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
64 |
</xsd:complexType> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
65 |
</xsd:element> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
66 |
</xsd:choice> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
67 |
</xsd:complexType> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
68 |
</xsd:element> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
69 |
</xsd:sequence> |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3985
diff
changeset
|
70 |
<xsd:attribute name="Use_MQTT_5" type="xsd:boolean" use="optional" default="true"/> |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
71 |
<xsd:attribute name="Broker_URI" type="xsd:string" use="optional" default="ws://localhost:1883"/> |
3980
96acfee19caf
MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3979
diff
changeset
|
72 |
<xsd:attribute name="Client_ID" type="xsd:string" use="optional" default=""/> |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
73 |
</xsd:complexType> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
74 |
</xsd:element> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
75 |
</xsd:schema> |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
76 |
""" |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
77 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
78 |
EditorType = MQTTClientEditor |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
79 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
80 |
def __init__(self): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
81 |
self.modeldata = MQTTClientModel(self.Log, self.CTNMarkModified) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
82 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
83 |
filepath = self.GetFileName() |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
84 |
if os.path.isfile(filepath): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
85 |
self.modeldata.LoadCSV(filepath) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
86 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
87 |
def Log(self, msg): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
88 |
self.GetCTRoot().logger.write(msg) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
89 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
90 |
def GetModelData(self): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
91 |
return self.modeldata |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
92 |
|
4010
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
93 |
def GetTypes(self): |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
94 |
datatype_candidates = self.GetCTRoot().GetDataTypes() |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
95 |
return datatype_candidates |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
96 |
|
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
97 |
def GetConfig(self): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
98 |
def cfg(path): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
99 |
try: |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
100 |
attr=self.GetParamsAttributes("MQTTClient."+path) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
101 |
except ValueError: |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
102 |
return None |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
103 |
return attr["value"] |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
104 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
105 |
AuthType = cfg("AuthType") |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3985
diff
changeset
|
106 |
res = dict( |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3985
diff
changeset
|
107 |
URI=cfg("Broker_URI"), |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3985
diff
changeset
|
108 |
AuthType=AuthType, |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3985
diff
changeset
|
109 |
clientID=cfg("Client_ID"), |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3985
diff
changeset
|
110 |
UseMQTT5=cfg("Use_MQTT_5")) |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
111 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
112 |
paramList = authParams.get(AuthType, None) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
113 |
if paramList: |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
114 |
for name,default in paramList: |
4005
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
115 |
|
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
116 |
# Translate internal config naming into user config naming |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
117 |
displayed_name = {"KeyStore" : "Client_certificate", |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
118 |
"TrustStore" : "Broker_certificate", |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
119 |
"Verify" : "Verify_hostname"}.get(name, name) |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
120 |
|
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
121 |
value = cfg("AuthType." + displayed_name) |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
122 |
if value == "" or value is None: |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
123 |
value = default |
4005
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
124 |
|
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
125 |
if value is not None: |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
126 |
# cryptomaterial is expected to be in project's user provided file directory |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
127 |
|
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
128 |
# User input may contain char incompatible with C string literal escaping |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
129 |
if name in ["User","Password","TrustStore","KeyStore","Broker_URI","Client_ID"]: |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
130 |
value = re.sub(r'([\"\\])', r'\\\1', value) |
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
131 |
|
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
132 |
res[name] = value |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
133 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
134 |
return res |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
135 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
136 |
def GetFileName(self): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
137 |
return os.path.join(self.CTNPath(), 'selected.csv') |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
138 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
139 |
def OnCTNSave(self, from_project_path=None): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
140 |
self.modeldata.SaveCSV(self.GetFileName()) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
141 |
return True |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
142 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
143 |
def CTNGenerate_C(self, buildpath, locations): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
144 |
current_location = self.GetCurrentLocation() |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
145 |
locstr = "_".join(map(str, current_location)) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
146 |
c_path = os.path.join(buildpath, "mqtt_client__%s.c" % locstr) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
147 |
|
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
148 |
c_code = """ |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
149 |
#include "iec_types_all.h" |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
150 |
#include "beremiz.h" |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3980
diff
changeset
|
151 |
""" |
4001
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
152 |
config = self.GetConfig() |
4005
482452574fb4
MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents:
4001
diff
changeset
|
153 |
c_code += self.modeldata.GenerateC(c_path, locstr, config) |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
154 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
155 |
with open(c_path, 'w') as c_file: |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
156 |
c_file.write(c_code) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
157 |
|
4001
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
158 |
if config["AuthType"] == "x509": |
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
159 |
static_lib = "libpaho-mqtt3cs.a" |
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
160 |
libs = ['-lssl', '-lcrypto'] |
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
161 |
else: |
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
162 |
static_lib = "libpaho-mqtt3c.a" |
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
163 |
libs = [] |
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
164 |
|
5e0660d394e3
MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3991
diff
changeset
|
165 |
LDFLAGS = [' "' + os.path.join(PahoMqttCLibraryPath, static_lib) + '"'] + libs |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
166 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
167 |
CFLAGS = ' '.join(['-I"' + path + '"' for path in PahoMqttCIncludePaths]) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
168 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
169 |
return [(c_path, CFLAGS)], LDFLAGS, True |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
170 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
171 |
def GetVariableLocationTree(self): |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
172 |
current_location = self.GetCurrentLocation() |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
173 |
locstr = "_".join(map(str, current_location)) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
174 |
name = self.BaseParams.getName() |
3991
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
175 |
|
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
176 |
entries = [] |
3991
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
177 |
children = [] |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
178 |
|
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
179 |
for row in self.modeldata["output"]: |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
180 |
Topic, QoS, _Retained, iec_type, iec_number = row |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
181 |
entries.append((Topic, QoS, iec_type, iec_number, "Q", LOCATION_VAR_OUTPUT)) |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
182 |
|
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
183 |
for row in self.modeldata["input"]: |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
184 |
Topic, QoS, iec_type, iec_number = row |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
185 |
entries.append((Topic, QoS, iec_type, iec_number, "I", LOCATION_VAR_INPUT)) |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
186 |
|
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
187 |
for Topic, QoS, iec_type, iec_number, iec_dir_prefix, loc_type in entries: |
4009
51272fe96999
MQTT+IDE: Allow user to associate IEC structure datatype to a topic.
Edouard Tisserant <edouard@beremiz.fr>
parents:
4005
diff
changeset
|
188 |
_C_type, iec_size_prefix = MQTT_IEC_types.get(iec_type,(None,"")) |
3991
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
189 |
c_loc_name = "__" + iec_dir_prefix + iec_size_prefix + locstr + "_" + str(iec_number) |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
190 |
children.append({ |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
191 |
"name": Topic, |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
192 |
"type": loc_type, |
4009
51272fe96999
MQTT+IDE: Allow user to associate IEC structure datatype to a topic.
Edouard Tisserant <edouard@beremiz.fr>
parents:
4005
diff
changeset
|
193 |
"size": {"X":1, "B":8, "W":16, "D":32, "L":64, "":None}[iec_size_prefix], |
3991
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
194 |
"IEC_type": iec_type, |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
195 |
"var_name": c_loc_name, |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
196 |
"location": "%" + iec_dir_prefix + iec_size_prefix + ".".join([str(i) for i in current_location]) + "." + str(iec_number), |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
197 |
"description": "", |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
198 |
"children": []}) |
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
199 |
|
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
200 |
return {"name": name, |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
201 |
"type": LOCATION_CONFNODE, |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
202 |
"location": ".".join([str(i) for i in current_location]) + ".x", |
3991
28354ba489b9
MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
203 |
"children": children} |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
204 |
|
4010
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
205 |
|
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
206 |
def CTNGlobalInstances(self): |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
207 |
location_str = "_".join(map(str, self.GetCurrentLocation())) |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
208 |
return [("MQTT_HAPPY_"+location_str, "DINT", "")] |
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
209 |
|
3779356cca8f
MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents:
4009
diff
changeset
|
210 |