Compare commits
2 commits
7ab0b1ce62
...
80bfd414ec
Author | SHA1 | Date | |
---|---|---|---|
80bfd414ec | |||
1ccea2bf9c |
8 changed files with 54 additions and 33 deletions
12
config-example.yaml
Normal file
12
config-example.yaml
Normal 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
|
|
@ -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))
|
||||||
|
|
|
@ -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__":
|
||||||
|
|
|
@ -3,5 +3,5 @@ from abc import ABCMeta
|
||||||
from io import RawIOBase
|
from io import RawIOBase
|
||||||
|
|
||||||
|
|
||||||
class BaseSource(RawIOBase, metaclass=ABCMeta):
|
class BaseInterface(RawIOBase, metaclass=ABCMeta):
|
||||||
pass
|
pass
|
10
srnemqtt/interfaces/feasycom.py
Normal file
10
srnemqtt/interfaces/feasycom.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from ..lib.feasycom_ble import BTLEUart
|
||||||
|
from . import BaseInterface
|
||||||
|
|
||||||
|
|
||||||
|
class FeasycomInterface(BTLEUart, BaseInterface):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# BTLEUart(mac=MAC, timeout=5)
|
8
srnemqtt/interfaces/serial.py
Normal file
8
srnemqtt/interfaces/serial.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import serial
|
||||||
|
|
||||||
|
from . import BaseInterface
|
||||||
|
|
||||||
|
|
||||||
|
class SerialInterface(serial.Serial, BaseInterface):
|
||||||
|
pass
|
|
@ -1,7 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# from ..lib.feasycom_ble import BTLEUart
|
|
||||||
from . import BaseSource
|
|
||||||
|
|
||||||
|
|
||||||
class FeasycomSource(BaseSource):
|
|
||||||
pass
|
|
|
@ -1,8 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# import serial
|
|
||||||
|
|
||||||
from . import BaseSource
|
|
||||||
|
|
||||||
|
|
||||||
class SerialSource(BaseSource):
|
|
||||||
pass
|
|
Loading…
Reference in a new issue