mqtt/client.py
author Edouard Tisserant <edouard@beremiz.fr>
Fri, 30 Aug 2024 11:50:23 +0200
changeset 4008 f30573e98600
parent 4005 482452574fb4
child 4009 51272fe96999
permissions -rw-r--r--
IDE: allow structures to be located.
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):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    32
        return MQTTClientPanel(parent, self.Controler.GetModelData(), self.Log, self.Controler.GetConfig)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    33
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    34
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
    35
    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
    36
    <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
    37
      <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
    38
        <xsd:complexType>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    39
          <xsd:sequence>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    40
            <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
    41
              <xsd:complexType>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    42
                <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
    43
                  <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
    44
                    <xsd:complexType>
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
    45
                      <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
    46
                      <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
    47
                      <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
    48
                    </xsd:complexType>
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
    49
                  </xsd:element>
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
    50
                  <xsd:element name="PSK">
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
    51
                    <xsd:complexType>
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
    52
                      <xsd:attribute name="Secret" type="xsd:string" use="optional" default=""/>
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
    53
                      <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
    54
                    </xsd:complexType>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    55
                  </xsd:element>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    56
                  <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
    57
                    <xsd:complexType>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    58
                      <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
    59
                      <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
    60
                    </xsd:complexType>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    61
                  </xsd:element>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    62
                </xsd:choice>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    63
              </xsd:complexType>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    64
            </xsd:element>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    65
          </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
    66
          <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
    67
          <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
    68
          <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
    69
        </xsd:complexType>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    70
      </xsd:element>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    71
    </xsd:schema>
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    72
    """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    73
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    74
    EditorType = MQTTClientEditor
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    75
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    76
    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
    77
        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
    78
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    79
        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
    80
        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
    81
            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
    82
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    83
    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
    84
        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
    85
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    86
    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
    87
        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
    88
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    89
    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
    90
        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
    91
            try:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    92
                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
    93
            except ValueError:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    94
                return None
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    95
            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
    96
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    97
        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
    98
        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
    99
            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
   100
            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
   101
            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
   102
            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
   103
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   104
        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
   105
        if paramList:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   106
            for name,default in paramList:
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   107
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   108
                # Translate internal config naming into user config naming
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   109
                displayed_name = {"KeyStore"   : "Client_certificate",
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   110
                                  "TrustStore" : "Broker_certificate", 
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   111
                                  "Verify"     : "Verify_hostname"}.get(name, name)
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   112
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   113
                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
   114
                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
   115
                    value = default
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   116
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   117
                if value is not None:
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   118
                    # 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
   119
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   120
                    # 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
   121
                    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
   122
                        value = re.sub(r'([\"\\])',  r'\\\1', value)
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   123
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   124
                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
   125
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   126
        return res
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   127
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   128
    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
   129
        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
   130
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   131
    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
   132
        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
   133
        return True
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   134
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   135
    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
   136
        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
   137
        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
   138
        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
   139
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3980
diff changeset
   140
        c_code = """
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3980
diff changeset
   141
#include "iec_types_all.h"
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3980
diff changeset
   142
#include "beremiz.h"
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3980
diff changeset
   143
"""
4001
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   144
        config = self.GetConfig()
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4001
diff changeset
   145
        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
   146
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   147
        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
   148
            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
   149
4001
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   150
        if config["AuthType"] == "x509":
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   151
            static_lib = "libpaho-mqtt3cs.a"
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   152
            libs = ['-lssl', '-lcrypto']
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   153
        else:
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   154
            static_lib = "libpaho-mqtt3c.a"
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   155
            libs = []
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   156
5e0660d394e3 MQTT: link to SSL only when needed.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3991
diff changeset
   157
        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
   158
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   159
        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
   160
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   161
        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
   162
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   163
    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
   164
        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
   165
        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
   166
        name = self.BaseParams.getName()
3991
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   167
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   168
        entries = []
3991
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   169
        children = []
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   170
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   171
        for row in self.modeldata["output"]:
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   172
            Topic, QoS, _Retained, iec_type, iec_number = row
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   173
            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
   174
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   175
        for row in self.modeldata["input"]:
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   176
            Topic, QoS, iec_type, iec_number = row
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   177
            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
   178
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   179
        for Topic, QoS, iec_type, iec_number, iec_dir_prefix, loc_type in entries:
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   180
            C_type, iec_size_prefix = MQTT_IEC_types[iec_type]
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   181
            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
   182
            children.append({
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   183
                "name": Topic,
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   184
                "type": loc_type,
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   185
                "size": {"X":1, "B":8, "W":16, "D":32, "L":64}[iec_size_prefix],
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   186
                "IEC_type": iec_type,
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   187
                "var_name": c_loc_name,
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   188
                "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
   189
                "description": "",
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   190
                "children": []})
28354ba489b9 MQTT: Fix crash when browsing locations
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   191
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   192
        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
   193
                "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
   194
                "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
   195
                "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
   196