This commit is contained in:
tkl 2021-11-07 09:35:10 +01:00
parent 387a447b99
commit d1f6944216
2 changed files with 24 additions and 13 deletions

View File

@ -12,6 +12,11 @@ import paho.mqtt.client as mqtt
from climate_control.sensors import Dht22 from climate_control.sensors import Dht22
from climate_control.heat import Heat from climate_control.heat import Heat
LOG_FILE = "/var/log/climate.log"
LOG_FORMAT = "%(asctime)s %(filename)s:%(lineno)d %(message)s"
LOG_LEVEL = logging.INFO
LOG_INTERVAL = 5
MQTT_BROKER = "mqtt.blackfinn.de" MQTT_BROKER = "mqtt.blackfinn.de"
MQTT_PORT = 8883 MQTT_PORT = 8883
MQTT_CERT = "/etc/ssl/certs/DST_Root_CA_X3.pem" MQTT_CERT = "/etc/ssl/certs/DST_Root_CA_X3.pem"
@ -26,6 +31,8 @@ ROOM_HEAT_THRESHOLD = {"on": 1.0, "off": 2.0}
STATE_OFF = "off" STATE_OFF = "off"
STATE_ON = "ON" STATE_ON = "ON"
#logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE)
class ClimateControl(threading.Thread): class ClimateControl(threading.Thread):
__water_heat = Heat(20) __water_heat = Heat(20)
__room_heat = Heat(21) __room_heat = Heat(21)
@ -69,6 +76,7 @@ class ClimateControl(threading.Thread):
self.__now = time.time() self.__now = time.time()
if self.__next <= self.__now: if self.__next <= self.__now:
humidity, temperature = self.__climate.read() humidity, temperature = self.__climate.read()
logging.info("Temperature: {}\tHumidity: {}".format(temperature, humidity))
try: try:
client = mqtt.Client() client = mqtt.Client()
client.tls_set(MQTT_CERT) client.tls_set(MQTT_CERT)
@ -86,8 +94,8 @@ class ClimateControl(threading.Thread):
client.publish(MQTT_ROOM_HEAT_TOPIC, msg, qos=2, retain=True) client.publish(MQTT_ROOM_HEAT_TOPIC, msg, qos=2, retain=True)
client.loop_stop() client.loop_stop()
client.disconnect() client.disconnect()
except(ValueError, TypeError, socket.error, ssl.CertificateError): except(ValueError, TypeError, socket.error, ssl.CertificateError) as error:
logging.info('unable to publish to mqtt') logging.info('unable to publish to mqtt ({})'.format(error))
self.__next = self.__now + self.__interval self.__next = self.__now + self.__interval
time.sleep(1) time.sleep(1)

View File

@ -85,7 +85,9 @@ class FakeLight(object):
sun = self.city.sun(now, local=True) sun = self.city.sun(now, local=True)
sunset = sun['sunset'] sunset = sun['sunset']
if now.hour >= self.opening_hour and now < sunset: close_time = now - datetime.timedelta(minutes=30)
if now.hour >= self.opening_hour and close_time < sunset:
fake_light = 1 fake_light = 1
return fake_light return fake_light
@ -129,8 +131,8 @@ class Gate(object):
self.__light_read_timeout = current_time + LIGHT_READ_DELAY_S self.__light_read_timeout = current_time + LIGHT_READ_DELAY_S
light_read = self.__light_sensor.read() light_read = self.__light_sensor.read()
self.__light_data.push(light_read) self.__light_data.push(light_read)
logging.info('light - abs: ' + str(light_read) + ', avg: ' + \ # logging.info('light - abs: ' + str(light_read) + ', avg: ' + \
str(self.__light_data.average())) # str(self.__light_data.average()))
power_read = self.__power_sensor.power_mw() power_read = self.__power_sensor.power_mw()
self.__power_data.push(power_read) self.__power_data.push(power_read)
self.__state_handler[self.__next_state](self.__light_data.average()) self.__state_handler[self.__next_state](self.__light_data.average())
@ -235,8 +237,8 @@ class Gate(object):
self.__client.loop_start() self.__client.loop_start()
self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True) self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True)
self.__client.loop_stop() self.__client.loop_stop()
except (ValueError, TypeError, socket.error, ssl.CertificateError): except (ValueError, TypeError, socket.error, ssl.CertificateError) as err:
logging.info('unable to publish to mqtt') logging.info('unable to publish to mqtt ({})'.format(err))
if check_to_close(light_avg) is True: if check_to_close(light_avg) is True:
next_state = STATE_CLOSING_1 next_state = STATE_CLOSING_1
self.__update_state(next_state) self.__update_state(next_state)
@ -251,8 +253,8 @@ class Gate(object):
self.__client.loop_start() self.__client.loop_start()
self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True) self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True)
self.__client.loop_stop() self.__client.loop_stop()
except (ValueError, TypeError, socket.error, ssl.CertificateError): except (ValueError, TypeError, socket.error, ssl.CertificateError) as err:
logging.info('unable to publish to mqtt') logging.info('unable to publish to mqtt ({})'.format(err))
if check_to_open(light_avg) is True: if check_to_open(light_avg) is True:
next_state = STATE_OPENING_1 next_state = STATE_OPENING_1
self.__update_state(next_state) self.__update_state(next_state)
@ -267,8 +269,8 @@ class Gate(object):
self.__client.loop_start() self.__client.loop_start()
self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True) self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True)
self.__client.loop_stop() self.__client.loop_stop()
except (ValueError, TypeError, socket.error, ssl.CertificateError): except (ValueError, TypeError, socket.error, ssl.CertificateError) as err:
logging.info('unable to publish to mqtt') logging.info('unable to publish to mqtt ({})'.format(err))
self.__gate_run_time = time.time() self.__gate_run_time = time.time()
# workaround for high power after starting engine # workaround for high power after starting engine
@ -316,8 +318,8 @@ class Gate(object):
self.__client.loop_start() self.__client.loop_start()
self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True) self.__client.publish(MQTT_TOPIC, msg, qos=2, retain=True)
self.__client.loop_stop() self.__client.loop_stop()
except (ValueError, TypeError, socket.error, ssl.CertificateError): except (ValueError, TypeError, socket.error, ssl.CertificateError) as err:
logging.info('unable to publish to mqtt') logging.info('unable to publish to mqtt ({})'.format(err))
self.__gate_run_time = time.time() self.__gate_run_time = time.time()
# workaround for high power after starting engine # workaround for high power after starting engine
@ -360,3 +362,4 @@ class Gate(object):
self.__engine_2.stop() self.__engine_2.stop()
next_state = STATE_INIT_1 next_state = STATE_INIT_1
self.__update_state(next_state) self.__update_state(next_state)