4 |
4 |
5 import os |
5 import os |
6 |
6 |
7 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor |
7 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor |
8 from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT |
8 from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT |
9 from .opcua_client_maker import OPCUAClientPanel, OPCUAClientModel, UA_IEC_types |
9 from .opcua_client_maker import OPCUAClientPanel, OPCUAClientModel, UA_IEC_types, authParams |
10 |
10 |
11 import util.paths as paths |
11 import util.paths as paths |
12 |
12 |
13 # Paths to open62541 assume that |
13 # Paths to open62541 assume that |
14 # - open62541 directory is aside beremiz directory |
14 # - open62541 directory is aside beremiz directory |
27 (_("OPC-UA Client"), "CreateOPCUAClient_UI")] |
27 (_("OPC-UA Client"), "CreateOPCUAClient_UI")] |
28 |
28 |
29 def Log(self, msg): |
29 def Log(self, msg): |
30 self.Controler.GetCTRoot().logger.write(msg) |
30 self.Controler.GetCTRoot().logger.write(msg) |
31 |
31 |
32 def UriGetter(self): |
|
33 return self.Controler.GetServerURI() |
|
34 |
|
35 def CreateOPCUAClient_UI(self, parent): |
32 def CreateOPCUAClient_UI(self, parent): |
36 return OPCUAClientPanel(parent, self.Controler.GetModelData(), self.Log, self.UriGetter) |
33 return OPCUAClientPanel(parent, self.Controler.GetModelData(), self.Log, self.Controler.GetConfig) |
37 |
34 |
38 class OPCUAClient(object): |
35 class OPCUAClient(object): |
39 XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
36 XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
40 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
37 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
41 <xsd:element name="OPCUAClient"> |
38 <xsd:element name="OPCUAClient"> |
42 <xsd:complexType> |
39 <xsd:complexType> |
|
40 <xsd:sequence> |
|
41 <xsd:element name="AuthType" minOccurs="0"> |
|
42 <xsd:complexType> |
|
43 <xsd:choice minOccurs="0"> |
|
44 <xsd:element name="x509"> |
|
45 <xsd:complexType> |
|
46 <xsd:sequence> |
|
47 <xsd:element name="Policy"> |
|
48 <xsd:annotation> |
|
49 <xsd:documentation>Default to Basic256Sha256 if not specified</xsd:documentation> |
|
50 </xsd:annotation> |
|
51 <xsd:complexType> |
|
52 <xsd:choice minOccurs="0"> |
|
53 <xsd:element name="Basic256Sha256"/> |
|
54 <xsd:element name="Basic128Rsa15"/> |
|
55 <xsd:element name="Basic256"/> |
|
56 </xsd:choice> |
|
57 </xsd:complexType> |
|
58 </xsd:element> |
|
59 <xsd:element name="Mode"> |
|
60 <xsd:complexType> |
|
61 <xsd:choice minOccurs="0"> |
|
62 <xsd:element name="SignAndEncrypt"/> |
|
63 <xsd:element name="Sign"/> |
|
64 </xsd:choice> |
|
65 </xsd:complexType> |
|
66 </xsd:element> |
|
67 </xsd:sequence> |
|
68 <xsd:attribute name="Certificate" type="xsd:string" use="optional" default="certificate.pem"/> |
|
69 <xsd:attribute name="PrivateKey" type="xsd:string" use="optional" default="private_key.pem"/> |
|
70 </xsd:complexType> |
|
71 </xsd:element> |
|
72 <xsd:element name="UserPassword"> |
|
73 <xsd:complexType> |
|
74 <xsd:attribute name="User" type="xsd:string" use="optional"/> |
|
75 <xsd:attribute name="Password" type="xsd:string" use="optional"/> |
|
76 </xsd:complexType> |
|
77 </xsd:element> |
|
78 </xsd:choice> |
|
79 </xsd:complexType> |
|
80 </xsd:element> |
|
81 </xsd:sequence> |
43 <xsd:attribute name="Server_URI" type="xsd:string" use="optional" default="opc.tcp://localhost:4840"/> |
82 <xsd:attribute name="Server_URI" type="xsd:string" use="optional" default="opc.tcp://localhost:4840"/> |
44 </xsd:complexType> |
83 </xsd:complexType> |
45 </xsd:element> |
84 </xsd:element> |
46 </xsd:schema> |
85 </xsd:schema> |
47 """ |
86 """ |
59 self.GetCTRoot().logger.write(msg) |
98 self.GetCTRoot().logger.write(msg) |
60 |
99 |
61 def GetModelData(self): |
100 def GetModelData(self): |
62 return self.modeldata |
101 return self.modeldata |
63 |
102 |
64 def GetServerURI(self): |
103 def GetConfig(self): |
65 return self.GetParamsAttributes("OPCUAClient.Server_URI")["value"] |
104 cfg = lambda path: self.GetParamsAttributes("OPCUAClient."+path)["value"] |
|
105 AuthType = cfg("AuthType") |
|
106 res = dict(URI=cfg("Server_URI"), AuthType=AuthType) |
|
107 |
|
108 paramList = authParams.get(AuthType, None) |
|
109 if paramList: |
|
110 for name,default in paramList: |
|
111 res[name] = cfg("AuthType."+name) |
|
112 |
|
113 print(res) |
|
114 return res |
66 |
115 |
67 def GetFileName(self): |
116 def GetFileName(self): |
68 return os.path.join(self.CTNPath(), 'selected.csv') |
117 return os.path.join(self.CTNPath(), 'selected.csv') |
69 |
118 |
70 def OnCTNSave(self, from_project_path=None): |
119 def OnCTNSave(self, from_project_path=None): |
74 def CTNGenerate_C(self, buildpath, locations): |
123 def CTNGenerate_C(self, buildpath, locations): |
75 current_location = self.GetCurrentLocation() |
124 current_location = self.GetCurrentLocation() |
76 locstr = "_".join(map(str, current_location)) |
125 locstr = "_".join(map(str, current_location)) |
77 c_path = os.path.join(buildpath, "opcua_client__%s.c" % locstr) |
126 c_path = os.path.join(buildpath, "opcua_client__%s.c" % locstr) |
78 |
127 |
79 c_code = self.modeldata.GenerateC(c_path, locstr, |
128 c_code = self.modeldata.GenerateC(c_path, locstr, self.GetConfig()) |
80 self.GetParamsAttributes("OPCUAClient.Server_URI")["value"]) |
|
81 |
129 |
82 with open(c_path, 'wb') as c_file: |
130 with open(c_path, 'wb') as c_file: |
83 c_file.write(c_code) |
131 c_file.write(c_code) |
84 |
132 |
85 LDFLAGS = [' "' + os.path.join(Open62541LibraryPath, "libopen62541.a") + '"'] |
133 LDFLAGS = [' "' + os.path.join(Open62541LibraryPath, "libopen62541.a") + '"'] |