opc_ua/client.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 15 Dec 2022 14:54:05 +0100
changeset 3700 302492568cf4
parent 3678 6c496a8ed2a9
child 3718 7841b651d601
permissions -rw-r--r--
Backed out changeset ddadbdf20e70, no effect on current problem.
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
# opcua/client.py
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
from __future__ import absolute_import
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
import os
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
3339
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
     8
from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
     9
from .opcua_client_maker import OPCUAClientPanel, OPCUAClientModel, UA_IEC_types, authParams
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
import util.paths as paths
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
# Paths to open62541 assume that 
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
# - open62541 directory is aside beremiz directory
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
# - open62541 was just built (not installed)
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
Open62541Path = paths.ThirdPartyPath("open62541")
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
Open62541LibraryPath = os.path.join(Open62541Path,"build","bin") 
6097bca230e8 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 [
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
    ("plugins","include"),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
    ("build","src_generated"),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
    ("include",),
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
    ("arch",)]]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
class OPCUAClientEditor(ConfTreeNodeEditor):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
    CONFNODEEDITOR_TABS = [
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
        (_("OPC-UA Client"), "CreateOPCUAClient_UI")]
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
    def Log(self, msg):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
        self.Controler.GetCTRoot().logger.write(msg)
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
    def CreateOPCUAClient_UI(self, parent):
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    33
        return OPCUAClientPanel(parent, self.Controler.GetModelData(), self.Log, self.Controler.GetConfig)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
