-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.py
More file actions
126 lines (105 loc) · 4.25 KB
/
worker.py
File metadata and controls
126 lines (105 loc) · 4.25 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import time
import threading
import sqlite3
import serial
import requests
# environment variables can be set in .env file by using the following format:
# VARIABLE_NAME=VARIABLE_VALUE
# example:
# port=/tes/tport123
# baudrate=123456
# # read all environment variables from .env file
environment_variables = {}
with open('.env') as f:
for line in f:
if line[0] != '#':
key, value = line.split('=')
environment_variables[key] = value.strip()
class Reader:
port = None
baudrate = None
api_key = environment_variables['api_key']
location = environment_variables['location']
weather_api_url = f"https://weerlive.nl/api/weerlive_api_v2.php?key={api_key}&locatie={location}"
ser = None
value_store = {}
weather_data = {}
translation_table = {
'1-0:1.8.1': 'verbruik stand 1',
'1-0:1.8.2': 'verbruik stand 2',
'1-0:2.8.1': 'lever stand 1',
'1-0:2.8.2': 'lever stand 2',
'1-0:1.7.0': 'verbruik',
'1-0:2.7.0': 'leveren',
'0-1:24.2.1': 'aardgas'
}
def __init__(self, port, baudrate, api_key, location):
self.port = port
self.baudrate = baudrate
self.ser = serial.Serial(port, baudrate)
self.api_key = api_key
self.location = location
# function that continuously reads data from the serial port and keeps the connection open
def read_continuously(self):
while True:
try:
line = self.ser.readline().decode().strip()
except:
continue
# check if any of the keys in the translation table are in the line
if any(key in line for key in self.translation_table.keys()):
# check if the value is gas
if '0-1:24.2.1' in line:
value = line.split('(')[2].split('*')[0]
else:
# get the value from the line
value = line.split('(')[1].split('*')[0]
# translate the code to the name of the value
code = line.split('(')[0].strip()
self.value_store[self.translation_table[code]] = value
def get_weather_data(self):
res = requests.get(self.weather_api_url)
self.weather_data = res.json()['liveweer'][0]
class collector:
_instance = None
database = "data.db" # TODO: use the existing database instead of creating a new one
reader = Reader(environment_variables['port'], environment_variables['baudrate'], environment_variables['api_key'], environment_variables['location'])
def __init__(self):
# create the database if it doesn't exist
conn = sqlite3.connect(self.database)
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS Data (type INTEGER, time INTEGER, data INTEGER)")
c.execute("CREATE TABLE IF NOT EXISTS DataTypes (type INTEGER, name TEXT, unit TEXT)")
conn.commit()
conn.close()
def __new__(cls):
if cls._instance is None:
cls._instance = super(collector, cls).__new__(cls)
return cls._instance
def start(self):
# create a thread that will run in the background and collect data
thread = threading.Thread(target=self.collect)
thread.daemon = True
thread.start()
thread_weather = threading.Thread(target=self.update_weather_data)
thread_weather.daemon = True
thread_weather.start()
def collect(self):
# start reading data from the serial port
self.reader.read_continuously()
def update_weather_data(self):
while True:
self.reader.get_weather_data()
time.sleep(90)
def store_data(self):
pass
def fill_data_types(self):
conn = sqlite3.connect(self.database)
c = conn.cursor()
c.execute("INSERT INTO DataTypes VALUES (0, 'verbruik stand', 'kWh')")
c.execute("INSERT INTO DataTypes VALUES (1, 'lever stand', 'kWh')")
c.execute("INSERT INTO DataTypes VALUES (2, 'verbruik', 'kW')")
c.execute("INSERT INTO DataTypes VALUES (3, 'leveren', 'kW')")
c.execute("INSERT INTO DataTypes VALUES (4, 'aardgas', 'm3')")
conn.commit()
conn.close()