Go to the documentation of this file.00001 import logging
00002 from datetime import datetime
00003 import uuid
00004
00005 from opcua import ua
00006 from opcua import Node
00007 from opcua.common import events
00008 from opcua.common import event_objects
00009
00010
00011 class EventGenerator(object):
00012
00013 """
00014 Create an event based on an event type. Per default is BaseEventType used.
00015 Object members are dynamically created from the base event type and send to
00016 client when evebt is triggered (see example code in source)
00017
00018 Arguments to constructor are:
00019
00020 server: The InternalSession object to use for query and event triggering
00021
00022 source: The emiting source for the node, either an objectId, NodeId or a Node
00023
00024 etype: The event type, either an objectId, a NodeId or a Node object
00025 """
00026
00027 def __init__(self, isession, etype=None, source=ua.ObjectIds.Server):
00028 if not etype:
00029 etype = event_objects.BaseEvent()
00030
00031 self.logger = logging.getLogger(__name__)
00032 self.isession = isession
00033 self.event = None
00034 node = None
00035
00036 if isinstance(etype, event_objects.BaseEvent):
00037 self.event = etype
00038 elif isinstance(etype, Node):
00039 node = etype
00040 elif isinstance(etype, ua.NodeId):
00041 node = Node(self.isession, etype)
00042 else:
00043 node = Node(self.isession, ua.NodeId(etype))
00044
00045 if node:
00046 self.event = events.get_event_obj_from_type_node(node)
00047
00048 if isinstance(source, Node):
00049 pass
00050 elif isinstance(source, ua.NodeId):
00051 source = Node(isession, source)
00052 else:
00053 source = Node(isession, ua.NodeId(source))
00054
00055 if self.event.SourceNode:
00056 if source.nodeid != self.event.SourceNode:
00057 self.logger.warning(
00058 "Source NodeId: '%s' and event SourceNode: '%s' are not the same. Using '%s' as SourceNode",
00059 str(source.nodeid), str(self.event.SourceNode), str(self.event.SourceNode))
00060 source = Node(self.isession, self.event.SourceNode)
00061
00062 self.event.SourceNode = source.nodeid
00063 self.event.SourceName = source.get_browse_name().Name
00064
00065 source.set_event_notifier([ua.EventNotifier.SubscribeToEvents, ua.EventNotifier.HistoryRead])
00066 refs = []
00067 ref = ua.AddReferencesItem()
00068 ref.IsForward = True
00069 ref.ReferenceTypeId = ua.NodeId(ua.ObjectIds.GeneratesEvent)
00070 ref.SourceNodeId = source.nodeid
00071 ref.TargetNodeClass = ua.NodeClass.ObjectType
00072 ref.TargetNodeId = self.event.EventType
00073 refs.append(ref)
00074 results = self.isession.add_references(refs)
00075
00076
00077 def __str__(self):
00078 return "EventGenerator(Type:{0}, Source:{1}, Time:{2}, Message: {3})".format(self.event.EventType,
00079 self.event.SourceNode,
00080 self.event.Time,
00081 self.event.Message)
00082 __repr__ = __str__
00083
00084 def trigger(self, time=None, message=None):
00085 """
00086 Trigger the event. This will send a notification to all subscribed clients
00087 """
00088 self.event.EventId = ua.Variant(uuid.uuid4().hex, ua.VariantType.ByteString)
00089 if time:
00090 self.event.Time = time
00091 else:
00092 self.event.Time = datetime.utcnow()
00093 self.event.ReceiveTime = datetime.utcnow()
00094
00095 self.event.LocalTime = datetime.utcnow()
00096 if message:
00097 self.event.Message = ua.LocalizedText(message)
00098 elif not self.event.Message:
00099 self.event.Message = ua.LocalizedText(Node(self.isession, self.event.SourceNode).get_browse_name().Text)
00100 self.isession.subscription_service.trigger_event(self.event)