opc_ua/opcua_client_maker.py
author Edouard Tisserant <edouard@beremiz.fr>
Tue, 23 Jul 2024 17:05:59 +0200
changeset 3999 1479acf750e2
parent 3820 46f3ca3f0157
permissions -rw-r--r--
MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.

Fix loading of CSV that was not applying model types, and that not checking conformance either.
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
     1
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
     2
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
import csv
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
     5
import asyncio
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
     6
import functools
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
     7
from threading import Thread
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
     8
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
     9
from asyncua import Client
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    10
from asyncua import ua
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
import wx
3371
d486b98d7005 Backed out changeset c646a1f0fb0b - was on wrong branch
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3369
diff changeset
    13
import wx.lib.gizmos as gizmos  # Formerly wx.gizmos in Classic
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
import wx.dataview as dv
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
UA_IEC_types = dict(
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
#   pyopcua | IEC61131|  C  type  | sz |  open62541  enum  | open62541
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
    Boolean = ("BOOL" , "uint8_t" , "X", "UA_TYPES_BOOLEAN", "UA_Boolean"),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
    SByte   = ("SINT" , "int8_t"  , "B", "UA_TYPES_SBYTE"  , "UA_SByte"  ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
    Byte    = ("USINT", "uint8_t" , "B", "UA_TYPES_BYTE"   , "UA_Byte"   ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
    Int16   = ("INT"  , "int16_t" , "W", "UA_TYPES_INT16"  , "UA_Int16"  ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
    UInt16  = ("UINT" , "uint16_t", "W", "UA_TYPES_UINT16" , "UA_UInt16" ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
    Int32   = ("DINT" , "uint32_t", "D", "UA_TYPES_INT32"  , "UA_Int32"  ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
    UInt32  = ("UDINT", "int32_t" , "D", "UA_TYPES_UINT32" , "UA_UInt32" ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
    Int64   = ("LINT" , "int64_t" , "L", "UA_TYPES_INT64"  , "UA_Int64"  ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
    UInt64  = ("ULINT", "uint64_t", "L", "UA_TYPES_UINT64" , "UA_UInt64" ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
    Float   = ("REAL" , "float"   , "D", "UA_TYPES_FLOAT"  , "UA_Float"  ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
    Double  = ("LREAL", "double"  , "L", "UA_TYPES_DOUBLE" , "UA_Double" ),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
UA_NODE_ID_types = {
3338
fe0da9a8a225 OPCUS client: fix handling of string type Node Identifier
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
    33
    "int"   : ("UA_NODEID_NUMERIC", "{}"  ),
fe0da9a8a225 OPCUS client: fix handling of string type Node Identifier
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
    34
    "str"   : ("UA_NODEID_STRING" , '"{}"'),
3406
3aac4241dafa OPC-UA: cosmetic fix and armless typo
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3392
diff changeset
    35
    "UUID"  : ("UA_NODEID_UUID"   , '"{}"'),
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
}
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
lstcolnames  = [  "Name", "NSIdx", "IdType", "Id", "Type", "IEC"]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
lstcolwidths = [     100,      50,      100,  100,    100,    50]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
lstcoltypess = [     str,     int,      str,  str,    str,   int]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
directions = ["input", "output"]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    44
authParams = {
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    45
    "x509":[
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    46
        ("Certificate", "certificate.der"),
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    47
        ("PrivateKey", "private_key.pem"),
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    48
        ("Policy", "Basic256Sha256"),
3652
b5c6bb72bfc9 Backed out changeset c6068b674b30
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3643
diff changeset
    49
        ("Mode", "SignAndEncrypt")],
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    50
    "UserPassword":[
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    51
        ("User", None),
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    52
        ("Password", None)]}
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
    53
3371
d486b98d7005 Backed out changeset c646a1f0fb0b - was on wrong branch
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3369
diff changeset
    54
class OPCUASubListModel(dv.DataViewIndexListModel):
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
    def __init__(self, data, log):
3371
d486b98d7005 Backed out changeset c646a1f0fb0b - was on wrong branch
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3369
diff changeset
    56
        dv.DataViewIndexListModel.__init__(self, len(data))
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
        self.data = data
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
        self.log = log
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
    def GetColumnType(self, col):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
        return "string"
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
    def GetValueByRow(self, row, col):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
        return str(self.data[row][col])
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
    # This method is called when the user edits a data item in the view.
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
    def SetValueByRow(self, value, row, col):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
        expectedtype = lstcoltypess[col]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
        try:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
            v = expectedtype(value)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
        except ValueError: 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
            self.log("String {} is invalid for type {}\n".format(value,expectedtype.__name__))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
            return False
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
        if col == lstcolnames.index("IdType") and v not in UA_NODE_ID_types:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
            self.log("{} is invalid for IdType\n".format(value))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
            return False
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
        self.data[row][col] = v
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    81
        return True
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    82
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    83
    # Report how many columns this model provides data for.
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    84
    def GetColumnCount(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    85
        return len(lstcolnames)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    86
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    87
    # Report the number of rows in the model
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    88
    def GetCount(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    89
        #self.log.write('GetCount')
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    90
        return len(self.data)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    91
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    92
    # Called to check if non-standard attributes should be used in the
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    93
    # cell at (row, col)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    94
    def GetAttrByRow(self, row, col, attr):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    95
        if col == 5:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    96
            attr.SetColour('blue')
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    97
            attr.SetBold(True)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    98
            return True
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    99
        return False
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   100
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   101
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   102
    def DeleteRows(self, rows):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   103
        # make a copy since we'll be sorting(mutating) the list
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   104
        # use reverse order so the indexes don't change as we remove items
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   105
        rows = sorted(rows, reverse=True)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   106
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   107
        for row in rows:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   108
            # remove it from our data structure
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   109
            del self.data[row]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   110
            # notify the view(s) using this model that it has been removed
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   111
            self.RowDeleted(row)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   112
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   113
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   114
    def AddRow(self, value):
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   115
        if self.data.append(value):
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   116
            # notify views
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   117
            self.RowAppended()
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   118
    
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   119
    def ResetData(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   120
        self.Reset(len(self.data))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   121
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   122
OPCUAClientDndMagicWord = "text/beremiz-opcuaclient"
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   123
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   124
class NodeDropTarget(wx.DropTarget):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   125
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   126
    def __init__(self, parent):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   127
        data = wx.CustomDataObject(OPCUAClientDndMagicWord)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   128
        wx.DropTarget.__init__(self, data)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   129
        self.ParentWindow = parent
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   130
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   131
    def OnDrop(self, x, y):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   132
        self.ParentWindow.OnNodeDnD()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   133
        return True
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   134
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   135
class OPCUASubListPanel(wx.Panel):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   136
    def __init__(self, parent, log, model, direction):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   137
        self.log = log
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
        wx.Panel.__init__(self, parent, -1)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
        self.dvc = dv.DataViewCtrl(self,
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
                                   style=wx.BORDER_THEME
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
                                   | dv.DV_ROW_LINES
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
                                   | dv.DV_HORIZ_RULES
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
                                   | dv.DV_VERT_RULES
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   145
                                   | dv.DV_MULTIPLE
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
                                   )
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
        self.model = model
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
        self.dvc.AssociateModel(self.model)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
        for idx,(colname,width) in enumerate(zip(lstcolnames,lstcolwidths)):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
            self.dvc.AppendTextColumn(colname,  idx, width=width, mode=dv.DATAVIEW_CELL_EDITABLE)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   154
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   155
        DropTarget = NodeDropTarget(self)
3666
bf3b5f58c832 OPC-UA: Extend Drag'N'Drop target to whole input or output panel not only grid.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3652
diff changeset
   156
        self.SetDropTarget(DropTarget)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   157
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   158
        self.Sizer = wx.BoxSizer(wx.VERTICAL)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   159
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   160
        self.direction =  direction
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   161
        titlestr = direction + " variables"
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   162
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   163
        title = wx.StaticText(self, label = titlestr)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   164
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   165
        delbt = wx.Button(self, label="Delete Row(s)")
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   166
        self.Bind(wx.EVT_BUTTON, self.OnDeleteRows, delbt)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   167
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   168
        topsizer = wx.BoxSizer(wx.HORIZONTAL)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   169
        topsizer.Add(title, 1, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 5)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   170
        topsizer.Add(delbt, 0, wx.LEFT|wx.RIGHT, 5)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   171
        self.Sizer.Add(topsizer, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   172
        self.Sizer.Add(self.dvc, 1, wx.EXPAND)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   173
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   174
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   175
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   176
    def OnDeleteRows(self, evt):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   177
        items = self.dvc.GetSelections()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   178
        rows = [self.model.GetRow(item) for item in items]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   179
        self.model.DeleteRows(rows)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   180
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   181
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   182
    def OnNodeDnD(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
        # Have to find OPC-UA client extension panel from here 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
        # in order to avoid keeping reference (otherwise __del__ isn't called)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   185
        #             splitter.        panel.      splitter
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   186
        ClientPanel = self.GetParent().GetParent().GetParent()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   187
        nodes = ClientPanel.GetSelectedNodes()
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   188
        for node, properties in nodes:
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   189
            if properties.cname != "Variable":
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   190
                self.log("Node {} ignored (not a variable)".format(properties.dname))
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   191
                continue
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   192
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   193
            tname = properties.variant_type
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   194
            if tname not in UA_IEC_types:
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   195
                self.log("Node {} ignored (unsupported type)".format(properties.dname))
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   196
                continue
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   197
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   198
            if {"input":ua.AccessLevel.CurrentRead,
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   199
                "output":ua.AccessLevel.CurrentWrite}[self.direction] not in properties.access:
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   200
                self.log("Node {} ignored because of insuficient access rights".format(properties.dname))
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   201
                continue
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   202
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   203
            nid_type =  type(properties.nid).__name__
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   204
            iecid = properties.nid
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   205
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   206
            value = [properties.dname,
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   207
                     properties.nsid,
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   208
                     nid_type,
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   209
                     properties.nid,
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   210
                     tname,
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   211
                     iecid]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   212
            self.model.AddRow(value)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   213
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   214
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   215
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   216
il = None
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   217
fldridx = None    
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   218
fldropenidx = None
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   219
fileidx = None
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   220
smileidx = None
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   221
isz = (16,16)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   222
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   223
treecolnames  = [  "Name", "Class", "NSIdx", "Id"]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   224
treecolwidths = [     250,     100,      50,  200]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   225
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   226
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   227
def prepare_image_list():
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   228
    global il, fldridx, fldropenidx, fileidx, smileidx    
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   229
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   230
    if il is not None: 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   231
        return
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   232
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   233
    il = wx.ImageList(isz[0], isz[1])
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   234
    fldridx     = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER,      wx.ART_OTHER, isz))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   235
    fldropenidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN,   wx.ART_OTHER, isz))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   236
    fileidx     = il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   237
    smileidx    = il.Add(wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK, wx.ART_OTHER, isz))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   238
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   239
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   240
AsyncUAClientLoop = None
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   241
def AsyncUAClientLoopProc():
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   242
    asyncio.set_event_loop(AsyncUAClientLoop)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   243
    AsyncUAClientLoop.run_forever()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   244
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   245
def ExecuteSychronously(func, timeout=1):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   246
    def AsyncSychronizer(*args, **kwargs):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   247
        global AsyncUAClientLoop
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   248
        # create asyncio loop
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   249
        if AsyncUAClientLoop is None:
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   250
            AsyncUAClientLoop = asyncio.new_event_loop()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   251
            Thread(target=AsyncUAClientLoopProc, daemon=True).start()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   252
        # schedule work in this loop
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   253
        future = asyncio.run_coroutine_threadsafe(func(*args, **kwargs), AsyncUAClientLoop)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   254
        # wait max 5sec until connection completed
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   255
        return future.result(timeout)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   256
    return AsyncSychronizer
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   257
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   258
def ExecuteSychronouslyWithTimeout(timeout):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   259
    return functools.partial(ExecuteSychronously,timeout=timeout)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   260
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   261
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   262
class OPCUAClientPanel(wx.SplitterWindow):
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   263
    def __init__(self, parent, modeldata, log, config_getter):
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   264
        self.log = log
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   265
        wx.SplitterWindow.__init__(self, parent, -1)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   266
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   267
        self.ordered_nps = []
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   268
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   269
        self.inout_panel = wx.Panel(self)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   270
        self.inout_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   271
        self.inout_sizer.AddGrowableCol(0)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   272
        self.inout_sizer.AddGrowableRow(1)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   273
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   274
        self.clientloop = None
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   275
        self.client = None
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   276
        self.config_getter = config_getter
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   277
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   278
        self.connect_button = wx.ToggleButton(self.inout_panel, -1, "Browse Server")
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   279
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   280
        self.selected_splitter = wx.SplitterWindow(self.inout_panel, style=wx.SUNKEN_BORDER | wx.SP_3D)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   281
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   282
        self.selected_datas = modeldata
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   283
        self.selected_models = { direction:OPCUASubListModel(self.selected_datas[direction], log) for direction in directions }
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   284
        self.selected_lists = { direction:OPCUASubListPanel(
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   285
                self.selected_splitter, log, 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   286
                self.selected_models[direction], direction) 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   287
            for direction in directions }
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   288
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   289
        self.selected_splitter.SplitHorizontally(*[self.selected_lists[direction] for direction in directions]+[300])
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   290
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   291
        self.inout_sizer.Add(self.connect_button, flag=wx.GROW)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   292
        self.inout_sizer.Add(self.selected_splitter, flag=wx.GROW)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   293
        self.inout_sizer.Layout()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   294
        self.inout_panel.SetAutoLayout(True)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   295
        self.inout_panel.SetSizer(self.inout_sizer)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   296
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   297
        self.Initialize(self.inout_panel)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   298
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   299
        self.Bind(wx.EVT_TOGGLEBUTTON, self.OnConnectButton, self.connect_button)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   300
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   301
    def OnClose(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   302
        if self.client is not None:
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   303
            asyncio.run(self.client.disconnect())
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   304
            self.client = None
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   305
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   306
    def __del__(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   307
        self.OnClose()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   308
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   309
    async def GetAsyncUANodeProperties(self, node):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   310
        properties = type("UANodeProperties",(),dict(
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   311
                nsid = node.nodeid.NamespaceIndex,
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   312
                nid =  node.nodeid.Identifier,
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   313
                dname = (await node.read_display_name()).Text,
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   314
                cname = (await node.read_node_class()).name,
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   315
            ))
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   316
        if properties.cname == "Variable":
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   317
            properties.access = await node.get_access_level()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   318
            properties.variant_type = (await node.read_data_type_as_variant_type()).name
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   319
        return properties
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   320
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   321
    @ExecuteSychronouslyWithTimeout(5)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   322
    async def ConnectAsyncUAClient(self, config):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   323
        client = Client(config["URI"])
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   324
        
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   325
        AuthType = config["AuthType"]
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   326
        if AuthType=="UserPasword":
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   327
            await client.set_user(config["User"])
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   328
            await client.set_password(config["Password"])
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   329
        elif AuthType=="x509":
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   330
            await client.set_security_string(
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   331
                "{Policy},{Mode},{Certificate},{PrivateKey}".format(**config))
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   332
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   333
        await client.connect()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   334
        self.client = client
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   335
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   336
        # load definition of server specific structures/extension objects
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   337
        await self.client.load_type_definitions()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   338
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   339
        # returns root node object and its properties
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   340
        rootnode = self.client.get_root_node()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   341
        return rootnode, await self.GetAsyncUANodeProperties(rootnode)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   342
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   343
    @ExecuteSychronously
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   344
    async def DisconnectAsyncUAClient(self):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   345
        if self.client is not None:
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   346
            await self.client.disconnect()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   347
            self.client = None
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   348
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   349
    @ExecuteSychronously
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   350
    async def GetAsyncUANodeChildren(self, node):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   351
        children = await node.get_children()
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   352
        return [ (child, await self.GetAsyncUANodeProperties(child)) for child in children]
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   353
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   354
    def OnConnectButton(self, event):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   355
        if self.connect_button.GetValue():
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   356
            
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   357
            config = self.config_getter()
3667
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   358
            self.log("OPCUA browser: connecting to {}\n".format(config["URI"]))
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   359
            
3667
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   360
            try :
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   361
                rootnode, rootnodeproperties = self.ConnectAsyncUAClient(config)
3667
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   362
            except Exception as e:
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   363
                self.log("Exception in OPCUA browser: "+repr(e)+"\n")
3667
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   364
                self.client = None
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   365
                self.connect_button.SetValue(False)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   366
                return
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   367
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   368
            self.tree_panel = wx.Panel(self)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   369
            self.tree_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   370
            self.tree_sizer.AddGrowableCol(0)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   371
            self.tree_sizer.AddGrowableRow(0)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   372
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   373
            self.tree = gizmos.TreeListCtrl(self.tree_panel, -1, style=0, agwStyle=
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   374
                                            gizmos.TR_DEFAULT_STYLE
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   375
                                            | gizmos.TR_MULTIPLE
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   376
                                            | gizmos.TR_FULL_ROW_HIGHLIGHT
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   377
                                       )
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   378
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   379
            prepare_image_list()
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   380
            self.tree.SetImageList(il)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   381
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   382
            for idx,(colname, width) in enumerate(zip(treecolnames, treecolwidths)):
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   383
                self.tree.AddColumn(colname)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   384
                self.tree.SetColumnWidth(idx, width)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   385
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   386
            self.tree.SetMainColumn(0)
49e6b73de505 OPC-UA: better handling and reporting of server browser's connection errors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3666
diff changeset
   387
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   388
            rootitem = self.AddNodeItem(self.tree.AddRoot, rootnode, rootnodeproperties)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   389
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   390
            # Populate first level so that root can be expanded
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   391
            self.CreateSubItems(rootitem)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   392
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   393
            self.tree.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnExpand)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   394
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   395
            self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeNodeSelection)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   396
            self.tree.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   397
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   398
            self.tree.Expand(rootitem)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   399
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   400
            hint = wx.StaticText(self.tree_panel, label = "Drag'n'drop desired variables from tree to Input or Output list")
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   401
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   402
            self.tree_sizer.Add(self.tree, flag=wx.GROW)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   403
            self.tree_sizer.Add(hint, flag=wx.GROW)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   404
            self.tree_sizer.Layout()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   405
            self.tree_panel.SetAutoLayout(True)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   406
            self.tree_panel.SetSizer(self.tree_sizer)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   407
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   408
            self.SplitVertically(self.tree_panel, self.inout_panel, 500)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   409
        else:
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   410
            self.DisconnectAsyncUAClient()
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   411
            self.Unsplit(self.tree_panel)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   412
            self.tree_panel.Destroy()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   413
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   414
    def CreateSubItems(self, item):
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   415
        node, properties, browsed = self.tree.GetPyData(item)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   416
        if not browsed:
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   417
            children = self.GetAsyncUANodeChildren(node)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   418
            for subnode, subproperties in children:
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   419
                self.AddNodeItem(lambda n: self.tree.AppendItem(item, n), subnode, subproperties)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   420
            self.tree.SetPyData(item,(node, properties, True))
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   421
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   422
    def AddNodeItem(self, item_creation_func, node, properties):
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   423
        item = item_creation_func(properties.dname)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   424
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   425
        if properties.cname == "Variable":
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   426
            access = properties.access
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   427
            normalidx = fileidx
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   428
            r = ua.AccessLevel.CurrentRead in access
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   429
            w = ua.AccessLevel.CurrentWrite in access
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   430
            if r and w:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   431
                ext = "RW"
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   432
            elif r:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   433
                ext = "RO"
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   434
            elif w:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   435
                ext = "WO"  # not sure this one exist
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   436
            else:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   437
                ext = "no access"  # not sure this one exist
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   438
            cname = "Var "+properties.variant_type+" (" + ext + ")"
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   439
        else:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   440
            normalidx = fldridx
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   441
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   442
        self.tree.SetPyData(item,(node, properties, False))
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   443
        self.tree.SetItemText(item, properties.cname, 1)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   444
        self.tree.SetItemText(item, str(properties.nsid), 2)
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   445
        self.tree.SetItemText(item, type(properties.nid).__name__+": "+str(properties.nid), 3)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   446
        self.tree.SetItemImage(item, normalidx, which = wx.TreeItemIcon_Normal)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   447
        self.tree.SetItemImage(item, fldropenidx, which = wx.TreeItemIcon_Expanded)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   448
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   449
        return item
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   450
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   451
    def OnExpand(self, evt):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   452
        for item in evt.GetItem().GetChildren():
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   453
            self.CreateSubItems(item)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   454
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   455
    # def OnActivate(self, evt):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   456
    #     item = evt.GetItem()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   457
    #     node, browsed = self.tree.GetPyData(item)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   458
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   459
    def OnTreeNodeSelection(self, event):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   460
        items = self.tree.GetSelections()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   461
        items_pydata = [self.tree.GetPyData(item) for item in items]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   462
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   463
        nps = [(node,properties) for node, properties, unused in items_pydata]
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   464
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   465
        # append new nodes to ordered list
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   466
        for np in nps:
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   467
            if np not in self.ordered_nps:
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   468
                self.ordered_nps.append(np)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   469
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   470
        # filter out vanished items
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   471
        self.ordered_nps = [
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   472
            np 
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   473
            for np in self.ordered_nps 
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   474
            if np in nps]
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   475
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   476
    def GetSelectedNodes(self):
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   477
        return self.ordered_nps 
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   478
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   479
    def OnTreeBeginDrag(self, event):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   480
        """
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   481
        Called when a drag is started in tree
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   482
        @param event: wx.TreeEvent
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   483
        """
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   484
        if self.ordered_nps:
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   485
            # Just send a recognizable mime-type, drop destination
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   486
            # will get python data from parent
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   487
            data = wx.CustomDataObject(OPCUAClientDndMagicWord)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   488
            dragSource = wx.DropSource(self)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   489
            dragSource.SetData(data)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   490
            dragSource.DoDragDrop()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   491
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   492
    def Reset(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   493
        for direction in directions:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   494
            self.selected_models[direction].ResetData() 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   495
        
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   496
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   497
class OPCUAClientList(list):
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   498
    def __init__(self, log, change_callback):
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   499
        super(OPCUAClientList, self).__init__(self)
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   500
        self.log = log
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   501
        self.change_callback = change_callback
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   502
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   503
    def append(self, value):
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
   504
        v = dict(list(zip(lstcolnames, value)))
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   505
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   506
        if type(v["IEC"]) != int:
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   507
            if len(self) == 0:
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   508
                v["IEC"] = 0
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   509
            else:
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   510
                iecnums = set(zip(*self)[lstcolnames.index("IEC")])
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   511
                greatest = max(iecnums)
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   512
                holes = set(range(greatest)) - iecnums
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   513
                v["IEC"] = min(holes) if holes else greatest+1
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   514
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   515
        if v["IdType"] not in UA_NODE_ID_types:
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   516
            self.log("Unknown IdType\n".format(value))
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   517
            return False
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   518
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   519
        try:
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   520
            for t,n in zip(lstcoltypess, lstcolnames):
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   521
                v[n] = t(v[n]) 
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   522
        except ValueError: 
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   523
            self.log("Variable {} (Id={}) has invalid type\n".format(v["Name"],v["Id"]))
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   524
            return False
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   525
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
   526
        if len(self)>0 and v["Id"] in list(zip(*self))[lstcolnames.index("Id")]:
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   527
            self.log("Variable {} (Id={}) already in list\n".format(v["Name"],v["Id"]))
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   528
            return False
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   529
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   530
        list.append(self, [v[n] for n in lstcolnames])
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   531
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   532
        self.change_callback()
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   533
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   534
        return True
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   535
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   536
    def __delitem__(self, index):
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   537
        list.__delitem__(self, index)
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   538
        self.change_callback()
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   539
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   540
class OPCUAClientModel(dict):
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   541
    def __init__(self, log, change_callback = lambda : None):
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   542
        super(OPCUAClientModel, self).__init__()
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   543
        for direction in directions:
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   544
            self[direction] = OPCUAClientList(log, change_callback)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   545
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   546
    def LoadCSV(self,path):
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   547
        with open(path, 'r') as csvfile:
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   548
            reader = csv.reader(csvfile, delimiter=',', quotechar='"')
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
   549
            buf = {direction:[] for direction, _model in self.items()}
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
   550
            for direction, model in self.items():
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   551
                self[direction][:] = []
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   552
            for row in reader:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   553
                direction = row[0]
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   554
                # avoids calling change callback whe loading CSV
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3667
diff changeset
   555
                list.append(self[direction],row[1:])
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   556
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   557
    def SaveCSV(self,path):
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   558
        with open(path, 'w') as csvfile:
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
   559
            for direction, data in self.items():
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   560
                writer = csv.writer(csvfile, delimiter=',',
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   561
                                quotechar='"', quoting=csv.QUOTE_MINIMAL)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   562
                for row in data:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   563
                    writer.writerow([direction] + row)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   564
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   565
    def GenerateC(self, path, locstr, config):
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   566
        template = """/* code generated by beremiz OPC-UA extension */
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   567
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   568
#include <open62541/client_config_default.h>
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   569
#include <open62541/client_highlevel.h>
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   570
#include <open62541/plugin/log_stdout.h>
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   571
#include <open62541/plugin/securitypolicy.h>
3677
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   572
#include <open62541/plugin/securitypolicy_default.h>
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   573
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   574
#include <open62541/types.h>
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   575
#include <open62541/types_generated_handling.h>
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   576
3633
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   577
#define _Log(level, ...)                                                                           \\
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   578
    {{                                                                                             \\
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   579
        char mstr[256];                                                                            \\
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   580
        snprintf(mstr, 255, __VA_ARGS__);                                                          \\
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   581
        LogMessage(level, mstr, strlen(mstr));                                                     \\
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   582
    }}
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   583
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   584
#define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__);
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   585
#define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__);
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   586
#define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__);
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   587
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   588
static UA_INLINE UA_ByteString
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   589
loadFile(const char *const path) {{
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   590
    UA_ByteString fileContents = UA_STRING_NULL;
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   592
    FILE *fp = fopen(path, "rb");
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   593
    if(!fp) {{
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   594
        errno = 0;
3633
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   595
        LogError("OPC-UA could not open %s", path);
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   596
        return fileContents;
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   597
    }}
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   598
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   599
    fseek(fp, 0, SEEK_END);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   600
    fileContents.length = (size_t)ftell(fp);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   601
    fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   602
    if(fileContents.data) {{
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   603
        fseek(fp, 0, SEEK_SET);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   604
        size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
3633
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   605
        if(read != fileContents.length){{
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   606
            UA_ByteString_clear(&fileContents);
3633
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   607
            LogError("OPC-UA could not read %s", path);
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   608
        }}
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   609
    }} else {{
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   610
        fileContents.length = 0;
3633
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   611
        LogError("OPC-UA Not enough memoty to load %s", path);
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   612
    }}
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   613
    fclose(fp);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   614
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   615
    return fileContents;
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   616
}}
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   617
3407
2f5c30054d50 OPC-UA client: "static" class was forgotten for open62541 client structure preventing multiple client.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3406
diff changeset
   618
static UA_Client *client;
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   619
static UA_ClientConfig *cc;
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   620
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   621
#define DECL_VAR(ua_type, C_type, c_loc_name)                                                       \\
3407
2f5c30054d50 OPC-UA client: "static" class was forgotten for open62541 client structure preventing multiple client.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3406
diff changeset
   622
static UA_Variant c_loc_name##_variant;                                                             \\
2f5c30054d50 OPC-UA client: "static" class was forgotten for open62541 client structure preventing multiple client.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3406
diff changeset
   623
static C_type c_loc_name##_buf = 0;                                                                 \\
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   624
C_type *c_loc_name = &c_loc_name##_buf;
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   625
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   626
{decl}
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   627
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   628
void __cleanup_{locstr}(void)
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   629
{{
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   630
    UA_Client_disconnect(client);
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   631
    UA_Client_delete(client);
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   632
}}
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   633
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   634
#define INIT_NoAuth()                                                                              \\
3620
1cf21430ed4a OPCUA: cosmetic fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3612
diff changeset
   635
    LogInfo("OPC-UA Init no auth");                                                                \\
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   636
    UA_ClientConfig_setDefault(cc);                                                                \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   637
    retval = UA_Client_connect(client, uri);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   638
3677
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   639
/* Note : Single policy is enforced here, by default open62541 client supports all policies */
3652
b5c6bb72bfc9 Backed out changeset c6068b674b30
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3643
diff changeset
   640
#define INIT_x509(Policy, UpperCaseMode, PrivateKey, Certificate)                                  \\
3633
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   641
    LogInfo("OPC-UA Init x509 %s,%s,%s,%s", #Policy, #UpperCaseMode, PrivateKey, Certificate);     \\
837e7782d83e OPC-UA: better logging in case of failure at init
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3620
diff changeset
   642
                                                                                                   \\
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   643
    UA_ByteString certificate = loadFile(Certificate);                                             \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   644
    UA_ByteString privateKey  = loadFile(PrivateKey);                                              \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   645
                                                                                                   \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   646
    cc->securityMode = UA_MESSAGESECURITYMODE_##UpperCaseMode;                                     \\
3677
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   647
                                                                                                   \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   648
    /* replacement for default behaviour */                                                        \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   649
    /* UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0); */     \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   650
    do{{                                                                                           \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   651
        retval = UA_ClientConfig_setDefault(cc);                                                   \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   652
        if(retval != UA_STATUSCODE_GOOD)                                                           \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   653
            break;                                                                                 \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   654
                                                                                                   \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   655
        UA_SecurityPolicy *sp = (UA_SecurityPolicy*)                                               \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   656
            UA_realloc(cc->securityPolicies, sizeof(UA_SecurityPolicy) * 2);                       \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   657
        if(!sp){{                                                                                  \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   658
            retval = UA_STATUSCODE_BADOUTOFMEMORY;                                                 \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   659
            break;                                                                                 \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   660
        }}                                                                                         \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   661
        cc->securityPolicies = sp;                                                                 \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   662
                                                                                                   \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   663
        retval = UA_SecurityPolicy_##Policy(&cc->securityPolicies[cc->securityPoliciesSize],       \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   664
                                                 certificate, privateKey, &cc->logger);            \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   665
        if(retval != UA_STATUSCODE_GOOD) {{                                                        \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   666
            UA_LOG_WARNING(&cc->logger, UA_LOGCATEGORY_USERLAND,                                   \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   667
                           "Could not add SecurityPolicy Policy with error code %s",               \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   668
                           UA_StatusCode_name(retval));                                            \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   669
            UA_free(cc->securityPolicies);                                                         \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   670
            cc->securityPolicies = NULL;                                                           \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   671
            break;                                                                                 \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   672
        }}                                                                                         \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   673
                                                                                                   \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   674
        ++cc->securityPoliciesSize;                                                                \\
6d9040e07c32 OPC-UA: only support the encryption policy selected in config.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   675
    }} while(0);                                                                                   \\
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   676
                                                                                                   \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   677
    retval = UA_Client_connect(client, uri);                                                       \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   678
                                                                                                   \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   679
    UA_ByteString_clear(&certificate);                                                             \\
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   680
    UA_ByteString_clear(&privateKey);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   681
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   682
#define INIT_UserPassword(User, Password)                                                          \\
3620
1cf21430ed4a OPCUA: cosmetic fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3612
diff changeset
   683
    LogInfo("OPC-UA Init UserPassword %s,%s", User, Password);                                     \\
3612
428fb2472f43 OPC-UA: add logging to help user to understang what went wrong when connecting.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3591
diff changeset
   684
    UA_ClientConfig_setDefault(cc);                                                                \\
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   685
    retval = UA_Client_connectUsername(client, uri, User, Password);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   686
3406
3aac4241dafa OPC-UA: cosmetic fix and armless typo
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3392
diff changeset
   687
#define INIT_READ_VARIANT(ua_type, c_loc_name)                                                     \\
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   688
    UA_Variant_init(&c_loc_name##_variant);
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   689
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   690
#define INIT_WRITE_VARIANT(ua_type, ua_type_enum, c_loc_name)                                      \\
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   691
    UA_Variant_setScalar(&c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]);
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   692
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   693
int __init_{locstr}(int argc,char **argv)
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   694
{{
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   695
    UA_StatusCode retval;
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   696
    client = UA_Client_new();
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   697
    cc = UA_Client_getConfig(client);
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   698
    char *uri = "{uri}";
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   699
{init}
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   700
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   701
    if(retval != UA_STATUSCODE_GOOD) {{
3620
1cf21430ed4a OPCUA: cosmetic fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3612
diff changeset
   702
        LogError("OPC-UA Init Failed %d", retval);
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   703
        UA_Client_delete(client);
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   704
        return EXIT_FAILURE;
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   705
    }}
3612
428fb2472f43 OPC-UA: add logging to help user to understang what went wrong when connecting.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3591
diff changeset
   706
    return 0;
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   707
}}
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   708
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   709
#define READ_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id)        \\
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   710
    retval = UA_Client_readValueAttribute(                                                         \\
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   711
        client, ua_nodeid_type(ua_nsidx, ua_node_id), &c_loc_name##_variant);                      \\
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   712
    if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(&c_loc_name##_variant) &&               \\
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   713
       c_loc_name##_variant.type == &UA_TYPES[ua_type_enum]) {{                                    \\
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   714
            c_loc_name##_buf = *(ua_type*)c_loc_name##_variant.data;                               \\
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   715
            UA_Variant_clear(&c_loc_name##_variant);  /* Unalloc requiered on each read ! */       \\
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   716
    }}
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   717
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   718
void __retrieve_{locstr}(void)
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   719
{{
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   720
    UA_StatusCode retval;
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   721
{retrieve}
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   722
}}
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   723
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   724
#define WRITE_VALUE(ua_type, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id)                     \\
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   725
    UA_Client_writeValueAttribute(                                                                 \\
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   726
        client, ua_nodeid_type(ua_nsidx, ua_node_id), &c_loc_name##_variant);
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   727
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   728
void __publish_{locstr}(void)
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   729
{{
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   730
{publish}
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   731
}}
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   732
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   733
"""
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   734
        
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   735
        formatdict = dict(
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   736
            locstr   = locstr,
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   737
            uri      = config["URI"],
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   738
            decl     = "",
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   739
            cleanup  = "",
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   740
            init     = "",
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   741
            retrieve = "",
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   742
            publish  = "" 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   743
        )
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   744
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   745
        AuthType = config["AuthType"]
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   746
        if AuthType == "x509":
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   747
            config["UpperCaseMode"] = config["Mode"].upper()
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   748
            formatdict["init"] += """
3652
b5c6bb72bfc9 Backed out changeset c6068b674b30
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3643
diff changeset
   749
    INIT_x509({Policy}, {UpperCaseMode}, "{PrivateKey}", "{Certificate}")""".format(**config)
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   750
        elif AuthType == "UserPassword":
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   751
            formatdict["init"] += """
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   752
    INIT_UserPassword("{User}", "{Password}")""".format(**config)
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   753
        else:
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   754
            formatdict["init"] += """
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   755
    INIT_NoAuth()"""
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   756
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
   757
        for direction, data in self.items():
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   758
            iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   759
            for row in data:
3338
fe0da9a8a225 OPCUS client: fix handling of string type Node Identifier
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   760
                name, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   761
                iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   762
                c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number)
3338
fe0da9a8a225 OPCUS client: fix handling of string type Node Identifier
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   763
                ua_nodeid_type, id_formating = UA_NODE_ID_types[ua_nodeid_type]
fe0da9a8a225 OPCUS client: fix handling of string type Node Identifier
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   764
                ua_node_id = id_formating.format(_ua_node_id)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   765
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   766
                formatdict["decl"] += """
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   767
DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals())
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   768
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   769
                if direction == "input":
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   770
                    formatdict["init"] += """
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   771
    INIT_READ_VARIANT({ua_type}, {c_loc_name})""".format(**locals())
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   772
                    formatdict["retrieve"] += """
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   773
    READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   774
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   775
                if direction == "output":
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   776
                    formatdict["init"] += """
3392
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   777
    INIT_WRITE_VARIANT({ua_type}, {ua_type_enum}, {c_loc_name})""".format(**locals())
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   778
                    formatdict["publish"] += """
4353c56a53d1 OPC-UA client: generated code now take UA_Variants on the stack directly, avoid useless allocation on write, and unalloc implicitely allocated data on read
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   779
    WRITE_VALUE({ua_type}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   780
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   781
        Ccode = template.format(**formatdict)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   782
        
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   783
        return Ccode
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   784
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   785
if __name__ == "__main__":
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   786
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   787
    import wx.lib.mixins.inspection as wit
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   788
    import sys,os
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   789
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   790
    app = wit.InspectableApp()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   791
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   792
    frame = wx.Frame(None, -1, "OPCUA Client Test App", size=(800,600))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   793
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   794
    argc = len(sys.argv)
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   795
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   796
    config={}
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   797
    config["URI"] = sys.argv[1] if argc>1 else "opc.tcp://localhost:4840"
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   798
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   799
    if argc > 2:
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   800
        AuthType = sys.argv[2]
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   801
        config["AuthType"] = AuthType
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3677
diff changeset
   802
        for (name, default), value in zip_longest(authParams[AuthType], sys.argv[3:]):
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   803
            if value is None:
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   804
                if default is None:
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   805
                    raise Exception(name+" param expected")
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   806
                value = default
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   807
            config[name] = value
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   808
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   809
    test_panel = wx.Panel(frame)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   810
    test_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   811
    test_sizer.AddGrowableCol(0)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   812
    test_sizer.AddGrowableRow(0)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   813
3378
e655ec8162e1 OPC-UA: fix types in model not being preserved after reload of CSV file, leading to exception when changing selection lists content.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3377
diff changeset
   814
    modeldata = OPCUAClientModel(print)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   815
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   816
    opcuatestpanel = OPCUAClientPanel(test_panel, modeldata, print, lambda:config)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   817
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   818
    def OnGenerate(evt):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   819
        dlg = wx.FileDialog(
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   820
            frame, message="Generate file as ...", defaultDir=os.getcwd(),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   821
            defaultFile="", 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   822
            wildcard="C (*.c)|*.c", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   823
            )
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   824
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   825
        if dlg.ShowModal() == wx.ID_OK:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   826
            path = dlg.GetPath()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   827
            Ccode = """
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   828
/*
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   829
In case open62541 was built just aside beremiz, you can build this test with:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   830
gcc %s -o %s \\
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   831
    -I ../../open62541/plugins/include/ \\
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   832
    -I ../../open62541/build/src_generated/ \\
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   833
    -I ../../open62541/include/ \\
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   834
    -I ../../open62541/arch/ ../../open62541/build/bin/libopen62541.a
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   835
*/
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   836
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3422
diff changeset
   837
"""%(path, path[:-2]) + modeldata.GenerateC(path, "test", config) + """
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   838
3612
428fb2472f43 OPC-UA: add logging to help user to understang what went wrong when connecting.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3591
diff changeset
   839
int LogMessage(uint8_t level, char* buf, uint32_t size){
3620
1cf21430ed4a OPCUA: cosmetic fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3612
diff changeset
   840
    printf("log level:%d message:'%.*s'\\n", level, size, buf);
3612
428fb2472f43 OPC-UA: add logging to help user to understang what went wrong when connecting.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3591
diff changeset
   841
};
428fb2472f43 OPC-UA: add logging to help user to understang what went wrong when connecting.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3591
diff changeset
   842
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   843
int main(int argc, char *argv[]) {
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   844
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   845
    __init_test(arc,argv);
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   846
   
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   847
    __retrieve_test();
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   848
   
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   849
    __publish_test();
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   850
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   851
    __cleanup_test();
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   852
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   853
    return EXIT_SUCCESS;
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   854
}
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   855
"""
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   856
3820
46f3ca3f0157 OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   857
            with open(path, 'w') as Cfile:
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   858
                Cfile.write(Ccode)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   859
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   860
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   861
        dlg.Destroy()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   862
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   863
    def OnLoad(evt):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   864
        dlg = wx.FileDialog(
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   865
            frame, message="Choose a file",
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   866
            defaultDir=os.getcwd(),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   867
            defaultFile="",
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   868
            wildcard="CSV (*.csv)|*.csv",
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   869
            style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST )
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   870
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   871
        if dlg.ShowModal() == wx.ID_OK:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   872
            path = dlg.GetPath()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   873
            modeldata.LoadCSV(path)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   874
            opcuatestpanel.Reset()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   875
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   876
        dlg.Destroy()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   877
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   878
    def OnSave(evt):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   879
        dlg = wx.FileDialog(
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   880
            frame, message="Save file as ...", defaultDir=os.getcwd(),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   881
            defaultFile="", 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   882
            wildcard="CSV (*.csv)|*.csv", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   883
            )
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   884
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   885
        if dlg.ShowModal() == wx.ID_OK:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   886
            path = dlg.GetPath()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   887
            modeldata.SaveCSV(path)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   888
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   889
        dlg.Destroy()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   890
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   891
    test_sizer.Add(opcuatestpanel, flag=wx.GROW)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   892
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   893
    testbt_sizer = wx.BoxSizer(wx.HORIZONTAL)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   894
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   895
    loadbt = wx.Button(test_panel, label="Load")
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   896
    test_panel.Bind(wx.EVT_BUTTON, OnLoad, loadbt)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   897
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   898
    savebt = wx.Button(test_panel, label="Save")
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   899
    test_panel.Bind(wx.EVT_BUTTON, OnSave, savebt)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   900
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   901
    genbt = wx.Button(test_panel, label="Generate")
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   902
    test_panel.Bind(wx.EVT_BUTTON, OnGenerate, genbt)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   903
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   904
    testbt_sizer.Add(loadbt, 0, wx.LEFT|wx.RIGHT, 5)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   905
    testbt_sizer.Add(savebt, 0, wx.LEFT|wx.RIGHT, 5)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   906
    testbt_sizer.Add(genbt, 0, wx.LEFT|wx.RIGHT, 5)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   907
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   908
    test_sizer.Add(testbt_sizer, flag=wx.GROW)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   909
    test_sizer.Layout()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   910
    test_panel.SetAutoLayout(True)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   911
    test_panel.SetSizer(test_sizer)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   912
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   913
    def OnClose(evt):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   914
        opcuatestpanel.OnClose()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   915
        evt.Skip()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   916
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   917
    frame.Bind(wx.EVT_CLOSE, OnClose)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   918
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   919
    frame.Show()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   920
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   921
    app.MainLoop()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   922