1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-12-21 18:43:27 +01:00

Compare commits

...

4 Commits

Author SHA1 Message Date
482e6c8c60 feat(itho-wpu): getsettings support
A loop around getsetting based on the settings available in the
database to retrieve all settings from the WPU.

Example usage:
> ./itho-wpu.py --action getsettings
> 0. Niet Gebruikt: 0 (min: 0, max: 65535, step: 1)
> 1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1)
> 2. Jaar Inbedrijfstelling: 2010 (min: 2004, max: 2099, step: 1)
2023-07-11 16:07:35 +02:00
47ba1225c8 feat(itho-wpu): getsetting support
Action "getsetting" retrieves the value of a setting byi the provided id.

Example usage:
> ./itho-wpu.py --action getsetting --settingid 1
> 1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1)
> ./itho-wpu.py --action getsetting --settingid 2
> 2. Jaar Inbedrijfstelling: 2010 (min: 2004, max: 2099, step: 1)
2023-07-11 16:07:21 +02:00
da3b873174 refactor: put datatype formatting in a separate function 2023-07-10 20:47:21 +02:00
a1f8328b50 feat: log the hex request in debug mode 2023-06-05 21:33:45 +02:00
3 changed files with 216 additions and 32 deletions

View File

@ -99,6 +99,28 @@ See the [pislave](https://github.com/ootjersb/pislave#wiring) project
... ...
``` ```
* Retrieve a single setting from the WPU
```
# ./itho-wpu.py --action getsetting --settingid 1
1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1)
```
* Retrieve all settings from the WPU
```
# ./itho-wpu.py --action getsettings
0. Niet Gebruikt: 0 (min: 0, max: 65535, step: 1)
1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1)
2. Jaar Inbedrijfstelling: 2010 (min: 2004, max: 2099, step: 1)
3. Datum Van Inbedrijfstelling: 101 (min: 0, max: 3112, step: 1)
4. Max Handbedieningstijd (min): 0 (min: 0, max: 600, step: 1)
5. Vorsttemp (°C): 2.0 (min: -10.0, max: 10.0, step: 0.1)
6. Offset Voor Vorst Temp (K): 2.0 (min: 0.0, max: 10.0, step: 0.1)
7. Differentie Van Vorst Om Elektrisch Element Te Starten (K): 1.5 (min: 0.0, max: 10.0, step: 0.1)
8. Fout Reset Tijd (min): 120 (min: 1, max: 1440, step: 1)
9. Loginterval Tijd (sec): 5 (min: 1, max: 300, step: 1)
...
```
# Exporting measurements # Exporting measurements
## InfluxDB ## InfluxDB

View File

@ -23,6 +23,7 @@ actions = {
"getserial": [0x90, 0xE1], "getserial": [0x90, 0xE1],
"getdatatype": [0xA4, 0x00], "getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01], "getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10],
} }
@ -33,9 +34,15 @@ def parse_args():
"--action", "--action",
nargs="?", nargs="?",
required=True, required=True,
choices=actions.keys(), choices=list(actions.keys()) + ["getsettings"],
help="Execute an action", help="Execute an action",
) )
parser.add_argument(
"--settingid",
nargs="?",
type=int,
help="Setting identifier",
)
parser.add_argument( parser.add_argument(
"--loglevel", "--loglevel",
nargs="?", nargs="?",
@ -75,7 +82,7 @@ class IthoWPU:
self.datatype = self.get("getdatatype") self.datatype = self.get("getdatatype")
self.heatpump_db = db.sqlite("heatpump.sqlite") self.heatpump_db = db.sqlite("heatpump.sqlite")
def get(self, action): def get(self, action, identifier=None):
if not self.no_cache: if not self.no_cache:
response = self.cache.get(action.replace("get", "")) response = self.cache.get(action.replace("get", ""))
if response is not None: if response is not None:
@ -93,7 +100,7 @@ class IthoWPU:
if not self.slave_only: if not self.slave_only:
master = I2CMaster(address=0x41, bus=1, queue=self._q) master = I2CMaster(address=0x41, bus=1, queue=self._q)
if action: if action:
response = master.execute_action(action) response = master.execute_action(action, identifier)
logger.debug(f"Response: {response}") logger.debug(f"Response: {response}")
master.close() master.close()
@ -150,6 +157,36 @@ class IthoWPU:
return datalog return datalog
return datalog return datalog
def get_settings(self):
listversion = self.get_listversion_from_nodeid()
parameterlist_version = self.heatpump_db.execute(
f"SELECT parameterlist FROM versiebeheer WHERE version = {listversion}"
)[0]["parameterlist"]
if parameterlist_version is None or not type(parameterlist_version) == int:
logger.error(f"Parameterlist not found in database for version {listversion}")
return None
settings = self.heatpump_db.execute(
"SELECT id, name, min, max, def, title, description, unit "
+ f"FROM parameterlijst_v{parameterlist_version}"
)
return settings
def get_setting_by_id(self, settingid):
listversion = self.get_listversion_from_nodeid()
parameterlist_version = self.heatpump_db.execute(
f"SELECT parameterlist FROM versiebeheer WHERE version = {listversion}"
)[0]["parameterlist"]
if parameterlist_version is None or not type(parameterlist_version) == int:
logger.error(f"Parameterlist not found in database for version {listversion}")
return None
setting_details = self.heatpump_db.execute(
"SELECT name, min, max, def, title, description, unit "
+ f"FROM parameterlijst_v{parameterlist_version} WHERE id = {settingid}"
)
if len(setting_details) != 1:
return None
return setting_details[0]
class IthoWPUCache: class IthoWPUCache:
def __init__(self): def __init__(self):
@ -219,6 +256,8 @@ def process_response(action, response, args, wpu):
from itho_export import export_to_influxdb from itho_export import export_to_influxdb
export_to_influxdb(action, measurements) export_to_influxdb(action, measurements)
elif action == "getsetting":
process_setting(response, wpu)
elif action == "getnodeid": elif action == "getnodeid":
process_nodeid(response) process_nodeid(response)
elif action == "getserial": elif action == "getserial":
@ -259,36 +298,114 @@ def process_datalog(response, wpu):
measurements = {} measurements = {}
for d in datalog: for d in datalog:
if d.type == 0x0 or d.type == 0xC: if d.type == 0x0 or d.type == 0xC:
m = message[d.index : d.index + 1] # noqa: E203 length = 1
num = int(m[0], 0) elif d.type == 0x10 or d.type == 0x12 or d.type == 0x90 or d.type == 0x92:
elif d.type == 0x10: length = 2
m = message[d.index : d.index + 2] # noqa: E203
num = (int(m[0], 0) << 8) + int(m[1], 0)
elif d.type == 0x12:
m = message[d.index : d.index + 2] # noqa: E203
num = round((int(m[0], 0) << 8) + int(m[1], 0) / 100, 2)
elif d.type == 0x90:
m = message[d.index : d.index + 2] # noqa: E203
num = (int(m[0], 0) << 8) + int(m[1], 0)
if num >= 32768:
num -= 65536
elif d.type == 0x92:
m = message[d.index : d.index + 2] # noqa: E203
num = (int(m[0], 0) << 8) + int(m[1], 0)
if num >= 32768:
num -= 65536
num = round(num / 100, 2)
elif d.type == 0x20: elif d.type == 0x20:
m = message[d.index : d.index + 4] # noqa: E203 length = 4
num = (int(m[0], 0) << 24) + (int(m[1], 0) << 16) + (int(m[2], 0) << 8) + int(m[3], 0)
else: else:
logger.error(f"Unknown message type for datalog {d.name}: {d.type}") logger.error(f"Unknown message type for datalog {d.label}: {d.type}")
num = format_datatype(d.label, message[d.index : d.index + length], d.type) # noqa: E203
logger.info(f"{d.description}: {num}") logger.info(f"{d.description}: {num}")
measurements[d.label] = num measurements[d.label] = num
return measurements return measurements
if __name__ == "__main__": def process_setting(response, wpu):
message = response[5:]
settingid = int(message[17], 0)
setting = wpu.get_setting_by_id(settingid)
if setting is None:
logger.error(f"Setting '{settingid}' is invalid")
return
datatype = message[16]
value = format_datatype(setting["name"], message[0:4], datatype)
minimum = format_datatype(setting["name"], message[4:8], datatype)
maximum = format_datatype(setting["name"], message[8:12], datatype)
step = format_datatype(setting["name"], message[12:16], datatype)
logger.info(
"{}. {}{}: {} (min: {}, max: {}, step: {})".format(
settingid,
setting["title"].title(),
f' ({setting["unit"]})' if setting["unit"] is not None else "",
value,
minimum,
maximum,
step,
)
)
def process_settings(wpu, args):
settings = wpu.get_settings()
for setting in settings:
response = wpu.get("getsetting", int(setting["id"]))
if response is not None:
process_response("getsetting", response, args, wpu)
def format_datatype(name, m, dt):
num = None
if type(dt) is str:
dt = int(dt, 0)
if dt == 0x0 or dt == 0xC:
num = int(m[-1], 0)
elif dt == 0x1:
num = round(int(m[-1], 0) / 10, 1)
elif dt == 0x2:
num = round(int(m[-1], 0) / 100, 2)
elif dt == 0x10:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
elif dt == 0x12:
num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 100, 2)
elif dt == 0x13:
num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 1000, 3)
elif dt == 0x14:
num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 10000, 4)
elif dt == 0x80:
num = int(m[-1], 0)
if num >= 128:
num -= 256
elif dt == 0x81:
num = int(m[-1], 0)
if num >= 128:
num -= 256
num = round(num / 10, 1)
elif dt == 0x82:
num = int(m[-1], 0)
if num >= 128:
num -= 256
num = round(num / 100, 2)
elif dt == 0x8F:
num = int(m[-1], 0)
if num >= 128:
num -= 256
num = round(num / 1000, 3)
elif dt == 0x90:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768:
num -= 65536
elif dt == 0x91:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768:
num -= 65536
num = round(num / 10, 2)
elif dt == 0x92:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768:
num -= 65536
num = round(num / 100, 2)
elif dt == 0x20:
num = (int(m[-4], 0) << 24) + (int(m[-3], 0) << 16) + (int(m[-2], 0) << 8) + int(m[-1], 0)
else:
logger.error(f"Unknown datatype for '{name}': 0x{dt:X}")
return num
def main():
args = parse_args() args = parse_args()
if args.loglevel: if args.loglevel:
@ -299,7 +416,20 @@ if __name__ == "__main__":
logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s") logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s")
) )
if args.action == "getsetting" and args.settingid is None:
logger.error("`--settingid` is required with `--action getsetting`")
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)
response = wpu.get(args.action)
if args.action == "getsettings":
process_settings(wpu, args)
return
response = wpu.get(args.action, args.settingid)
if response is not None: if response is not None:
process_response(args.action, response, args, wpu) process_response(args.action, response, args, wpu)
if __name__ == "__main__":
main()

