Exemple #1
0
    def test_sense_get_humidity(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_humidity()

            expected_data = {
                "sensor_id": 3,
                "values": {
                    "humidity": "20"
                },
                "date": date_str,
            }

            self.assertDictEqual(expected_data, data)
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)
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)))
                raise
            time.sleep(1)