Compare commits

..

No commits in common. "80bfd414ec699ad22a8815c997f73f5585e01740" and "7ab0b1ce62821dce3b85218a77ef9d684969a4a0" have entirely different histories.

8 changed files with 33 additions and 54 deletions

View file

@ -1,12 +0,0 @@
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,13 +8,15 @@ 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, get_interface from .config import get_config, get_consumers
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, IOError): class CommunicationError(BTLEDisconnectError, SerialException):
pass pass
@ -32,7 +34,7 @@ def main():
while True: while True:
try: try:
log("Connecting...") log("Connecting...")
with get_interface() as dev: with BTLEUart(MAC, timeout=5) as dev:
log("Connected.") log("Connected.")
# write(dev, construct_request(0, 32)) # write(dev, construct_request(0, 32))

View file

@ -1,13 +1,12 @@
# -*- 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
@ -54,28 +53,21 @@ def get_consumers(conf: Optional[Dict[str, Any]] = None) -> List[BaseConsumer]:
return consumers return consumers
def _get_interface(name: str) -> Type[BaseInterface]: def get_producers(conf: Optional[Dict[str, Any]] = None) -> List[RawIOBase]:
mod_name, cls_name = name.rsplit(".", 1) raise NotImplementedError
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()
name = conf["interface"]["name"] consumers = []
params = conf["interface"].get("params", {}) 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))
mod = _get_interface(name) write_config(conf)
assert mod return consumers
return mod(**params)
if __name__ == "__main__": if __name__ == "__main__":

View file

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

View file

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

View file

@ -3,5 +3,5 @@ from abc import ABCMeta
from io import RawIOBase from io import RawIOBase
class BaseInterface(RawIOBase, metaclass=ABCMeta): class BaseSource(RawIOBase, metaclass=ABCMeta):
pass pass

View file

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# from ..lib.feasycom_ble import BTLEUart
from . import BaseSource
class FeasycomSource(BaseSource):
pass

View file

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