Erster Stand der Hintergrundprozesse, die auf der Synology unter /volume1/homes/wagner/SolarManager laufen: der Manager selbst, die Sammler je Geraet, die MQTT-Bruecke, der Wecker und - neu hinzugezogen - der AutoAction-Runner, der als Hintergrundprozess hierher gehoert und nicht ins Web-Verzeichnis. Zugangsdaten stehen nicht mehr im Quelltext, sondern in config.ini, die nicht mit eingecheckt wird. Vorlage ist config.ini.example, gelesen wird sie von konfig.py. Betroffen waren solarManager.py (Datenbank und Wattpilot), zeit.py, gatherWaterData.py, wecker.py und skoda_testdaten.py, das sich das Passwort bisher aus dem Quelltext eines anderen Moduls herausgesucht hat. Die Kia-Anbindung ist mit dem Fahrzeug entfallen: kiaTest.py, gatherCarData.py und hyundai_kia_connect_api sind nicht mehr dabei, ebenso gatherInverterData.py, auf das nur noch eine auskommentierte Zeile zeigte. Die mitgelieferten Bibliotheken bleiben im Repository - die NAS hat kein pip, sie muessen neben den Skripten liegen. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import socket
|
|
import uuid
|
|
from select import select
|
|
from time import time
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
client_id = 'paho-mqtt-python/issue72/' + str(uuid.uuid4())
|
|
topic = client_id
|
|
print("Using client_id / topic: " + client_id)
|
|
|
|
|
|
class SelectMqttExample:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def on_connect(self, client, userdata, flags, reason_code, properties):
|
|
print("Subscribing")
|
|
client.subscribe(topic)
|
|
|
|
def on_message(self, client, userdata, msg):
|
|
if self.state not in {1, 3, 5}:
|
|
print("Got unexpected message: {}".format(msg.decode()))
|
|
return
|
|
|
|
print("Got message with len {}".format(len(msg.payload)))
|
|
self.state += 1
|
|
self.t = time()
|
|
|
|
def on_disconnect(self, client, userdata, flags, reason_code, properties):
|
|
self.disconnected = True, reason_code
|
|
|
|
def do_select(self):
|
|
sock = self.client.socket()
|
|
if not sock:
|
|
raise Exception("Socket is gone")
|
|
|
|
print("Selecting for reading" + (" and writing" if self.client.want_write() else ""))
|
|
r, w, e = select(
|
|
[sock],
|
|
[sock] if self.client.want_write() else [],
|
|
[],
|
|
1
|
|
)
|
|
|
|
if sock in r:
|
|
print("Socket is readable, calling loop_read")
|
|
self.client.loop_read()
|
|
|
|
if sock in w:
|
|
print("Socket is writable, calling loop_write")
|
|
self.client.loop_write()
|
|
|
|
self.client.loop_misc()
|
|
|
|
def main(self):
|
|
self.disconnected = (False, None)
|
|
self.t = time()
|
|
self.state = 0
|
|
|
|
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=client_id)
|
|
self.client.on_connect = self.on_connect
|
|
self.client.on_message = self.on_message
|
|
self.client.on_disconnect = self.on_disconnect
|
|
|
|
self.client.connect('mqtt.eclipseprojects.io', 1883, 60)
|
|
print("Socket opened")
|
|
self.client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)
|
|
|
|
while not self.disconnected[0]:
|
|
self.do_select()
|
|
|
|
if self.state in {0, 2, 4}:
|
|
if time() - self.t >= 5:
|
|
print("Publishing")
|
|
self.client.publish(topic, b'Hello' * 40000)
|
|
self.state += 1
|
|
|
|
if self.state == 6:
|
|
self.state += 1
|
|
self.client.disconnect()
|
|
|
|
print("Disconnected: {}".format(self.disconnected[1]))
|
|
|
|
|
|
print("Starting")
|
|
SelectMqttExample().main()
|
|
print("Finished")
|