author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Mon, 18 Oct 2021 20:41:31 +0200 | |
changeset 3365 | 9f7c9051c9ba |
parent 3364 | fa2365fa6154 |
child 3339 | 057b4ba30c35 |
permissions | -rw-r--r-- |
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 |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
8 |
from .opcua_client_maker import OPCUAClientPanel, OPCUAClientModel |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
9 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
10 |
import util.paths as paths |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
11 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
12 |
# Paths to open62541 assume that |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
13 |
# - open62541 directory is aside beremiz directory |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
14 |
# - open62541 was just built (not installed) |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
15 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
16 |
Open62541Path = paths.ThirdPartyPath("open62541") |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
17 |
Open62541LibraryPath = os.path.join(Open62541Path,"build","bin") |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
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
|
19 |
("plugins","include"), |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
20 |
("build","src_generated"), |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
21 |
("include",), |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
22 |
("arch",)]] |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
23 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
24 |
class OPCUAClientEditor(ConfTreeNodeEditor): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
25 |
CONFNODEEDITOR_TABS = [ |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
26 |
(_("OPC-UA Client"), "CreateOPCUAClient_UI")] |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
27 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
28 |
def Log(self, msg): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
29 |
self.Controler.GetCTRoot().logger.write(msg) |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
30 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
31 |
def UriGetter(self): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
32 |
return self.Controler.GetServerURI() |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
33 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
34 |
def CreateOPCUAClient_UI(self, parent): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
35 |
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
|
36 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
37 |
class OPCUAClient(object): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
38 |
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
|
39 |
<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
|
40 |
<xsd:element name="OPCUAClient"> |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
41 |
<xsd:complexType> |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
42 |
<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
|
43 |
</xsd:complexType> |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
44 |
</xsd:element> |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
45 |
</xsd:schema> |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
46 |
""" |
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 |
EditorType = OPCUAClientEditor |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
49 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
50 |
def __init__(self): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
51 |
self.modeldata = OPCUAClientModel() |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
52 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
53 |
filepath = self.GetFileName() |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
54 |
if os.path.isfile(filepath): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
55 |
self.modeldata.LoadCSV(filepath) |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
56 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
57 |
def GetModelData(self): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
58 |
return self.modeldata |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
59 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
60 |
def GetServerURI(self): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
61 |
return self.GetParamsAttributes("OPCUAClient.Server_URI")["value"] |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
62 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
63 |
def GetFileName(self): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
64 |
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
|
65 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
66 |
def OnCTNSave(self, from_project_path=None): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
67 |
self.modeldata.SaveCSV(self.GetFileName()) |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
68 |
return True |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
69 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
70 |
def CTNGenerate_C(self, buildpath, locations): |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
71 |
current_location = self.GetCurrentLocation() |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
72 |
locstr = "_".join(map(str, current_location)) |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
73 |
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
|
74 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
75 |
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
|
76 |
self.GetParamsAttributes("OPCUAClient.Server_URI")["value"]) |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
77 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
78 |
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
|
79 |
c_file.write(c_code) |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
80 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
81 |
LDFLAGS = [' "' + os.path.join(Open62541LibraryPath, "libopen62541.a") + '"'] |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
82 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
83 |
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
|
84 |
|
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
85 |
return [(c_path, CFLAGS)], LDFLAGS, True |
fa2365fa6154
Add OPC-UA simple synchronous client extension.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
86 |