class OPCUAClient(object):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
      <xsd:element name="OPCUAClient">
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
        <xsd:complexType>
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    40
          <xsd:sequence>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    41
            <xsd:element name="AuthType" minOccurs="0">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    42
              <xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    43
                <xsd:choice minOccurs="0">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    44
                  <xsd:element name="x509">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    45
                    <xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    46
                      <xsd:sequence>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    47
                        <xsd:element name="Policy">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    48
                          <xsd:annotation>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    49
                            <xsd:documentation>Default to Basic256Sha256 if not specified</xsd:documentation>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    50
                          </xsd:annotation>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    51
                          <xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    52
                            <xsd:choice minOccurs="0">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    53
                              <xsd:element name="Basic256Sha256"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    54
                              <xsd:element name="Basic128Rsa15"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    55
                              <xsd:element name="Basic256"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    56
                            </xsd:choice>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    57
                          </xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    58
                        </xsd:element>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    59
                        <xsd:element name="Mode">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    60
                          <xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    61
                            <xsd:choice minOccurs="0">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    62
                              <xsd:element name="SignAndEncrypt"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    63
                              <xsd:element name="Sign"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    64
                            </xsd:choice>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    65
                          </xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    66
                        </xsd:element>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    67
                      </xsd:sequence>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    68
                      <xsd:attribute name="Certificate" type="xsd:string" use="optional" default="certificate.pem"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    69
                      <xsd:attribute name="PrivateKey" type="xsd:string" use="optional" default="private_key.pem"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    70
                    </xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    71
                  </xsd:element>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    72
                  <xsd:element name="UserPassword">
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    73
                    <xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    74
                      <xsd:attribute name="User" type="xsd:string" use="optional"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    75
                      <xsd:attribute name="Password" type="xsd:string" use="optional"/>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    76
                    </xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    77
                  </xsd:element>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    78
                </xsd:choice>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    79
              </xsd:complexType>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    80
            </xsd:element>
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
    81
          </xsd:sequence>
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    82
          <xsd:attribute name="Server_URI" type="xsd:string" use="optional" default="opc.tcp://localhost:4840"/>
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    83
        </xsd:complexType>
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    84
      </xsd:element>
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    85
    </xsd:schema>
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
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    88
    EditorType = OPCUAClientEditor
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    89
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    90
    def __init__(self):
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3652
diff changeset
    91
        self.modeldata = OPCUAClientModel(self.Log, self.CTNMarkModified)
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    92
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    93
        filepath = self.GetFileName()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    94
        if os.path.isfile(filepath):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    95
            self.modeldata.LoadCSV(filepath)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    96
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: 3339
diff changeset
    97
    def Log(self, msg):
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: 3339
diff changeset
    98
        self.GetCTRoot().logger.write(msg)
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: 3339
diff changeset
    99
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   100
    def GetModelData(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   101
        return self.modeldata
3674
d10a7907fb43 OPC-UA: Update CTN "ChangesToSave" status when variable selection change.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3652
diff changeset
   102
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   103
    def GetConfig(self):
3678
6c496a8ed2a9 OPC-UA: tolerate better pre-encryption formated client configuration file.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   104
        def cfg(path): 
6c496a8ed2a9 OPC-UA: tolerate better pre-encryption formated client configuration file.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   105
            try:
6c496a8ed2a9 OPC-UA: tolerate better pre-encryption formated client configuration file.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   106
                attr=self.GetParamsAttributes("OPCUAClient."+path)
6c496a8ed2a9 OPC-UA: tolerate better pre-encryption formated client configuration file.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   107
            except ValueError:
6c496a8ed2a9 OPC-UA: tolerate better pre-encryption formated client configuration file.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   108
                return None
6c496a8ed2a9 OPC-UA: tolerate better pre-encryption formated client configuration file.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   109
            return attr["value"]
6c496a8ed2a9 OPC-UA: tolerate better pre-encryption formated client configuration file.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3674
diff changeset
   110
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   111
        AuthType = cfg("AuthType")
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   112
        res = dict(URI=cfg("Server_URI"), AuthType=AuthType)
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   113
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   114
        paramList = authParams.get(AuthType, None)
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   115
        if paramList:
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   116
            for name,default in paramList:
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   117
                value = cfg("AuthType."+name)
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   118
                if value == "" or value is None:
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   119
                    value = default
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   120
                # cryptomaterial is expected to be in project's user provide file directory
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   121
                if name in ["Certificate","PrivateKey"]:
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   122
                    value = os.path.join(self.GetCTRoot()._getProjectFilesPath(), value)
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   123
                res[name] = value
3589
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   124
a0b645a934c9 OPC-UA, IDE: add CTN parameters to support OPC-UA encryption and authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3378
diff changeset
   125
        return res
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   126
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   127
    def GetFileName(self):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   128
        return os.path.join(self.CTNPath(), 'selected.csv')
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   129
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   130
    def OnCTNSave(self, from_project_path=None):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   131
        self.modeldata.SaveCSV(self.GetFileName())
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   132
        return True
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   133
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   134
    def CTNGenerate_C(self, buildpath, locations):
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   135
        current_location = self.GetCurrentLocation()
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   136
        locstr = "_".join(map(str, current_location))
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   137
        c_path = os.path.join(buildpath, "opcua_client__%s.c" % locstr)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
3612
428fb2472f43 OPC-UA: add logging to help user to understang what went wrong when connecting.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3611
diff changeset
   139
        c_code = '#include "beremiz.h"\n'
428fb2472f43 OPC-UA: add logging to help user to understang what went wrong when connecting.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3611
diff changeset
   140
        c_code += self.modeldata.GenerateC(c_path, locstr, self.GetConfig())
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
        with open(c_path, 'wb') as c_file:
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
            c_file.write(c_code)
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
3611
2c041d3d6ee3 OPC-UA: fix LDFLAG, missing OpenSSL's -lcrypto
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3591
diff changeset
   145
        LDFLAGS = ['"' + os.path.join(Open62541LibraryPath, "libopen62541.a") + '"', '-lcrypto']
3337
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
        CFLAGS = ' '.join(['-I"' + path + '"' for path in Open62541IncludePaths])
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
3591
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   149
        # Note: all user provided files are systematicaly copied, including cryptomaterial
50600b946ea7 OPC-US client: add support for x509 and user+password authentication.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3589
diff changeset
   150
3337
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
        return [(c_path, CFLAGS)], LDFLAGS, True
6097bca230e8 Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
3339
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   153
    def GetVariableLocationTree(self):
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   154
        current_location = self.GetCurrentLocation()
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   155
        locstr = "_".join(map(str, current_location))
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   156
        name = self.BaseParams.getName()
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   157
        entries = []
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   158
        for direction, data in self.modeldata.iteritems():
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   159
            iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   160
            for row in data:
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   161
                dname, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   162
                iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type]
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   163
                c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number)
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   164
                entries.append({
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   165
                    "name": dname,
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   166
                    "type": {"input": LOCATION_VAR_INPUT, "output": LOCATION_VAR_OUTPUT}[direction],
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   167
                    "size": {"X":1, "B":8, "W":16, "D":32, "L":64}[iec_size_prefix],
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   168
                    "IEC_type": iec_type,
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   169
                    "var_name": c_loc_name,
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   170
                    "location": iec_size_prefix + ".".join([str(i) for i in current_location]) + "." + str(iec_number),
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   171
                    "description": "",
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   172
                    "children": []})
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   173
        return {"name": name,
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   174
                "type": LOCATION_CONFNODE,
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   175
                "location": ".".join([str(i) for i in current_location]) + ".x",
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   176
                "children": entries}
057b4ba30c35 OPCUA: Add browsing of variables exposed by client when selecting location from variable panel.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3337
diff changeset
   177