def test_get_bytearray(self):
     self.assertEqual(Calculator.get_bytearray(0), bytearray(b'\x00'))
     self.assertEqual(Calculator.get_bytearray(1), bytearray(b'\x01'))
     self.assertEqual(Calculator.get_bytearray(22), bytearray(b'\x16'))
     self.assertEqual(Calculator.get_bytearray(222222222),
                      bytearray(b'\x0d\x3e\xd7\x8e'))
     self.assertEqual(Calculator.get_bytearray(12345),
                      bytearray(b'\x30\x39'))
    def get_heartbeat_request(params):
        heartbeat_id = params['heartbeat_id']
        curr_heartbeat_period = params['heartbeat_period']
        data = DataPacketFactory.init_packet()
        data.append(
            GlobalConstants.MESSAGE_CODE_DICT['HEARTBEAT_REQUEST'])  # msg_code
        data_content = Calculator.get_bytearray(curr_heartbeat_period,
                                                1) + Calculator.get_bytearray(
                                                    heartbeat_id, 4)
        DataPacketFactory.append_data(data, data_content)
        data.append(0x81)  # end_code

        DataPacketFactory.append_len(data)
        DataPacketFactory.append_checksum(data)
        return data
Example #3
0
 def verify_checksum(self, data: Data):
     data_type = '0' + str(data.msg_code)
     data_for_checksum = data_type + data.data_payload
     byte_len = int(
         len(data_for_checksum) / GlobalConstants.HEX_DIGITS_PER_BYTE)
     dec_num = Calculator.get_int(data_for_checksum)
     data_int = Calculator.get_bytearray(dec_num, byte_len)
     expected_checksum = Checksum.crc32(data_int)
     return data.checksum == expected_checksum
    def get_reset_response(params):
        data = DataPacketFactory.init_packet()
        data.append(
            GlobalConstants.MESSAGE_CODE_DICT['RESET_RESPONSE'])  #msg_code
        reset_reason = params['reset_reason']
        reset_reason = Calculator.get_bytearray(reset_reason, 1)
        DataPacketFactory.append_data(data, reset_reason)
        data.append(0x81)

        DataPacketFactory.append_len(data)
        DataPacketFactory.append_checksum(data)
        return data
    def get_heartbeat_response(params):
        heartbeat_id = params['heartbeat_id']
        data = DataPacketFactory.init_packet()
        data.append(GlobalConstants.MESSAGE_CODE_DICT['HEARTBEAT_RESPONSE']
                    )  # msg_code
        DataPacketFactory.append_data(
            data, Calculator.get_bytearray(heartbeat_id, 4))
        data.append(0x81)  # end_code

        DataPacketFactory.append_len(data)
        DataPacketFactory.append_checksum(data)
        return data
    def get_ecc_period_change(params=[]):
        period_id = params['period_id']
        data = DataPacketFactory.init_packet()
        data.append(
            GlobalConstants.MESSAGE_CODE_DICT['ECC_PERIOD_CHANGE'])  # msg_code
        DataPacketFactory.append_data(
            data, Calculator.get_bytearray(period_id, bytelen=1))
        data.append(0x81)  # end_code

        DataPacketFactory.append_len(data)
        DataPacketFactory.append_checksum(data)
        return data
    def append_data(data: bytearray, data_content: bytearray):
        data_content_paired = list(DataStructFunctions.chunk(data_content, 2))
        num_seq = range(0, len(data_content_paired))
        num_seq = list(
            map(lambda x: Calculator.get_bytearray(x, bytelen=2), num_seq))

        i = 0
        while i < len(num_seq):
            cur_index = num_seq[i]
            cur_data = data_content_paired[i]
            data += cur_index
            data += cur_data
            i += 1
    def get_ecc_check(params=[]):
        index = params['index']
        err_cnt = params['err_cnt']
        overflow = params['overflow']
        ecc_addresses_str = params['ecc_addresses']
        ecc_addresses = Calculator.get_int(ecc_addresses_str)

        data = DataPacketFactory.init_packet()
        data.append(
            GlobalConstants.MESSAGE_CODE_DICT['ECC_CHECKED'])  # msg_code

        data_content = Calculator.get_bytearray(index, bytelen=2)
        data_content += Calculator.get_bytearray(err_cnt, bytelen=3)
        overflow_byte = 0 if overflow else 1
        data_content += Calculator.get_bytearray(overflow_byte, bytelen=1)
        data_content += Calculator.get_bytearray(
            ecc_addresses, bytelen=int(len(ecc_addresses_str) / 2))

        DataPacketFactory.append_data(data, data_content)
        data.append(0x81)  # end_code

        DataPacketFactory.append_len(data)
        DataPacketFactory.append_checksum(data)
        return data