Exemple #1
0
    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 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")
        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")