Beispiel #1
0
    def __call__(self, characteristic, event_args):

        if isinstance(event_args, ReadCompleteEventArgs):
            if event_args.status == GattStatusCode.success:
                decoded_value = self.decode(event_args.value)
            else:
                decoded_value = None
            decoded_event_args = DecodedReadCompleteEventArgs.from_read_complete_event_args(
                event_args, decoded_value)

        elif isinstance(event_args, NotificationReceivedEventArgs):
            decoded_value = self.decode(event_args.value)
            decoded_event_args = DecodedReadCompleteEventArgs.from_notification_complete_event_args(
                event_args, decoded_value)
        elif isinstance(event_args, WriteEventArgs):
            decoded_value = self.decode(event_args.value)
            decoded_event_args = DecodedWriteEventArgs(decoded_value,
                                                       event_args.value)
        else:
            if self.logger:
                self.logger.error(
                    "Unable to handle unknown event args {}".format(
                        event_args))
            return

        self.event_to_raise.notify(self.owner, decoded_event_args)
Beispiel #2
0
    def _on_battery_level_notification(self, characteristic, event_args):
        """
        :param characteristic:
        :type event_args: blatann.event_args.NotificationReceivedEventArgs
        :return:
        """
        decoded_value = None
        try:
            stream = ble_data_types.BleDataStream(event_args.value)
            decoded_value = BatteryLevel.decode(stream)
        except Exception as e:  # TODO not so generic
            logger.error("Failed to decode Battery Level, stream: [{}]".format(event_args.value.encode("hex")))
            logger.exception(e)

        decoded_event_args = DecodedReadCompleteEventArgs.from_notification_complete_event_args(event_args, decoded_value)
        self._on_battery_level_updated_event.notify(self, decoded_event_args)
Beispiel #3
0
    def _on_read_complete(self, characteristic, event_args):
        """
        :param characteristic:
        :type event_args: blatann.event_args.ReadCompleteEventArgs
        """
        decoded_value = None
        if event_args.status == GattStatusCode.success:
            try:
                stream = ble_data_types.BleDataStream(event_args.value)
                decoded_value = BatteryLevel.decode(stream)
            except Exception as e:  # TODO not so generic
                logger.error("Failed to decode Battery Level, stream: [{}]".format(binascii.hexlify(event_args.value)))
                logger.exception(e)

        decoded_event_args = DecodedReadCompleteEventArgs.from_read_complete_event_args(event_args, decoded_value)
        self._on_battery_level_updated_event.notify(self, decoded_event_args)
Beispiel #4
0
    def _read_complete(self, characteristic, event_args):
        """
        :param characteristic:
        :type event_args: blatann.event_args.ReadCompleteEventArgs
        """
        decoded_value = None
        if event_args.status == GattStatusCode.success:
            try:
                stream = ble_data_types.BleDataStream(event_args.value)
                decoded_value = self.data_class.decode(stream)
            except Exception as e:  # TODO not so generic
                logger.error("Service {}, Characteristic {} failed to decode value on read. "
                             "Stream: [{}]".format(self.service.uuid, self.uuid, binascii.hexlify(event_args.value)))
                logger.exception(e)

        decoded_event_args = DecodedReadCompleteEventArgs.from_read_complete_event_args(event_args, decoded_value)
        self._on_read_complete_event.notify(characteristic, decoded_event_args)