-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_service.py
75 lines (50 loc) · 1.56 KB
/
database_service.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import time
from dotenv import load_dotenv
from influxdb import InfluxDBClient
from influxdb import SeriesHelper
load_dotenv()
host = os.getenv("INFLUXDB_HOST")
port = 8086
user = os.getenv("INFLUXDB_USERNAME")
password = os.getenv("INFLUXDB_PASSWORD")
dbname = os.getenv("INFLUXDB_DB")
myclient = InfluxDBClient(host, port, user, password, dbname)
def connect_to_database():
connected = False
while not connected:
try:
myclient.ping()
connected = True
print('Connection to Influxdb successful!')
except Exception:
print('Connection to Influxdb failed. Sleeping 1 Second')
time.sleep(1)
class BaseMeta:
client = myclient
tags = []
# Defines the number of data points to store prior to writing
# on the wire.
bulk_size = 1
# autocommit must be set to True when using bulk_size
autocommit = True
class BME280Helper(SeriesHelper):
class Meta(BaseMeta):
series_name = 'bme280'
fields = ['temperature', 'humidity', 'pressure']
class LTR559Helper(SeriesHelper):
class Meta(BaseMeta):
series_name = 'ltr559'
fields = ['illuminance']
class S8Helper(SeriesHelper):
class Meta(BaseMeta):
series_name = 's8'
fields = ['co2']
class MICS6814Helper(SeriesHelper):
class Meta(BaseMeta):
series_name = 'mics6814'
fields = ['oxidising', 'reducing' ,'nh3']
class PMS7003Helper(SeriesHelper):
class Meta(BaseMeta):
series_name = 'pms7003'
fields = ['pm1_0', 'pm2_5', 'pm10']