Commit e66350c6 authored by ORD's avatar ORD Committed by GitHub

Merge pull request #210 from bitkeeper/master

Support providing objecttype for the method add_object of Node
parents 5b146814 a083a9b6
<UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" xmlns:s1="https://github.com/FreeOpcUa/python-opcua/customobject/Types.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NamespaceUris>
<Uri>https://github.com/FreeOpcUa/python-opcua/customobject</Uri>
</NamespaceUris>
<Aliases>
<Alias Alias="Double">i=11</Alias>
<Alias Alias="HasModellingRule">i=37</Alias>
<Alias Alias="HasTypeDefinition">i=40</Alias>
<Alias Alias="HasSubtype">i=45</Alias>
<Alias Alias="HasComponent">i=47</Alias>
</Aliases>
<Extensions>
<Extension>
<ModelInfo Tool="UaModeler" Hash="qnoF+0+hDsL4GCM37vuINw==" Version="1.4.3"/>
</Extension>
</Extensions>
<UAObjectType NodeId="ns=1;i=1002" BrowseName="1:MyObjectType">
<DisplayName>MyObjectType</DisplayName>
<References>
<Reference ReferenceType="HasComponent">ns=1;i=6001</Reference>
<Reference ReferenceType="HasSubtype" IsForward="false">i=58</Reference>
</References>
</UAObjectType>
<UAVariable DataType="Double" ParentNodeId="ns=1;i=1002" NodeId="ns=1;i=6001" BrowseName="1:MyVariable" UserAccessLevel="3" AccessLevel="3">
<DisplayName>MyVariable</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
<Reference ReferenceType="HasModellingRule">i=78</Reference>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=1002</Reference>
</References>
<Value>
<uax:Double>0</uax:Double>
</Value>
</UAVariable>
</UANodeSet>
import sys
sys.path.insert(0, "..")
import time
from opcua import ua, Server
if __name__ == "__main__":
# setup our server
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
# setup our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
# Import customobject type
server.import_xml('customobject.xml')
# get Objects node, this is where we should put our custom stuff
objects = server.get_objects_node()
# get nodeid of custom object type by :
# 1) Use node ID
# 2) Or Full path
# 3) Or As child from parent
nodeid_a = ua.NodeId.from_string('ns=1;i=1002')
nodeid_b = server.get_root_node().get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType", "1:MyObjectType"]).nodeid
nodeid_c = server.get_node(ua.ObjectIds.BaseObjectType).get_child(["1:MyObjectType"]).nodeid
myobject_type_nodeid = nodeid_a
# populating our address space
myobj = objects.add_object(idx, "MyObject",)
myobj = objects.add_object(idx, "MyCustomObject", myobject_type_nodeid)
# starting!
server.start()
try:
count = 0
while True:
time.sleep(1)
count += 0.1
myvar.set_value(count)
finally:
# close connection, remove subcsriptions, etc
server.stop()
......@@ -47,7 +47,24 @@ def create_object(parent, *args):
or namespace index, name
"""
nodeid, qname = _parse_add_args(*args)
return node.Node(parent.server, _create_object(parent.server, parent.nodeid, nodeid, qname, ua.ObjectIds.BaseObjectType))
objecttype = ua.ObjectIds.BaseObjectType
if(len(args) == 3):
objecttype = args[2]
try:
if isinstance(objecttype, int):
objecttype = ua.NodeId(objecttype)
elif isinstance(objecttype, ua.NodeId):
objecttype = objecttype
elif isinstance(objecttype, str):
objecttype = ua.NodeId.from_string(objecttype)
else:
raise RuntimeError()
return nodeid, qname
except ua.UaError:
raise
except Exception as ex:
raise TypeError("This provided objecttype takes either a index, nodeid or string. Received arguments {} and got exception {}".format(args, ex))
return node.Node(parent.server, _create_object(parent.server, parent.nodeid, nodeid, qname, objecttype))
def create_property(parent, *args):
......@@ -122,7 +139,10 @@ def _create_object(server, parentnodeid, nodeid, qname, objecttype):
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasComponent)
addnode.NodeClass = ua.NodeClass.Object
addnode.TypeDefinition = ua.NodeId(objecttype)
if isinstance(objecttype, int):
addnode.TypeDefinition = ua.NodeId(objecttype)
elif isinstance(objecttype, ua.NodeId):
addnode.TypeDefinition = objecttype
attrs = ua.ObjectAttributes()
attrs.EventNotifier = 0
......
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