opc_ua/client.py
changeset 3364 fa2365fa6154
child 3339 057b4ba30c35
equal deleted inserted replaced
3363:c28a064d7f1a 3364:fa2365fa6154
       
     1 # opcua/client.py
       
     2 
       
     3 from __future__ import absolute_import
       
     4 
       
     5 import os
       
     6 
       
     7 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
       
     8 from .opcua_client_maker import OPCUAClientPanel, OPCUAClientModel
       
     9 
       
    10 import util.paths as paths
       
    11 
       
    12 # Paths to open62541 assume that 
       
    13 # - open62541 directory is aside beremiz directory
       
    14 # - open62541 was just built (not installed)
       
    15 
       
    16 Open62541Path = paths.ThirdPartyPath("open62541")
       
    17 Open62541LibraryPath = os.path.join(Open62541Path,"build","bin") 
       
    18 Open62541IncludePaths = [os.path.join(Open62541Path, *dirs) for dirs in [
       
    19     ("plugins","include"),
       
    20     ("build","src_generated"),
       
    21     ("include",),
       
    22     ("arch",)]]
       
    23 
       
    24 class OPCUAClientEditor(ConfTreeNodeEditor):
       
    25     CONFNODEEDITOR_TABS = [
       
    26         (_("OPC-UA Client"), "CreateOPCUAClient_UI")]
       
    27 
       
    28     def Log(self, msg):
       
    29         self.Controler.GetCTRoot().logger.write(msg)
       
    30 
       
    31     def UriGetter(self):
       
    32         return self.Controler.GetServerURI() 
       
    33 
       
    34     def CreateOPCUAClient_UI(self, parent):
       
    35         return OPCUAClientPanel(parent, self.Controler.GetModelData(), self.Log, self.UriGetter)
       
    36 
       
    37 class OPCUAClient(object):
       
    38     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
       
    39     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       
    40       <xsd:element name="OPCUAClient">
       
    41         <xsd:complexType>
       
    42           <xsd:attribute name="Server_URI" type="xsd:string" use="optional" default="opc.tcp://localhost:4840"/>
       
    43         </xsd:complexType>
       
    44       </xsd:element>
       
    45     </xsd:schema>
       
    46     """
       
    47 
       
    48     EditorType = OPCUAClientEditor
       
    49 
       
    50     def __init__(self):
       
    51         self.modeldata = OPCUAClientModel()
       
    52 
       
    53         filepath = self.GetFileName()
       
    54         if os.path.isfile(filepath):
       
    55             self.modeldata.LoadCSV(filepath)
       
    56 
       
    57     def GetModelData(self):
       
    58         return self.modeldata
       
    59     
       
    60     def GetServerURI(self):
       
    61         return self.GetParamsAttributes("OPCUAClient.Server_URI")["value"]
       
    62 
       
    63     def GetFileName(self):
       
    64         return os.path.join(self.CTNPath(), 'selected.csv')
       
    65 
       
    66     def OnCTNSave(self, from_project_path=None):
       
    67         self.modeldata.SaveCSV(self.GetFileName())
       
    68         return True
       
    69 
       
    70     def CTNGenerate_C(self, buildpath, locations):
       
    71         current_location = self.GetCurrentLocation()
       
    72         locstr = "_".join(map(str, current_location))
       
    73         c_path = os.path.join(buildpath, "opcua_client__%s.c" % locstr)
       
    74 
       
    75         c_code = self.modeldata.GenerateC(c_path, locstr, 
       
    76             self.GetParamsAttributes("OPCUAClient.Server_URI")["value"])
       
    77 
       
    78         with open(c_path, 'wb') as c_file:
       
    79             c_file.write(c_code)
       
    80 
       
    81         LDFLAGS = [' "' + os.path.join(Open62541LibraryPath, "libopen62541.a") + '"']
       
    82 
       
    83         CFLAGS = ' '.join(['-I"' + path + '"' for path in Open62541IncludePaths])
       
    84 
       
    85         return [(c_path, CFLAGS)], LDFLAGS, True
       
    86