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

Inital version of implememntation

parent 49c7ea31
......@@ -76,13 +76,41 @@ class EventGenerator(object):
def get_event_from_node(node):
event = None
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:
pass
#node.get
parent_identifier, parent_eventtype = _find_parent_eventtype(node)
if not parent_eventtype:
return None
class CustomEvent(parent_eventtype):
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())
parents = node.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
if len(parents) != 1: # Something went wrong
return None
curr_node = parents[0]
#TODO: Extend to show all fields of CustomEvent
def __str__(self):
s = 'CustomEvent('
s += 'EventId:{}'.format(self.EventId)
s += ', EventType:{}'.format(self.EventType)
s += ', SourceNode:{}'.format(self.SourceNode)
s += ', SourceName:{}'.format(self.SourceName)
s += ', Time:{}'.format(self.Time)
s += ', RecieveTime:{}'.format(self.RecieveTime)
s += ', LocalTime:{}'.format(self.LocalTime)
s += ', Message:{}'.format(self.Message)
s += ', Severity:{}'.format(self.Severity)
s += ')'
#class CustomEvent():
......@@ -92,29 +120,15 @@ def get_event_from_node(node):
#child = Node(self.isession, desc.NodeId)
#setattr(self.event, desc.BrowseName.Name, child.get_value())
return event
return CustomEvent()
class CustomEvent(ua.BaseEvent):
def __init__(self, etype=ua.BaseEvent, sourcenode=ua.NodeId(ua.ObjectIds.Server), message=None, severity=1):
super(CustomEvent, self).__init__(sourcenode, message, severity, True)
#TODO: Add fileds
def _find_parent_eventtype(node):
parents = node.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
#TODO: Extend to show all fields of CustomEvent
def __str__(self):
s = 'CustomEvent('
s += 'EventId:{}'.format(self.EventId)
s += ', EventType:{}'.format(self.EventType)
s += ', SourceNode:{}'.format(self.SourceNode)
s += ', SourceName:{}'.format(self.SourceName)
s += ', Time:{}'.format(self.Time)
s += ', RecieveTime:{}'.format(self.RecieveTime)
s += ', LocalTime:{}'.format(self.LocalTime)
s += ', Message:{}'.format(self.Message)
s += ', Severity:{}'.format(self.Severity)
s += ')'
return s
__repr__ = __str__
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):
return _create_method(parent, nodeid, qname, callback, inputs, outputs)
def _create_folder(server, parentnodeid, nodeid, qname):
addnode = ua.AddNodesItem()
addnode.RequestedNewNodeId = nodeid
addnode.BrowseName = qname
addnode.NodeClass = ua.NodeClass.Object
addnode.ParentNodeId = parentnodeid
addnode.ReferenceTypeId = ua.NodeId.from_string("i=35")
addnode.TypeDefinition = ua.NodeId.from_string("i=61")
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_subtype(parent, *args):
"""
create a child node subtype
arguments are nodeid, browsename
or namespace index, name
"""
nodeid, qname = _parse_add_args(*args)
return node.Node(parent.server, _create_object(parent.server, parent.nodeid, nodeid, qname, None))
def _create_object(server, parentnodeid, nodeid, qname):
def _create_object(server, parentnodeid, nodeid, qname, objecttype):
addnode = ua.AddNodesItem()
addnode.RequestedNewNodeId = nodeid
addnode.BrowseName = qname
addnode.NodeClass = ua.NodeClass.Object
addnode.ParentNodeId = parentnodeid
addnode.ReferenceTypeId = ua.NodeId.from_string("i=35")
addnode.TypeDefinition = ua.NodeId(ua.ObjectIds.BaseObjectType)
#TODO: maybe move to address_space.py and implement for all node types?
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.Description = ua.LocalizedText(qname.Name)
attrs.DisplayName = ua.LocalizedText(qname.Name)
......
......@@ -369,6 +369,10 @@ class Node(object):
from opcua.common import manage_nodes
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):
from opcua.common import methods
return methods.call_method(*args, **kwargs)
......@@ -329,12 +329,14 @@ class Server(object):
"""
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)
custom_event = base_event.add_object(name)
base_event = self.get_node(ua.NodeId(baseetype))
custom_event = base_event.add_subtype(idx, name)
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):
"""
......
......@@ -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)))
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):
ev = opcua.common.event.get_event_from_node(opcua.Node(self.opc.iserver.isession, ua.NodeId(ua.ObjectIds.AuditEventType)))
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