Commit 49c07a7d authored by Denis Štogl's avatar Denis Štogl

Inital version of implememntation

parent 49c7ea31
...@@ -76,30 +76,27 @@ class EventGenerator(object): ...@@ -76,30 +76,27 @@ class EventGenerator(object):
def get_event_from_node(node): def get_event_from_node(node):
event = None
if node.nodeid.Identifier in ua.uaevents_auto.IMPLEMNTED_EVENTS.keys(): if node.nodeid.Identifier in ua.uaevents_auto.IMPLEMNTED_EVENTS.keys():
event = ua.uaevents_auto.IMPLEMNTED_EVENTS[node.nodeid.Identifier]() return ua.uaevents_auto.IMPLEMNTED_EVENTS[node.nodeid.Identifier]()
else: else:
pass parent_identifier, parent_eventtype = _find_parent_eventtype(node)
#node.get if not parent_eventtype:
return None
#class CustomEvent():
#pass
#references = node.get_children_descriptions(refs=ua.ObjectIds.HasProperty)
#for desc in references:
#child = Node(self.isession, desc.NodeId)
#setattr(self.event, desc.BrowseName.Name, child.get_value())
return event
class CustomEvent(parent_eventtype):
class CustomEvent(ua.BaseEvent): def __init__(self):
super(CustomEvent, self).__init__()
curr_node = node
while curr_node.nodeid.Identifier != parent_identifier:
properties = curr_node.get_referenced_nodes(refs=ua.ObjectIds.HasProperty, direction=ua.BrowseDirection.Forward)
for prop in properties:
setattr(self, prop.get_browse_name().Name, prop.get_value())
def __init__(self, etype=ua.BaseEvent, sourcenode=ua.NodeId(ua.ObjectIds.Server), message=None, severity=1): parents = node.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
super(CustomEvent, self).__init__(sourcenode, message, severity, True) if len(parents) != 1: # Something went wrong
#TODO: Add fileds return None
curr_node = parents[0]
#TODO: Extend to show all fields of CustomEvent #TODO: Extend to show all fields of CustomEvent
def __str__(self): def __str__(self):
...@@ -115,6 +112,23 @@ class CustomEvent(ua.BaseEvent): ...@@ -115,6 +112,23 @@ class CustomEvent(ua.BaseEvent):
s += ', Severity:{}'.format(self.Severity) s += ', Severity:{}'.format(self.Severity)
s += ')' s += ')'
#class CustomEvent():
return s #pass
__repr__ = __str__ #references = node.get_children_descriptions(refs=ua.ObjectIds.HasProperty)
#for desc in references:
#child = Node(self.isession, desc.NodeId)
#setattr(self.event, desc.BrowseName.Name, child.get_value())
return CustomEvent()
def _find_parent_eventtype(node):
parents = node.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
if len(parents) != 1: # Something went wrong
return None
if parents[0].nodeid.Identifier in ua.uaevents_auto.IMPLEMNTED_EVENTS.keys():
return node.nodeid.Identifier, ua.uaevents_auto.IMPLEMNTED_EVENTS[parents[0].nodeid.Identifier]
else:
_find_parent_eventtype(parents[0])
...@@ -94,34 +94,31 @@ def create_method(parent, *args): ...@@ -94,34 +94,31 @@ def create_method(parent, *args):
return _create_method(parent, nodeid, qname, callback, inputs, outputs) return _create_method(parent, nodeid, qname, callback, inputs, outputs)
def _create_folder(server, parentnodeid, nodeid, qname): def create_subtype(parent, *args):
addnode = ua.AddNodesItem() """
addnode.RequestedNewNodeId = nodeid create a child node subtype
addnode.BrowseName = qname arguments are nodeid, browsename
addnode.NodeClass = ua.NodeClass.Object or namespace index, name
addnode.ParentNodeId = parentnodeid """
addnode.ReferenceTypeId = ua.NodeId.from_string("i=35") nodeid, qname = _parse_add_args(*args)
addnode.TypeDefinition = ua.NodeId.from_string("i=61") return node.Node(parent.server, _create_object(parent.server, parent.nodeid, nodeid, qname, None))
attrs = ua.ObjectAttributes()
attrs.Description = ua.LocalizedText(qname.Name)
attrs.DisplayName = ua.LocalizedText(qname.Name)
attrs.WriteMask = 0
attrs.UserWriteMask = 0
attrs.EventNotifier = 0
addnode.NodeAttributes = attrs
results = server.add_nodes([addnode])
results[0].StatusCode.check()
return results[0].AddedNodeId
def _create_object(server, parentnodeid, nodeid, qname): def _create_object(server, parentnodeid, nodeid, qname, objecttype):
addnode = ua.AddNodesItem() addnode = ua.AddNodesItem()
addnode.RequestedNewNodeId = nodeid addnode.RequestedNewNodeId = nodeid
addnode.BrowseName = qname addnode.BrowseName = qname
addnode.NodeClass = ua.NodeClass.Object addnode.NodeClass = ua.NodeClass.Object
addnode.ParentNodeId = parentnodeid addnode.ParentNodeId = parentnodeid
addnode.ReferenceTypeId = ua.NodeId.from_string("i=35") #TODO: maybe move to address_space.py and implement for all node types?
addnode.TypeDefinition = ua.NodeId(ua.ObjectIds.BaseObjectType) if not objecttype:
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubtype)
else:
addnode.TypeDefinition = ua.NodeId(objecttype)
if node.Node(server, parentnodeid).get_type_definition() == ua.ObjectIds.FolderType:
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes)
else:
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasComponent)
attrs = ua.ObjectAttributes() attrs = ua.ObjectAttributes()
attrs.Description = ua.LocalizedText(qname.Name) attrs.Description = ua.LocalizedText(qname.Name)
attrs.DisplayName = ua.LocalizedText(qname.Name) attrs.DisplayName = ua.LocalizedText(qname.Name)
......
...@@ -369,6 +369,10 @@ class Node(object): ...@@ -369,6 +369,10 @@ class Node(object):
from opcua.common import manage_nodes from opcua.common import manage_nodes
return manage_nodes.create_method(*args, **kwargs) return manage_nodes.create_method(*args, **kwargs)
def add_subtype(*args, **kwargs):
from opcua.common import manage_nodes
return manage_nodes.create_subtype(*args, **kwargs)
def call_method(*args, **kwargs): def call_method(*args, **kwargs):
from opcua.common import methods from opcua.common import methods
return methods.call_method(*args, **kwargs) return methods.call_method(*args, **kwargs)
...@@ -329,12 +329,14 @@ class Server(object): ...@@ -329,12 +329,14 @@ class Server(object):
""" """
return EventGenerator(self.iserver.isession, etype, source) return EventGenerator(self.iserver.isession, etype, source)
def create_custom_event(self, name, baseetype=ua.ObjectIds.BaseEventType, properties=[]): def create_custom_event(self, idx, name, baseetype=ua.ObjectIds.BaseEventType, properties=[]):
base_event = self.get_node(baseetype) base_event = self.get_node(ua.NodeId(baseetype))
custom_event = base_event.add_object(name) custom_event = base_event.add_subtype(idx, name)
for property in properties: for property in properties:
custom_event.add_property(property[0], property[1]) custom_event.add_property(idx, property[0], ua.Variant(None, property[1]))
return custom_event
def import_xml(self, path): def import_xml(self, path):
""" """
......
...@@ -142,6 +142,17 @@ class TestServer(unittest.TestCase, CommonTests): ...@@ -142,6 +142,17 @@ class TestServer(unittest.TestCase, CommonTests):
ev = opcua.common.event.get_event_from_node(opcua.Node(self.opc.iserver.isession, ua.NodeId(ua.ObjectIds.BaseEventType))) ev = opcua.common.event.get_event_from_node(opcua.Node(self.opc.iserver.isession, ua.NodeId(ua.ObjectIds.BaseEventType)))
check_base_event(self, ev) check_base_event(self, ev)
def test_create_custom_event(self):
event = self.opc.create_custom_event(2, 'MyEvent', ua.ObjectIds.BaseEventType, [('PropertyNum', ua.VariantType.Float), ('PropertyString', ua.VariantType.String)])
base = opcua.Node(self.opc.iserver.isession, ua.NodeId(ua.ObjectIds.BaseEventType))
self.assertTrue(event in base.get_children())
nodes = event.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
self.assertEqual(base, nodes[0])
properties = event.get_properties()
self.assertIsNot(properties, None)
def test_get_event_from_node_CustomEvent(self): def test_get_event_from_node_CustomEvent(self):
ev = opcua.common.event.get_event_from_node(opcua.Node(self.opc.iserver.isession, ua.NodeId(ua.ObjectIds.AuditEventType))) ev = opcua.common.event.get_event_from_node(opcua.Node(self.opc.iserver.isession, ua.NodeId(ua.ObjectIds.AuditEventType)))
check_base_event(self, ev) check_base_event(self, ev)
......
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