Exemplo n.º 1
0
def multi_sensor_last_update(readings):
    sql = ''
    for sensorId, reading in readings:
        sql = "update LastSensorReading set reading={} where id={};".format(
            reading, sensorId)
        result = sensorDB(sql)
    return True
Exemplo n.º 2
0
def get_last_reading(sensor_id):
    sql = 'select reading from SensorReadings '
    sql += 'where sensorId={} '.format(sensor_id)
    sql += 'order by created desc limit 1'
    sql += ';'
    last_readings = sensorDB(sql)
    return last_readings
Exemplo n.º 3
0
def get_sensors_for(room):
    room_id = ROOM_ID_LOOKUP.get(room, None)
    if room_id is None:
        return []
    sql = 'select * from Sensor where room_id={};'.format(room_id)
    sensors = sensorDB(sql)
    return sensors
Exemplo n.º 4
0
def get_express_readings_for(room_name, convertTemp=False):
    response = None
    if room_name in ROOM_SENSOR_ID_LOOKUP:
        response = {}
        sensor_id_bundle = ROOM_SENSOR_ID_LOOKUP[room_name]
        if len(sensor_id_bundle) > 2:
            # do averages too
            t = []
            h = []
            for entry in sensor_id_bundle:
                title = next(iter(entry))
                sensor_id = entry[title]
                sql = "select reading from LastSensorReading where sensorId={}".format(
                    sensor_id)
                reading = sensorDB(sql)[0]['reading']
                if title == 'temperature':
                    if convertTemp:
                        reading = (float(reading) - 32.0) * 5.0 / 9.0
                    t.append(reading)
                if title == 'humidity':
                    h.append(reading)
            t_val = sum(t) / len(t) if len(t) > 1 else t[0]
            h_val = sum(h) / len(h) if len(h) > 1 else h[0]
            response['temperature'] = float("{:.2f}".format(t_val))
            response['humidity'] = int(h_val)
        else:
            for entry in sensor_id_bundle:
                title = next(iter(entry))
                sensor_id = entry[title]
                sql = "select reading from LastSensorReading where sensorId={}".format(
                    sensor_id)
                reading = sensorDB(sql)[0]['reading']

                if title == 'temperature':
                    if convertTemp:
                        reading = (float(reading) - 32.0) * 5.0 / 9.0
                    reading = float("{:.2f}".format(reading))
                if title == 'humidity':
                    reading = int(reading)
                response[title] = reading

    return response
Exemplo n.º 5
0
def multi_sensor_insert(insertion_str):
    sql = 'insert into SensorReadings (`sensorId`,`reading`) values '
    sql += insertion_str
    sql += ';'
    result = sensorDB(sql)
    return True