diff --git a/hardware/CommunicationsPi/lan_server.py b/hardware/CommunicationsPi/lan_server.py index e49f35b1..62564a48 100644 --- a/hardware/CommunicationsPi/lan_server.py +++ b/hardware/CommunicationsPi/lan_server.py @@ -42,7 +42,7 @@ def run(server_class=HTTPServer, handler_class=S, port=8080): logging.basicConfig(level=logging.INFO) server_address = ("", port) httpd = server_class(server_address, handler_class) - logging.info("Starting httpd...\n") + log.info("Starting httpd...\n") try: httpd.serve_forever() except KeyboardInterrupt: diff --git a/hardware/CommunicationsPi/radio_transceiver.py b/hardware/CommunicationsPi/radio_transceiver.py new file mode 100644 index 00000000..59d77741 --- /dev/null +++ b/hardware/CommunicationsPi/radio_transceiver.py @@ -0,0 +1,46 @@ +import os +import serial +import json + +from .utils import get_logger, get_serial_stream + + +class Transceiver: + def __init__(self, log_file_name=None, port=None): + if log_file_name is None: + self.logging = get_logger("TRANSMITTER_LOG_FILE") + else: + self.logging = get_logger(log_file_name) + + port = os.environ["RADIO_TRANSMITTER_PORT"] if port is None else port + baudrate = 9600 + parity = serial.PARITY_NONE + stopbits = serial.STOPBITS_ONE + bytesize = serial.EIGHTBITS + timeout = 1 + + self.logging.info("Opening serial") + self.serial = serial.Serial( + port=port, + baudrate=baudrate, + parity=parity, + stopbits=stopbits, + bytesize=bytesize, + timeout=timeout, + ) + + def send(self, payload): + self.logging.info("sending") + self.serial.write(get_serial_stream(payload)) + self.logging.info(payload) + + def listen(self): + payload = self.serial.readline().decode("utf-8") + message = "Error: Check logs" + if payload != "": + try: + message = json.loads(payload) + self.logging.info(message) + except json.JSONDecodeError: + self.logging.error(json.JSONDecodeError) + return message diff --git a/hardware/CommunicationsPi/serial_read.py b/hardware/CommunicationsPi/serial_read.py deleted file mode 100644 index e299fe5c..00000000 --- a/hardware/CommunicationsPi/serial_read.py +++ /dev/null @@ -1,27 +0,0 @@ -import os -import json -import serial -from utils import get_logger - -logging = get_logger("RECEIVER_LOG_FILE") - -logging.info("Opening serial") -ser = serial.Serial( - port=os.environ["RADIO_RECEIVER_PORT"], - baudrate=9600, - parity=serial.PARITY_NONE, - stopbits=serial.STOPBITS_ONE, - bytesize=serial.EIGHTBITS, - timeout=1, -) - -logging.info("listening") -while 1: - x = ser.readline().decode("utf-8") - if x != "": - try: - message = json.loads(x) - logging.info(message) - print(message["value"]["value_c_name"]) # indexes into JSON message - except json.JSONDecodeError: - logging.error(message) diff --git a/hardware/CommunicationsPi/serial_write.py b/hardware/CommunicationsPi/serial_write.py deleted file mode 100644 index ba5f29ae..00000000 --- a/hardware/CommunicationsPi/serial_write.py +++ /dev/null @@ -1,28 +0,0 @@ -import os -import time -import serial - -from utils import get_logger, get_serial_stream - -logging = get_logger("TRANSMITTER_LOG_FILE") - -logging.info("Opening serial") -ser = serial.Serial( - port=os.environ["RADIO_TRANSMITTER_PORT"], - baudrate=9600, - parity=serial.PARITY_NONE, - stopbits=serial.STOPBITS_ONE, - bytesize=serial.EIGHTBITS, - timeout=1, -) - -logging.info("sending") - -while 1: - message = { - "id": 5, - "value": {"value_a_name": 15.0, "value_b_name": 26.5, "value_c_name": 13.3}, - } - ser.write(get_serial_stream(message)) - logging.info(message) - time.sleep(1)