opc_ua/client.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 21 Oct 2021 20:56:05 +0200
changeset 3370 3c31dce1a661
parent 3366 1ca385429bba
child 3378 e655ec8162e1
permissions -rw-r--r--
Make OPC-UA client compatible with wxPython3, util wxPython4 branch deserve to be merged
3364
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
# opcua/client.py
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
from __future__ import absolute_import
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
import os
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
3366
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
     8
from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
     9
from .opcua_client_maker import OPCUAClientPanel, OPCUAClientModel, UA_IEC_types
3364
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
import util.paths as paths
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
# Paths to open62541 assume that 
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
# - open62541 directory is aside beremiz directory
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
# - open62541 was just built (not installed)
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
Open62541Path = paths.ThirdPartyPath("open62541")
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
Open62541LibraryPath = os.path.join(Open62541Path,"build","bin") 
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
Open62541IncludePaths = [os.path.join(Open62541Path, *dirs) for dirs in [
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
    ("plugins","include"),
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
    ("build","src_generated"),
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
    ("include",),
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
    ("arch",)]]
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
class OPCUAClientEditor(ConfTreeNodeEditor):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
    CONFNODEEDITOR_TABS = [
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
        (_("OPC-UA Client"), "CreateOPCUAClient_UI")]
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
    def Log(self, msg):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
        self.Controler.GetCTRoot().logger.write(msg)
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
    def UriGetter(self):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
        return self.Controler.GetServerURI() 
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
    def CreateOPCUAClient_UI(self, parent):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
        return OPCUAClientPanel(parent, self.Controler.GetModelData(), self.Log, self.UriGetter)
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
class OPCUAClient(object):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
      <xsd:element name="OPCUAClient">
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
        <xsd:complexType>
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
          <xsd:attribute name="Server_URI" type="xsd:string" use="optional" default="opc.tcp://localhost:4840"/>
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
        </xsd:complexType>
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
      </xsd:element>
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
    </xsd:schema>
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
    """
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
    EditorType = OPCUAClientEditor
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
    def __init__(self):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
        self.modeldata = OPCUAClientModel()
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
        filepath = self.GetFileName()
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
        if os.path.isfile(filepath):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
            self.modeldata.LoadCSV(filepath)
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
    def GetModelData(self):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
        return self.modeldata
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
    
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
    def GetServerURI(self):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
        return self.GetParamsAttributes("OPCUAClient.Server_URI")["value"]
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
    def GetFileName(self):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
        return os.path.join(self.CTNPath(), 'selected.csv')
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
    def OnCTNSave(self, from_project_path=None):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
        self.modeldata.SaveCSV(self.GetFileName())
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
        return True
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
    def CTNGenerate_C(self, buildpath, locations):
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
        current_location = self.GetCurrentLocation()
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
        locstr = "_".join(map(str, current_location))
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
        c_path = os.path.join(buildpath, "opcua_client__%s.c" % locstr)
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
        c_code = self.modeldata.GenerateC(c_path, locstr, 
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
            self.GetParamsAttributes("OPCUAClient.Server_URI")["value"])
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
        with open(c_path, 'wb') as c_file:
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
            c_file.write(c_code)
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    81
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    82
        LDFLAGS = [' "' + os.path.join(Open62541LibraryPath, "libopen62541.a") + '"']
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    83
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    84
        CFLAGS = ' '.join(['-I"' + path + '"' for path in Open62541IncludePaths])
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    85
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    86
        return [(c_path, CFLAGS)], LDFLAGS, True
fa2365fa6154 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    87
3366
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    88
    def GetVariableLocationTree(self):
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    89
        current_location = self.GetCurrentLocation()
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    90
        locstr = "_".join(map(str, current_location))
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    91
        name = self.BaseParams.getName()
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    92
        entries = []
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    93
        for direction, data in self.modeldata.iteritems():
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    94
            iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    95
            for row in data:
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    96
                dname, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    97
                iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type]
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    98
                c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number)
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
    99
                entries.append({
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   100
                    "name": dname,
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   101
                    "type": {"input": LOCATION_VAR_INPUT, "output": LOCATION_VAR_OUTPUT}[direction],
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   102
                    "size": {"X":1, "B":8, "W":16, "D":32, "L":64}[iec_size_prefix],
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   103
                    "IEC_type": iec_type,
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   104
                    "var_name": c_loc_name,
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   105
                    "location": iec_size_prefix + ".".join([str(i) for i in current_location]) + "." + str(iec_number),
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   106
                    "description": "",
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   107
                    "children": []})
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   108
        return {"name": name,
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   109
                "type": LOCATION_CONFNODE,
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   110
                "location": ".".join([str(i) for i in current_location]) + ".x",
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   111
                "children": entries}
1ca385429bba OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3364
diff changeset
   112