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

Compare commits

..

5 Commits

Author SHA1 Message Date
381dd4c6ec feat(itho-wpu): setsetting support
With action "setsetting" settings of an Itho WPU can be modified.
2023-07-19 16:31:49 +02:00
1ffd5a72d7 fix(itho_i2c): make loglevel in itho_i2c module adjustable 2023-07-19 15:35:04 +02:00
7a1140e26c 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-19 15:35:04 +02:00
d28acae756 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-19 15:35:04 +02:00
620cdaf880 refactor: put datatype formatting in a separate function 2023-07-19 14:56:41 +02:00
3 changed files with 128 additions and 30 deletions

View File

@ -121,6 +121,17 @@ 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 # Exporting measurements
## InfluxDB ## InfluxDB

View File

@ -24,6 +24,7 @@ actions = {
"getdatatype": [0xA4, 0x00], "getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01], "getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10], "getsetting": [0xA4, 0x10],
"setsetting": [0xA4, 0x10],
} }
@ -43,6 +44,11 @@ def parse_args():
type=int, type=int,
help="Setting identifier", help="Setting identifier",
) )
parser.add_argument(
"--value",
nargs="?",
help="Setting value",
)
parser.add_argument( parser.add_argument(
"--loglevel", "--loglevel",
nargs="?", nargs="?",
@ -82,7 +88,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, identifier=None): def get(self, action, identifier=None, value=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:
@ -100,7 +106,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, identifier) response = master.execute_action(action, identifier, value)
logger.debug(f"Response: {response}") logger.debug(f"Response: {response}")
master.close() master.close()
@ -311,7 +317,7 @@ def process_datalog(response, wpu):
return measurements return measurements
def process_setting(response, wpu): def parse_setting(response, wpu):
message = response[5:] message = response[5:]
settingid = int(message[17], 0) settingid = int(message[17], 0)
@ -325,6 +331,21 @@ def process_setting(response, wpu):
minimum = format_datatype(setting["name"], message[4:8], datatype) minimum = format_datatype(setting["name"], message[4:8], datatype)
maximum = format_datatype(setting["name"], message[8:12], datatype) maximum = format_datatype(setting["name"], message[8:12], datatype)
step = format_datatype(setting["name"], message[12:16], 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( logger.info(
"{}. {}{}: {} (min: {}, max: {}, step: {})".format( "{}. {}{}: {} (min: {}, max: {}, step: {})".format(
settingid, settingid,
@ -346,7 +367,62 @@ def process_settings(wpu, args):
process_response("getsetting", response, args, wpu) 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): 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 num = None
if type(dt) is str: if type(dt) is str:
dt = int(dt, 0) dt = int(dt, 0)
@ -410,13 +486,14 @@ def main():
if args.loglevel: if args.loglevel:
logger.setLevel(args.loglevel.upper()) logger.setLevel(args.loglevel.upper())
logging.getLogger("itho_i2c").setLevel(args.loglevel.upper())
if args.timestamp: if args.timestamp:
stdout_log_handler.setFormatter( stdout_log_handler.setFormatter(
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: if args.action in ["getsetting", "setsetting"] and args.settingid is None:
logger.error("`--settingid` is required with `--action getsetting`") logger.error("`--settingid` is required with `--action getsetting`")
return return
@ -426,6 +503,10 @@ def main():
process_settings(wpu, args) process_settings(wpu, args)
return return
if args.action == "setsetting":
process_setsetting(wpu, args)
return
response = wpu.get(args.action, args.settingid) 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)

View File

@ -4,8 +4,13 @@ import logging
import pigpio import pigpio
import struct import struct
import time import time
import sys
logger = logging.getLogger(__name__) 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 = { actions = {
"getnodeid": [0x90, 0xE0], "getnodeid": [0x90, 0xE0],
@ -13,6 +18,7 @@ actions = {
"getdatatype": [0xA4, 0x00], "getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01], "getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10], "getsetting": [0xA4, 0x10],
"setsetting": [0xA4, 0x10],
} }
@ -46,34 +52,29 @@ 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, identifier): def compose_request(self, action, identifier, value):
if action == "getsetting": if action == "getsetting":
request = ( request = (
[0x80] [0x80]
+ actions[action] + actions[action]
+ [ + [0x04, 0x13] # read, length
0x04, + [0x00, 0x00, 0x00, 0x00] # current
0x13, + [0x00, 0x00, 0x00, 0x00] # min
0x00, + [0x00, 0x00, 0x00, 0x00] # max
0x00, + [0x00, 0x00, 0x00, 0x00] # step
0x00, + [0x00, identifier, 0x00]
0x00, )
0x00, elif action == "setsetting":
0x00, byte_list_value = list(value.to_bytes(4, byteorder="big"))
0x00, request = (
0x00, [0x80]
0x00, + actions[action]
0x00, + [0x06, 0x13] # write, length
0x00, + byte_list_value # new
0x00, + [0x00, 0x00, 0x00, 0x00] # min
0x00, + [0x00, 0x00, 0x00, 0x00] # max
0x00, + [0x00, 0x00, 0x00, 0x00] # step
0x00, + [0x00, identifier, 0x00]
0x00,
0x00,
identifier,
0x00,
]
) )
else: else:
# 0x80 = source, 0x04 = msg_type, 0x00 = length # 0x80 = source, 0x04 = msg_type, 0x00 = length
@ -90,11 +91,16 @@ class I2CMaster:
checksum = 0 checksum = 0
return checksum return checksum
def execute_action(self, action, identifier): def execute_action(self, action, identifier, value):
request = self.compose_request(action, identifier) request = self.compose_request(action, identifier, value)
request_in_hex = [hex(c) for c in request] request_in_hex = [hex(c) for c in request]
logger.debug(f"Request: {request_in_hex}") logger.debug(f"Request: {request_in_hex}")
result = None 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): for i in range(0, 20):
logger.debug(f"Executing action: {action}") logger.debug(f"Executing action: {action}")
self.i.write_i2c_block_data(request) self.i.write_i2c_block_data(request)