2023-01-07 18:02:34 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2023-12-08 12:45:05 +00:00
|
|
|
from bluepy.btle import BTLEDisconnectError # type: ignore
|
|
|
|
from serial import SerialException # type: ignore
|
2023-01-07 18:02:34 +00:00
|
|
|
|
2023-04-07 21:57:37 +00:00
|
|
|
from .config import get_config, get_consumers, get_interface
|
2023-12-10 15:08:28 +00:00
|
|
|
from .protocol import ChargeController
|
2023-01-07 21:18:30 +00:00
|
|
|
from .util import Periodical, log
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
|
2023-04-07 21:57:37 +00:00
|
|
|
class CommunicationError(BTLEDisconnectError, SerialException, IOError):
|
2023-01-10 02:09:56 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-07 18:02:34 +00:00
|
|
|
def main():
|
|
|
|
conf = get_config()
|
|
|
|
consumers = get_consumers(conf)
|
|
|
|
|
|
|
|
per_voltages = Periodical(interval=15)
|
|
|
|
per_current_hist = Periodical(interval=60)
|
2023-01-10 02:09:56 +00:00
|
|
|
# import serial
|
|
|
|
|
|
|
|
# ser = serial.Serial()
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
log("Connecting...")
|
2023-04-07 21:57:37 +00:00
|
|
|
with get_interface() as dev:
|
2023-01-07 18:02:34 +00:00
|
|
|
log("Connected.")
|
|
|
|
|
2023-12-10 15:08:28 +00:00
|
|
|
cc = ChargeController(dev)
|
2023-12-10 22:50:34 +00:00
|
|
|
log(f"Controller model: {cc.model}")
|
|
|
|
log(f"Controller version: {cc.version}")
|
|
|
|
log(f"Controller serial: {cc.serial}")
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.controller = cc
|
2023-12-10 15:08:28 +00:00
|
|
|
|
2023-01-07 18:02:34 +00:00
|
|
|
# write(dev, construct_request(0, 32))
|
|
|
|
|
|
|
|
# Memory dump
|
|
|
|
# for address in range(0, 0x10000, 16):
|
|
|
|
# log(f"Reading 0x{address:04X}...")
|
|
|
|
# write(wd, construct_request(address, 16))
|
2023-12-10 15:08:28 +00:00
|
|
|
extra = cc.extra
|
|
|
|
days = extra.run_days
|
|
|
|
|
|
|
|
res = cc.today.as_dict()
|
|
|
|
res.update(extra.as_dict())
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write(res)
|
|
|
|
del extra
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
for i in range(days):
|
2023-12-10 15:08:28 +00:00
|
|
|
hist = cc.get_historical(i)
|
|
|
|
res = hist.as_dict()
|
|
|
|
log({i: res})
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write({str(i): res})
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
if per_voltages(now):
|
2023-12-10 15:08:28 +00:00
|
|
|
data = cc.state.as_dict()
|
|
|
|
log(data)
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write(data)
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
if per_current_hist(now):
|
2023-12-10 15:08:28 +00:00
|
|
|
data = cc.today.as_dict()
|
|
|
|
data.update(cc.extra.as_dict())
|
|
|
|
log(data)
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.write(data)
|
2023-01-07 18:02:34 +00:00
|
|
|
|
|
|
|
# print(".")
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.poll()
|
|
|
|
|
2023-12-10 15:08:28 +00:00
|
|
|
time.sleep(max(0, 1 - (time.time() - now)))
|
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:
|
|
|
|
for consumer in consumers:
|
|
|
|
consumer.exit()
|
|
|
|
|
|
|
|
if type(e) is not KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|