WIP: Initial work on configurable source device

This commit is contained in:
Odd Stråbø 2023-01-10 03:09:56 +01:00
parent 82ee08a1d7
commit fa9d5d73c4
5 changed files with 50 additions and 2 deletions

View file

@ -5,7 +5,8 @@ import time
from decimal import Decimal from decimal import Decimal
from typing import cast from typing import cast
from bluepy import btle from bluepy.btle import BTLEDisconnectError
from serial import SerialException
from .config import get_config, get_consumers from .config import get_config, get_consumers
from .constants import MAC from .constants import MAC
@ -15,12 +16,19 @@ from .solar_types import DataName
from .util import Periodical, log from .util import Periodical, log
class CommunicationError(BTLEDisconnectError, SerialException):
pass
def main(): def main():
conf = get_config() conf = get_config()
consumers = get_consumers(conf) consumers = get_consumers(conf)
per_voltages = Periodical(interval=15) per_voltages = Periodical(interval=15)
per_current_hist = Periodical(interval=60) per_current_hist = Periodical(interval=60)
# import serial
# ser = serial.Serial()
try: try:
while True: while True:
@ -96,7 +104,7 @@ def main():
# else: # else:
# write(wd, CMD_ENABLE_LOAD) # write(wd, CMD_ENABLE_LOAD)
except btle.BTLEDisconnectError: except CommunicationError:
log("ERROR: Disconnected") log("ERROR: Disconnected")
time.sleep(1) time.sleep(1)

View file

@ -1,6 +1,7 @@
# -*- 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
@ -52,6 +53,23 @@ 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]:
raise NotImplementedError
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))
write_config(conf)
return consumers
if __name__ == "__main__": if __name__ == "__main__":
conf = get_config() conf = get_config()

View file

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from abc import ABCMeta
from io import RawIOBase
class BaseSource(RawIOBase, metaclass=ABCMeta):
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