class CommPi(BaseHTTPRequestHandler): def __init__(self, *args, **kwargs): self.transceiver = Transceiver() super().__init__(*args, **kwargs) def _set_response(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() def do_GET(self): self._set_response() self.wfile.write("GET request for {}".format( self.path).encode("utf-8")) def do_POST(self): content_length = int(self.headers["Content-Length"]) post_data = self.rfile.read(content_length) self.send_data(str(post_data.decode("utf-8"))) self._set_response() self.wfile.write("POST request for {}".format( self.path).encode("utf-8")) def send_data(self, payload): self.transceiver.send(payload) return
class CommPi(BaseHTTPRequestHandler): def __init__(self, *args, **kwargs): self.transceiver = Transceiver() super().__init__(*args, **kwargs) def _set_response(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() def do_GET(self): self._set_response() self.wfile.write("GET request for {}".format(self.path).encode("utf-8")) def do_POST(self): content_length = int(self.headers["Content-Length"]) post_data = self.rfile.read(content_length) self.send_data(str(post_data.decode("utf-8"))) self._set_response() self.wfile.write("POST request for {}".format(self.path).encode("utf-8")) def send_data(self, payload): if os.environ.get("ENABLE_INTERNET_TRANSMISSION"): print("transmit via internet") if os.environ.get("ENABLE_RADIO_TRANSMISSION"): print("transmit via radio") self.transceiver.send(payload) return
def __init__(self, *args, **kwargs): if os.environ.get("ENABLE_RADIO_TRANSMISSION"): self.transceiver = Transceiver() if os.environ.get("ENABLE_INTERNET_TRANSMISSION"): apiUrl = os.environ.get("REMOTE_SERVER_API_ENDPOINT") self.web_client = WebClient(server_url=apiUrl) self.logging = get_logger("COMM_PI_LOG_FILE") super().__init__(*args, **kwargs)
def handleLocal(): """ starts listening on the defined serial port and passing received data along to the web client when received """ transceiver = Transceiver() url = os.environ.get("DJANGO_SERVER_API_ENDPOINT") if url: client = WebClient(server_url=url) while True: data = transceiver.listen() if data: print(data, type(data)) payload = json.loads(data) client.send(payload) else: print("DJANGO_SERVER_API_ENDPOINT not set")
class CommPi(BaseHTTPRequestHandler): def __init__(self, *args, **kwargs): if os.environ.get("ENABLE_RADIO_TRANSMISSION"): self.transceiver = Transceiver() if os.environ.get("ENABLE_INTERNET_TRANSMISSION"): apiUrl = os.environ.get("REMOTE_SERVER_API_ENDPOINT") self.web_client = WebClient(server_url=apiUrl) self.logging = get_logger("COMM_PI_LOG_FILE") super().__init__(*args, **kwargs) def _set_response(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() def do_GET(self): self._set_response() self.logging.info("GET request for {}".format(self.path).encode("utf-8")) self.wfile.write("GET request for {}".format(self.path).encode("utf-8")) def do_POST(self): content_length = int(self.headers["Content-Length"]) post_data = self.rfile.read(content_length) self.send_data(str(post_data.decode("utf-8"))) self._set_response() self.logging.info("POST request for {}".format(self.path).encode("utf-8")) self.wfile.write("POST request for {}".format(self.path).encode("utf-8")) def send_data(self, payload): self.logging.info("send_data called, payload: " + str(payload)) try: if os.environ.get("ENABLE_INTERNET_TRANSMISSION"): self.logging.info("transmit via internet") payload = json.loads(payload) self.web_client.send(payload) if os.environ.get("ENABLE_RADIO_TRANSMISSION"): self.logging.info("transmit via radio") self.transceiver.send(payload) except Exception as err: self.logging.error("error occurred: {}".format(str(err))) raise return
def test_is_serial_usb_manufacturer_match(self, mock_port_list, mock_serial): """ Tests the __init__ function where the port manufacturer is not empty and doesn't match the supplied port in RADIO_TRANSMITTER_PORT """ port, port2 = ListPortInfo(), ListPortInfo() port.vid = "foo" port.pid = "bar" port.manufacturer = "Microsoft" port.serial_number = "456" port.interface = "usb" port.device = "usb" port2.vid = "foo" port2.pid = "bar" port2.manufacturer = "Apple" port2.serial_number = "123" port2.interface = "bluetooth" port2.device = "usb2" mock_port_list.return_value = [port, port2] with patch.dict( os.environ, { "LOG_DIRECTORY": self.temp_dir.path, "RADIO_TRANSMITTER_PORT": "usb2", "LOG_FILE": "logger.txt", "TRANSCEIVER_BAUDRATE": "9600", "TRANSCEIVER_TIMEOUT": "1", }, ): transciever = Transceiver(log_file_name="LOG_FILE") self.assertTrue(transciever.logging is not None) self.assertTrue(transciever.logging.name == "LOG_FILE") self.assertIsInstance(transciever.logging, Logger) self.assertTrue(transciever.port == "usb2") self.assertTrue(transciever.port_vid == "foo") self.assertTrue(transciever.port_pid == "bar") self.assertTrue(transciever.port_vendor == "Apple") self.assertTrue(transciever.port_intf == "bluetooth") self.assertTrue(transciever.port_serial_number == "123") mock_serial.assert_called_with( port="usb2", baudrate=self.baudrate, parity=self.parity, stopbits=self.stopbits, bytesize=self.bytesize, timeout=self.timeout, )
def test_listen_exception(self, mock_port_list, mock_serial, mock_json): """ tests the listen method with invalid input """ port = ListPortInfo() port.vid = "vid" port.pid = "pid" port.manufacturer = "Microsoft" port.serial_number = "456" port.interface = "usb" port.device = "usb" mock_json.side_effect = Exception("ex") mock_port_list.return_value = [port] test_input = "{'value': 'value'}" with patch.dict( os.environ, { "LOG_DIRECTORY": self.temp_dir.path, "RADIO_TRANSMITTER_PORT": "usb", "LOG_FILE": "logger.txt", "TRANSCEIVER_BAUDRATE": "9600", "TRANSCEIVER_TIMEOUT": "1", }, ): with LogCapture() as capture: transceiver = Transceiver(log_file_name="LOG_FILE") mock_receiver = MagicMock() mock_receiver.readline.return_value.decode.return_value = test_input transceiver.serial = mock_receiver with self.assertRaises(Exception): transceiver.listen() capture.check( ("LOG_FILE", "INFO", "Port device found: usb"), ("LOG_FILE", "INFO", "Opening serial on: usb"), ("LOG_FILE", "ERROR", "error occurred: ex"), )
def test_send(self, mock_port_list, mock_serial): """ tests the send method """ port = ListPortInfo() port.vid = "vid" port.pid = "pid" port.manufacturer = "Microsoft" port.serial_number = "456" port.interface = "usb" port.device = "usb" mock_port_list.return_value = [port] test_payload = {"value": "value"} output = get_serial_stream(test_payload) with patch.dict( os.environ, { "LOG_DIRECTORY": self.temp_dir.path, "RADIO_TRANSMITTER_PORT": "usb", "LOG_FILE": "logger.txt", "TRANSCEIVER_BAUDRATE": "9600", "TRANSCEIVER_TIMEOUT": "1", }, ): with LogCapture() as capture: transciever = Transceiver(log_file_name="LOG_FILE") mock_serial_sender = MagicMock() transciever.serial = mock_serial_sender transciever.send(test_payload) mock_serial_sender.write.assert_called_with(output) capture.check( ("LOG_FILE", "INFO", "Port device found: usb"), ("LOG_FILE", "INFO", "Opening serial on: usb"), ("LOG_FILE", "INFO", "sending"), ("LOG_FILE", "INFO", "{'value': 'value'}"), )
def test_is_serial_usb_no_vid(self, mock_port_list, mock_serial): """ Tests the __init__ function where the port vid is empty """ port = ListPortInfo() port.vid = None port.pid = None port.manufacturer = None port.serial_number = None port.interface = None port.device = "usb" mock_port_list.return_value = [port] with patch.dict( os.environ, { "LOG_DIRECTORY": self.temp_dir.path, "RADIO_TRANSMITTER_PORT": "", "LOG_FILE": "logger.txt", "TRANSCEIVER_BAUDRATE": "9600", "TRANSCEIVER_TIMEOUT": "1", }, ): transciever = Transceiver(log_file_name="LOG_FILE") self.assertTrue(transciever.logging is not None) self.assertTrue(transciever.logging.name == "LOG_FILE") self.assertIsInstance(transciever.logging, Logger) self.assertTrue(transciever.port == "") self.assertIsNone(transciever.port_vid) self.assertIsNone(transciever.port_pid) self.assertIsNone(transciever.port_vendor) self.assertIsNone(transciever.port_intf) self.assertIsNone(transciever.port_serial_number) mock_serial.assert_called_with( port="", baudrate=self.baudrate, parity=self.parity, stopbits=self.stopbits, bytesize=self.bytesize, timeout=self.timeout, )
def __init__(self, *args, **kwargs): self.transceiver = Transceiver() super().__init__(*args, **kwargs)
all = sensePi.get_all() coords = gpsPi.get_geolocation() if coords is not None: data = [temp, pres, hum, acc, orie, coords, all] else: data = [temp, pres, hum, acc, orie, all] for i in data: payload = json.dumps(i) print(payload) try: client.ping_lan_server(payload) except Exception as err: print("error occurred: {}".format(str(err))) raise time.sleep(1) else: print("Local Django Server") transceiver = Transceiver() url = os.environ.get("DJANGO_SERVER_API_ENDPOINT") if url: client = WebClient(server_url=url) while True: data = transceiver.listen() if data: print(data) client.ping_lan_server(json.loads(data)) else: print("DJANGO_SERVER_API_ENDPOINT not set")