-
Notifications
You must be signed in to change notification settings - Fork 74
/
persistence.py
27 lines (21 loc) · 909 Bytes
/
persistence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from influxdb import InfluxDBClient
import datetime
import pytz
class DataPersistence(object):
def __init__(self, server, database, username="root", password="root"):
self.influxClient = InfluxDBClient(server, database=database, username=username, password=password)
def save_battery_level(self, battery_level_percent, device, **tags):
tags["device"] = device
fields = {
"battery_level_percent": battery_level_percent
}
self.save("device_battery", fields, tags)
def save(self, measurement, fields, tags):
d = datetime.datetime.now(pytz.utc)
d = datetime.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
self.influxClient.write_points([{
"measurement": measurement,
"tags": tags,
"fields": fields,
"time": datetime.datetime.utcnow().isoformat()
}])