mqtt/mqtt_client_gen.py
author Edouard Tisserant <edouard@beremiz.fr>
Thu, 08 Aug 2024 14:56:13 +0200
changeset 4004 19f8192b7d68
parent 3999 1479acf750e2
child 4005 482452574fb4
permissions -rw-r--r--
SVGHMI: cosmetic
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
     1
from __future__ import print_function
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
     2
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
     3
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
     4
import csv
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
     5
import functools
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
     6
from threading import Thread
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
     7
from collections import OrderedDict
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
     8
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
     9
import wx
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    10
import wx.dataview as dv
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    11
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    12
import util.paths as paths
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    13
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    14
# from perfect_hash import generate_code, IntSaltHash
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    15
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    16
MQTT_IEC_types = dict(
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    17
# IEC61131|  C  type   | sz
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    18
    BOOL  = ("uint8_t" , "X"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    19
    SINT  = ("int8_t"  , "B"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    20
    USINT = ("uint8_t" , "B"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    21
    INT   = ("int16_t" , "W"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    22
    UINT  = ("uint16_t", "W"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    23
    DINT  = ("uint32_t", "D"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    24
    UDINT = ("int32_t" , "D"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    25
    LINT  = ("int64_t" , "L"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    26
    ULINT = ("uint64_t", "L"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    27
    REAL  = ("float"   , "D"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    28
    LREAL = ("double"  , "L"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    29
)
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
"""
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    32
 QoS - Quality of Service
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    33
  0  : "At most one delivery"
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    34
  1  : "At least one delivery"
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    35
  2  : "Exactly one delivery"
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    36
"""
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    37
QoS_values = [0, 1, 2]
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    38
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    39
def boolean(v):
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    40
    if v in ["False","0"]:
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    41
        return False
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    42
    else:
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    43
        return bool(v)
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    44
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    45
_lstcolnames  = [ "Topic",  "QoS",  "Retained", "Type", "Location"]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    46
_lstcolwidths = [     100,     50,         100,    100,         50]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    47
_lstcoltypess = [     str,    int,     boolean,    str,        int]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    48
_lstcoldeflts = [ "a/b/c",    "1",       False, "DINT",        "0"]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    49
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    50
subsublist = lambda l : l[0:2] + l[3:5]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    51
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    52
lstcoldsc = {
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    53
    "input" : type("",(),dict(
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    54
        lstcolnames  = subsublist(_lstcolnames),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    55
        lstcolwidths = subsublist(_lstcolwidths),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    56
        lstcoltypess = subsublist(_lstcoltypess),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    57
        lstcoldeflts = subsublist(_lstcoldeflts),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    58
        Location_column = 3)),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    59
    "output" : type("",(),dict(
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    60
        lstcolnames  = _lstcolnames,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    61
        lstcolwidths = _lstcolwidths,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    62
        lstcoltypess = _lstcoltypess,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    63
        lstcoldeflts = _lstcoldeflts,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    64
        Location_column = 4)),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    65
}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    66
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    67
directions = ["input", "output"]
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    68
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    69
authParams = {
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    70
    "x509":[
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    71
        ("Certificate", "certificate.der"),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    72
        ("PrivateKey", "private_key.pem")],
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    73
    "UserPassword":[
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    74
        ("User", None),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    75
        ("Password", None)]}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    76
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    77
class MQTTTopicListModel(dv.PyDataViewIndexListModel):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    78
    def __init__(self, data, log, direction):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    79
        dv.PyDataViewIndexListModel.__init__(self, len(data))
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    80
        self.data = data
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    81
        self.log = log
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    82
        self.dsc = lstcoldsc[direction]
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    83
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    84
    def GetColumnType(self, col):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    85
        return "string"
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 GetValueByRow(self, row, col):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    88
        return str(self.data[row][col])
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
    # This method is called when the user edits a data item in the view.
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    91
    def SetValueByRow(self, value, row, col):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    92
        expectedtype = self.dsc.lstcoltypess[col]
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    93
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    94
        try:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    95
            v = expectedtype(value)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    96
        except ValueError: 
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    97
            self.log("String {} is invalid for type {}\n".format(value,expectedtype.__name__))
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    98
            return False
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    99
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   100
        if col == self.dsc.lstcolnames.index("QoS") and v not in QoS_values:
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   101
            self.log("{} is invalid for IdType\n".format(value))
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   102
            return False
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   103
3999
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   104
        line = self.data[row]
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   105
        line[col] = v
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   106
        self.data[row] = line
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   107
        return True
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   108
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   109
    # Report how many columns this model provides data for.
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   110
    def GetColumnCount(self):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   111
        return len(self.dsc.lstcolnames)
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   112
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   113
    # Report the number of rows in the model
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   114
    def GetCount(self):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   115
        #self.log.write('GetCount')
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   116
        return len(self.data)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   117
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   118
    # Called to check if non-standard attributes should be used in the
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   119
    # cell at (row, col)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   120
    def GetAttrByRow(self, row, col, attr):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   121
        if col == self.dsc.Location_column:
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   122
            attr.SetColour('blue')
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   123
            attr.SetBold(True)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   124
            return True
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   125
        return False
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   126
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 DeleteRows(self, rows):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   129
        # make a copy since we'll be sorting(mutating) the list
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   130
        # use reverse order so the indexes don't change as we remove items
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   131
        rows = sorted(rows, reverse=True)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   132
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   133
        for row in rows:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   134
            # remove it from our data structure
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   135
            del self.data[row]
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   136
            # notify the view(s) using this model that it has been removed
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   137
            self.RowDeleted(row)
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
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   140
    def AddRow(self, value):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   141
        if self.data.append(value):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   142
            # notify views
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   143
            self.RowAppended()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   144
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   145
    def InsertDefaultRow(self, row):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   146
        self.data.insert(row, self.dsc.lstcoldeflts[:])
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   147
        # notify views
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   148
        self.RowInserted(row)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   149
    
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   150
    def ResetData(self):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   151
        self.Reset(len(self.data))
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   152
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   153
class MQTTTopicListPanel(wx.Panel):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   154
    def __init__(self, parent, log, model, direction):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   155
        self.log = log
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   156
        wx.Panel.__init__(self, parent, -1)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   157
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   158
        self.dvc = dv.DataViewCtrl(self,
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   159
                                   style=wx.BORDER_THEME
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   160
                                   | dv.DV_ROW_LINES
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   161
                                   | dv.DV_HORIZ_RULES
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   162
                                   | dv.DV_VERT_RULES
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   163
                                   | dv.DV_MULTIPLE
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   164
                                   )
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   165
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   166
        self.model = model
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   167
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   168
        self.dvc.AssociateModel(self.model)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   169
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   170
        dsc = lstcoldsc[direction]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   171
        for idx,(colname,width) in enumerate(zip(dsc.lstcolnames,dsc.lstcolwidths)):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   172
            self.dvc.AppendTextColumn(colname,  idx, width=width, mode=dv.DATAVIEW_CELL_EDITABLE)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   173
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   174
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   175
        self.Sizer = wx.BoxSizer(wx.VERTICAL)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   176
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   177
        self.direction =  direction
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   178
        titlestr = direction + " variables"
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   179
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   180
        title = wx.StaticText(self, label = titlestr)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   181
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   182
        addbt = wx.Button(self, label="Add")
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   183
        self.Bind(wx.EVT_BUTTON, self.OnAddRow, addbt)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   184
        delbt = wx.Button(self, label="Delete")
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   185
        self.Bind(wx.EVT_BUTTON, self.OnDeleteRows, delbt)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   186
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   187
        topsizer = wx.BoxSizer(wx.HORIZONTAL)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   188
        topsizer.Add(title, 1, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 5)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   189
        topsizer.Add(addbt, 0, wx.LEFT|wx.RIGHT, 5)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   190
        topsizer.Add(delbt, 0, wx.LEFT|wx.RIGHT, 5)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   191
        self.Sizer.Add(topsizer, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   192
        self.Sizer.Add(self.dvc, 1, wx.EXPAND)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   193
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   194
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   195
    def OnAddRow(self, evt):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   196
        items = self.dvc.GetSelections()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   197
        row = self.model.GetRow(items[0]) if items else 0
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   198
        self.model.InsertDefaultRow(row)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   199
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   200
    def OnDeleteRows(self, evt):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   201
        items = self.dvc.GetSelections()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   202
        rows = [self.model.GetRow(item) for item in items]
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   203
        self.model.DeleteRows(rows)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   204
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   205
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   206
class MQTTClientPanel(wx.SplitterWindow):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   207
    def __init__(self, parent, modeldata, log, config_getter):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   208
        self.log = log
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   209
        wx.SplitterWindow.__init__(self, parent, style=wx.SUNKEN_BORDER | wx.SP_3D)
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   210
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   211
        self.config_getter = config_getter
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   212
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   213
        self.selected_datas = modeldata
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   214
        self.selected_models = { direction:MQTTTopicListModel(
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   215
            self.selected_datas[direction], log, direction) for direction in directions }
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   216
        self.selected_lists = { direction:MQTTTopicListPanel(
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   217
                self, log, 
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   218
                self.selected_models[direction], direction) 
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   219
            for direction in directions }
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   220
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   221
        self.SplitHorizontally(*[self.selected_lists[direction] for direction in directions]+[300])
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   222
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   223
        self.SetAutoLayout(True)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   224
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   225
    def OnClose(self):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   226
        pass
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   227
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   228
    def __del__(self):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   229
        self.OnClose()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   230
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   231
    def Reset(self):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   232
        for direction in directions:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   233
            self.selected_models[direction].ResetData() 
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   234
        
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   235
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   236
class MQTTClientList(list):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   237
    def __init__(self, log, change_callback, direction):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   238
        super(MQTTClientList, self).__init__(self)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   239
        self.log = log
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   240
        self.change_callback = change_callback
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   241
        self.dsc = lstcoldsc[direction]
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   242
3999
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   243
    def _filter_line(self, value):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   244
        v = dict(list(zip(self.dsc.lstcolnames, value)))
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   245
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   246
        if type(v["Location"]) != int:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   247
            if len(self) == 0:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   248
                v["Location"] = 0
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   249
            else:
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   250
                iecnums = set(zip(*self)[self.dsc.Location_column])
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   251
                greatest = max(iecnums)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   252
                holes = set(range(greatest)) - iecnums
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   253
                v["Location"] = min(holes) if holes else greatest+1
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   254
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   255
        try:
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   256
            for t,n in zip(self.dsc.lstcoltypess, self.dsc.lstcolnames):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   257
                v[n] = t(v[n]) 
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   258
        except ValueError: 
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   259
            self.log("MQTT topic {} (Location={}) has invalid type\n".format(v["Topic"],v["Location"]))
3999
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   260
            return None
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   261
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   262
        if v["QoS"] not in QoS_values:
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   263
            self.log("Unknown QoS\n".format(value))
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   264
            return None
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   265
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   266
        if len(self)>0 and v["Topic"] in list(zip(*self))[self.dsc.lstcolnames.index("Topic")]:
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   267
            self.log("MQTT topic {} (Location={}) already in the list\n".format(v["Topic"],v["Location"]))
3999
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   268
            return None
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   269
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   270
        return [v[n] for n in self.dsc.lstcolnames]
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   271
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   272
    def insert(self, row, value):
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   273
        v = self._filter_line(value)
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   274
        if v is not None:
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   275
            list.insert(self, row, v)
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   276
            self.change_callback()
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   277
            return True
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   278
        return False
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   279
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   280
    def append(self, value):
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   281
        v = self._filter_line(value)
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   282
        if v is not None:
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   283
            list.append(self, v)
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   284
            self.change_callback()
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   285
            return True
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   286
        return False
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   287
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   288
    def __setitem__(self, index, value):
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   289
        list.__setitem__(self, index, value)
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   290
        self.change_callback()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   291
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   292
    def __delitem__(self, index):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   293
        list.__delitem__(self, index)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   294
        self.change_callback()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   295
3999
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   296
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   297
class MQTTClientModel(dict):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   298
    def __init__(self, log, change_callback = lambda : None):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   299
        super(MQTTClientModel, self).__init__()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   300
        for direction in directions:
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   301
            self[direction] = MQTTClientList(log, change_callback, direction)
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   302
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   303
    def LoadCSV(self,path):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   304
        with open(path, 'r') as csvfile:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   305
            reader = csv.reader(csvfile, delimiter=',', quotechar='"')
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   306
            buf = {direction:[] for direction, _model in self.iteritems()}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   307
            for direction, model in self.iteritems():
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   308
                self[direction][:] = []
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   309
            for row in reader:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   310
                direction = row[0]
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   311
                # avoids calling change callback when loading CSV
3999
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   312
                l = self[direction]
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   313
                v = l._filter_line(row[1:])
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   314
                if v is not None:
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   315
                    list.append(l,v)
1479acf750e2 MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3998
diff changeset
   316
                # TODO be verbose in case of malformed CSV
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   317
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   318
    def SaveCSV(self,path):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   319
        with open(path, 'w') as csvfile:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   320
            for direction, data in self.items():
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   321
                writer = csv.writer(csvfile, delimiter=',',
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   322
                                quotechar='"', quoting=csv.QUOTE_MINIMAL)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   323
                for row in data:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   324
                    writer.writerow([direction] + row)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   325
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   326
    def GenerateC(self, path, locstr, config):
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   327
        c_template_filepath = paths.AbsNeighbourFile(__file__, "mqtt_template.c")
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   328
        c_template_file = open(c_template_filepath , 'rb')
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   329
        c_template = c_template_file.read()
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   330
        c_template_file.close()
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   331
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   332
        formatdict = dict(
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   333
            locstr          = locstr,
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   334
            uri             = config["URI"],
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   335
            clientID        = config["clientID"],
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   336
            decl            = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   337
            topics          = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   338
            cleanup         = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   339
            init            = "",
3998
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3995
diff changeset
   340
            init_pubsub     = "",
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   341
            retrieve        = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   342
            publish         = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   343
            publish_changes = ""
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   344
        )
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   345
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   346
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   347
        # Use Config's "MQTTVersion" to switch between protocol version at build time
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   348
        if config["UseMQTT5"]:
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   349
            formatdict["decl"] += """
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   350
#define USE_MQTT_5""".format(**config)
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   351
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   352
        AuthType = config["AuthType"]
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   353
        if AuthType == "x509":
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   354
            formatdict["init"] += """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   355
    INIT_x509("{PrivateKey}", "{Certificate}")""".format(**config)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   356
        elif AuthType == "UserPassword":
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   357
            formatdict["init"] += """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   358
    INIT_UserPassword("{User}", "{Password}")""".format(**config)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   359
        else:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   360
            formatdict["init"] += """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   361
    INIT_NoAuth()"""
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   362
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   363
        for row in self["output"]:
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   364
            Topic, QoS, _Retained, iec_type, iec_number = row
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   365
            Retained = 1 if _Retained=="True" else 0
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   366
            C_type, iec_size_prefix = MQTT_IEC_types[iec_type]
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   367
            c_loc_name = "__Q" + iec_size_prefix + locstr + "_" + str(iec_number)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   368
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   369
            formatdict["decl"] += """
3981
74035ea6792c MQTT WIP: intermediate state, added perfect hash to map topic to variables
Edouard Tisserant <edouard@beremiz.fr>
parents: 3980
diff changeset
   370
DECL_VAR({iec_type}, {C_type}, {c_loc_name})""".format(**locals())
3998
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3995
diff changeset
   371
            formatdict["init_pubsub"] += """
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   372
    INIT_PUBLICATION({Topic}, {QoS}, {C_type}, {c_loc_name}, {Retained})""".format(**locals())
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   373
            formatdict["publish"] += """
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   374
        WRITE_VALUE({c_loc_name}, {C_type})""".format(**locals())
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   375
            formatdict["publish_changes"] += """
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   376
            PUBLISH_CHANGE({Topic}, {QoS}, {C_type}, {c_loc_name}, {Retained})""".format(**locals())
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   377
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   378
        # inputs need to be sorted for bisection search 
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   379
        for row in sorted(self["input"]):
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   380
            Topic, QoS, iec_type, iec_number = row
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   381
            C_type, iec_size_prefix = MQTT_IEC_types[iec_type]
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   382
            c_loc_name = "__I" + iec_size_prefix + locstr + "_" + str(iec_number)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   383
            formatdict["decl"] += """
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   384
DECL_VAR({iec_type}, {C_type}, {c_loc_name})""".format(**locals())
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   385
            formatdict["topics"] += """
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   386
    INIT_TOPIC({Topic}, {iec_type}, {c_loc_name})""".format(**locals())
3998
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3995
diff changeset
   387
            formatdict["init_pubsub"] += """
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   388
    INIT_SUBSCRIPTION({Topic}, {QoS})""".format(**locals())
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   389
            formatdict["retrieve"] += """
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   390
        READ_VALUE({c_loc_name}, {C_type})""".format(**locals())
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   391
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   392
        Ccode = c_template.format(**formatdict)
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   393
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   394
        return Ccode
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   395
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   396
if __name__ == "__main__":
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   397
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   398
    import wx.lib.mixins.inspection as wit
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   399
    import sys,os
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   400
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   401
    app = wit.InspectableApp()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   402
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   403
    frame = wx.Frame(None, -1, "MQTT Client Test App", size=(800,600))
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   404
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   405
    argc = len(sys.argv)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   406
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   407
    config={}
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   408
    config["URI"] = sys.argv[1] if argc>1 else "tcp://localhost:1883"
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   409
    config["clientID"] = sys.argv[2] if argc>2 else ""
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   410
    config["AuthType"] = None
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   411
    config["UseMQTT5"] = True
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   412
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   413
    if argc > 3:
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   414
        AuthType = sys.argv[3]
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   415
        config["AuthType"] = AuthType
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   416
        for (name, default), value in zip_longest(authParams[AuthType], sys.argv[4:]):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   417
            if 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
   418
                if default is None:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   419
                    raise Exception(name+" param expected")
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   420
                value = default
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   421
            config[name] = value
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   422
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   423
    test_panel = wx.Panel(frame)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   424
    test_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   425
    test_sizer.AddGrowableCol(0)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   426
    test_sizer.AddGrowableRow(0)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   427
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   428
    modeldata = MQTTClientModel(print)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   429
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   430
    mqtttestpanel = MQTTClientPanel(test_panel, modeldata, print, lambda:config)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   431
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   432
    def OnGenerate(evt):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   433
        dlg = wx.FileDialog(
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   434
            frame, message="Generate file as ...", defaultDir=os.getcwd(),
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   435
            defaultFile="",
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   436
            wildcard="C (*.c)|*.c", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   437
            )
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   438
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   439
        if dlg.ShowModal() == wx.ID_OK:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   440
            path = dlg.GetPath()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   441
            Ccode = """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   442
/*
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   443
In case open62541 was built just aside beremiz, you can build this test with:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   444
gcc %s -o %s \\
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   445
    -I ../../open62541/plugins/include/ \\
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   446
    -I ../../open62541/build/src_generated/ \\
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   447
    -I ../../open62541/include/ \\
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   448
    -I ../../open62541/arch/ ../../open62541/build/bin/libopen62541.a
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   449
*/
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   450
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   451
"""%(path, path[:-2]) + modeldata.GenerateC(path, "test", config) + """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   452
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   453
int LogMessage(uint8_t level, char* buf, uint32_t size){
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   454
    printf("log level:%d message:'%.*s'\\n", level, size, buf);
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   455
};
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   456
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   457
int main(int argc, char *argv[]) {
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   458
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   459
    __init_test(arc,argv);
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   460
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   461
    __retrieve_test();
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   462
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   463
    __publish_test();
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   464
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   465
    __cleanup_test();
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   466
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   467
    return EXIT_SUCCESS;
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   468
}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   469
"""
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   470
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   471
            with open(path, 'w') as Cfile:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   472
                Cfile.write(Ccode)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   473
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   474
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   475
        dlg.Destroy()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   476
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   477
    def OnLoad(evt):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   478
        dlg = wx.FileDialog(
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   479
            frame, message="Choose a file",
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   480
            defaultDir=os.getcwd(),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   481
            defaultFile="",
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   482
            wildcard="CSV (*.csv)|*.csv",
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   483
            style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST )
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   484
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   485
        if dlg.ShowModal() == wx.ID_OK:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   486
            path = dlg.GetPath()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   487
            modeldata.LoadCSV(path)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   488
            mqtttestpanel.Reset()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   489
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   490
        dlg.Destroy()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   491
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   492
    def OnSave(evt):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   493
        dlg = wx.FileDialog(
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   494
            frame, message="Save file as ...", defaultDir=os.getcwd(),
3986
98bd0bb33ce4 MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3984
diff changeset
   495
            defaultFile="",
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   496
            wildcard="CSV (*.csv)|*.csv", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   497
            )
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   498
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   499
        if dlg.ShowModal() == wx.ID_OK:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   500
            path = dlg.GetPath()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   501
            modeldata.SaveCSV(path)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   502
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   503
        dlg.Destroy()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   504
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   505
    test_sizer.Add(mqtttestpanel, flag=wx.GROW|wx.EXPAND)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   506
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   507
    testbt_sizer = wx.BoxSizer(wx.HORIZONTAL)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   508
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   509
    loadbt = wx.Button(test_panel, label="Load")
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   510
    test_panel.Bind(wx.EVT_BUTTON, OnLoad, loadbt)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   511
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   512
    savebt = wx.Button(test_panel, label="Save")
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   513
    test_panel.Bind(wx.EVT_BUTTON, OnSave, savebt)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   514
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   515
    genbt = wx.Button(test_panel, label="Generate")
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   516
    test_panel.Bind(wx.EVT_BUTTON, OnGenerate, genbt)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   517
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   518
    testbt_sizer.Add(loadbt, 0, wx.LEFT|wx.RIGHT, 5)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   519
    testbt_sizer.Add(savebt, 0, wx.LEFT|wx.RIGHT, 5)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   520
    testbt_sizer.Add(genbt, 0, wx.LEFT|wx.RIGHT, 5)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   521
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   522
    test_sizer.Add(testbt_sizer, flag=wx.GROW)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   523
    test_sizer.Layout()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   524
    test_panel.SetAutoLayout(True)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   525
    test_panel.SetSizer(test_sizer)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   526
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   527
    def OnClose(evt):
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   528
        mqtttestpanel.OnClose()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   529
        evt.Skip()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   530
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   531
    frame.Bind(wx.EVT_CLOSE, OnClose)
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   532
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   533
    frame.Show()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   534
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   535
    app.MainLoop()