示例#1
0
 def get_datetime(self):
     response = self._send_message(_READ_RTC_REQUEST, 3)
     if response[0:2] != b'\x04\06':
         raise lifescan_common.MalformedCommand(
             'invalid response, expected 04 06, received %02x %02x' %
             (response[0], response[1]))
     (timestamp, ) = _STRUCT_TIMESTAMP.unpack(response[2:])
     return _convert_timestamp(timestamp)
示例#2
0
 def _read_parameter(self, parameter_key):
     response = self._send_message(_READ_PARAMETER_REQUEST + parameter_key,
                                   4)
     if response[0:2] != b'\x03\x06':
         raise lifescan_common.MalformedCommand(
             'invalid response, expected 03 06, received %02x %02x' %
             (response[0], response[1]))
     return response[2:]
示例#3
0
    def _get_reading_count(self):
        response = self._send_message(_READ_RECORD_COUNT_REQUEST, 3)
        if response[0:2] != b'\x04\06':
            raise lifescan_common.MalformedCommand(
                'invalid response, expected 04 06, received %02x %02x' %
                (response[0], response[1]))

        (record_count, ) = _STRUCT_RECORDID.unpack(response[2:])
        return record_count
示例#4
0
 def _query_string(self, query_key):
     response = self._send_message(_QUERY_REQUEST + query_key, 3)
     if response[0:2] != b'\x04\06':
         raise lifescan_common.MalformedCommand(
             'invalid response, expected 04 06, received %02x %02x' %
             (response[0], response[1]))
     # Strings are encoded in wide characters (LE), but they should
     # only contain ASCII characters. Note that the string is
     # null-terminated, so the last character should be dropped.
     return response[2:].decode('utf-16-le')[:-1]
示例#5
0
def _extract_message(register):
    """Parse the message preamble and verify checksums."""
    stx, length = _STRUCT_PREAMBLE.unpack_from(register)
    if stx != _STX:
        raise lifescan_common.MalformedCommand('invalid STX byte: %02x' % stx)
    if length > _REGISTER_SIZE:
        raise lifescan_common.MalformedCommand(
            'invalid length: %d > REGISTER_SIZE' % length)

    # 2 is the length of the checksum, so it should be ignored.
    calculated_checksum = lifescan_common.crc_ccitt(register[:(length - 2)])

    coda_offset = length - _STRUCT_CODA.size
    etx, encoded_checksum = _STRUCT_CODA.unpack_from(register[coda_offset:])
    if etx != _ETX:
        raise lifescan_common.MalformedCommand('invalid ETX byte: %02x' % etx)
    if encoded_checksum != calculated_checksum:
        raise exceptions.InvalidChecksum(encoded_checksum, calculated_checksum)

    response = register[_STRUCT_PREAMBLE.size:coda_offset]
    return response
示例#6
0
    def _get_reading(self, record_number):
        request = (_READ_RECORD_REQUEST_PREFIX +
                   _STRUCT_RECORDID.pack(record_number) +
                   _READ_RECORD_REQUEST_SUFFIX)
        response = self._send_message(request, 3)
        if response[0:2] != b'\x04\06':
            raise lifescan_common.MalformedCommand(
                'invalid response, expected 04 06, received %02x %02x' %
                (response[0], response[1]))

        (unused_const1, unused_const2, unused_counter, unused_const3,
         unused_counter2, timestamp, value, unused_flags, unused_const4,
         unused_const5) = _STRUCT_RECORD.unpack(response)

        return common.Reading(_convert_timestamp(timestamp), float(value))
示例#7
0
    def set_datetime(self, date=datetime.datetime.now()):
        epoch = datetime.datetime.utcfromtimestamp(_EPOCH_BASE)
        delta = date - epoch
        timestamp = int(delta.total_seconds())

        timestamp_bytes = _STRUCT_TIMESTAMP.pack(timestamp)
        response = self._send_message(_WRITE_RTC_REQUEST + timestamp_bytes, 3)

        if response[0:2] != b'\x04\06':
            raise lifescan_common.MalformedCommand(
                'invalid response, expected 04 06, received %02x %02x' %
                (response[0], response[1]))

        # The device does not return the new datetime, so confirm by
        # calling READ RTC again.
        return self.get_datetime()
示例#8
0
 def zero_log(self):
     response = self._send_message(_MEMORY_ERASE_REQUEST, 3)
     if response[0:2] != b'\x04\06':
         raise lifescan_common.MalformedCommand(
             'invalid response, expected 04 06, received %02x %02x' %
             (response[0], response[1]))