From da3b87317403cd947460b79430cc5c523c40ee38 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Mon, 10 Jul 2023 20:46:39 +0200 Subject: [PATCH] refactor: put datatype formatting in a separate function --- itho-wpu.py | 55 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/itho-wpu.py b/itho-wpu.py index 7c277ef..f857d77 100755 --- a/itho-wpu.py +++ b/itho-wpu.py @@ -259,35 +259,46 @@ def process_datalog(response, wpu): measurements = {} for d in datalog: if d.type == 0x0 or d.type == 0xC: - m = message[d.index : d.index + 1] # noqa: E203 - num = int(m[0], 0) - elif d.type == 0x10: - 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) + length = 1 + elif d.type == 0x10 or d.type == 0x12 or d.type == 0x90 or d.type == 0x92: + length = 2 elif d.type == 0x20: - m = message[d.index : d.index + 4] # noqa: E203 - num = (int(m[0], 0) << 24) + (int(m[1], 0) << 16) + (int(m[2], 0) << 8) + int(m[3], 0) + length = 4 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}") measurements[d.label] = num return measurements +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 == 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 == 0x90: + num = (int(m[-2], 0) << 8) + int(m[-1], 0) + if num >= 32768: + num -= 65536 + 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 + + if __name__ == "__main__": args = parse_args()