示例#1
0
    def factory(self, type):
        data = {}
        if type in sensor_keys:
            sensor_data = {
                sensor_keys["TEMPERATURE"]: self.sense.get_temperature(),
                sensor_keys["PRESSURE"]: self.sense.get_pressure(),
                sensor_keys["HUMIDITY"]: self.sense.get_humidity(),
                sensor_keys["ACCELERATION"]:
                self.sense.get_accelerometer_raw(),
                sensor_keys["ORIENTATION"]: self.sense.get_orientation(),
            }

            if type == "ALL":
                # ToDo: implement get all sensor data
                # This is just example
                data["values"] = sensor_data
            elif sensor_keys[type] in sensor_data:
                key = sensor_keys[type]
                id = self.sensor_ids[key]
                data["sensor_id"] = id
                data["values"] = {}
                data["values"][key] = sensor_data[key]

            data["date"] = date_str_with_current_timezone()
        return data
 def test_date_str_with_current_timezone(self):
     s = date_str_with_current_timezone()
     date = dateutil.parser.isoparse(s)
     self.assertTrue("T" in s)
     self.assertAlmostEqual(date.timestamp(),
                            datetime.now().timestamp(),
                            places=1)
示例#3
0
def humidity():
    sense = SenseHat()
    humidity = sense.get_humidity()
    date = date_str_with_current_timezone()
    data = {}
    data["sensor_id"] = HUMIDITY_ID
    data["values"] = {"humidity": humidity}
    data["values"] = json.dumps(data["values"])
    data["date"] = date
    return data
示例#4
0
def pressure():
    sense = SenseHat()
    pressure = sense.get_pressure()
    date = date_str_with_current_timezone()
    data = {}
    data["sensor_id"] = PRESSURE_ID
    data["values"] = {"pressure": pressure}
    data["values"] = json.dumps(data["values"])
    data["date"] = date
    return data
示例#5
0
def temperature():
    sense = SenseHat()
    temperature = sense.get_temperature()
    date = date_str_with_current_timezone()
    data = {}
    data["sensor_id"] = TEMPERATURE_ID
    data["values"] = {"temperature": temperature}
    data["values"] = json.dumps(data["values"])
    data["date"] = date
    return data
示例#6
0
def orientation():
    sense = SenseHat()
    orientation = sense.get_orientation()
    date = date_str_with_current_timezone()
    data = {}
    data["sensor_id"] = ORIENTATION_ID
    data["values"] = {
        "roll": orientation["roll"],
        "pitch": orientation["pitch"],
        "yaw": orientation["yaw"],
    }
    data["values"] = json.dumps(data["values"])
    data["date"] = date
    return data
示例#7
0
def acceleration():
    sense = SenseHat()
    acceleration = sense.get_accelerometer_raw()
    date = date_str_with_current_timezone()
    data = {}
    data["sensor_id"] = ACCELERATION_ID
    data["values"] = {
        "x": acceleration["x"],
        "y": acceleration["y"],
        "z": acceleration["z"],
    }
    data["values"] = json.dumps(data["values"])
    data["date"] = date
    return data
    def get_geolocation(self):
        while self.gps.inWaiting() == 0:
            pass

        nmeaSentence = self.gps.readline().split(",")
        nmeaType = nmeaSentence[0]

        # Added additional check to verify if nmeaSentence has valid data
        if nmeaType == "$GPRMC" and nmeaSentence[2] == "A":
            latitude_hours = float(nmeaSentence[3][0:2])
            latitude_minutes = float(nmeaSentence[3][2:])
            longitude_hours = float(nmeaSentence[5][0:3])
            longitude_minutes = float(nmeaSentence[5][3:])

            latitude_decimal = latitude_hours + latitude_minutes / 60
            longitude_decimal = longitude_hours + longitude_minutes / 60

            latitude_dir = nmeaSentence[4]
            longitude_dir = nmeaSentence[6]

            if latitude_dir == "S":
                latitude_decimal = latitude_decimal * -1
            if longitude_dir == "W":
                longitude_decimal = longitude_decimal * -1

            self.logging.info("latitude_decimal: " + str(latitude_decimal))
            self.logging.info("longitude_decimal: " + str(longitude_decimal))

            data = {}
            data["sensor_id"] = GPS_ID
            data["values"] = {
                "latitude": latitude_decimal,
                "longitude": longitude_decimal,
            }
            data["date"] = date_str_with_current_timezone()
            return data
        else:
            return None
    def get_speed_mph(self):
        while self.gps.inWaiting() == 0:
            pass

        nmeaSentence = str(self.gps.readline()).split(",")
        nmeaType = nmeaSentence[0]

        # verify nmeaSentence type and verify if gps has valid data
        if nmeaType == "b'$GPRMC" and nmeaSentence[2] == "A":

            speed_in_knots = nmeaSentence[7]
            speed_in_mph = float(speed_in_knots) * 1.151

            self.logging.info("speed: " + str(speed_in_mph))

            data = {}
            data["sensor_id"] = GPS_ID
            data["values"] = {
                "speed": speed_in_mph,
            }
            data["date"] = date_str_with_current_timezone()
            return data
        else:
            return None