2023-01-07 18:02:34 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import time
|
2023-04-10 01:39:19 +00:00
|
|
|
from typing import List, Optional, cast
|
2023-01-07 18:02:34 +00:00
|
|
|
|
2023-01-10 02:09:56 +00:00
|
|
|
from bluepy.btle import BTLEDisconnectError
|
|
|
|
from serial import SerialException
|
2023-01-07 18:02:34 +00:00
|
|
|
|
2023-04-10 01:39:19 +00:00
|
|
|
from srnemqtt.consumers import BaseConsumer
|
|
|
|
|
2023-04-07 21:57:37 +00:00
|
|
|
from .config import get_config, get_consumers, get_interface
|
2023-04-10 01:39:19 +00:00
|
|
|
from .srne import Srne
|
2023-01-07 21:18:30 +00:00
|
|
|
from .util import Periodical, log
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
|
2023-04-10 01:39:19 +00:00
|
|
|
class CommunicationError(BTLEDisconnectError, SerialException, TimeoutError):
|
2023-01-10 02:09:56 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-04-10 01:39:19 +00:00
|
|
|
def main() -> None:
|
2023-01-07 18:02:34 +00:00
|
|
|
conf = get_config()
|
2023-04-10 01:39:19 +00:00
|
|
|
consumers: Optional[List[BaseConsumer]] = None
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
per_voltages = Periodical(interval=15)
|
|
|
|
per_current_hist = Periodical(interval=60)
|
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
log("Connecting...")
|
2023-04-07 21:57:37 +00:00
|
|
|
with get_interface() as dev:
|
2023-04-10 01:39:19 +00:00
|
|
|
srne = Srne(dev)
|
2023-01-07 18:02:34 +00:00
|
|
|
log("Connected.")
|
|
|
|
|
2023-04-10 01:39:19 +00:00
|
|
|
if consumers is None:
|
|
|
|
consumers = get_consumers(srne, conf)
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
days = 7
|
2023-04-10 01:39:19 +00:00
|
|
|
res = srne.get_historical_entry()
|
2023-01-07 18:02:34 +00:00
|
|
|
if res:
|
|
|
|
log(res)
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write(res)
|
|
|
|
days = cast(int, res.get("run_days", 7))
|
|
|
|
|
|
|
|
for i in range(days):
|
2023-04-10 01:39:19 +00:00
|
|
|
res = srne.get_historical_entry(i)
|
2023-01-07 18:02:34 +00:00
|
|
|
if res:
|
|
|
|
log({i: res})
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write({str(i): res})
|
|
|
|
|
|
|
|
while True:
|
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
if per_voltages(now):
|
2023-04-10 01:39:19 +00:00
|
|
|
data = srne.get_battery_state()
|
2023-01-07 18:02:34 +00:00
|
|
|
if data:
|
|
|
|
log(data)
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write(data)
|
|
|
|
|
|
|
|
if per_current_hist(now):
|
2023-04-10 01:39:19 +00:00
|
|
|
try:
|
|
|
|
data = srne.get_historical_entry()
|
2023-01-07 18:02:34 +00:00
|
|
|
log(data)
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write(data)
|
2023-04-10 01:39:19 +00:00
|
|
|
except TimeoutError:
|
|
|
|
pass
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
# print(".")
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.poll()
|
|
|
|
|
2023-04-10 01:39:19 +00:00
|
|
|
time.sleep(max(0, 1 - time.time() - now)) # 1s loop
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
# if STATUS.get('load_enabled'):
|
|
|
|
# write(wd, CMD_DISABLE_LOAD)
|
|
|
|
# else:
|
|
|
|
# write(wd, CMD_ENABLE_LOAD)
|
|
|
|
|
2023-01-10 02:09:56 +00:00
|
|
|
except CommunicationError:
|
2023-01-07 18:02:34 +00:00
|
|
|
log("ERROR: Disconnected")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
except (KeyboardInterrupt, SystemExit, Exception) as e:
|
2023-04-10 01:39:19 +00:00
|
|
|
if consumers is not None:
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.exit()
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
if type(e) is not KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|