View File

@ -12,6 +12,7 @@ actions = {
"getserial": [0x90, 0xE1], "getserial": [0x90, 0xE1],
"getdatatype": [0xA4, 0x00], "getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01], "getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10],
} }
@ -45,9 +46,38 @@ class I2CMaster:
self.i = I2CRaw(address=address, bus=bus) self.i = I2CRaw(address=address, bus=bus)
self.queue = queue self.queue = queue
def compose_request(self, action): def compose_request(self, action, identifier):
# 0x80 = source, 0x04 = msg_type, 0x00 = length if action == "getsetting":
request = [0x80] + actions[action] + [0x04, 0x00] request = (
[0x80]
+ actions[action]
+ [
0x04,
0x13,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
identifier,
0x00,
]
)
else:
# 0x80 = source, 0x04 = msg_type, 0x00 = length
request = [0x80] + actions[action] + [0x04, 0x00]
request.append(self.calculate_checksum(request)) request.append(self.calculate_checksum(request))
return request return request
@ -60,8 +90,10 @@ class I2CMaster:
checksum = 0 checksum = 0
return checksum return checksum
def execute_action(self, action): def execute_action(self, action, identifier):
request = self.compose_request(action) request = self.compose_request(action, identifier)
request_in_hex = [hex(c) for c in request]
logger.debug(f"Request: {request_in_hex}")
result = None result = None
for i in range(0, 20): for i in range(0, 20):
logger.debug(f"Executing action: {action}") logger.debug(f"Executing action: {action}")