Exemplo n.º 1
0
    def handle_event(self, event):
        """ Handle events from event system """
        _event = None
        try:
            _event = decode_event_data(event)
        except Exception as error:
            _event = decode_event_data(event['data'])

        if _event == self._last_event:
            # Event already handled
            return

        self._last_event = _event
        if _event is not None:
            try:
                if _event['event'] == 'Switch':
                    if _event.get('data', None):
                        if _event['data'].get('state', 0):
                            self.turn_on()
                        else:
                            self.turn_off()
                elif _event['event'] == 'Toggle':
                    self.toggle()
                elif _event['event'] == 'On':
                    self.turn_on()
                elif _event['event'] == 'Off':
                    self.turn_off()
            except Exception as error:
                Logger.log(LOG_LEVEL["error"],
                           f'Error handling event for {self.id}')
Exemplo n.º 2
0
    def handle_event(self, event):
        """ Handle events from event system """
        _event = None
        try: 
            _event = decode_event_data(event)
        except Exception as error:
            _event = decode_event_data(event['data'])

        if _event == self._last_event:
            # Event already handled
            return

        self._last_event = _event
        if _event is not None:
            try:
                if _event['event'] == 'Message':
                    if _event.get('data', None):
                        self.add_message(_event['data'])
                elif _event['event'] == 'Clear':
                    self.clear()
                elif _event['event'] == 'ClearQueue':
                    self.clear_queue()
            except Exception as error:
                Logger.log(LOG_LEVEL["error"],
                           f'Error handling event for {self.id}')
Exemplo n.º 3
0
    def handle_event(self, event):
        """ Handle the event data from the event system """
        _event_data = decode_event_data(event)

        if _event_data == self._last_event:
            # Event already handled
            return

        self._last_event = _event_data
        if _event_data.get('event'):
            try:
                if _event_data['event'] == 'StateUpdated':
                    if _event_data['component_id'] == self.source:
                        sensor_value = self._parse_data(
                            _event_data["new_state"]["state"])
                        if self.evaluate_thresholds(sensor_value):
                            self.active = True
                            if self._previous_state != self.active:
                                # Trigger is reset, Fire
                                self.trigger(_event_data)
                            else:
                                # Trigger not reset check if its multi fire
                                if self.frequency == 'many':
                                    self.trigger(_event_data)
                        else:
                            self.active = False
            except Exception as error:
                Logger.log(
                    LOG_LEVEL["error"],
                    f'Error evaluating thresholds for trigger {self.id}')
                Logger.log(LOG_LEVEL["debug"], error)
        self._previous_state = self.active
Exemplo n.º 4
0
 def callback_handler(message):
     """ callback handler to allow multiple hanlders on one topic """
     try:
         _topic = message["channel"].decode('utf-8')
     except Exception as error:
         _topic = message["channel"]
     if _topic in self.callbacks:
         for callbk in self.callbacks[_topic]:
             callbk(decode_event_data(message["data"]))
Exemplo n.º 5
0
 def handle_event(self, event={}):
     """ Handle event from redis pubsub """
     data = decode_event_data(event['data'])
     if data is not None:
         try:
             # _event_data = self.last_event = decode_event_data(data)
             self._state = data
         except:
             Logger.log(
                 LOG_LEVEL["info"],
                 f"Error Decoding Event for Redis Sensor {self.id}"
             )
Exemplo n.º 6
0
    def handle_event(self, event):
        """ Process event data for the sequence """
        _event_data = decode_event_data(event)

        if _event_data == self._last_event:
            # Event already handled
            return

        self._last_event = _event_data
        if _event_data.get('event'):
            try:
                if _event_data['event'] == 'SequenceNextStep':
                    self.advance_step()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Sequence {self.name} Next Step Triggered'
                    )
                elif _event_data['event'] == 'SequencePreviousStep':
                    self.previous_step()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Sequence {self.name} Previous Step Triggered'
                    )
                elif _event_data['event'] == 'SequenceStart':
                    self.start()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Sequence {self.name} Start Triggered'
                    )
                elif _event_data['event'] == 'SequenceSkipStep':
                    self.skip_step()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Sequence {self.name} Skip Step Triggered'
                    )
                elif _event_data['event'] == 'SequenceStop':
                    self.stop()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Sequence {self.name} Stop Triggered'
                    )
            except:
                Logger.log(
                    LOG_LEVEL["info"],
                    f"Error Decoding Event for Sequence {self.id}"
                )
