-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheizung.py
executable file
·289 lines (233 loc) · 9.84 KB
/
heizung.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import json
import logging
import os
import platform
import socket
import sys
import tomllib
from logging import config
from time import gmtime
from time import sleep
from time import strftime
from time import time
import requests
from ta.fieldlists import get_messurements
raspberry = False
if 'raspberrypi' in platform.uname():
# global raspberry
raspberry = True
import RPi.GPIO as GPIO
# gpio 4 = BCM 23 = Pin 16
RelaisHeizung = 23
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(RelaisHeizung, GPIO.OUT)
GPIO.output(RelaisHeizung, GPIO.LOW)
# Set up a specific logger with our desired output level
_config_path = os.path.abspath(os.path.dirname(sys.argv[0]))
_config_file = _config_path + "/etc/heizung.conf"
_config_logger = _config_path + '/etc/logging.conf'
print("config heizung: ", _config_file)
print("config logger : ", _config_logger)
with open(_config_file, 'rb') as f:
config = tomllib.load(f)
url = config['heizung'].get('url')
api_url = config['heizung'].get('api_url')
url_internal = config['heizung'].get('url_internal')
blnet_host = config['heizung'].get('blnet_host')
operating_mode = config['heizung'].get('operating_mode')
ip = socket.gethostbyname(blnet_host)
log2log = config['heizung'].get('logger')
print("print2logger : ", log2log)
logging.config.fileConfig(_config_logger)
logger = logging.getLogger('heizung')
logger.propagate = False
def log_message(message):
if log2log == "True":
logger.info(message)
else:
print(message)
log_message("+----- S T A R T ----------------------------------")
log_message("| %r" % strftime("%Y-%m-%d %H:%M:%S", gmtime()))
log_message("+----------------------------------------------------")
log_message("| operation mode: %s" % operating_mode)
def get_time_difference_from_now(timestamp):
""" :return minutes from now """
time_diff = datetime.datetime.now() - timestamp
return int(time_diff.total_seconds() / 60)
class FiringControl(object):
def __init__(self):
self.firing_start = None
self.measurements = {}
@staticmethod
def start_firing():
"""
closes the relay which start the wood gasifier in firewood mode
closes the relay which start/keep burning the wood gasifier in pellets mode
:return:
"""
message = "START_KESSEL: "
if raspberry:
message += " set RelaisHeizung-GPIO to HIGH"
GPIO.output(RelaisHeizung, GPIO.HIGH)
else:
message += "test only (no raspberry)"
log_message(message)
@staticmethod
def stop_firing():
"""
stops burning in pellets mode, stop starting in firewood mode
:return:
"""
message = "STOP_KESSEL: "
if raspberry:
message += " set RelaisHeizung-GPIO to LOW"
GPIO.output(RelaisHeizung, GPIO.LOW)
else:
message += "test only (no raspberry)"
log_message(message)
def transfer_data(self, data):
"""
This method transfers the data from uvr1611 to the api of same project to hosting server
"""
request_url = api_url + "/databasewrapper/insertData"
result_insert = requests.post(request_url, json=[[data]])
result = requests.get(api_url + "/databasewrapper/updateTables")
log_message(f"transfer data uvr1611=>API: insertData: {result_insert.status_code} result: {result_insert.text} :: updateTables: {result.status_code}")
def get_current_measurements_from_blnet(self):
field_list, mapping, api_data = get_messurements(ip=ip, reset=False)
self.measurements[mapping['timestamp']] = {
"field_list": field_list,
"mapping": mapping,
"api_data": api_data
}
log_message(f"dh={mapping}")
log_message(f"fl={field_list}")
log_message(f"ad={api_data}")
if len(self.measurements) > 30:
oldest_measurement = min(self.measurements.keys())
del self.measurements[oldest_measurement]
return field_list, mapping, api_data
def check_measurements(self):
for attempt in range(10):
dt_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_message("=" * 99)
log_message(f"New test on measurements: {dt_now}")
try:
field_list, mapping, api_data = self.get_current_measurements_from_blnet()
break
except Exception as e:
# raise
log_message(f"#{attempt} Error while fetching data from BLNET: {e}")
if attempt == 9:
return "OFF"
sleep(30)
newest_date = max(self.measurements.keys())
return_do_firing = "OFF" # default
start_list = []
solar_list = []
try:
for messurement_date in self.measurements.keys():
heizungs_dict = self.measurements[messurement_date]['mapping']
minutes_ago_since_now = get_time_difference_from_now(heizungs_dict['timestamp'])
do_firing = "-"
# spread = heizungs_dict['heizung_vl'] - heizungs_dict['heizung_rl']
if heizungs_dict['speicher_3_kopf'] < 39 \
and heizungs_dict['speicher_4_mitte'] < 35 \
and heizungs_dict['speicher_5_boden'] < 32:
do_firing = "ON"
elif heizungs_dict['speicher_1_kopf'] < 45 \
and heizungs_dict['speicher_1_kopf'] < 45 \
and heizungs_dict['speicher_2_kopf'] < 45 \
and heizungs_dict['speicher_3_kopf'] < 45 \
and heizungs_dict['speicher_4_mitte'] < 45 \
and heizungs_dict['speicher_5_boden'] < 45:
do_firing = "ON"
# elif heizungs_dict['speicher_3_kopf'] < 35 \
# and heizungs_dict['speicher_4_mitte'] < 30 \
# and heizungs_dict['speicher_5_boden'] < 30 \
# and spread <= 2:
# # if heizungs_dict['heizung_d'] == 0:
# #if minutes_ago_since_now < 15: # only if measurements are not so long ago
# do_firing = "ON"
# this is enough energy!
if heizungs_dict['speicher_5_boden'] > 72:
do_firing = "OFF"
if minutes_ago_since_now <= 30:
start_list.append(do_firing)
solar_list.append(heizungs_dict['solar_strahlung'])
# log_message("%r, %r, %.1f, %r, %r, data=%r" % (
# heizungs_dict['timestamp']
# # , heizungs_dict['heizung_d'] # not available
# , heizungs_dict['d_heizung_pumpe']
# , spread
# , do_firing
# , minutes_ago_since_now
# , data
# ))
except IndexError:
log_message("ERROR: there is nothing to examine???")
# check if wood gasifier start is necessary:
api_data['digital1'] = 0
if "OFF" in start_list or not start_list:
return_do_firing = "OFF"
elif "ON" in start_list:
return_do_firing = "ON"
api_data['digital1'] = 1
else:
return_do_firing = "-"
self.transfer_data(api_data)
# for a very sunny day exeception should be made here:
# be optimistic that enough hot water will be produced
# if the mean of the solar radiation values is big enough, shut off firing
mean_solar = 0
mean_solar = sum(solar_list) / len(solar_list)
if mean_solar > 400:
return_do_firing = "OFF"
log_message(json.dumps({"t": dt_now, "mean solar": mean_solar, "solar_list_30m": solar_list}))
log_message(json.dumps({"t": dt_now, "firing_decision": return_do_firing, "start_list_30m": start_list,
"fire_since": self.firing_start}))
return return_do_firing
def run(self):
# blnet = getMeasurementsFromUVR1611(blnet_host, timeout=(3.05, 5), password=None)
# this is only for operating_mode firewood!
if len(sys.argv) > 1 and sys.argv[1] == 'ON':
log_message("Start burn-off per comandline...")
self.start_firing()
log_message("manually start done....")
sleep(5)
# better wait some time?
self.stop_firing()
else:
while True:
start = time()
result = self.check_measurements()
if operating_mode == 'firewood':
if result == "ON":
self.start_firing()
else:
self.stop_firing()
elif operating_mode == 'pellets':
if result == "ON":
if self.firing_start is None:
self.firing_start = time()
self.start_firing()
elif result == 'OFF':
self.stop_firing()
if self.firing_start:
message = "combustion time: %r hours" % (round((time() - self.firing_start) / 3600, 1))
log_message(message)
self.firing_start = None
else: # result == '--'
pass
end = time()
seconds_processing = end - start
to_sleep = 60 - seconds_processing
if to_sleep > 0:
sleep(to_sleep) # sleeping time in seconds
if __name__ == '__main__':
h = FiringControl()
h.run()