Implement get_interface

This commit is contained in:
Odd Stråbø 2023-04-07 23:57:37 +02:00
parent 1ccea2bf9c
commit 80bfd414ec
5 changed files with 44 additions and 23 deletions

12
config-example.yaml Normal file
View file

@ -0,0 +1,12 @@
consumers:
stdio.StdoutConsumer: {}
interface:
name: serial.SerialInterface
params:
device: /dev/ttyUSB0
baudrate: 9600
timeout: 2
# name: feasycom.FeasycomInterface
# params:
# mac: DC:0D:30:9C:61:BA
# timeout: 5

View file

@ -8,15 +8,13 @@ from typing import cast
from bluepy.btle import BTLEDisconnectError from bluepy.btle import BTLEDisconnectError
from serial import SerialException from serial import SerialException
from .config import get_config, get_consumers from .config import get_config, get_consumers, get_interface
from .constants import MAC
from .lib.feasycom_ble import BTLEUart
from .protocol import parse_battery_state, parse_historical_entry, try_read_parse from .protocol import parse_battery_state, parse_historical_entry, try_read_parse
from .solar_types import DataName from .solar_types import DataName
from .util import Periodical, log from .util import Periodical, log
class CommunicationError(BTLEDisconnectError, SerialException): class CommunicationError(BTLEDisconnectError, SerialException, IOError):
pass pass
@ -34,7 +32,7 @@ def main():
while True: while True:
try: try:
log("Connecting...") log("Connecting...")
with BTLEUart(MAC, timeout=5) as dev: with get_interface() as dev:
log("Connected.") log("Connected.")
# write(dev, construct_request(0, 32)) # write(dev, construct_request(0, 32))

View file

@ -1,12 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import importlib import importlib
import os import os
from io import RawIOBase
from time import sleep from time import sleep
from typing import Any, Dict, List, Optional, Type from typing import Any, Dict, List, Optional, Type
import yaml import yaml
from srnemqtt.interfaces import BaseInterface
from .consumers import BaseConsumer from .consumers import BaseConsumer
@ -53,21 +54,28 @@ def get_consumers(conf: Optional[Dict[str, Any]] = None) -> List[BaseConsumer]:
return consumers return consumers
def get_producers(conf: Optional[Dict[str, Any]] = None) -> List[RawIOBase]: def _get_interface(name: str) -> Type[BaseInterface]:
raise NotImplementedError mod_name, cls_name = name.rsplit(".", 1)
mod = importlib.import_module(f".consumers.{mod_name}", package=__package__)
res = getattr(mod, cls_name)
assert issubclass(res, BaseConsumer)
return res
def get_interface(conf: Optional[Dict[str, Any]] = None) -> BaseInterface:
if conf is None: if conf is None:
conf = get_config() conf = get_config()
consumers = [] name = conf["interface"]["name"]
for name, consumer_config in conf["consumers"].items(): params = conf["interface"].get("params", {})
# print(name, consumer_config)
mod = get_consumer(name)
if mod:
# print(mod)
consumers.append(mod(consumer_config))
write_config(conf) mod = _get_interface(name)
return consumers assert mod
return mod(**params)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,7 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# from ..lib.feasycom_ble import BTLEUart from ..lib.feasycom_ble import BTLEUart
from . import BaseSource from . import BaseInterface
class FeasycomSource(BaseSource): class FeasycomInterface(BTLEUart, BaseInterface):
pass pass
# BTLEUart(mac=MAC, timeout=5)

View file

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# import serial import serial
from . import BaseSource from . import BaseInterface
class SerialSource(BaseSource): class SerialInterface(serial.Serial, BaseInterface):
pass pass