diff --git a/libs/charging.py b/libs/charging.py index a5de899..f47e45d 100644 --- a/libs/charging.py +++ b/libs/charging.py @@ -1,3 +1,4 @@ +import traceback from datetime import datetime from sqlite3 import IntegrityError @@ -8,10 +9,10 @@ from libs.elec_price import ElecPrice from mylogger import logger from web.db import Database -elec_price = ElecPrice.read_config() - class Charging: + elec_price: ElecPrice = None + @staticmethod def get_chargings(mini=None, maxi=None) -> List[dict]: conn = Database.get_db() @@ -29,18 +30,18 @@ class Charging: @staticmethod def set_default_price(): - if elec_price.is_enable(): + if Charging.elec_price.is_enable(): conn = Database.get_db() charge_list = list(map(dict, conn.execute("SELECT * FROM battery WHERE price IS NULL").fetchall())) for charge in charge_list: - charge["price"] = elec_price.get_price(charge["start_at"], charge["stop_at"], charge["kw"]) + charge["price"] = Charging.elec_price.get_price(charge["start_at"], charge["stop_at"], charge["kw"]) Database.set_chargings_price(conn, charge["start_at"], charge["price"]) conn.close() # pylint: disable=too-many-arguments @staticmethod def update_chargings(conn, start_at, stop_at, level, co2_per_kw, consumption_kw, vin): - price = elec_price.get_price(start_at, stop_at, consumption_kw) + price = Charging.elec_price.get_price(start_at, stop_at, consumption_kw) conn.execute( "UPDATE battery set stop_at=?, end_level=?, co2=?, kw=?, price=? WHERE start_at=? and VIN=?", (stop_at, level, co2_per_kw, consumption_kw, price, start_at, vin)) @@ -77,6 +78,6 @@ class Charging: Charging.update_chargings(conn, start_at, charge_date, level, co2_per_kw, consumption_kw, car.vin) except TypeError: - logger.debug("battery table is empty") + logger.debug("battery table is probably empty : %s", traceback.format_exc()) conn.commit() conn.close() diff --git a/libs/elec_price.py b/libs/elec_price.py index edb17cf..91d660d 100644 --- a/libs/elec_price.py +++ b/libs/elec_price.py @@ -2,7 +2,8 @@ from datetime import datetime, timezone, timedelta import configparser from statistics import mean -CONFIG_FILENAME = "config.ini" +from mylogger import logger + def set_number(value): @@ -18,13 +19,14 @@ def utc_to_local(utc_dt): class ElecPrice: currency = "" + CONFIG_FILENAME = "config.ini" def __init__(self, day_price, night_price=None, nights_hours=None): self.day_price = set_number(day_price) self.night_price = set_number(night_price) self.nights_hour = None self.set_night_hour(nights_hours) - self.config_filename = CONFIG_FILENAME + self.config_filename = ElecPrice.CONFIG_FILENAME def set_night_hour(self, value): if value is not None and isinstance(value, list): @@ -55,13 +57,20 @@ class ElecPrice: while date < end: prices.append(self.get_instant_price(date)) date = date + timedelta(minutes=30) - return round(consumption * mean(prices), 2) + try: + res = round(consumption * mean(prices), 2) + except TypeError: + logger.error("Can't get_price of charge, check config") + res = None + return res def is_enable(self): return self.day_price is not None @staticmethod - def read_config(name=CONFIG_FILENAME): + def read_config(name=None): + if name is None: + name = ElecPrice.CONFIG_FILENAME config = configparser.ConfigParser() if len(config.read(name)) == 0: ElecPrice.write_default_config(name) @@ -79,7 +88,9 @@ class ElecPrice: return ElecPrice(elec_config["day price"], night_price, night_hours) @staticmethod - def write_default_config(name=CONFIG_FILENAME): + def write_default_config(name=None): + if name is None: + name = ElecPrice.CONFIG_FILENAME config = configparser.ConfigParser() config["General"] = { "currency": "€" diff --git a/otp/otp.py b/otp/otp.py index 617d879..63301d9 100644 --- a/otp/otp.py +++ b/otp/otp.py @@ -278,7 +278,8 @@ class Otp: def __getstate__(self): odict = self.__dict__.copy() - del odict['cipher'] # don't pickle this + if 'cipher' in odict: + del odict['cipher'] # don't pickle this return odict def __setstate__(self, dict_param): diff --git a/server.py b/server.py index 0782b03..2f5a164 100755 --- a/server.py +++ b/server.py @@ -11,6 +11,8 @@ from oauth2_client.credentials_manager import OAuthError import web.app from charge_control import ChargeControls +from libs.charging import Charging +from libs.elec_price import ElecPrice from mylogger import my_logger from mylogger import logger from my_psacc import MyPSACC @@ -55,6 +57,7 @@ if __name__ == "__main__": web.app.myp = MyPSACC.load_config(name=CONFIG_NAME) atexit.register(web.app.myp.save_config) web.app.myp.set_record(args.record) + Charging.elec_price = ElecPrice.read_config() if args.offline: logger.info("offline mode") else: diff --git a/web/db.py b/web/db.py index f94317e..9056742 100644 --- a/web/db.py +++ b/web/db.py @@ -14,7 +14,7 @@ from geojson import dumps as geo_dumps from mylogger import logger from utils import get_temp -NEW_BATTERY_COLUMNS = [["battery", "INTEGER"], ["charging_mode", "TEXT"]] +NEW_BATTERY_COLUMNS = [["price", "INTEGER"], ["charging_mode", "TEXT"]] NEW_POSITION_COLUMNS = [["level_fuel", "INTEGER"], ["altitude", "INTEGER"]] DATE_FORMAT = "%Y-%m-%d %H:%M:%S+00:00" @@ -157,7 +157,7 @@ class Database: try: while True: res = conn.execute("SELECT DISTINCT latitude,longitude " - "FROM position WHERE altitude IS NULL LIMIT ?;", max_pos_by_req).fetchall() + "FROM position WHERE altitude IS NULL LIMIT ?;", (max_pos_by_req,)).fetchall() nb_res = len(res) if nb_res > 0: logger.debug("add altitude for %s positions point", len(nb_null)) diff --git a/web/figures.py b/web/figures.py index a8d8812..729bd46 100644 --- a/web/figures.py +++ b/web/figures.py @@ -220,7 +220,7 @@ def get_figures(trips: Trips, charging: List[dict]): else: consumption_graph_by_temp = html.Div(Graph(style={'display': 'none'}), id="consumption_graph_by_temp") - + return True def __calculate_co2_per_kw(charging_data): try: