From 821464498b8a91b321703ddb6e13a5d4f42b3e06 Mon Sep 17 00:00:00 2001 From: PrabhanshuAttri Date: Wed, 1 Apr 2020 15:10:32 -0400 Subject: [PATCH 1/7] Added radio_transeiver class --- hardware/CommunicationsPi/lan_server.py | 2 +- .../CommunicationsPi/radio_transceiver.py | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 hardware/CommunicationsPi/radio_transceiver.py 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..272baab3 --- /dev/null +++ b/hardware/CommunicationsPi/radio_transceiver.py @@ -0,0 +1,46 @@ +import os +import time +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=1, + ) + + + 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 is not "": + try: + message = json.loads(payload) + self.logging.info(message) + except json.JSONDecodeError: + logging.error(json.JSONDecodeError) + return message From 4afeda48257b75f03b006bf365d6e553d62d8272 Mon Sep 17 00:00:00 2001 From: PrabhanshuAttri Date: Wed, 1 Apr 2020 15:11:09 -0400 Subject: [PATCH 2/7] Removed redundant code --- hardware/CommunicationsPi/serial_read.py | 27 ---------------------- hardware/CommunicationsPi/serial_write.py | 28 ----------------------- 2 files changed, 55 deletions(-) delete mode 100644 hardware/CommunicationsPi/serial_read.py delete mode 100644 hardware/CommunicationsPi/serial_write.py 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) From f83ea1123dd3623587f0e2a3b61bf0f285e6bb12 Mon Sep 17 00:00:00 2001 From: PrabhanshuAttri Date: Wed, 1 Apr 2020 15:14:09 -0400 Subject: [PATCH 3/7] import fixes --- hardware/CommunicationsPi/radio_transceiver.py | 11 ++++++----- hardware/CommunicationsPi/utils.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/hardware/CommunicationsPi/radio_transceiver.py b/hardware/CommunicationsPi/radio_transceiver.py index 272baab3..a645c570 100644 --- a/hardware/CommunicationsPi/radio_transceiver.py +++ b/hardware/CommunicationsPi/radio_transceiver.py @@ -4,6 +4,7 @@ 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: @@ -12,11 +13,11 @@ def __init__(self, log_file_name=None, port=None): 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, + baudrate=9600 + parity=serial.PARITY_NONE + stopbits=serial.STOPBITS_ONE + bytesize=serial.EIGHTBITS + timeout=1 self.logging.info("Opening serial") self.serial = serial.Serial( diff --git a/hardware/CommunicationsPi/utils.py b/hardware/CommunicationsPi/utils.py index ff83b53f..a6f1af65 100644 --- a/hardware/CommunicationsPi/utils.py +++ b/hardware/CommunicationsPi/utils.py @@ -1,6 +1,6 @@ import os import json -from .logger import Logger +from logger import Logger def get_serial_stream(s): From c502cfb514a868434453d8ec3f985a5471cedd59 Mon Sep 17 00:00:00 2001 From: PrabhanshuAttri Date: Wed, 1 Apr 2020 15:15:16 -0400 Subject: [PATCH 4/7] Fixed formatting --- .../CommunicationsPi/radio_transceiver.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/hardware/CommunicationsPi/radio_transceiver.py b/hardware/CommunicationsPi/radio_transceiver.py index a645c570..0e22ba33 100644 --- a/hardware/CommunicationsPi/radio_transceiver.py +++ b/hardware/CommunicationsPi/radio_transceiver.py @@ -5,6 +5,7 @@ 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: @@ -13,22 +14,21 @@ def __init__(self, log_file_name=None, port=None): 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 + 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=1, - ) - + port=port, + baudrate=baudrate, + parity=parity, + stopbits=stopbits, + bytesize=bytesize, + timeout=1, + ) def send(self, payload): self.logging.info("sending") @@ -37,7 +37,7 @@ def send(self, payload): def listen(self): payload = self.serial.readline().decode("utf-8") - message = 'Error: Check logs' + message = "Error: Check logs" if payload is not "": try: message = json.loads(payload) From 8baddef9d604f202c9ef6368b0696efe4db962bf Mon Sep 17 00:00:00 2001 From: PrabhanshuAttri Date: Wed, 1 Apr 2020 15:16:39 -0400 Subject: [PATCH 5/7] minor fix --- hardware/CommunicationsPi/radio_transceiver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware/CommunicationsPi/radio_transceiver.py b/hardware/CommunicationsPi/radio_transceiver.py index 0e22ba33..950dad1d 100644 --- a/hardware/CommunicationsPi/radio_transceiver.py +++ b/hardware/CommunicationsPi/radio_transceiver.py @@ -27,7 +27,7 @@ def __init__(self, log_file_name=None, port=None): parity=parity, stopbits=stopbits, bytesize=bytesize, - timeout=1, + timeout=timeout, ) def send(self, payload): From a2141139947956a831b6b84f1d678419f348daa3 Mon Sep 17 00:00:00 2001 From: PrabhanshuAttri Date: Wed, 1 Apr 2020 16:01:29 -0400 Subject: [PATCH 6/7] fixed errors --- hardware/CommunicationsPi/radio_transceiver.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hardware/CommunicationsPi/radio_transceiver.py b/hardware/CommunicationsPi/radio_transceiver.py index 950dad1d..9690b596 100644 --- a/hardware/CommunicationsPi/radio_transceiver.py +++ b/hardware/CommunicationsPi/radio_transceiver.py @@ -1,5 +1,4 @@ import os -import time import serial import json @@ -38,10 +37,10 @@ def send(self, payload): def listen(self): payload = self.serial.readline().decode("utf-8") message = "Error: Check logs" - if payload is not "": + if payload != "": try: message = json.loads(payload) self.logging.info(message) except json.JSONDecodeError: - logging.error(json.JSONDecodeError) + self.logging.error(json.JSONDecodeError) return message From ae6d64ed60bc305ba5ed4ff80e574335b27589d8 Mon Sep 17 00:00:00 2001 From: PrabhanshuAttri Date: Wed, 1 Apr 2020 16:24:09 -0400 Subject: [PATCH 7/7] Added import fix --- hardware/CommunicationsPi/radio_transceiver.py | 2 +- hardware/CommunicationsPi/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hardware/CommunicationsPi/radio_transceiver.py b/hardware/CommunicationsPi/radio_transceiver.py index 9690b596..59d77741 100644 --- a/hardware/CommunicationsPi/radio_transceiver.py +++ b/hardware/CommunicationsPi/radio_transceiver.py @@ -2,7 +2,7 @@ import serial import json -from utils import get_logger, get_serial_stream +from .utils import get_logger, get_serial_stream class Transceiver: diff --git a/hardware/CommunicationsPi/utils.py b/hardware/CommunicationsPi/utils.py index a6f1af65..ff83b53f 100644 --- a/hardware/CommunicationsPi/utils.py +++ b/hardware/CommunicationsPi/utils.py @@ -1,6 +1,6 @@ import os import json -from logger import Logger +from .logger import Logger def get_serial_stream(s):