def setUp(self) -> None: """The CAN messages here are of no magic significant other than they were verified by a human to meet the implementation requirements of the CAN decoder and mapping to Sensors.""" self.temp_data = CANDecoder( 0b100000000001000100000000001000000010000000100000001000000010000000100000001000000010000000000000001110000000000 # noqa E501 ).decode_can_message() self.accel_data = CANDecoder( 0b1000000000100000001000000010000000000000001110000000000 ).decode_can_message() self.wheel_speed_data = CANDecoder( 0b1000000000110000001000000010000000000000001110000000000 ).decode_can_message() self.suspension_data = CANDecoder( 0b1000000001000000001000000010000000000000001110000000000 ).decode_can_message() self.fuel_data = CANDecoder( 0b1000000001010000001000000010000000000000001110000000000 ).decode_can_message()
def post(request, *args, **kwargs): if "can_msg" in request.POST: # origin is the ui log.debug("Request came from the UI") can_msg = request.POST.get("can_msg") else: # origin is the api log.debug("Request came from the API") if not request.body: msg = "Request body is empty. Please re-POST with a non-empty body." return _bad_request({}, msg) else: can_msg = request.body try: decoder = CANDecoder(can_msg) except (MessageLengthException, BadInputException) as e: return JsonResponse(e.error, status=400) try: decoded_data = decoder.decode_can_message() except (InvalidBitException, NoMoreBitsException) as e: return JsonResponse(e.error, status=400) sensor_type = _determine_sensor(decoded_data) if sensor_type is None: msg = "Unable to determine sensor based on can_id. Please check can_id value." return _bad_request(decoded_data, msg) try: sensor = _create_populated_sensor(sensor_type, decoded_data) except DataTooShortException as e: return _bad_request( decoded_data, e.error["error"], words_expected=e.error["words_expected"], words_found=e.error["words_found"], ) log.debug("Saving a new instance of {}".format(sensor)) sensor.save() return JsonResponse(dict(can_msg=decoded_data), status=201)
def setUp(self) -> None: binary_number_decoder = CANDecoder( 0b10000000001101000000000000000000000001000000010000000000000001110000000000 ) self.bin_data = binary_number_decoder.decode_can_message() binary_string_decoder = CANDecoder( "0b10000000001101000000000000000000000001000000010000000000000001110000000000" ) self.bin_str_data = binary_string_decoder.decode_can_message() integer_decoder = CANDecoder(9459720945368167357440) self.int_data = integer_decoder.decode_can_message() bytes_decoder = CANDecoder( b"0b10000000001101000000000000000000000001000000010000000000000001110000000000" ) self.bytes_data = bytes_decoder.decode_can_message()
def setUp(self) -> None: decoder = CANDecoder(EXAMPLE_CAN_MSG_EXTENDED) self.data = decoder.decode_can_message()
def test_bad_input_raises_exception(self): with self.assertRaises(BadInputException): CANDecoder("thisisbadCANdata")
def test_too_short_message(self): temp_data = CANDecoder( 0b1000000001010000001000000010000000000000001110000000) with self.assertRaises(NoMoreBitsException): temp_data.decode_can_message()
def test_value_error_for_int_as_str_caught(self): temp_data = CANDecoder("9459720945368167357440") self.assertTrue(isinstance(temp_data, CANDecoder))
def test_invalid_bit_ack(self): temp_data = CANDecoder( 0b10000000001101000000000000000000000001000000010000000000000001100000000000 ) with self.assertRaises(InvalidBitException): temp_data.decode_can_message()
def test_msg_length_exception(self): with self.assertRaises(MessageLengthException): CANDecoder( 0b100000000001000100000000001000000010000000100000001000000010000000100000001000000010000000000000001110000000000000000000000000000000000 # noqa E501 )