improve charge control and logging

This commit is contained in:
Florian Bezannier
2020-11-19 10:52:19 +01:00
parent 726d9017cf
commit c5b174def8
2 changed files with 31 additions and 18 deletions
+18 -10
View File
@@ -1,13 +1,16 @@
import json
import threading
import traceback
from copy import copy
from datetime import datetime, timedelta
from hashlib import md5
from time import sleep
from MyPSACC import MyPSACC
from MyLogger import logger
from psa_connectedcar.rest import ApiException
class ChargeControls:
def __init__(self):
self.list: dict = {}
self._confighash = None
@@ -44,13 +47,15 @@ class ChargeControls:
class ChargeControl:
periodicity = 120
MQTT_TIMEOUT = 60
def __init__(self, psacc:MyPSACC, vin, percentage_threshold, stop_hour):
self.vin = vin
self.percentage_threshold = percentage_threshold
self.set_stop_hour(stop_hour)
self.psacc = psacc
self.retry_count = 0
self.thread = None
self.thread:threading.Timer = None
def set_stop_hour(self,stop_hour):
if stop_hour == [0, 0]:
@@ -63,33 +68,36 @@ class ChargeControl:
self._next_stop_hour += timedelta(days=1)
def start(self):
periodicity = 60 * 1
periodicity = ChargeControl.periodicity
now = datetime.now()
if self._next_stop_hour is not None and self._next_stop_hour < now:
stop_charge = True
self._next_stop_hour += timedelta(days=1)
print("stop charge")
logger.info("it's time to stop the charge")
else :
stop_charge = False
if self.percentage_threshold != 100 or stop_charge:
res = self.psacc.get_vehicle_info(self.vin)
res = None
try:
res = self.psacc.get_vehicle_info(self.vin)
except ApiException:
logger.error(traceback.format_exc())
if res is not None:
status = res.energy[0]['charging']['status']
print(f"charging status of {self.vin} is {status}")
level = res.energy[0]["level"]
logger.info(f"charging status of {self.vin} is {status}, battery level: {level}")
if status == "InProgress":
level = res.energy[0]["level"]
if (level >= self.percentage_threshold and self.retry_count < 2) or stop_charge:
self.psacc.charge_now(self.vin,False)
self.retry_count += 1
sleep(45)
sleep(ChargeControl.MQTT_TIMEOUT)
res = self.psacc.get_vehicle_info(self.vin)
status = res.energy[0]['charging']['status']
if status == "InProgress":
print(f"retry to stop the charge of {self.vin}")
logger.warn(f"retry to stop the charge of {self.vin}")
self.psacc.charge_now(self.vin, False)
self.retry_count += 1
periodicity = 60 * 1
if self._next_stop_hour is not None:
next_in_second = (self._next_stop_hour- now).total_seconds()
if next_in_second < periodicity:
@@ -97,7 +105,7 @@ class ChargeControl:
else:
self.retry_count = 0
else:
print(f"error when get vehicle info of {self.vin}")
logger.error(f"error when get vehicle info of {self.vin}")
self.thread = threading.Timer(periodicity, self.start)
self.thread.start()