Files
SolarManager/phaoUtils/examples/client_rpc_math.py
T
adminandClaude Opus 5 79843aa2ae SolarManager unter Versionsverwaltung
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>
2026-09-02 20:46:59 +02:00

115 lines
3.0 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Frank Pagliughi <fpagliughi@mindspring.com>
# All rights reserved.
#
# This program and the accompanying materials are made available
# under the terms of the Eclipse Distribution License v1.0
# which accompanies this distribution.
#
# The Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Frank Pagliughi - initial implementation
#
# This shows an example of an MQTTv5 Remote Procedure Call (RPC) client.
# You should run the server_rpc_math.py before.
import json
import sys
import time
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.client as mqtt
from paho.mqtt.packettypes import PacketTypes
# These will be updated with the server-assigned Client ID
client_id = "mathcli"
reply_to = ""
# This correlates the outbound request with the returned reply
corr_id = b"1"
# This is sent in the message callback when we get the response
reply = None
# The MQTTv5 callback takes the additional 'props' parameter.
def on_connect(mqttc, userdata, flags, reason_code, props):
global client_id, reply_to
print(f"Connected: '{flags}', '{reason_code}', '{props}'")
if hasattr(props, 'AssignedClientIdentifier'):
client_id = props.AssignedClientIdentifier
reply_to = "replies/math/" + client_id
mqttc.subscribe(reply_to)
# An incoming message should be the reply to our request
def on_message(mqttc, userdata, msg):
global reply
print(msg.topic+" "+str(msg.payload)+" "+str(msg.properties))
props = msg.properties
if not hasattr(props, 'CorrelationData'):
print("No correlation ID")
# Match the response to the request correlation ID.
if props.CorrelationData == corr_id:
reply = msg.payload
if len(sys.argv) < 3:
print("USAGE: client_rpc_math.py [add|mult] n1 n2 ...")
sys.exit(1)
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="", protocol=mqtt.MQTTv5)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.connect(host="mqtt.eclipseprojects.io", clean_start=True)
mqttc.loop_start()
# Wait for connection to set `client_id`, etc.
while not mqttc.is_connected():
time.sleep(0.1)
# Properties for the request specify the ResponseTopic and CorrelationData
props = mqtt.Properties(PacketTypes.PUBLISH)
props.CorrelationData = corr_id
props.ResponseTopic = reply_to
# Uncomment to see what got set
#print("Client ID: "+client_id)
#print("Reply To: "+reply_to)
#print(props)
# The requested operation, 'add' or 'mult'
func = sys.argv[1]
# Gather the numeric parameters as an array of numbers
# These can be int's or float's
args = []
for s in sys.argv[2:]:
args.append(float(s))
# Send the request
topic = "requests/math/" + func
payload = json.dumps(args)
mqttc.publish(topic, payload, qos=1, properties=props)
# Wait for the reply
while reply is None:
time.sleep(0.1)
# Extract the response and print it.
rsp = json.loads(reply)
print("Response: "+str(rsp))
mqttc.loop_stop()