diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0103a8e..b615718 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: rev: v4.4.0 hooks: - id: trailing-whitespace - #- id: end-of-file-fixer + - id: end-of-file-fixer - id: fix-byte-order-marker - id: fix-encoding-pragma - id: check-executables-have-shebangs @@ -41,9 +41,6 @@ repos: args: - "--install-types" - "--non-interactive" - - "--check-untyped-defs" - additional_dependencies: - - typing_extensions==4.8.0 - repo: https://github.com/psf/black rev: 23.3.0 diff --git a/.vscode/settings.json b/.vscode/settings.json index 727a3b0..2148ad5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,11 +4,5 @@ "--disable=missing-function-docstring,missing-class-docstring,missing-module-docstring" ], "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true, - "mypy-type-checker.importStrategy": "fromEnvironment", - "mypy-type-checker.reportingScope": "workspace", - "mypy-type-checker.preferDaemon": true, - "mypy-type-checker.args": [ - "--check-untyped-defs" - ] + "python.testing.pytestEnabled": true } \ No newline at end of file diff --git a/deploy.sh b/deploy.sh index 79979b7..dc92d5b 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,3 +1,2 @@ #!/bin/bash rsync --exclude=.git --exclude='packet format.java' --exclude-from=.gitignore -ravC . pi@solarpi:ble -ssh pi@solarpi 'systemctl --user daemon-reload; systemctl --user restart solarmppt' diff --git a/misc/draw_memory_map.py b/misc/draw_memory_map.py index 5aedbbd..37a3513 100644 --- a/misc/draw_memory_map.py +++ b/misc/draw_memory_map.py @@ -99,20 +99,20 @@ def parse_log(fh, chunksize=32): yield None -if __name__ == "__main__": - with open("z_solar copy.log") as fh: - data = list(parse_log(fh)) - # print(data) +with open("z_solar copy.log") as fh: + data = list(parse_log(fh)) + # print(data) - # data = list(range(256)) +# data = list(range(256)) - print( - memory_table( - data, - wordsize=2, - skip_nullrows=True, - ) + +print( + memory_table( + data, + wordsize=2, + skip_nullrows=True, ) +) # diff --git a/misc/dump_memory_map.py b/misc/dump_memory_map.py deleted file mode 100644 index c69ef64..0000000 --- a/misc/dump_memory_map.py +++ /dev/null @@ -1,66 +0,0 @@ -# -*- coding: utf-8 -*- -import os -import sys -from typing import List - -sys.path.insert(1, os.path.dirname(os.path.dirname(sys.argv[0]))) - -from draw_memory_map import memory_table # noqa: E402 - -from srnemqtt.config import get_config, get_interface # noqa: E402 -from srnemqtt.interfaces import BaseInterface # noqa: E402 -from srnemqtt.protocol import readMemory # noqa: E402 - - -def get_device_name(iface: BaseInterface) -> str | None: - data = readMemory(iface, 0x0C, 8) - if data is None: - return None - - return data.decode("utf-8").strip() - - -def get_device_version(iface: BaseInterface) -> str | None: - data = readMemory(iface, 0x14, 4) - if data is None: - return None - - major = (data[0] << 8) + data[1] - minor = data[2] - patch = data[3] - - return f"{major}.{minor}.{patch}" - - -def get_device_serial(iface: BaseInterface) -> str | None: - data = readMemory(iface, 0x18, 3) - if data is None: - return None - - p1 = data[0] - p2 = data[1] - p3 = (data[2] << 8) + data[3] - return f"{p1}-{p2}-{p3}" - - -if __name__ == "__main__": - conf = get_config() - iface = get_interface(conf) - - print(get_device_name(iface)) - print(get_device_version(iface)) - print(get_device_serial(iface)) - - data: List[int] = [] - for i in range(0, 0xFFFF, 16): - newdata = readMemory(iface, i, 16) - if newdata: - data.extend(newdata) - # !!! FIXME: Naively assumes all queries return the exact words requested - print( - memory_table( - data, - wordsize=2, - skip_nullrows=True, - ) - ) diff --git a/misc/memory_dump_MT2410.txt b/misc/memory_dump_MT2410.txt deleted file mode 100644 index a329446..0000000 --- a/misc/memory_dump_MT2410.txt +++ /dev/null @@ -1,153 +0,0 @@ -MT2410N10 -1.1.0 -13-19-740 -┌────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ -│ │ ···0│ ···1│ ···2│ ···3│ ···4│ ···5│ ···6│ ···7│ ···8│ ···9│ ···A│ ···B│ ···C│ ···D│ ···E│ ···F│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│000·│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│18 0A│0A 00│20 20│20 20│4D 54│32 34│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ M T│ 2 4│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│001·│31 30│4E 31│30 20│20 20│00 01│01 00│02 00│00 01│0D 13│02 E4│00 01│00 00│00 00│03 09│14 02│0A 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ 1 0│ N 1│ 0 │ │ │ │ │ │ │ ä│ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│002·│00 02│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│003·│00 00│00 00│00 00│00 00│00 00│00 31│00 32│00 33│00 34│00 35│00 36│00 37│00 38│00 39│00 3A│00 3B│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ │ │ │ │ │ 1│ 2│ 3│ 4│ 5│ 6│ 7│ 8│ 9│ :│ ;│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│004·│00 3C│00 3D│00 3E│00 3F│00 40│00 41│00 42│00 43│00 44│00 53│00 6F│00 6C│00 61│00 72│00 20│00 43│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ <│ =│ >│ ?│ @│ A│ B│ C│ D│ S│ o│ l│ a│ r│ │ C│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│005·│00 68│00 61│00 72│00 67│00 65│00 72│00 20│00 20│00 20│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ h│ a│ r│ g│ e│ r│ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│010·│00 64│00 85│00 00│15 19│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 7E│00 86│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ d│ │ │ │ │ │ │ │ │ │ │ ~│ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│011·│00 00│00 00│00 00│00 00│00 00│00 01│00 01│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│DF0·│00 01│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│DF2·│44 44│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ D D│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│DF4·│00 00│00 00│00 00│00 00│00 00│00 00│00 00│44 44│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ │ │ │ │ │ │ │ D D│ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│E00·│00 00│03 E8│00 C8│FF 0C│00 02│00 A0│00 9B│00 92│00 90│00 8A│00 84│00 7E│00 78│00 6F│00 6A│64 32│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ │ è│ È│ ÿ │ │ │ │ │ │ │ │ ~│ x│ o│ j│ d 2│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│E01·│00 05│00 78│00 78│00 1E│00 03│00 41│00 A3│00 4B│00 A3│00 00│00 00│00 00│00 00│00 0F│00 05│00 05│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ │ x│ x│ │ │ A│ £│ K│ £│ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│E02·│00 04│01 00│00 00│00 01│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│E30·│66 66│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ f f│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│E31·│00 00│00 00│00 00│00 64│00 32│00 64│00 32│00 3C│00 05│00 C8│00 02│02 BC│00 0A│03 84│03 84│02 58│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ │ │ │ d│ 2│ d│ 2│ <│ │ È│ │ ¼│ │ │ │ X│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│E32·│00 14│00 60│00 00│00 00│00 00│00 00│00 00│00 00│00 01│66 66│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ │ `│ │ │ │ │ │ │ │ f f│ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F00·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F0A·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F14·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F1E·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F28·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F32·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F3C·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F46·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F50·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F5A·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F64·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F6E·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F78·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F82·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F8C·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│F96·│00 7E│00 86│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│00 00│ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -│ │ ~│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ -├────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ -└────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ diff --git a/misc/test_serial.py b/misc/test_serial.py index c55be89..49c6f11 100644 --- a/misc/test_serial.py +++ b/misc/test_serial.py @@ -11,8 +11,7 @@ sys.path.insert(1, os.path.dirname(os.path.dirname(sys.argv[0]))) # from srnemqtt.lib.feasycom_ble import BTLEUart from srnemqtt.protocol import construct_request, write # noqa: E402 -# for rate in [1200, 2400, 4800, 9600, 115200]: -for rate in [9600]: +for rate in [1200, 2400, 4800, 9600, 115200]: print(rate) with Serial("/dev/ttyUSB0", baudrate=rate, timeout=2) as x: sleep(2) diff --git a/srnemqtt/config.py b/srnemqtt/config.py index fd5ec8c..4b3a4c1 100644 --- a/srnemqtt/config.py +++ b/srnemqtt/config.py @@ -6,8 +6,9 @@ from typing import Any, Dict, List, Optional, Type import yaml +from srnemqtt.interfaces import BaseInterface + from .consumers import BaseConsumer -from .interfaces import BaseInterface def get_consumer(name: str) -> Optional[Type[BaseConsumer]]: diff --git a/srnemqtt/constants.py b/srnemqtt/constants.py index a68bb5c..2a13eac 100644 --- a/srnemqtt/constants.py +++ b/srnemqtt/constants.py @@ -5,7 +5,7 @@ MAC = "DC:0D:30:9C:61:BA" # read_service = "0000fff0-0000-1000-8000-00805f9b34fb" ACTION_READ = 0x03 -ACTION_WRITE = 0x06 +ACTION_WRITE = 0x03 POSSIBLE_MARKER = (0x01, 0xFD, 0xFE, 0xFF) diff --git a/srnemqtt/consumers/mqtt.py b/srnemqtt/consumers/mqtt.py index 21a29a2..6cd7497 100644 --- a/srnemqtt/consumers/mqtt.py +++ b/srnemqtt/consumers/mqtt.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import json from time import sleep -from typing import Any, Dict, List, Optional, TypeAlias +from typing import Any, Dict, List, Optional from uuid import uuid4 import paho.mqtt.client as mqtt @@ -112,9 +112,6 @@ MAP_VALUES: Dict[DataName, Dict[str, Any]] = { } -PayloadType: TypeAlias = str | bytes | bytearray | int | float | None - - class MqttConsumer(BaseConsumer): client: mqtt.Client initialized: List[str] @@ -173,8 +170,8 @@ class MqttConsumer(BaseConsumer): def get_ha_config( self, - id: str, - name: str, + id, + name, unit: Optional[str] = None, type: Optional[str] = None, expiry: int = 90, @@ -250,7 +247,7 @@ class MqttConsumer(BaseConsumer): return super().poll() - def write(self, data: Dict[str, PayloadType]): + def write(self, data: Dict[str, Any]): self.client.publish(f"{self.topic_prefix}/raw", payload=json.dumps(data)) for k, v in data.items(): @@ -260,7 +257,6 @@ class MqttConsumer(BaseConsumer): pretty_name = k.replace("_", " ").capitalize() disc_prefix = self.settings["discovery_prefix"] device_id = self.settings["device_id"] - self.client.publish( f"{disc_prefix}/sensor/{device_id}_{k}/config", payload=json.dumps(self.get_ha_config(k, pretty_name, **km)), diff --git a/srnemqtt/interfaces/__init__.py b/srnemqtt/interfaces/__init__.py index 5b3bdbd..e8ecc37 100644 --- a/srnemqtt/interfaces/__init__.py +++ b/srnemqtt/interfaces/__init__.py @@ -4,4 +4,4 @@ from io import RawIOBase class BaseInterface(RawIOBase, metaclass=ABCMeta): - timeout: float | None + pass diff --git a/srnemqtt/lib/feasycom_ble.py b/srnemqtt/lib/feasycom_ble.py index 624093d..63317c2 100644 --- a/srnemqtt/lib/feasycom_ble.py +++ b/srnemqtt/lib/feasycom_ble.py @@ -6,6 +6,10 @@ from typing import TYPE_CHECKING, Optional, cast from bluepy import btle # type: ignore +if TYPE_CHECKING: + from _typeshed import ReadableBuffer, WriteableBuffer + + WRITE_DEVICE = "0000ffd1-0000-1000-8000-00805f9b34fb" READ_DEVICE = "0000fff1-0000-1000-8000-00805f9b34fb" @@ -14,7 +18,7 @@ class BTLEUart(io.RawIOBase): mac: str write_endpoint: str read_endpoint: str - timeout: float | None + timeout: float device: Optional[btle.Peripheral] = None _write_handle: Optional[btle.Characteristic] = None @@ -82,12 +86,13 @@ class BTLEUart(io.RawIOBase): self._write_handle = self.device.getCharacteristics(uuid=self.write_endpoint)[0] # print("Handles:", self._read_handle.handle, self._write_handle.handle) - def _read(self, num: Optional[int] = None): + def _read(self, num: Optional[int] = None, timeout: Optional[float] = None): self._ensure_connected() if TYPE_CHECKING: self.device = cast(btle.Peripheral, self.device) - timeout = self.timeout or 30 + if timeout is None: + timeout = self.timeout if num is None: start = time.time() @@ -127,9 +132,7 @@ class BTLEUart(io.RawIOBase): del self._read_buffer[:num] return data or None - def readinto(self, buffer: bytearray | memoryview) -> Optional[int]: # type: ignore [override] - # Buffer does not provide Sized, and bytes is read only. - # bytearray | memoryview is the default implementations that provide WriteableBuffer + def readinto(self, buffer: "WriteableBuffer") -> Optional[int]: data = self._read(len(buffer)) if data is None: @@ -141,15 +144,23 @@ class BTLEUart(io.RawIOBase): def readall(self) -> bytes: return self._read() - def read(self, size: Optional[int] = None) -> Optional[bytes]: + def read( + self, size: Optional[int] = None, timeout: Optional[float] = None + ) -> Optional[bytes]: + if timeout: + _timeout = self.timeout + self.timeout = timeout + if size is None: res = super().read() else: res = super().read(size) + if timeout: + self.timeout = _timeout return res - def write(self, b: bytes | bytearray | memoryview) -> Optional[int]: # type: ignore [override] + def write(self, b: "ReadableBuffer") -> Optional[int]: self._ensure_connected() if TYPE_CHECKING: self.device = cast(btle.Peripheral, self.device) @@ -163,9 +174,8 @@ class BTLEUart(io.RawIOBase): return self def __exit__(self, type, value, traceback): - if self.device is not None: - self.device.disconnect() - self.device = None + self.device.disconnect() + del self.device def seekable(self) -> bool: return False diff --git a/srnemqtt/protocol.py b/srnemqtt/protocol.py index f371db3..c8c2241 100644 --- a/srnemqtt/protocol.py +++ b/srnemqtt/protocol.py @@ -2,12 +2,13 @@ import struct import sys import time +from io import RawIOBase from typing import Callable, Collection, Optional from libscrc import modbus # type: ignore from .constants import ACTION_READ, POSSIBLE_MARKER -from .interfaces import BaseInterface +from .lib.feasycom_ble import BTLEUart from .solar_types import DATA_BATTERY_STATE, HISTORICAL_DATA, DataItem from .util import log @@ -60,23 +61,23 @@ def parse_packet(data): if crc != calculated_crc: e = ValueError(f"CRC missmatch: expected {crc:04X}, got {calculated_crc:04X}.") - # e.tag = tag - # e.operation = operation - # e.size = size - # e.payload = payload - # e.crc = crc - # e.calculated_crc = calculated_crc + e.tag = tag + e.operation = operation + e.size = size + e.payload = payload + e.crc = crc + e.calculated_crc = calculated_crc raise e return payload -def discardUntil(fh: BaseInterface, byte: int, timeout=10) -> Optional[int]: +def discardUntil(fh: RawIOBase, byte: int, timeout=10) -> Optional[int]: assert byte >= 0 and byte < 256, f"byte: Expected 8bit unsigned int, got {byte}" def expand(b: Optional[bytes]): - if not b: - return None + if b is None: + return b return b[0] start = time.time() @@ -87,7 +88,6 @@ def discardUntil(fh: BaseInterface, byte: int, timeout=10) -> Optional[int]: if not discarded: log("Discarding", end="") discarded += 1 - print(read_byte) print(f" {read_byte:02X}", end="") sys.stdout.flush() @@ -104,7 +104,7 @@ def discardUntil(fh: BaseInterface, byte: int, timeout=10) -> Optional[int]: return read_byte -def readMemory(fh: BaseInterface, address: int, words: int = 1) -> Optional[bytes]: +def readMemory(fh: RawIOBase, address: int, words: int = 1) -> Optional[bytes]: # log(f"Reading {words} words from 0x{address:04X}") request = construct_request(address, words=words) # log("Request:", request) @@ -135,7 +135,7 @@ def readMemory(fh: BaseInterface, address: int, words: int = 1) -> Optional[byte def try_read_parse( - dev: BaseInterface, + dev: BTLEUart, address: int, words: int = 1, parser: Optional[Callable] = None, @@ -151,10 +151,7 @@ def try_read_parse( except struct.error as e: log(e) log("0x0100 Unpack error:", len(res), res) - _timeout = dev.timeout - dev.timeout = 0.5 - log("Flushed from read buffer; ", dev.read()) - dev.timeout = _timeout + log("Flushed from read buffer; ", dev.read(timeout=0.5)) else: log(f"No data read, expected {words*2} bytes (attempts left: {attempts})") return None diff --git a/srnemqtt/solar_types.py b/srnemqtt/solar_types.py index ee926a0..8fdcb83 100644 --- a/srnemqtt/solar_types.py +++ b/srnemqtt/solar_types.py @@ -43,9 +43,6 @@ class DataName(str, Enum): def __repr__(self): return repr(self.value) - def __str__(self): - return repr(self) - class DataItem: name: DataName diff --git a/tox.ini b/tox.ini index c24b36c..8a5a3ef 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [flake8] -max-line-length = 120 +max-line-length = 88 extend-ignore = E203, I201, I101 [pytest]