1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-12-22 18:53:28 +01:00

Compare commits

..

3 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
3 changed files with 30 additions and 128 deletions

View File

@ -121,17 +121,6 @@ See the [pislave](https://github.com/ootjersb/pislave#wiring) project
...
```
* Change a setting of the WPU
```
# ./itho-wpu.py --action setsetting --settingid 139 --value 48
Current setting:
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
Updating setting 139 to `48`
Are you really sure? (Type uppercase yes): YES
139. Blokkade Tijd Van Verwarmen Naar Koelen (uur): 48 (min: 0, max: 168, step: 1)
```
# Exporting measurements
## InfluxDB

View File

@ -24,7 +24,6 @@ actions = {
"getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10],
"setsetting": [0xA4, 0x10],
}
@ -44,11 +43,6 @@ def parse_args():
type=int,
help="Setting identifier",
)
parser.add_argument(
"--value",
nargs="?",
help="Setting value",
)
parser.add_argument(
"--loglevel",
nargs="?",
@ -88,7 +82,7 @@ class IthoWPU:
self.datatype = self.get("getdatatype")
self.heatpump_db = db.sqlite("heatpump.sqlite")
def get(self, action, identifier=None, value=None):
def get(self, action, identifier=None):
if not self.no_cache:
response = self.cache.get(action.replace("get", ""))
if response is not None:
@ -106,7 +100,7 @@ class IthoWPU:
if not self.slave_only:
master = I2CMaster(address=0x41, bus=1, queue=self._q)
if action:
response = master.execute_action(action, identifier, value)
response = master.execute_action(action, identifier)
logger.debug(f"Response: {response}")
master.close()
@ -317,7 +311,7 @@ def process_datalog(response, wpu):
return measurements
def parse_setting(response, wpu):
def process_setting(response, wpu):
message = response[5:]
settingid = int(message[17], 0)
@ -331,21 +325,6 @@ def parse_setting(response, wpu):
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)
return value, minimum, maximum, step
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
value, minimum, maximum, step = parse_setting(response, wpu)
logger.info(
"{}. {}{}: {} (min: {}, max: {}, step: {})".format(
settingid,
@ -367,62 +346,7 @@ def process_settings(wpu, args):
process_response("getsetting", response, args, wpu)
def process_setsetting(wpu, args):
logger.info("Current setting:")
response = wpu.get("getsetting", int(args.settingid))
if response is None:
return
process_response("getsetting", response, args, wpu)
message = response[5:]
datatype = message[16]
if args.value is None:
value = input("Provide a new value: ")
else:
value = args.value
# TODO: add support for negative values
if float(value) < 0:
logger.error("Negative values are not supported yet.")
return
logger.debug(f"New setting datatype: {datatype}")
logger.debug(f"New setting (input): {value}")
normalized_value = int(value.replace(".", ""))
logger.debug(f"New setting (normalized): {normalized_value}")
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}")
parsed_value = format_datatype(args.settingid, hex_list_value, datatype)
logger.debug(f"New setting (parsed): {parsed_value}")
_, minimum, maximum, _ = parse_setting(response, wpu)
if parsed_value < minimum or parsed_value > maximum:
logger.error(f"New value `{parsed_value}` is not between `{minimum}` and `{maximum}`")
return
sure = input(f"Setting `{args.settingid}` will be changed to `{parsed_value}`? [y/N] ")
if sure in ["y", "Y"]:
logger.info(f"Updating setting {args.settingid} to `{parsed_value}`")
else:
logger.error("Aborted")
return
response = wpu.get("setsetting", args.settingid, normalized_value)
if response is None:
return
process_response("getsetting", response, args, wpu)
def format_datatype(name, m, dt):
"""
Transform a list of bytes to a readable number based on the datatype.
:param str name: Name/label of the data
:param list[str] m: List of bytes in hexadecimal string format
:param dt: Datatype
:type dt: str or int
"""
num = None
if type(dt) is str:
dt = int(dt, 0)
@ -486,14 +410,13 @@ def main():
if args.loglevel:
logger.setLevel(args.loglevel.upper())
logging.getLogger("itho_i2c").setLevel(args.loglevel.upper())
if args.timestamp:
stdout_log_handler.setFormatter(
logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s")
)
if args.action in ["getsetting", "setsetting"] and args.settingid is None:
if args.action == "getsetting" and args.settingid is None:
logger.error("`--settingid` is required with `--action getsetting`")
return
@ -503,10 +426,6 @@ def main():
process_settings(wpu, args)
return
if args.action == "setsetting":
process_setsetting(wpu, args)
return
response = wpu.get(args.action, args.settingid)
if response is not None:
process_response(args.action, response, args, wpu)

View File

@ -4,13 +4,8 @@ import logging
import pigpio
import struct
import time
import sys
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
stdout_log_handler = logging.StreamHandler(sys.stdout)
stdout_log_handler.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(stdout_log_handler)
actions = {
"getnodeid": [0x90, 0xE0],
@ -18,7 +13,6 @@ actions = {
"getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10],
"setsetting": [0xA4, 0x10],
}
@ -52,29 +46,34 @@ class I2CMaster:
self.i = I2CRaw(address=address, bus=bus)
self.queue = queue
def compose_request(self, action, identifier, value):
def compose_request(self, action, identifier):
if action == "getsetting":
request = (
[0x80]
+ actions[action]
+ [0x04, 0x13] # read, length
+ [0x00, 0x00, 0x00, 0x00] # current
+ [0x00, 0x00, 0x00, 0x00] # min
+ [0x00, 0x00, 0x00, 0x00] # max
+ [0x00, 0x00, 0x00, 0x00] # step
+ [0x00, identifier, 0x00]
)
elif action == "setsetting":
byte_list_value = list(value.to_bytes(4, byteorder="big"))
request = (
[0x80]
+ actions[action]
+ [0x06, 0x13] # write, length
+ byte_list_value # new
+ [0x00, 0x00, 0x00, 0x00] # min
+ [0x00, 0x00, 0x00, 0x00] # max
+ [0x00, 0x00, 0x00, 0x00] # step
+ [0x00, identifier, 0x00]
+ [
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
@ -91,16 +90,11 @@ class I2CMaster:
checksum = 0
return checksum
def execute_action(self, action, identifier, value):
request = self.compose_request(action, identifier, value)
def execute_action(self, action, identifier):
request = self.compose_request(action, identifier)
request_in_hex = [hex(c) for c in request]
logger.debug(f"Request: {request_in_hex}")
result = None
if action == "setsetting":
sure = input("Are you really sure? (Type uppercase yes): ")
if sure != "YES":
logger.error("Aborted")
return
for i in range(0, 20):
logger.debug(f"Executing action: {action}")
self.i.write_i2c_block_data(request)