def handle_node_event(node_data: NodeData) -> core_pb2.Event: """ Handle node event when there is a node event :param node_data: node data :return: node event that contains node id, name, model, position, and services """ node = node_data.node x, y, _ = node.position.get() position = core_pb2.Position(x=x, y=y) lon, lat, alt = node.position.get_geo() geo = core_pb2.Geo(lon=lon, lat=lat, alt=alt) services = [x.name for x in node.services] node_proto = core_pb2.Node( id=node.id, name=node.name, model=node.type, icon=node.icon, position=position, geo=geo, services=services, ) message_type = node_data.message_type.value node_event = core_pb2.NodeEvent(message_type=message_type, node=node_proto) return core_pb2.Event(node_event=node_event, source=node_data.source)
def process(self) -> core_pb2.Event: """ Process the next event in the queue. :return: grpc event, or None when invalid event or queue timeout """ event = core_pb2.Event(session_id=self.session.id) try: data = self.queue.get(timeout=1) if isinstance(data, NodeData): event.node_event.CopyFrom(handle_node_event(data)) elif isinstance(data, LinkData): event.link_event.CopyFrom(handle_link_event(data)) elif isinstance(data, EventData): event.session_event.CopyFrom(handle_session_event(data)) elif isinstance(data, ConfigData): event.config_event.CopyFrom(handle_config_event(data)) elif isinstance(data, ExceptionData): event.exception_event.CopyFrom(handle_exception_event(data)) elif isinstance(data, FileData): event.file_event.CopyFrom(handle_file_event(data)) else: logging.error("unknown event: %s", data) event = None except Empty: event = None return event
def handle_link_event(link_data: LinkData) -> core_pb2.Event: """ Handle link event when there is a link event :param link_data: link data :return: link event that has message type and link information """ link = convert_link(link_data) message_type = link_data.message_type.value link_event = core_pb2.LinkEvent(message_type=message_type, link=link) return core_pb2.Event(link_event=link_event, source=link_data.source)
def handle_exception_event(exception_data: ExceptionData) -> core_pb2.Event: """ Handle exception event when there is exception event :param exception_data: exception data :return: exception event """ exception_event = core_pb2.ExceptionEvent( node_id=exception_data.node, level=exception_data.level.value, source=exception_data.source, date=exception_data.date, text=exception_data.text, opaque=exception_data.opaque, ) return core_pb2.Event(exception_event=exception_event)
def Events(self, request, context): session = self.get_session(request.session_id, context) queue = Queue() session.node_handlers.append(queue.put) session.link_handlers.append(queue.put) session.config_handlers.append(queue.put) session.file_handlers.append(queue.put) session.exception_handlers.append(queue.put) session.event_handlers.append(queue.put) while self._is_running(context): event = core_pb2.Event() try: data = queue.get(timeout=1) if isinstance(data, NodeData): event.node_event.CopyFrom(self._handle_node_event(data)) elif isinstance(data, LinkData): event.link_event.CopyFrom(self._handle_link_event(data)) elif isinstance(data, EventData): event.session_event.CopyFrom( self._handle_session_event(data)) elif isinstance(data, ConfigData): event.config_event.CopyFrom( self._handle_config_event(data)) # TODO: remove when config events are fixed event.config_event.session_id = session.id elif isinstance(data, ExceptionData): event.exception_event.CopyFrom( self._handle_exception_event(data)) elif isinstance(data, FileData): event.file_event.CopyFrom(self._handle_file_event(data)) else: logging.error("unknown event: %s", data) continue yield event except Empty: continue session.node_handlers.remove(queue.put) session.link_handlers.remove(queue.put) session.config_handlers.remove(queue.put) session.file_handlers.remove(queue.put) session.exception_handlers.remove(queue.put) session.event_handlers.remove(queue.put) self._cancel_stream(context)
def handle_session_event(event_data: EventData) -> core_pb2.Event: """ Handle session event when there is a session event :param event_data: event data :return: session event """ event_time = event_data.time if event_time is not None: event_time = float(event_time) session_event = core_pb2.SessionEvent( node_id=event_data.node, event=event_data.event_type.value, name=event_data.name, data=event_data.data, time=event_time, ) return core_pb2.Event(session_event=session_event)
def handle_file_event(file_data: FileData) -> core_pb2.Event: """ Handle file event :param file_data: file data :return: file event """ file_event = core_pb2.FileEvent( message_type=file_data.message_type.value, node_id=file_data.node, name=file_data.name, mode=file_data.mode, number=file_data.number, type=file_data.type, source=file_data.source, data=file_data.data, compressed_data=file_data.compressed_data, ) return core_pb2.Event(file_event=file_event)
def handle_config_event(config_data: ConfigData) -> core_pb2.Event: """ Handle configuration event when there is configuration event :param config_data: configuration data :return: configuration event """ config_event = core_pb2.ConfigEvent( message_type=config_data.message_type, node_id=config_data.node, object=config_data.object, type=config_data.type, captions=config_data.captions, bitmap=config_data.bitmap, data_values=config_data.data_values, possible_values=config_data.possible_values, groups=config_data.groups, iface_id=config_data.iface_id, network_id=config_data.network_id, opaque=config_data.opaque, data_types=config_data.data_types, ) return core_pb2.Event(config_event=config_event)