Ejemplo n.º 1
0
    def test_sense_get_orientation(self, mock_date, mock_sense):

        mock_show_message = MagicMock()
        mock_clear = MagicMock()
        mock_sense.return_value.show_message = mock_show_message
        mock_sense.return_value.clear = mock_clear
        mock_sense.return_value.get_temperature.return_value = "100"
        mock_sense.return_value.get_pressure.return_value = "50"
        mock_sense.return_value.get_humidity.return_value = "20"
        mock_sense.return_value.get_accelerometer_raw.return_value = "20"
        mock_sense.return_value.get_orientation.return_value = (1, 1, 1)

        date_str = "example_date"
        mock_date.return_value = date_str

        with patch.dict(
                os.environ,
            {
                "SENSE_HAT_LOG_FILE": "logger.txt",
                "LOG_DIRECTORY": self.temp_dir.path
            },
        ):
            sense = SensePi()
            data = sense.get_orientation()

            expected_data = {
                "sensor_id": 5,
                "values": {
                    "orientation": (1, 1, 1)
                },
                "date": date_str,
            }

            self.assertDictEqual(expected_data, data)
Ejemplo n.º 2
0
    def test_init_logs_ids(self, mock_sense):

        mock_show_message = MagicMock()
        mock_clear = MagicMock()
        mock_sense.return_value.show_message = mock_show_message
        mock_sense.return_value.clear = mock_clear

        expected_sensor_ids = {
            "temperature": 5,
            "pressure": 4,
            "humidity": 3,
            "acceleration": 2,
            "orientation": 1,
        }

        expected_color = (87, 46, 140)

        with patch.dict(os.environ, {
                "SENSE_LOG": "logger.txt",
                "LOG_DIRECTORY": self.temp_dir.path
        }):
            sense = SensePi(log_file_name="SENSE_LOG",
                            sensor_ids=expected_sensor_ids)

            mock_show_message.assert_called_with("MERCURY",
                                                 text_colour=expected_color,
                                                 scroll_speed=0.04)
            mock_clear.assert_called()

            self.assertTrue(sense.logging is not None)
            self.assertTrue(sense.logging.name == "SENSE_LOG")
            self.assertIsInstance(sense.logging, Logger)

            self.assertDictEqual(sense.sensor_ids, expected_sensor_ids)
Ejemplo n.º 3
0
    def test_factory_invalid_key(self, mock_date, mock_sense):

        mock_show_message = MagicMock()
        mock_clear = MagicMock()
        mock_sense.return_value.show_message = mock_show_message
        mock_sense.return_value.clear = mock_clear

        date_str = "example_date"
        mock_date.return_value = date_str

        with patch.dict(
                os.environ,
            {
                "SENSE_HAT_LOG_FILE": "logger.txt",
                "LOG_DIRECTORY": self.temp_dir.path
            },
        ):
            sense = SensePi()
            data = sense.factory("SOME_KEY")

            expected_data = {}

            self.assertDictEqual(expected_data, data)
Ejemplo n.º 4
0
def handleSense():
    """
    Starts up the SensorPi runtime, begins listening for SenseHat input,
    establishing a connection to a local CommPi via LAN, and sending data
    for transmission to the CommPi
    """
    sensor_keys = get_sensor_keys()
    sensor_ids = {}
    sensor_ids[sensor_keys["TEMPERATURE"]] = 2
    sensor_ids[sensor_keys["PRESSURE"]] = 3
    sensor_ids[sensor_keys["HUMIDITY"]] = 4
    sensor_ids[sensor_keys["ACCELERATION"]] = 5
    sensor_ids[sensor_keys["ORIENTATION"]] = 6
    sensePi = SensePi(sensor_ids=sensor_ids)

    client = WebClient()

    while True:
        print("while true")
        temp = sensePi.get_temperature()
        pres = sensePi.get_pressure()
        hum = sensePi.get_humidity()
        acc = sensePi.get_acceleration()
        orie = sensePi.get_orientation()
        all = sensePi.get_all()

        dataArr = [temp, pres, hum, acc, orie, all]

        for payload in dataArr:
            payload = json.dumps(payload)
            payload = json.loads(payload)
            print(payload)
            try:
                client.send(payload)
            except Exception as err:
                print("error occurred: {}".format(str(err)))
                raise
            time.sleep(1)
import os
import time

from hardware.CommunicationsPi.comm_pi import CommPi
from hardware.CommunicationsPi.lan_server import runServer
from hardware.CommunicationsPi.lan_client import LANClient
from hardware.SensorPi.sense_pi import SensePi

if os.environ["PI_TYPE"] == "commPi":
    print("CommunicationsPi")
    runServer(handler_class=CommPi)
else:
    print("SensePi")
    sensePi = SensePi()
    client = LANClient()

    while True:
        temp = sensePi.get_temperature()
        pres = sensePi.get_pressure()
        hum = sensePi.get_humidity()
        acc = sensePi.get_acceleration()
        orie = sensePi.get_orientation()
        all = sensePi.get_all()

        data = [temp, pres, hum, acc, orie, all]
        for i in data:
            print(i)
            try:
                client.ping_lan_server(i)
            except Exception as err:
                print("error occurred: {}".format(str(err)))