1
0
mirror of https://github.com/FreeOpcUa/opcua-asyncio synced 2025-10-29 17:07:18 +08:00

Fix localtime field of events

Before, the localtime field was set to a wrong datatype (DateTime
instead of TimeZoneDataType), and didn’t contain the local timezone
offset, but the current localtime.
This commit is contained in:
Dario Ernst 2019-06-04 16:31:44 +02:00 committed by oroulet
parent 6db39b1a20
commit 60e40ce3bc
2 changed files with 17 additions and 6 deletions

View File

@ -18,7 +18,7 @@ class BaseEvent(Event):
self.add_property('SourceName', None, ua.VariantType.String)
self.add_property('Time', None, ua.VariantType.DateTime)
self.add_property('ReceiveTime', None, ua.VariantType.DateTime)
self.add_property('LocalTime', None, ua.VariantType.DateTime)
self.add_property('LocalTime', ua.NodeId(ua.ObjectIds.TimeZoneDataType), ua.VariantType.ExtensionObject)
self.add_property('Message', ua.LocalizedText(message), ua.VariantType.LocalizedText)
self.add_property('Severity', severity, ua.VariantType.UInt16)

View File

@ -1,7 +1,9 @@
import logging
from datetime import datetime
import time
import uuid
from typing import Optional
import sys
from asyncua import ua
from ..common import events, event_objects, Node
@ -73,20 +75,29 @@ class EventGenerator:
__repr__ = __str__
def trigger(self, time=None, message=None):
def trigger(self, time_attr=None, message=None):
"""
Trigger the event. This will send a notification to all subscribed clients
"""
self.event.EventId = ua.Variant(uuid.uuid4().hex.encode('utf-8'), ua.VariantType.ByteString)
if time:
self.event.Time = time
if time_attr:
self.event.Time = time_attr
else:
self.event.Time = datetime.utcnow()
self.event.ReceiveTime = datetime.utcnow()
# FIXME: LocalTime is wrong but currently know better. For description s. Part 5 page 18
self.event.LocalTime = datetime.utcnow()
self.event.LocalTime = ua.uaprotocol_auto.TimeZoneDataType()
if sys.version_info.major > 2:
localtime = time.localtime(self.event.Time.timestamp())
self.event.LocalTime.Offset = localtime.tm_gmtoff//60
else:
localtime = time.localtime(time.mktime(self.event.Time.timetuple()))
self.event.LocalTime.Offset = -(time.altzone if localtime.tm_isdst else time.timezone)
self.event.LocalTime.DaylightSavingInOffset = bool(localtime.tm_isdst != -1)
if message:
self.event.Message = ua.LocalizedText(message)
elif not self.event.Message:
self.event.Message = ua.LocalizedText(Node(self.isession, self.event.SourceNode).get_browse_name().Text)
self.isession.subscription_service.trigger_event(self.event)