Exemplo n.º 7
0
    def handle_event(self, event):
        """ Process event data for the timer """
        _event_data = decode_event_data(event)

        if _event_data == self._last_event:
            # Event already handled
            return

        self._last_event = _event_data
        if _event_data.get('event'):
            try:
                if _event_data['event'] == 'TimerStart':
                    self.start()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Timer Trigger {FONT_MAGENTA}{self.name}{FONT_RESET} Started'
                    )
                elif _event_data['event'] == 'TimerStop':
                    self.stop()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Timer Trigger {FONT_MAGENTA}{self.name}{FONT_RESET} Stopped'
                    )
                elif _event_data['event'] == 'TimerReset':
                    self.reset()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Timer Trigger {FONT_MAGENTA}{self.name}{FONT_RESET} Reset'
                    )
                elif _event_data['event'] == 'TimerRestart':
                    self.restart()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Timer Trigger {FONT_MAGENTA}{self.name}{FONT_RESET} Restarted'
                    )
                elif _event_data['event'] == 'TimerPause':
                    self.pause()
                    Logger.log(
                        LOG_LEVEL["debug"],
                        f'Timer Triger {FONT_MAGENTA}{self.name}{FONT_RESET} Paused'
                    )
            except:
                Logger.log(
                    LOG_LEVEL["info"],
                    f"Error Decoding Event for Timer Trigger {self.id}")
Exemplo n.º 8
0
 def listenToClient(self, client, address, ip):
     size = 1024
     while self.mudpi.is_prepared:
         try:
             data = client.recv(size)
             if data:
                 data = decode_event_data(data)
                 if data.get("topic", None) is not None:
                     self.mudpi.events.publish(data["topic"], data)
                     Logger.log(LOG_LEVEL['debug'], f"Socket Event {data['event']} from {data['source']} Dispatched")
                     # response = { "status": "OK", "code": 200 }
                     # client.send(json.dumps(response).encode('utf-8'))
                 else:
                     Logger.log(LOG_LEVEL['error'], f"Socket Data Recieved. {FONT_RED}Dispatch Failed:{FONT_RESET} Missing Data 'Topic'")
                     Logger.log(LOG_LEVEL['debug'], data)
             else:
                 pass
                 # raise error('Client Disconnected')
         except Exception as e:
             Logger.log(LOG_LEVEL['info'], f"Socket Client {ip} Disconnected")
             client.close()
             return False
     Logger.log_formatted(LOG_LEVEL['info'], 'Closing Client Connection ', 'Complete', 'success')
Exemplo n.º 9
0
    def handle_event(self, event):
        """ Process event data for the NFC tag """
        _event_data = decode_event_data(event)

        if _event_data == self._last_event:
            # Event already handled
            return

        self._last_event = _event_data

        if self.serial:
            if _event_data['tag_id'] != self.serial:
                return

        if self.uid:
            if _event_data['tag_uid'] != self.uid:
                return

        if self.reader_id:
            if _event_data['reader_id'] != self.reader_id:
                return

        if self.locked:
            Logger.log(
                LOG_LEVEL["warning"],
                f'Tag {self.name} Scanned. This card has been locked for security.'
            )
            return

        if _event_data.get('event'):
            try:
                if _event_data['event'] == 'NFCTagScanned':
                    self._scan_count += 1
                    self._present = True
                    self.update_tag(_event_data)
                    Logger.log(LOG_LEVEL["debug"], f'Tag {self.name} Scanned')
                elif _event_data['event'] == 'NFCNewTagScanned':
                    self._scan_count += 1
                    self._present = True
                    self.update_tag(_event_data)
                    Logger.log(LOG_LEVEL["debug"],
                               f'Tag {self.name} Scanned for First Time')
                elif _event_data['event'] == 'NFCTagRemoved':
                    self._present = False
                    Logger.log(LOG_LEVEL["debug"], f'Tag {self.name} Removed')
                elif _event_data['event'] == 'NFCTagUIDMismatch':
                    if self.security > 0:
                        self._security_check = 'UID Mismatch'
                        if self.security > 1:
                            self._locked = True
                        Logger.log(
                            LOG_LEVEL["warning"],
                            f'Security Warning: Tag {self.name} UID Mismatches Saved One'
                        )
                elif _event_data['event'] == 'NFCTagUIDMissing':
                    if self.security > 0:
                        self._security_check = 'UID Missing'
                        Logger.log(LOG_LEVEL["debug"],
                                   f'Tag {self.name} UID Missing on Tag')
                elif _event_data['event'] == 'NFCDuplicateUID':
                    if self.security > 0:
                        self._security_check = 'Duplicate UID'
                        if self.security > 1:
                            self._locked = True
                        Logger.log(
                            LOG_LEVEL["debug"],
                            f'Tag {self.name} has UID found on multiple tags!')
                elif _event_data['event'] == 'NFCNewUIDCreated':
                    self.update_tag(_event_data)
                    Logger.log(LOG_LEVEL["debug"],
                               f'New UID created for tag {self.name}')
            except:
                Logger.log(LOG_LEVEL["info"],
                           f"Error Decoding Event for Sequence {self.id}")