def test_parse_custom_epoch(self):
     self.assertEqual(
         construct_extras.Timestamp(
             construct.Int32ul,
             epoch=_NEW_EPOCH).parse(b"\x00\x00\x00\x00"),
         _TEST_DATE2,
     )
 def test_invalid_value(self):
     with self.assertRaises(AssertionError):
         construct_extras.Timestamp(construct.Int32ul).build('foo')
 def test_build_varint(self):
     self.assertEqual(
         construct_extras.Timestamp(construct.VarInt).build(_TEST_DATE3),
         b'\x00')
 def test_build_custom_epoch_negative_success(self):
     self.assertEqual(
         construct_extras.Timestamp(construct.Int32sl,
                                    epoch=_NEW_EPOCH).build(_TEST_DATE1),
         b'\x00\x1e\x20\xfe')
 def test_build_custom_epoch_negative_failure(self):
     with self.assertRaises(construct.core.FormatFieldError):
         construct_extras.Timestamp(construct.Int32ul,
                                    epoch=_NEW_EPOCH).build(_TEST_DATE1)
 def test_build_custom_epoch(self):
     self.assertEqual(
         construct_extras.Timestamp(construct.Int32ul,
                                    epoch=_NEW_EPOCH).build(_TEST_DATE2),
         b'\x00\x00\x00\x00')
 def test_parse_unix_epoch(self):
     self.assertEqual(
         construct_extras.Timestamp(
             construct.Int32ul).parse(b'\x803\xe1\x01'), _TEST_DATE2)
 def test_build_unix_epoch(self):
     self.assertEqual(
         construct_extras.Timestamp(construct.Int32ul).build(_TEST_DATE1),
         b'\x80\x51\x01\x00')
Пример #9
0
    command_prefix_construct = construct.Const(command_prefix, construct.Byte)

    return construct.Struct(
        'data' / construct.RawCopy(
            construct.Struct(
                construct.Const(b'\x02'),  # stx
                'length' / construct.Rebuild(
                    construct.Byte, lambda this: len(this.message) + 7),
                'link_control' / link_control_construct,
                'command_prefix' / command_prefix_construct,
                'message' / construct.Bytes(lambda this: this.length - 7),
                construct.Const(b'\x03'),  # etx
            ), ),
        'checksum' / construct.Checksum(construct.Int16ul, lifescan.crc_ccitt,
                                        construct.this.data.data),
    )


COMMAND_SUCCESS = construct.Const(b'\x06')

VERIO_TIMESTAMP = construct_extras.Timestamp(
    construct.Int32ul, epoch=946684800)  # 2000-01-01 (not 2010)

_GLUCOSE_UNIT_MAPPING_TABLE = {
    common.Unit.MG_DL: 0x00,
    common.Unit.MMOL_L: 0x01,
}

GLUCOSE_UNIT = construct.Mapping(construct.Byte, _GLUCOSE_UNIT_MAPPING_TABLE)
Пример #10
0
    'version' / construct.PascalString(construct.Byte, encoding='ascii'),
)

_SERIAL_NUMBER_REQUEST = construct.Const(
    b'\x05\x0B\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00')

_SERIAL_NUMBER_RESPONSE = construct.Struct(
    _COMMAND_SUCCESS,
    'serial_number' / construct.GreedyString(encoding='ascii'),
)

_DATETIME_REQUEST = construct.Struct(
    construct.Const(b'\x05\x20'),  # 0x20 is the datetime
    'request_type' / construct.Enum(construct.Byte, write=0x01, read=0x02),
    'timestamp' / construct.Default(
        construct_extras.Timestamp(construct.Int32ul),  # type: ignore
        datetime.datetime(1970, 1, 1, 0, 0)),
)

_DATETIME_RESPONSE = construct.Struct(
    _COMMAND_SUCCESS,
    'timestamp' /
    construct_extras.Timestamp(construct.Int32ul),  # type: ignore
)

_GLUCOSE_UNIT_REQUEST = construct.Const(b'\x05\x09\x02\x09\x00\x00\x00\x00')

_GLUCOSE_UNIT_RESPONSE = construct.Struct(
    _COMMAND_SUCCESS,
    'unit' / lifescan_binary_protocol.GLUCOSE_UNIT,
    construct.Padding(3),
Пример #11
0
    'version' / construct.PascalString(construct.Byte, encoding='ascii'),
)

_SERIAL_NUMBER_REQUEST = construct.Const(
    b'\x05\x0B\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00')

_SERIAL_NUMBER_RESPONSE = construct.Struct(
    _COMMAND_SUCCESS,
    'serial_number' / construct.GreedyString(encoding='ascii'),
)

_DATETIME_REQUEST = construct.Struct(
    construct.Const(b'\x05\x20'),  # 0x20 is the datetime
    'request_type' / construct.Enum(construct.Byte, write=0x01, read=0x02),
    'timestamp' /
    construct.Default(construct_extras.Timestamp(construct.Int32ul),
                      datetime.datetime(1970, 1, 1, 0, 0)),
)

_DATETIME_RESPONSE = construct.Struct(
    _COMMAND_SUCCESS,
    'timestamp' / construct_extras.Timestamp(construct.Int32ul),
)

_GLUCOSE_UNIT_REQUEST = construct.Const(b'\x05\x09\x02\x09\x00\x00\x00\x00')

_GLUCOSE_UNIT_RESPONSE = construct.Struct(
    _COMMAND_SUCCESS,
    'unit' / lifescan_binary_protocol.GLUCOSE_UNIT,
    construct.Padding(3),
)