Index: dialin/protocols/CAN.py =================================================================== diff -u -r12fc8d4dee213f861ee7645ce83c8b3a79896e10 -r93629045e925eea85b2808a0e7a7b0ea77426f7a --- dialin/protocols/CAN.py (.../CAN.py) (revision 12fc8d4dee213f861ee7645ce83c8b3a79896e10) +++ dialin/protocols/CAN.py (.../CAN.py) (revision 93629045e925eea85b2808a0e7a7b0ea77426f7a) @@ -26,6 +26,7 @@ import struct from .. import common from ..utils import SingletonMeta +from concurrent.futures import ThreadPoolExecutor class DenaliMessage: @@ -449,16 +450,17 @@ self.logger = logger self.log_can = log_can self.message_queue = deque() + self.thread_pool_executor = ThreadPoolExecutor(max_workers=1) # try to setup can bus and exit if the can bus has not ben setup to use. try: self.bus = can.interfaces.socketcan.SocketcanBus(channel=can_interface) self.loop = asyncio.get_event_loop() if self.bus is not None: - self.canbus_thread = threading.Thread(target=self.listener, daemon=True) - self.message_queue_thread = threading.Thread(target=self.handle_messages, daemon=True) + self.thread_canbus = threading.Thread(target=self.listener, daemon=True) + self.thread_message_queue = threading.Thread(target=self.handle_messages, daemon=True) else: - self.canbus_thread = None + self.thread_canbus = None s = "Can connection is not valid" self.logger.debug(s) sys.exit(s) @@ -493,12 +495,12 @@ return else: self.run = True - if self.message_queue_thread is not None and self.canbus_thread is not None: - if not self.canbus_thread.is_alive(): - self.canbus_thread.start() + if self.thread_message_queue is not None and self.thread_canbus is not None: + if not self.thread_canbus.is_alive(): + self.thread_canbus.start() self.logger.info("Canbus thread has started.") - if not self.message_queue_thread.is_alive(): - self.message_queue_thread.start() + if not self.thread_message_queue.is_alive(): + self.thread_message_queue.start() self.logger.info("Message queue thread has started.") else: self.logger.error("Cannot start listener...") @@ -531,7 +533,6 @@ else: self.loop.create_task(_listener()) - def handle_messages(self): """ Handles messages added to the dialin canbus message queue @@ -626,9 +627,11 @@ # If it is not, this is a publication message and we need to call it's register function elif dialin_ch_id in self.sync_response_dictionary.keys() and \ dialin_msg_id in self.sync_response_dictionary[channel_id].keys(): + self.thread_pool_executor.submit( + self.sync_response_dictionary[dialin_ch_id][dialin_msg_id], + complete_dialin_message + ) - self.sync_response_dictionary[dialin_ch_id][dialin_msg_id](complete_dialin_message) - # Done with this message, let's get the next one self.messages = None Index: dialin/utils/conversions.py =================================================================== diff -u -r12fc8d4dee213f861ee7645ce83c8b3a79896e10 -r93629045e925eea85b2808a0e7a7b0ea77426f7a --- dialin/utils/conversions.py (.../conversions.py) (revision 12fc8d4dee213f861ee7645ce83c8b3a79896e10) +++ dialin/utils/conversions.py (.../conversions.py) (revision 93629045e925eea85b2808a0e7a7b0ea77426f7a) @@ -14,64 +14,77 @@ # ############################################################################ import struct +from typing import List -def byte_to_bytearray(val): +def byte_to_bytearray(val: int) -> bytes: """ Converts a byte value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ + if type(val) != int: + raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts a short integer (2 bytes) value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ + if type(val) != int: + raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts an integer value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ + if type(val) != int: + raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts an integer value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ + if type(val) != int or val < 0: + raise ValueError("Expected unsigned integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts a float value into a byte array (little endian) @param val: (float) to convert to byte array @return:: byte array """ + if type(val) != float: + raise ValueError("Expected float but received {0} with type {1}".format(val, type(val))) return struct.pack(' List[int]: """ Converts an integer to a bit array. For direct conversion to binary please use bin(val), as this function has a separate purpose @param val: (int) the number to convert @param num_bits: (int) the number of bits needed @return: (List[int]) The number represented in binary form as a list of 1's and 0's """ + if type(val) != int: + raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return [(val >> bit) & 1 for bit in range(num_bits - 1, -1, -1)] \ No newline at end of file Index: dialin/utils/data_logger.py =================================================================== diff -u -rca7a00d1584b97eed9471fe4a5f17a7aa3ea62d8 -r93629045e925eea85b2808a0e7a7b0ea77426f7a --- dialin/utils/data_logger.py (.../data_logger.py) (revision ca7a00d1584b97eed9471fe4a5f17a7aa3ea62d8) +++ dialin/utils/data_logger.py (.../data_logger.py) (revision 93629045e925eea85b2808a0e7a7b0ea77426f7a) @@ -23,7 +23,6 @@ from typing import Tuple from multiprocessing import Process from pathlib import Path -from typing import Union import pandas as pd from dialin.utils.base import _FauxLogger