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
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): 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_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'}"), )