Example #1
0
    async def handle_event(self, event: core_events.Event) -> None:
        if not isinstance(event, core_events.ValueChange):
            return  # We're only interested in port value changes

        if not system.date.has_real_date_time():
            return  # Don't record history unless we've got real date/time

        port = event.get_port()
        history_interval = await port.get_history_interval()
        if history_interval != -1:
            return  # Only consider ports with history interval set to special -1 (on value change)

        now_ms = int(time.time() * 1000)

        await save_sample(port, now_ms)
        port.set_history_last_timestamp(now_ms)
    async def handle_event(self, event: core_events.Event) -> None:
        (value_pair, old_attrs, new_attrs, changed_attrs, added_attrs,
         removed_attrs) = await self._update_from_event(event)

        accepted = await self.accepts(event, value_pair, old_attrs, new_attrs,
                                      changed_attrs, added_attrs,
                                      removed_attrs)

        if not accepted:
            return

        self.debug('handling event %s', event)

        try:
            await self.on_event(event)

        except Exception as e:
            self.error('failed to handle event %s: %s',
                       event,
                       e,
                       exc_info=True)

        try:
            if isinstance(event, core_events.ValueChange):
                old_value, new_value = value_pair
                await self.on_value_change(event, event.get_port(), old_value,
                                           new_value, new_attrs)

            elif isinstance(event, core_events.PortUpdate):
                await self.on_port_update(event, event.get_port(), old_attrs,
                                          new_attrs, changed_attrs,
                                          added_attrs, removed_attrs)

            elif isinstance(event, core_events.PortAdd):
                await self.on_port_add(event, event.get_port(), new_attrs)

            elif isinstance(event, core_events.PortRemove):
                await self.on_port_remove(event, event.get_port(), new_attrs)

            elif isinstance(event, core_events.DeviceUpdate):
                await self.on_device_update(event, old_attrs, new_attrs,
                                            changed_attrs, added_attrs,
                                            removed_attrs)

            elif isinstance(event, core_events.FullUpdate):
                await self.on_full_update(event)

            elif isinstance(event, slaves_events.SlaveDeviceUpdate):
                await self.on_slave_device_update(event, event.get_slave(),
                                                  old_attrs, new_attrs,
                                                  changed_attrs, added_attrs,
                                                  removed_attrs)

            elif isinstance(event, slaves_events.SlaveDeviceAdd):
                await self.on_slave_device_add(event, event.get_slave(),
                                               new_attrs)

            elif isinstance(event, slaves_events.SlaveDeviceRemove):
                await self.on_slave_device_remove(event, event.get_slave(),
                                                  new_attrs)

        except Exception as e:
            self.error('failed to handle event %s: %s',
                       event,
                       e,
                       exc_info=True)
    async def _update_from_event(
        self, event: core_events.Event
    ) -> Tuple[Tuple[NullablePortValue, NullablePortValue], Attributes,
               Attributes, Dict[str, Tuple[
                   Attribute, Attribute]], Attributes, Attributes]:

        value_pair = (None, None)
        old_attrs = {}
        new_attrs = {}
        changed_attrs = {}
        added_attrs = {}
        removed_attrs = {}

        if isinstance(event, core_events.PortEvent):
            port = event.get_port()

            old_attrs = self._port_attrs.get(port.get_id(), {})
            new_attrs = await port.get_attrs()

            old_value = self._port_values.get(port.get_id())
            new_value = port.get_last_read_value()
            value_pair = (old_value, new_value)

            if isinstance(event,
                          (core_events.PortAdd, core_events.PortUpdate)):
                changed_attrs, added_attrs, removed_attrs = self._make_changed_added_removed(
                    old_attrs, new_attrs)
                self._port_values[port.get_id()] = new_value
                self._port_attrs[port.get_id()] = new_attrs

            elif isinstance(event, core_events.PortRemove):
                self._port_values.pop(port.get_id(), None)
                removed_attrs = self._port_attrs.pop(port.get_id(), {})

            elif isinstance(event, core_events.ValueChange):
                self._port_values[port.get_id()] = new_value

        elif isinstance(event, core_events.DeviceEvent):
            old_attrs = self._device_attrs
            new_attrs = event.get_attrs()

            changed_attrs, added_attrs, removed_attrs = self._make_changed_added_removed(
                old_attrs, new_attrs)
            self._device_attrs = new_attrs

        elif isinstance(event, slaves_events.SlaveDeviceEvent):
            slave = event.get_slave()
            slave_json = slave.to_json()

            # Flatten slave master properties and attributes
            old_attrs = self._slave_attrs.get(slave.get_name(), {})
            new_attrs = dict(slave_json, **slave_json.pop('attrs'))

            if isinstance(event, (slaves_events.SlaveDeviceAdd,
                                  slaves_events.SlaveDeviceUpdate)):
                changed_attrs, added_attrs, removed_attrs = self._make_changed_added_removed(
                    old_attrs, new_attrs)
                self._slave_attrs[slave.get_name()] = new_attrs

            elif isinstance(event, slaves_events.SlaveDeviceRemove):
                removed_attrs = self._slave_attrs.pop(slave.get_name(), {})

        return value_pair, old_attrs, new_attrs, changed_attrs, added_attrs, removed_attrs