mqtt/mqtt_client_gen.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 16 Oct 2024 12:18:14 +0200
changeset 4030 45532de22b75
parent 4022 dee0efbf8452
permissions -rw-r--r--
TESTS: Update Matiec revision in GH workflow
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
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
     7
from collections import OrderedDict as OD
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
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    14
MQTT_UNSUPPORTED_types = set([
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    15
    "TIME",
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    16
    "DATE",
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    17
    "TOD",
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    18
    "DT",
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    19
    "STEP",
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    20
    "TRANSITION",
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    21
    "ACTION",
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    22
    "STRING"
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    23
])
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    24
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    25
MQTT_IEC_types_list =[ 
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    26
# IEC61131|  C  type   | sz
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    27
    ("BOOL" , ("uint8_t" , "X")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    28
    ("SINT" , ("int8_t"  , "B")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    29
    ("USINT", ("uint8_t" , "B")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    30
    ("BYTE" , ("uint8_t" , "X")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    31
    ("INT"  , ("int16_t" , "W")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    32
    ("UINT" , ("uint16_t", "W")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    33
    ("WORD" , ("uint16_t", "W")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    34
    ("DINT" , ("int32_t" , "D")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    35
    ("UDINT", ("uint32_t", "D")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    36
    ("DWORD", ("uint32_t", "D")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    37
    ("LINT" , ("int64_t" , "L")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    38
    ("ULINT", ("uint64_t", "L")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    39
    ("LWORD", ("uint64_t", "L")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    40
    ("REAL" , ("float"   , "D")),
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    41
    ("LREAL", ("double"  , "L"))
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    42
]
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    43
MQTT_IEC_SUPPORTED_types = list(zip(*MQTT_IEC_types_list)[0])
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    44
MQTT_IEC_types = dict(MQTT_IEC_types_list)
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    45
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    46
MQTT_JSON_SUPPORTED_types = set(MQTT_IEC_types.keys()+["STRING"])
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    47
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    48
"""
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    49
 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
    50
  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
    51
  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
    52
  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
    53
"""
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    54
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
    55
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    56
def boolean(v):
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    57
    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
    58
        return False
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    59
    else:
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    60
        return bool(v)
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    61
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    62
_lstcolnames  = [ "Topic",  "QoS",  "Retained", "Type", "Location"]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    63
_lstcolwidths = [     100,     50,         100,    100,         50]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    64
_lstcoltypess = [     str,    int,     boolean,    str,        int]
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    65
_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
    66
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    67
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
    68
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    69
lstcoldsc = {
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    70
    "input" : type("",(),dict(
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    71
        lstcolnames  = subsublist(_lstcolnames),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    72
        lstcolwidths = subsublist(_lstcolwidths),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    73
        lstcoltypess = subsublist(_lstcoltypess),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    74
        lstcoldeflts = subsublist(_lstcoldeflts),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    75
        Location_column = 3)),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    76
    "output" : type("",(),dict(
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    77
        lstcolnames  = _lstcolnames,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    78
        lstcolwidths = _lstcolwidths,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    79
        lstcoltypess = _lstcoltypess,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    80
        lstcoldeflts = _lstcoldeflts,
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    81
        Location_column = 4)),
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    82
}
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
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
    85
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
    86
# expected configuration entries with internal default value
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    87
authParams = {
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    88
    "x509":[
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
    89
        ("Verify", True),
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
    90
        ("KeyStore", None),
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
    91
        ("TrustStore", None)],
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    92
    "UserPassword":[
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    93
        ("User", None),
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    94
        ("Password", None)]}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    95
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    96
class MQTTTopicListModel(dv.PyDataViewIndexListModel):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
    97
    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
    98
        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
    99
        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
   100
        self.log = log
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   101
        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
   102
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   103
    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
   104
        return "string"
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   105
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   106
    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
   107
        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
   108
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   109
    # 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
   110
    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
   111
        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
   112
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   113
        try:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   114
            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
   115
        except ValueError: 
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   116
            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
   117
            return False
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   118
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   119
        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
   120
            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
   121
            return False
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   122
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
   123
        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
   124
        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
   125
        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
   126
        return True
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
    # 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
   129
    def GetColumnCount(self):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   130
        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
   131
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   132
    # 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
   133
    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
   134
        #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
   135
        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
   136
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   137
    # 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
   138
    # 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
   139
    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
   140
        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
   141
            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
   142
            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
   143
            return True
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   144
        return False
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   145
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   146
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   147
    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
   148
        # 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
   149
        # 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
   150
        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
   151
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   152
        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
   153
            # 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
   154
            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
   155
            # 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
   156
            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
   157
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   158
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   159
    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
   160
        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
   161
            # notify views
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   162
            self.RowAppended()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   163
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   164
    def InsertDefaultRow(self, row):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   165
        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
   166
        # notify views
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   167
        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
   168
    
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   169
    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
   170
        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
   171
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   172
class MQTTTopicListPanel(wx.Panel):
4010
3779356cca8f MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   173
    def __init__(self, parent, log, model, direction, types_getter):
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   174
        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
   175
        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
   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.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
   178
                                   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
   179
                                   | 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
   180
                                   | 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
   181
                                   | 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
   182
                                   | 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
   183
                                   )
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   184
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   185
        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
   186
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   187
        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
   188
4014
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   189
        self.types_getter = types_getter
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   190
        self.direction =  direction
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   191
        self.CreateDVCColumns()
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   192
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   193
        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
   194
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   195
        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
   196
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   197
        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
   198
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   199
        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
   200
        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
   201
        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
   202
        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
   203
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   204
        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
   205
        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
   206
        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
   207
        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
   208
        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
   209
        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
   210
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   211
4014
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   212
    def CreateDVCColumns(self):
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   213
        dsc = lstcoldsc[self.direction]
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   214
        for idx,(colname,width) in enumerate(zip(dsc.lstcolnames,dsc.lstcolwidths)):
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   215
            if colname == "Type":
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   216
                choice_DV_render = dv.DataViewChoiceRenderer(MQTT_IEC_SUPPORTED_types + self.types_getter())
4014
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   217
                choice_DV_col = dv.DataViewColumn(colname, choice_DV_render, idx, width=width)
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   218
                self.dvc.AppendColumn(choice_DV_col)
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   219
            else:
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   220
                self.dvc.AppendTextColumn(colname,  idx, width=width, mode=dv.DATAVIEW_CELL_EDITABLE)
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   221
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   222
    def ResetDVCColumns(self):
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   223
        self.dvc.ClearColumns()
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   224
        self.CreateDVCColumns()
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   225
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   226
    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
   227
        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
   228
        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
   229
        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
   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 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
   232
        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
   233
        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
   234
        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
   235
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   236
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   237
class MQTTClientPanel(wx.SplitterWindow):
4010
3779356cca8f MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   238
    def __init__(self, parent, modeldata, log, types_getter):
3979
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
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   240
        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
   241
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   242
        self.selected_datas = modeldata
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   243
        self.selected_models = { direction:MQTTTopicListModel(
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   244
            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
   245
        self.selected_lists = { direction:MQTTTopicListPanel(
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   246
                self, log, 
4010
3779356cca8f MQTT: add dropdown to select type assigned to a topic
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   247
                self.selected_models[direction], direction, types_getter) 
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   248
            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
   249
3992
056657cd1484 MQTT: Fix overkill use of wx sizer
Edouard Tisserant <edouard@beremiz.fr>
parents: 3990
diff changeset
   250
        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
   251
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   252
        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
   253
4014
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   254
    def RefreshView(self):
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   255
        for direction in directions:
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   256
            self.selected_lists[direction].ResetDVCColumns()
da52eabd0e6d MQTT: Update available type choices when data types are changed in the IDE
Edouard Tisserant <edouard@beremiz.fr>
parents: 4013
diff changeset
   257
        
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   258
    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
   259
        pass
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   260
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   261
    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
   262
        self.OnClose()
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   263
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   264
    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
   265
        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
   266
            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
   267
        
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   268
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   269
class MQTTClientList(list):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   270
    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
   271
        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
   272
        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
   273
        self.change_callback = change_callback
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   274
        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
   275
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
   276
    def _filter_line(self, value):
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   277
        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
   278
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   279
        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
   280
            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
   281
                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
   282
            else:
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   283
                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
   284
                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
   285
                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
   286
                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
   287
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   288
        try:
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   289
            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
   290
                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
   291
        except ValueError: 
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   292
            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
   293
            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
   294
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
   295
        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
   296
            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
   297
            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
   298
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   299
        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
   300
            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
   301
            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
   302
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
   303
        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
   304
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
   305
    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
   306
        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
   307
        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
   308
            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
   309
            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
   310
            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
   311
        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
   312
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
    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
   314
        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
   315
        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
   316
            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
   317
            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
   318
            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
   319
        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
   320
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
   321
    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
   322
        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
   323
        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
   324
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   325
    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
   326
        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
   327
        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
   328
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
   329
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   330
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
   331
    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
   332
        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
   333
        for direction in directions:
3988
150599d9073f MQTT: WIP, subscibed topics have no "Retained" attribute.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3987
diff changeset
   334
            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
   335
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   336
    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
   337
        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
   338
            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
   339
            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
   340
            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
   341
                self[direction][:] = []
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   342
            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
   343
                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
   344
                # 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
   345
                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
   346
                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
   347
                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
   348
                    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
   349
                # 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
   350
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   351
    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
   352
        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
   353
            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
   354
                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
   355
                                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
   356
                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
   357
                    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
   358
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   359
    def GenerateC(self, path, locstr, config, datatype_info_getter):
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   360
        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
   361
        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
   362
        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
   363
        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
   364
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   365
        json_types = OD()
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   366
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   367
        formatdict = dict(
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   368
            locstr          = locstr,
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   369
            uri             = config["URI"],
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   370
            clientID        = config["clientID"],
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   371
            decl            = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   372
            topics          = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   373
            cleanup         = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   374
            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
   375
            init_pubsub     = "",
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   376
            retrieve        = "",
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   377
            publish         = "",
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   378
            publish_changes = "",
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   379
            json_decl       = ""
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   380
        )
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   381
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
   382
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
   383
        # 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
   384
        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
   385
            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
   386
#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
   387
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   388
        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
   389
        if AuthType == "x509":
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
   390
            for k in ["KeyStore","TrustStore"]:
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
   391
                config[k] = '"'+config[k]+'"' if config[k] else "NULL"
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   392
            formatdict["init"] += """
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
   393
    INIT_x509({Verify:d}, {KeyStore}, {TrustStore})""".format(**config)
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
   394
        if AuthType == "PSK":
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
   395
            formatdict["init"] += """
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 3999
diff changeset
   396
    INIT_PSK("{Secret}", "{ID}")""".format(**config)
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   397
        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
   398
            formatdict["init"] += """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   399
    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
   400
        else:
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   401
            formatdict["init"] += """
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   402
    INIT_NoAuth()"""
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   403
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   404
        for row in self["output"]:
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   405
            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
   406
            Retained = 1 if _Retained=="True" else 0
4011
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   407
            if iec_type in MQTT_IEC_types:
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   408
                C_type, iec_size_prefix = MQTT_IEC_types[iec_type]
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   409
                c_loc_name = "__Q" + iec_size_prefix + locstr + "_" + str(iec_number)
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   410
                encoding = "SIMPLE"
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   411
            elif iec_type in MQTT_UNSUPPORTED_types:
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   412
                raise Exception("Type "+iec_type+" is not supported in MQTT")
4011
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   413
            else:
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   414
                C_type = iec_type.upper();
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   415
                c_loc_name = "__Q" + locstr + "_" + str(iec_number)
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   416
                json_types.setdefault(iec_type,OD()).setdefault("OUTPUT",[]).append(c_loc_name)
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   417
                encoding = "JSON"
4011
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   418
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   419
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   420
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   421
            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
   422
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
   423
            formatdict["init_pubsub"] += """
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   424
    INIT_PUBLICATION({encoding}, {Topic}, {QoS}, {C_type}, {c_loc_name}, {Retained})""".format(**locals())
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   425
            formatdict["publish"] += """
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   426
        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
   427
            formatdict["publish_changes"] += """
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   428
            PUBLISH_CHANGE({encoding}, {Topic}, {QoS}, {C_type}, {c_loc_name}, {Retained})""".format(**locals())
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   429
4017
3977d55217ec MQTT: remove accidentally committed debug printfs + cosmetic
Edouard Tisserant <edouard@beremiz.fr>
parents: 4014
diff changeset
   430
        # inputs need to be sorted for bisection search
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   431
        for row in sorted(self["input"]):
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   432
            Topic, QoS, iec_type, iec_number = row
4011
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   433
            if iec_type in MQTT_IEC_types:
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   434
                C_type, iec_size_prefix = MQTT_IEC_types[iec_type]
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   435
                c_loc_name = "__I" + iec_size_prefix + locstr + "_" + str(iec_number)
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   436
                init_topic_call = "INIT_TOPIC"
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   437
            elif iec_type in MQTT_UNSUPPORTED_types:
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   438
                raise Exception("Type "+iec_type+" is not supported in MQTT")
4011
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   439
            else:
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   440
                C_type = iec_type.upper();
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   441
                c_loc_name = "__I" + locstr + "_" + str(iec_number)
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   442
                init_topic_call = "INIT_JSON_TOPIC"
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   443
                json_types.setdefault(iec_type,OD()).setdefault("INPUT",[]).append(c_loc_name)
4011
fdbb3c4ebbf6 MQTT: WIP supporting structured types : do not crash when generating C code if using location with no size
Edouard Tisserant <edouard@beremiz.fr>
parents: 4010
diff changeset
   444
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   445
            formatdict["decl"] += """
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   446
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
   447
            formatdict["topics"] += """
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   448
    {init_topic_call}({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
   449
            formatdict["init_pubsub"] += """
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   450
    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
   451
            formatdict["retrieve"] += """
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   452
        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
   453
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   454
        # collect all used type with their dependencies
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   455
        basetypes=[]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   456
        arrays=set()
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   457
        structures=set()
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   458
        already_generated_types = set()
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   459
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   460
        def recurseJsonTypes(datatype):
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   461
            # append derivated type first so we can expect the list
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   462
            # to be sorted with base types in last position
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   463
            basetypes.append(datatype)
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   464
            infos = datatype_info_getter(datatype)
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   465
            element_type = infos["type"]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   466
            if element_type == "Structure":
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   467
                structures.add(datatype)
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   468
                for element in infos["elements"]:
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   469
                    field_datatype = element["Type"]
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   470
                    if field_datatype not in MQTT_JSON_SUPPORTED_types and\
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   471
                       field_datatype not in MQTT_UNSUPPORTED_types:
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   472
                        recurseJsonTypes(field_datatype)
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   473
            elif element_type == "Array":
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   474
                arrays.add(datatype)
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   475
                item_datatype = infos["base_type"]
4022
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   476
                if item_datatype not in MQTT_JSON_SUPPORTED_types and\
dee0efbf8452 MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   477
                   item_datatype not in MQTT_UNSUPPORTED_types:
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   478
                    recurseJsonTypes(item_datatype)
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   479
        def typeCategory(iec_type):
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   480
            if field_iec_type in arrays:
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   481
                return "ARRAY"
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   482
            elif field_iec_type in structures:
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   483
                return "OBJECT"
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   484
            return "SIMPLE"
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   485
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   486
        for iec_type,_instances in json_types.items():
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   487
            recurseJsonTypes(iec_type)
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   488
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   489
        # go backard to get most derivated type definition last
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   490
        # so that CPP can always find base type deinition before
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   491
        for iec_type in reversed(basetypes):
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   492
            # avoid repeating type definition
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   493
            if iec_type in already_generated_types:
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   494
                continue
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   495
            already_generated_types.add(iec_type)
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   496
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   497
            C_type = iec_type.upper()
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   498
            json_decl = "#define TYPE_" + C_type + "(_P, _A) \\\n"
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   499
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   500
            infos = datatype_info_getter(iec_type)
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   501
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   502
            element_type = infos["type"]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   503
            if element_type == "Structure":
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   504
                elements = infos["elements"]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   505
                last = len(elements) - 1
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   506
                for idx, element in enumerate(elements):
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   507
                    field_iec_type = element["Type"]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   508
                    if type(field_iec_type) == tuple and field_iec_type[0] == "array":
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   509
                        raise Exception("Inline arrays in structure are not supported. Please use a separate data type for array.")
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   510
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   511
                    field_C_type = field_iec_type.upper()
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   512
                    field_name = element["Name"]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   513
                    field_C_name = field_name.upper()
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   514
                    decl_type = typeCategory(field_iec_type)
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   515
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   516
                    json_decl += ("    _P##_" + decl_type + "(" + 
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   517
                                  field_C_type + ", " + field_C_name + ", " + field_name + ", _A)" +
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   518
                                  ("\n\n" if idx == last else " _P##_separator \\\n"))
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   519
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   520
            elif element_type == "Array":
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   521
                dimensions = infos["dimensions"]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   522
                if len(dimensions) > 1:
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   523
                    raise Exception("Only 1 dimension arrays are supported")
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   524
                count = int(dimensions[0][1]) - int(dimensions[0][0]) + 1
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   525
                field_iec_type = infos["base_type"]
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   526
                decl_type = typeCategory(field_iec_type)
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   527
                field_C_type = field_iec_type.upper()
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   528
                last = count - 1
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   529
                for idx in range(count):
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   530
                    json_decl += ("    _P##_ARRAY_" + decl_type + "(" +
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   531
                                  field_C_type + ", " + repr(idx) + " , _A)" +
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   532
                                  ("\n\n" if idx == last else " _P##_separator \\\n"))
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   533
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   534
            formatdict["json_decl"] += json_decl
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   535
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   536
        for iec_type, instances in json_types.items():
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   537
            C_type = iec_type.upper()
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   538
            for direction, instance_list in instances.items():
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   539
                for c_loc_name in instance_list:
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   540
                    formatdict["json_decl"] += "DECL_JSON_"+direction+"("+C_type+", "+c_loc_name+")\n"
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4011
diff changeset
   541
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   542
        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
   543
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   544
        return Ccode
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   545