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

View file

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

View file

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

View file

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