mqtt/client.py
changeset 4005 482452574fb4
parent 4001 5e0660d394e3
child 4009 51272fe96999
equal deleted inserted replaced
4004:19f8192b7d68 4005:482452574fb4
     1 # mqtt/client.py
     1 # mqtt/client.py
     2 
     2 
     3 from __future__ import absolute_import
     3 from __future__ import absolute_import
     4 
     4 
     5 import os
     5 import os
       
     6 import re
     6 
     7 
     7 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
     8 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
     8 from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT
     9 from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT
     9 from .mqtt_client_gen import MQTTClientPanel, MQTTClientModel, MQTT_IEC_types, authParams
    10 from .mqtt_client_gen import MQTTClientPanel, MQTTClientModel, MQTT_IEC_types, authParams
    10 
    11 
    39             <xsd:element name="AuthType" minOccurs="0">
    40             <xsd:element name="AuthType" minOccurs="0">
    40               <xsd:complexType>
    41               <xsd:complexType>
    41                 <xsd:choice minOccurs="0">
    42                 <xsd:choice minOccurs="0">
    42                   <xsd:element name="x509">
    43                   <xsd:element name="x509">
    43                     <xsd:complexType>
    44                     <xsd:complexType>
    44                       <xsd:attribute name="Certificate" type="xsd:string" use="optional" default="certificate.pem"/>
    45                       <xsd:attribute name="Client_certificate" type="xsd:string" use="optional" default="KeyStore.pem"/>
    45                       <xsd:attribute name="PrivateKey" type="xsd:string" use="optional" default="private_key.pem"/>
    46                       <xsd:attribute name="Broker_certificate" type="xsd:string" use="optional" default="TrustStore.pem"/>
       
    47                       <xsd:attribute name="Verify_hostname" type="xsd:boolean" use="optional" default="true"/>
       
    48                     </xsd:complexType>
       
    49                   </xsd:element>
       
    50                   <xsd:element name="PSK">
       
    51                     <xsd:complexType>
       
    52                       <xsd:attribute name="Secret" type="xsd:string" use="optional" default=""/>
       
    53                       <xsd:attribute name="ID" type="xsd:string" use="optional" default=""/>
    46                     </xsd:complexType>
    54                     </xsd:complexType>
    47                   </xsd:element>
    55                   </xsd:element>
    48                   <xsd:element name="UserPassword">
    56                   <xsd:element name="UserPassword">
    49                     <xsd:complexType>
    57                     <xsd:complexType>
    50                       <xsd:attribute name="User" type="xsd:string" use="optional"/>
    58                       <xsd:attribute name="User" type="xsd:string" use="optional"/>
    94             UseMQTT5=cfg("Use_MQTT_5"))
   102             UseMQTT5=cfg("Use_MQTT_5"))
    95 
   103 
    96         paramList = authParams.get(AuthType, None)
   104         paramList = authParams.get(AuthType, None)
    97         if paramList:
   105         if paramList:
    98             for name,default in paramList:
   106             for name,default in paramList:
    99                 value = cfg("AuthType."+name)
   107 
       
   108                 # Translate internal config naming into user config naming
       
   109                 displayed_name = {"KeyStore"   : "Client_certificate",
       
   110                                   "TrustStore" : "Broker_certificate", 
       
   111                                   "Verify"     : "Verify_hostname"}.get(name, name)
       
   112 
       
   113                 value = cfg("AuthType." + displayed_name)
   100                 if value == "" or value is None:
   114                 if value == "" or value is None:
   101                     value = default
   115                     value = default
   102                 # cryptomaterial is expected to be in project's user provide file directory
   116 
   103                 if name in ["Certificate","PrivateKey"]:
   117                 if value is not None:
   104                     value = os.path.join(self.GetCTRoot()._getProjectFilesPath(), value)
   118                     # cryptomaterial is expected to be in project's user provided file directory
       
   119 
       
   120                     # User input may contain char incompatible with C string literal escaping
       
   121                     if name in ["User","Password","TrustStore","KeyStore","Broker_URI","Client_ID"]:
       
   122                         value = re.sub(r'([\"\\])',  r'\\\1', value)
       
   123 
   105                 res[name] = value
   124                 res[name] = value
   106 
   125 
   107         return res
   126         return res
   108 
   127 
   109     def GetFileName(self):
   128     def GetFileName(self):
   121         c_code = """
   140         c_code = """
   122 #include "iec_types_all.h"
   141 #include "iec_types_all.h"
   123 #include "beremiz.h"
   142 #include "beremiz.h"
   124 """
   143 """
   125         config = self.GetConfig()
   144         config = self.GetConfig()
   126         c_code += self.modeldata.GenerateC(c_path, locstr, self.GetConfig())
   145         c_code += self.modeldata.GenerateC(c_path, locstr, config)
   127 
   146 
   128         with open(c_path, 'w') as c_file:
   147         with open(c_path, 'w') as c_file:
   129             c_file.write(c_code)
   148             c_file.write(c_code)
   130 
   149 
   131         if config["AuthType"] == "x509":
   150         if config["AuthType"] == "x509":