Commit e5fff184 authored by Kevin Levesque's avatar Kevin Levesque Committed by ORD

Added a simple server that import an xml file and a client that calls the methods defines

parent 473476c3
from opcua import Client, ua
from opcua.ua import ua_binary as uabin
from opcua.common.methods import call_method
class HelloClient:
def __init__(self, endpoint):
self.client = Client(endpoint)
def __enter__(self):
self.client.connect()
return self.client
def __exit__(self, exc_type, exc_val, exc_tb):
self.client.disconnect()
if __name__ == '__main__':
with HelloClient("opc.tcp://localhost:40840/freeopcua/server/") as client:
root = client.get_root_node()
print("Root node is: ", root)
objects = client.get_objects_node()
print("Objects node is: ", objects)
hellower = objects.get_child("0:Hellower")
print("Hellower is: ", hellower)
resulting_text = hellower.call_method("0:SayHello", False)
print(resulting_text)
resulting_text = hellower.call_method("1:SayHello2", True)
print(resulting_text)
resulting_array = hellower.call_method("1:SayHelloArray", False)
print(resulting_array)
import os.path
try:
from IPython import embed
except ImportError:
import code
def embed():
vars = globals()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
from opcua import ua, uamethod, Server
@uamethod
def say_hello_xml(parent, happy):
print("Calling say_hello_xml")
if happy:
result = "I'm happy"
else:
result = "I'm not happy"
print(result)
return result
@uamethod
def say_hello(parent, happy):
if happy:
result = "I'm happy"
else:
result = "I'm not happy"
print(result)
return result
@uamethod
def say_hello_array(parent, happy):
if happy:
result = "I'm happy"
else:
result = "I'm not happy"
print(result)
return [result, "Actually I am"]
class HelloServer:
def __init__(self, endpoint, name, model_filepath):
self.server = Server()
# This need to be imported at the start or else it will overwrite the data
self.server.import_xml(model_filepath)
self.server.set_endpoint(endpoint)
self.server.set_server_name(name)
objects = self.server.get_objects_node()
freeopcua_namespace = self.server.get_namespace_index("urn:freeopcua:python:server")
hellower = objects.get_child("0:Hellower")
hellower_say_hello = hellower.get_child("0:SayHello")
self.server.link_method(hellower_say_hello, say_hello_xml)
hellower.add_method(
freeopcua_namespace, "SayHello2", say_hello, [ua.VariantType.Boolean], [ua.VariantType.String])
hellower.add_method(
freeopcua_namespace, "SayHelloArray", say_hello_array, [ua.VariantType.Boolean], [ua.VariantType.String])
def __enter__(self):
self.server.start()
return self.server
def __exit__(self, exc_type, exc_val, exc_tb):
self.server.stop()
if __name__ == '__main__':
script_dir = os.path.dirname(__file__)
with HelloServer(
"opc.tcp://0.0.0.0:40840/freeopcua/server/",
"FreeOpcUa Example Server",
os.path.join(script_dir, "test_saying.xml")) as server:
embed()
<?xml version='1.0' encoding='utf-8'?>
<UANodeSet xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Aliases>
<Alias Alias="Organizes">i=35</Alias>
<Alias Alias="HasTypeDefinition">i=40</Alias>
<Alias Alias="HasProperty">i=46</Alias>
<Alias Alias="HasComponent">i=47</Alias>
<Alias Alias="Argument">i=296</Alias>
</Aliases>
<NamespaceUris />
<UAObject BrowseName="0:Hellower" NodeId="i=20001" ParentNodeId="i=85">
<DisplayName>BaseObjectType</DisplayName>
<Description>The base type for all object nodes.</Description>
<References>
<Reference IsForward="false" ReferenceType="Organizes">i=85</Reference>
<Reference ReferenceType="HasTypeDefinition">i=58</Reference>
<Reference ReferenceType="HasComponent">i=20002</Reference>
</References>
</UAObject>
<UAMethod BrowseName="0:SayHello" NodeId="i=20002" ParentNodeId="i=20001">
<DisplayName>SayHello</DisplayName>
<Description>SayHello</Description>
<References>
<Reference IsForward="false" ReferenceType="HasComponent">i=20001</Reference>
<Reference ReferenceType="HasProperty">i=20003</Reference>
<Reference ReferenceType="HasProperty">i=20004</Reference>
</References>
</UAMethod>
<UAVariable BrowseName="0:InputArguments" DataType="Argument" NodeId="i=20003" ParentNodeId="i=20002" ValueRank="0">
<DisplayName>InputArguments</DisplayName>
<Description>InputArguments</Description>
<Value>
<uax:ListOfExtensionObject>
<uax:ExtensionObject>
<uax:TypeId>
<uax:Identifier>i=296</uax:Identifier>
</uax:TypeId>
<uax:Body>
<uax:Argument>
<uax:ValueRank>-1</uax:ValueRank>
<uax:Description>
<uax:Text />
<uax:Locale />
</uax:Description>
<uax:DataType>
<uax:Identifier>i=1</uax:Identifier>
</uax:DataType>
<uax:Name>happy</uax:Name>
<uax:ArrayDimensions />
</uax:Argument>
</uax:Body>
</uax:ExtensionObject>
</uax:ListOfExtensionObject>
</Value>
<References>
<Reference IsForward="false" ReferenceType="HasProperty">i=20002</Reference>
<Reference ReferenceType="HasTypeDefinition">i=68</Reference>
</References>
</UAVariable>
<UAVariable BrowseName="0:OutputArguments" DataType="Argument" NodeId="i=20004" ParentNodeId="i=20002" ValueRank="0">
<DisplayName>OutputArguments</DisplayName>
<Description>OutputArguments</Description>
<Value>
<uax:ListOfExtensionObject>
<uax:ExtensionObject>
<uax:TypeId>
<uax:Identifier>i=296</uax:Identifier>
</uax:TypeId>
<uax:Body>
<uax:Argument>
<uax:ValueRank>-1</uax:ValueRank>
<uax:Description>
<uax:Text />
<uax:Locale />
</uax:Description>
<uax:DataType>
<uax:Identifier>i=12</uax:Identifier>
</uax:DataType>
<uax:Name>saying</uax:Name>
<uax:ArrayDimensions />
</uax:Argument>
</uax:Body>
</uax:ExtensionObject>
</uax:ListOfExtensionObject>
</Value>
<References>
<Reference IsForward="false" ReferenceType="HasProperty">i=20002</Reference>
<Reference ReferenceType="HasTypeDefinition">i=68</Reference>
</References>
</UAVariable>
</UANodeSet>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment