1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-11-21 13:52:15 +01:00

feat: add support for reading manual operations

This commit is contained in:
Pim van den Berg 2023-07-21 15:26:12 +02:00
parent 381dd4c6ec
commit 63f06400ef
5 changed files with 120 additions and 19 deletions

View File

@ -101,7 +101,7 @@ See the [pislave](https://github.com/ootjersb/pislave#wiring) project
* Retrieve a single setting from the WPU * Retrieve a single setting from the WPU
``` ```
# ./itho-wpu.py --action getsetting --settingid 1 # ./itho-wpu.py --action getsetting --id 1
1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1) 1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1)
``` ```
@ -123,7 +123,7 @@ See the [pislave](https://github.com/ootjersb/pislave#wiring) project
* Change a setting of the WPU * Change a setting of the WPU
``` ```
# ./itho-wpu.py --action setsetting --settingid 139 --value 48 # ./itho-wpu.py --action setsetting --id 139 --value 48
Current setting: Current setting:
139. Blokkade Tijd Van Verwarmen Naar Koelen (uur): 24 (min: 0, max: 168, step: 1) 139. Blokkade Tijd Van Verwarmen Naar Koelen (uur): 24 (min: 0, max: 168, step: 1)
Setting `139` will be changed to `48`? [y/N] y Setting `139` will be changed to `48`? [y/N] y
@ -132,6 +132,12 @@ See the [pislave](https://github.com/ootjersb/pislave#wiring) project
139. Blokkade Tijd Van Verwarmen Naar Koelen (uur): 48 (min: 0, max: 168, step: 1) 139. Blokkade Tijd Van Verwarmen Naar Koelen (uur): 48 (min: 0, max: 168, step: 1)
``` ```
* Retrieve a manual operation setting from the WPU
```
# ./itho-wpu.py --loglevel info --action getmanual --id 0
0. Buitentemp (°C): 10.0
```
# Exporting measurements # Exporting measurements
## InfluxDB ## InfluxDB

View File

@ -38,7 +38,7 @@ def convert(par_file, sqlite_db):
tables = [] tables = []
for table_info in par_cur.tables(tableType="TABLE"): for table_info in par_cur.tables(tableType="TABLE"):
if re.match("^(VersieBeheer|Data[Ll]abel|Parameterlijst)", table_info.table_name): if re.match("^(VersieBeheer|Data[Ll]abel|Parameterlijst|Handbed)", table_info.table_name):
tables.append(table_info.table_name) tables.append(table_info.table_name)
for t in sorted(tables): for t in sorted(tables):
@ -69,11 +69,31 @@ def convert(par_file, sqlite_db):
r.Eenheid_NL, r.Eenheid_NL,
) )
) )
if re.match("^VersieBeheer", t): if re.match("^Handbed", t):
par_cur.execute(f"select VersieNummer, DataLabel, ParameterLijst from {t}") par_cur.execute(
"select Index, Naam, Naam_fabriek, Min, Max, Default, "
f"Tekst_NL, Tooltip_NL, Eenheid_NL from {t}"
)
rows = par_cur.fetchall() rows = par_cur.fetchall()
for r in sorted(rows): for r in sorted(rows):
data.append((r.VersieNummer, r.DataLabel, r.ParameterLijst)) data.append(
(
r.Index,
r.Naam,
r.Naam_fabriek,
r.Min,
r.Max,
r.Default,
r.Tekst_NL,
r.Tooltip_NL,
r.Eenheid_NL,
)
)
if re.match("^VersieBeheer", t):
par_cur.execute(f"select VersieNummer, DataLabel, ParameterLijst, Handbed from {t}")
rows = par_cur.fetchall()
for r in sorted(rows):
data.append((r.VersieNummer, r.DataLabel, r.ParameterLijst, r.Handbed))
sqlite_db.insert(t.lower(), data) sqlite_db.insert(t.lower(), data)

29
db.py
View File

@ -44,6 +44,21 @@ class sqlite:
);""".format( );""".format(
t t
) )
elif t.startswith("handbed"):
query = """
CREATE TABLE {} (
id real,
name text,
name_factory text,
min real,
max real,
def real,
title text,
tooltip text,
unit text
);""".format(
t
)
elif t.startswith("parameterlijst"): elif t.startswith("parameterlijst"):
query = """ query = """
CREATE TABLE {} ( CREATE TABLE {} (
@ -64,7 +79,8 @@ class sqlite:
CREATE TABLE {} ( CREATE TABLE {} (
version integer primary key, version integer primary key,
datalabel integer, datalabel integer,
parameterlist integer parameterlist integer,
handbed integer
);""".format( );""".format(
t t
) )
@ -86,10 +102,17 @@ class sqlite:
""".format( """.format(
t t
) )
elif t.startswith("handbed"):
query = """
INSERT INTO {} (id, name, name_factory, min, max, def, title, tooltip, unit)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
""".format(
t
)
elif t.startswith("versiebeheer"): elif t.startswith("versiebeheer"):
query = """ query = """
INSERT INTO {} (version, datalabel, parameterlist) INSERT INTO {} (version, datalabel, parameterlist, handbed)
VALUES (?, ?, ?); VALUES (?, ?, ?, ?);
""".format( """.format(
t t
) )

View File

@ -25,6 +25,7 @@ actions = {
"getdatalog": [0xA4, 0x01], "getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10], "getsetting": [0xA4, 0x10],
"setsetting": [0xA4, 0x10], "setsetting": [0xA4, 0x10],
"getmanual": [0x40, 0x30],
} }
@ -39,10 +40,10 @@ def parse_args():
help="Execute an action", help="Execute an action",
) )
parser.add_argument( parser.add_argument(
"--settingid", "--id",
nargs="?", nargs="?",
type=int, type=int,
help="Setting identifier", help="Setting or manual identifier",
) )
parser.add_argument( parser.add_argument(
"--value", "--value",
@ -193,6 +194,22 @@ class IthoWPU:
return None return None
return setting_details[0] return setting_details[0]
def get_manual_by_id(self, manualid):
listversion = self.get_listversion_from_nodeid()
handbed_version = self.heatpump_db.execute(
f"SELECT handbed FROM versiebeheer WHERE version = {listversion}"
)[0]["handbed"]
if handbed_version is None or not type(handbed_version) == int:
logger.error(f"Handbed not found in database for version {listversion}")
return None
manual_details = self.heatpump_db.execute(
"SELECT name, min, max, def, title, tooltip, unit "
+ f"FROM handbed_v{handbed_version} WHERE id = {manualid}"
)
if len(manual_details) != 1:
return None
return manual_details[0]
class IthoWPUCache: class IthoWPUCache:
def __init__(self): def __init__(self):
@ -264,6 +281,8 @@ def process_response(action, response, args, wpu):
export_to_influxdb(action, measurements) export_to_influxdb(action, measurements)
elif action == "getsetting": elif action == "getsetting":
process_setting(response, wpu) process_setting(response, wpu)
elif action == "getmanual":
process_manual(response, wpu)
elif action == "getnodeid": elif action == "getnodeid":
process_nodeid(response) process_nodeid(response)
elif action == "getserial": elif action == "getserial":
@ -369,7 +388,7 @@ def process_settings(wpu, args):
def process_setsetting(wpu, args): def process_setsetting(wpu, args):
logger.info("Current setting:") logger.info("Current setting:")
response = wpu.get("getsetting", int(args.settingid)) response = wpu.get("getsetting", int(args.id))
if response is None: if response is None:
return return
process_response("getsetting", response, args, wpu) process_response("getsetting", response, args, wpu)
@ -392,7 +411,7 @@ def process_setsetting(wpu, args):
logger.debug(f"New setting (normalized): {normalized_value}") logger.debug(f"New setting (normalized): {normalized_value}")
hex_list_value = [hex(v) for v in list(normalized_value.to_bytes(4, byteorder="big"))] hex_list_value = [hex(v) for v in list(normalized_value.to_bytes(4, byteorder="big"))]
logger.debug(f"New setting (hex): {hex_list_value}") logger.debug(f"New setting (hex): {hex_list_value}")
parsed_value = format_datatype(args.settingid, hex_list_value, datatype) parsed_value = format_datatype(args.id, hex_list_value, datatype)
logger.debug(f"New setting (parsed): {parsed_value}") logger.debug(f"New setting (parsed): {parsed_value}")
_, minimum, maximum, _ = parse_setting(response, wpu) _, minimum, maximum, _ = parse_setting(response, wpu)
@ -400,19 +419,41 @@ def process_setsetting(wpu, args):
logger.error(f"New value `{parsed_value}` is not between `{minimum}` and `{maximum}`") logger.error(f"New value `{parsed_value}` is not between `{minimum}` and `{maximum}`")
return return
sure = input(f"Setting `{args.settingid}` will be changed to `{parsed_value}`? [y/N] ") sure = input(f"Setting `{args.id}` will be changed to `{parsed_value}`? [y/N] ")
if sure in ["y", "Y"]: if sure in ["y", "Y"]:
logger.info(f"Updating setting {args.settingid} to `{parsed_value}`") logger.info(f"Updating setting {args.id} to `{parsed_value}`")
else: else:
logger.error("Aborted") logger.error("Aborted")
return return
response = wpu.get("setsetting", args.settingid, normalized_value) response = wpu.get("setsetting", args.id, normalized_value)
if response is None: if response is None:
return return
process_response("getsetting", response, args, wpu) process_response("getsetting", response, args, wpu)
def process_manual(response, wpu):
message = response[5:]
manualid = int(message[2], 0)
manual = wpu.get_manual_by_id(manualid)
if manual is None:
logger.error(f"Manual '{manualid}' is invalid")
return
datatype = message[3]
value = format_datatype(manual["name"], message[4:6], datatype)
logger.info(
"{}. {}{}: {}".format(
manualid,
manual["title"].title(),
f' ({manual["unit"]})' if manual["unit"] is not None else "",
value,
)
)
def format_datatype(name, m, dt): def format_datatype(name, m, dt):
""" """
Transform a list of bytes to a readable number based on the datatype. Transform a list of bytes to a readable number based on the datatype.
@ -493,8 +534,8 @@ def main():
logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s") logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s")
) )
if args.action in ["getsetting", "setsetting"] and args.settingid is None: if args.action in ["getsetting", "setsetting", "getmanual"] and args.id is None:
logger.error("`--settingid` is required with `--action getsetting`") logger.error(f"`--id` is required with `--action {args.action}`")
return return
wpu = IthoWPU(args.master_only, args.slave_only, args.slave_timeout, args.no_cache) wpu = IthoWPU(args.master_only, args.slave_only, args.slave_timeout, args.no_cache)
@ -507,7 +548,7 @@ def main():
process_setsetting(wpu, args) process_setsetting(wpu, args)
return return
response = wpu.get(args.action, args.settingid) response = wpu.get(args.action, args.id)
if response is not None: if response is not None:
process_response(args.action, response, args, wpu) process_response(args.action, response, args, wpu)

View File

@ -19,6 +19,7 @@ actions = {
"getdatalog": [0xA4, 0x01], "getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10], "getsetting": [0xA4, 0x10],
"setsetting": [0xA4, 0x10], "setsetting": [0xA4, 0x10],
"getmanual": [0x40, 0x30],
} }
@ -76,6 +77,16 @@ class I2CMaster:
+ [0x00, 0x00, 0x00, 0x00] # step + [0x00, 0x00, 0x00, 0x00] # step
+ [0x00, identifier, 0x00] + [0x00, identifier, 0x00]
) )
elif action == "getmanual":
byte_identifier = list(identifier.to_bytes(2, byteorder="big"))
request = (
[0x80]
+ actions[action]
+ [0x04, 0x04] # read, length
+ [0x01] # bank
+ byte_identifier
+ [0x01] # 1 = manual
)
else: else:
# 0x80 = source, 0x04 = msg_type, 0x00 = length # 0x80 = source, 0x04 = msg_type, 0x00 = length
request = [0x80] + actions[action] + [0x04, 0x00] request = [0x80] + actions[action] + [0x04, 0x00]