From 4bc5ebcfc985de725eb2c04be51740fdc11ffcd0 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 31 Dec 2024 13:06:28 +0100 Subject: [PATCH] Bocktherm: WIP --- bocktherm/{test.py => burrow-bocktherm.py} | 87 +++++++++++++++------- 1 file changed, 61 insertions(+), 26 deletions(-) rename bocktherm/{test.py => burrow-bocktherm.py} (51%) diff --git a/bocktherm/test.py b/bocktherm/burrow-bocktherm.py similarity index 51% rename from bocktherm/test.py rename to bocktherm/burrow-bocktherm.py index 9a1533a..3f23315 100755 --- a/bocktherm/test.py +++ b/bocktherm/burrow-bocktherm.py @@ -2,28 +2,35 @@ import argparse from datetime import datetime +import json import paho.mqtt.client as mqtt import random +import sys import time -connected = False +class Thermostat: + connected = False + ident = "" + in_topic = "" + out_topic = "" + # mqc = mqtt client -def on_connect(mqc, userdata, flags, rc): +def on_connect(mqc, therm, flags, rc): print(f'# Connect: rc={rc}') if rc == 0: - global connected - connected = True + therm.connected = True + mqc.subscribe(therm.in_topic, 0) + mqc.subscribe(therm.out_topic, 0) -def on_disconnect(mqc, userdata, rc): +def on_disconnect(mqc, therm, rc): print(f'# Disconnect: rc={rc}') - global connected - connected = False + therm.connected = False -def on_message(mqc, userdata, msg): +def on_message(mqc, therm, msg): t = datetime.now().isoformat() print(f'@ {t} {msg.topic}') print('\t' + msg.payload.hex(' ')) @@ -58,11 +65,11 @@ def on_message(mqc, userdata, msg): print('\t\t!!! Unknown Packet !!!') -def on_subscribe(mqc, userdata, mid, granted_qos): +def on_subscribe(mqc, therm, mid, granted_qos): print(f'# Subscribed: mid={mid}, granted={granted_qos}') -def on_log(mqc, userdata, level, string): +def on_log(mqc, therm, level, string): print(f'# Log[{level}]: {string}') @@ -70,30 +77,58 @@ def gen_client_id(): return 'APP_' + "".join(["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTUVWXYZ1234567890"[random.randrange(60)] for _ in range(18)]) -mqc = mqtt.Client(transport='websockets', client_id=gen_client_id()) -mqc.on_message = on_message -mqc.on_connect = on_connect -mqc.on_disconnect = on_disconnect -mqc.on_subscribe = on_subscribe -mqc.on_log = on_log +def read_device_config(config_file, device_name): + with open(config_file) as f: + js = json.load(f) + assert isinstance(js, list) + for cf in js: + assert isinstance(cf, dict) + if cf['name'] == device_name: + return cf + print('Device not found in configuration file', file=sys.stderr) + sys.exit(1) + + +def init_therm(config): + therm = Thermostat() + therm.ident = str(config['uniqueIdentifier']).rjust(6, '0') + therm.in_topic = f'TSWIFI/I/{therm.ident}' + therm.out_topic = f'TSWIFI/O/{therm.ident}' + + mqc = mqtt.Client(transport='websockets', client_id=gen_client_id(), userdata=therm) + mqc.on_message = on_message + mqc.on_connect = on_connect + mqc.on_disconnect = on_disconnect + mqc.on_subscribe = on_subscribe + mqc.on_log = on_log + + mqc.ws_set_options("/wss") + mqc.tls_set_context() + mqc.username_pw_set(f'TSWIFI_{therm.ident}', config['mqttPass']) + mqc.connect("mqtt.elbock.cz", 8081, 60) + + mqc.loop_start() + + therm.mqc = mqc + return therm + -mqc.ws_set_options("/wss") -mqc.tls_set_context() -mqc.username_pw_set('TSWIFI_024299', 'iqjYBfrZGe') -mqc.connect("mqtt.elbock.cz", 8081, 60) +parser = argparse.ArgumentParser(description='A daemon watching Elektrobock thermostats') +parser.add_argument('--config', required=True, type=str, help='Configuration file from the web app (devices.json)') +parser.add_argument('--device', required=True, type=str, help='Device name as in the configuration file') -# FIXME: Move to connect callback -mqc.subscribe("TSWIFI/I/024299", 0) -mqc.subscribe("TSWIFI/O/024299", 0) +args = parser.parse_args() -mqc.loop_start() +config = read_device_config(args.config, args.device) +therm = init_therm(config) +time.sleep(1) while True: - if connected: + if therm.connected: print('Brum: Connected') # Request message sent by the web interface, but with a random message ID request = b'\x10\x0a' + random.randbytes(2) + b'\x00\x24\x20\x01\x00\x00\x20\x02\x00\x00\x20\x03\x00\x00\x20\x06\x00\x00\x20\x07\x00\x00\x20\x05\x00\x00\x20\x09\x00\x00\x20\x0b\x00\x00\x20\x0c\x00\x00' - mqc.publish('TSWIFI/I/024299', request) + therm.mqc.publish('TSWIFI/I/024299', request) time.sleep(60) else: print('Brum: Not connected') -- 2.39.5