Add sensors
This commit is contained in:
parent
fcb860af29
commit
6e4b1b062b
68
main.py
68
main.py
|
@ -24,7 +24,8 @@ app = web.Application()
|
||||||
http_session = None
|
http_session = None
|
||||||
|
|
||||||
from influxdb import InfluxDBClient
|
from influxdb import InfluxDBClient
|
||||||
client = InfluxDBClient('localhost', 8086, database='sensors1' if PROD else 'sensors1dev')
|
sensors_client = InfluxDBClient('localhost', 8086, database='sensors1' if PROD else 'sensors1dev')
|
||||||
|
solar_client = InfluxDBClient('localhost', 8086, database='solar2')
|
||||||
PORT = 6903 if PROD else 6904
|
PORT = 6903 if PROD else 6904
|
||||||
|
|
||||||
class Sensors():
|
class Sensors():
|
||||||
|
@ -109,14 +110,14 @@ class Sensor():
|
||||||
'tags': {'id': self.id_, 'name': self.name},
|
'tags': {'id': self.id_, 'name': self.name},
|
||||||
'fields': data,
|
'fields': data,
|
||||||
}
|
}
|
||||||
client.write_points([point])
|
sensors_client.write_points([point])
|
||||||
|
|
||||||
logging.info('Wrote %s data to InfluxDB.', self)
|
logging.info('Wrote %s data to InfluxDB.', self)
|
||||||
|
|
||||||
def check_update(self):
|
def check_update(self):
|
||||||
if self.update_period:
|
if self.update_period:
|
||||||
if time.time() - self.last_update > self.update_period:
|
if time.time() - self.last_update > self.update_period:
|
||||||
logging.error('Missed expected update from %s.', self)
|
logging.warning('Missed expected update from %s.', self)
|
||||||
self.last_update = time.time()
|
self.last_update = time.time()
|
||||||
|
|
||||||
def update(self, data):
|
def update(self, data):
|
||||||
|
@ -182,6 +183,18 @@ class DustSensor(Sensor):
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class SleepSensor(Sensor):
|
||||||
|
type_ = 'sleep'
|
||||||
|
update_period = 90
|
||||||
|
|
||||||
|
def transform(self, data):
|
||||||
|
for key, value in data.items():
|
||||||
|
# what happens if you do this to a timestamp?
|
||||||
|
try:
|
||||||
|
data[key] = float(round(value, 1))
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
class Acurite606TX(Sensor):
|
class Acurite606TX(Sensor):
|
||||||
type_ = 'temperature'
|
type_ = 'temperature'
|
||||||
bad_keys = [
|
bad_keys = [
|
||||||
|
@ -224,7 +237,7 @@ async def process_mqtt(message):
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
return
|
return
|
||||||
|
|
||||||
if 'id' not in data:
|
if type(data) != dict or 'id' not in data:
|
||||||
return
|
return
|
||||||
|
|
||||||
await process_data(data)
|
await process_data(data)
|
||||||
|
@ -257,38 +270,62 @@ async def history(request):
|
||||||
|
|
||||||
end_unix = request.rel_url.query.get('end', None)
|
end_unix = request.rel_url.query.get('end', None)
|
||||||
if end_unix:
|
if end_unix:
|
||||||
|
end_unix = int(end_unix)
|
||||||
end = datetime.fromtimestamp(end_unix)
|
end = datetime.fromtimestamp(end_unix)
|
||||||
else:
|
else:
|
||||||
end = datetime.now(tz=pytz.UTC)
|
end = datetime.now(tz=pytz.UTC)
|
||||||
|
|
||||||
duration = request.rel_url.query.get('duration', 'today')
|
duration = request.rel_url.query.get('duration', 'day')
|
||||||
|
|
||||||
if duration == 'today':
|
if duration == 'today':
|
||||||
now_tz = datetime.now(tz=TIMEZONE)
|
now_tz = datetime.now(tz=TIMEZONE)
|
||||||
start = now_tz.replace(hour=0, minute=0, second=0, microsecond=0)
|
start = now_tz.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
window = '5m'
|
window = '10m'
|
||||||
elif duration == 'day':
|
elif duration == 'day':
|
||||||
start = end - timedelta(days=1)
|
start = end - timedelta(days=1)
|
||||||
window = '5m'
|
window = '10m'
|
||||||
elif duration == 'week':
|
elif duration == 'week':
|
||||||
start = end - timedelta(days=7)
|
start = end - timedelta(days=7)
|
||||||
window = '30m'
|
window = '1h'
|
||||||
elif duration == 'month':
|
elif duration == 'month':
|
||||||
start = end - timedelta(days=30)
|
start = end - timedelta(days=30)
|
||||||
window = '2h'
|
window = '1d'
|
||||||
elif duration == 'year':
|
elif duration == 'year':
|
||||||
start = end - timedelta(days=365)
|
start = end - timedelta(days=365)
|
||||||
window = '24h'
|
window = '1d'
|
||||||
|
|
||||||
|
window = request.rel_url.query.get('window', window)
|
||||||
|
|
||||||
|
if name == 'Water':
|
||||||
|
scale = 10
|
||||||
|
elif name == 'Gas':
|
||||||
|
scale = 0.001055*1000
|
||||||
|
else:
|
||||||
|
scale = 1
|
||||||
|
|
||||||
start = int(start.timestamp())
|
start = int(start.timestamp())
|
||||||
end = int(end.timestamp())
|
end = int(end.timestamp())
|
||||||
|
|
||||||
if measurement == 'temperature':
|
if measurement == 'temperature':
|
||||||
q = 'select mean("temperature_C") as temperature_C from temperature where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(none)'.format(name, start, end, window)
|
client = sensors_client
|
||||||
|
q = 'select mean("temperature_C") as temperature_C from temperature where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name, start, end, window)
|
||||||
elif measurement == 'ertscm':
|
elif measurement == 'ertscm':
|
||||||
q = 'select max("consumption_data") as consumption_data from ertscm where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(none)'.format(name, start, end, window)
|
client = sensors_client
|
||||||
|
q = 'select derivative(max("consumption_data"))*{} as delta, max("consumption_data")*{} as max from ertscm where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(previous)'.format(scale, scale, name, start, end, window)
|
||||||
|
elif measurement == 'thermostat':
|
||||||
|
client = sensors_client
|
||||||
|
q = 'select first("spacetemp") as spacetemp, first("heattemp") as heattemp, first("state") as state from thermostat where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(previous)'.format(name, start, end, window)
|
||||||
elif measurement == 'dust':
|
elif measurement == 'dust':
|
||||||
q = 'select max("max_p10") as max_p10, max("max_p25") as max_p25 from dust where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(none)'.format(name, start, end, window)
|
client = sensors_client
|
||||||
|
q = 'select max("avg_p10") as max_p10, max("avg_p25") as max_p25 from dust where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name, start, end, window)
|
||||||
|
elif measurement == 'sleep':
|
||||||
|
client = sensors_client
|
||||||
|
q = 'select max("max_mag") as max_mag from sleep where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name, start, end, window)
|
||||||
|
elif measurement == 'solar':
|
||||||
|
client = solar_client
|
||||||
|
q = 'select max("actual_total") as actual_total from ecu where time >= {}s and time < {}s group by time({}) fill(linear)'.format(start, end, window)
|
||||||
|
|
||||||
|
q += ' tz(\'America/Edmonton\')'
|
||||||
|
|
||||||
#if window and moving_average:
|
#if window and moving_average:
|
||||||
# q = 'select moving_average(mean("value"),{}) as value from {} where "name" = \'{}\' and time >= {}s and time < {}s group by time({}m) fill(none)'.format(moving_average, measurement, name, start, end, window)
|
# q = 'select moving_average(mean("value"),{}) as value from {} where "name" = \'{}\' and time >= {}s and time < {}s group by time({}m) fill(none)'.format(moving_average, measurement, name, start, end, window)
|
||||||
|
@ -316,8 +353,11 @@ if __name__ == '__main__':
|
||||||
sensors.add(ERTSCMSensor('31005493', 'Water'))
|
sensors.add(ERTSCMSensor('31005493', 'Water'))
|
||||||
sensors.add(ERTSCMSensor('41249312', 'Gas'))
|
sensors.add(ERTSCMSensor('41249312', 'Gas'))
|
||||||
sensors.add(OwnTracksSensor('owntracks1', 'OwnTracks'))
|
sensors.add(OwnTracksSensor('owntracks1', 'OwnTracks'))
|
||||||
sensors.add(DustSensor('dust1', 'Nook'))
|
sensors.add(DustSensor('dust1', 'Living Room'))
|
||||||
sensors.add(Acurite606TX('231', 'Outside'))
|
sensors.add(Acurite606TX('231', 'Outside'))
|
||||||
|
sensors.add(Acurite606TX('226', 'Bedroom'))
|
||||||
|
sensors.add(Acurite606TX('132', 'Nook'))
|
||||||
|
sensors.add(SleepSensor('sleep1', 'Bedroom'))
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(poll_sensors())
|
loop.create_task(poll_sensors())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user