From 332821e91095fc03d8158d8d4bd2436bc8bee7fb Mon Sep 17 00:00:00 2001 From: jlayec Date: Sat, 1 May 2021 11:58:19 +0000 Subject: [PATCH 1/5] fix debug level by name --- server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.py b/server.py index b9e28cb..d878e7c 100755 --- a/server.py +++ b/server.py @@ -29,7 +29,7 @@ def parse_args(): parser.add_argument("-c", "--charge-control", help="enable charge control, default charge_config.json", const="charge_config.json", nargs='?', metavar='charge config file') parser.add_argument("-d", "--debug", help="enable debug", const=10, default=20, nargs='?', - metavar='Debug level number', type=int) + metavar='Debug level number or name') parser.add_argument("-l", "--listen", help="change server listen address", default="127.0.0.1", metavar="IP") parser.add_argument("-p", "--port", help="change server listen port", default="5000") parser.add_argument("-r", "--record", help="save vehicle data to db", action='store_true') From c9e85db17b2f3484a5dff724bee290f426e8ec08 Mon Sep 17 00:00:00 2001 From: jlayec Date: Sat, 1 May 2021 16:53:23 +0000 Subject: [PATCH 2/5] fix elec price error --- libs/elec_price.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/elec_price.py b/libs/elec_price.py index 1281e80..a0147ed 100644 --- a/libs/elec_price.py +++ b/libs/elec_price.py @@ -1,6 +1,6 @@ from datetime import datetime, timezone, timedelta import configparser -from statistics import mean +from statistics import mean, StatisticsError from mylogger import logger @@ -61,7 +61,7 @@ class ElecPrice: date = date + timedelta(minutes=30) try: res = round(consumption * mean(prices), 2) - except TypeError: + except (TypeError, StatisticsError): logger.error("Can't get_price of charge, check config") return res From 03b94d25ca42cda820fa19886c79c75226c2a49d Mon Sep 17 00:00:00 2001 From: jlayec Date: Sat, 1 May 2021 18:33:39 +0000 Subject: [PATCH 3/5] fix view charge without trip after --- web/db.py | 9 +++++++++ web/views.py | 10 +++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/web/db.py b/web/db.py index 6654a23..28a7caf 100644 --- a/web/db.py +++ b/web/db.py @@ -140,6 +140,15 @@ class Database: return None return res[0] + @staticmethod + def get_range_timestamp(): + conn = Database.get_db() + first = conn.execute("SELECT Timestamp FROM position ORDER BY Timestamp limit 1").fetchone() + last = conn.execute("SELECT Timestamp FROM position ORDER BY Timestamp DESC limit 1").fetchone() + if first is None or last is None: + return None, None + return first[0], last[0] + @staticmethod def set_chargings_price(conn, start_at, price): if isinstance(start_at, str): diff --git a/web/views.py b/web/views.py index ac330da..74d64b0 100644 --- a/web/views.py +++ b/web/views.py @@ -250,12 +250,14 @@ def update_trips(): chargings = Charging.get_chargings() except (StopIteration, AssertionError): logger.debug("No trips yet") - return + # return # update for slider global min_date, max_date, min_millis, max_millis, step, marks try: - min_date = trips[0].start_at - max_date = trips[-1].start_at + # min_date = trips[0].start_at + # max_date = trips[-1].start_at + min_date, max_date = Database.get_range_timestamp() + logger.debug("min_date:%s - max_date:%s",min_date, max_date) min_millis = figures.unix_time_millis(min_date) max_millis = figures.unix_time_millis(max_date) step = (max_millis - min_millis) / 100 @@ -263,6 +265,8 @@ def update_trips(): cached_layout = None # force regenerate layout except (ValueError, IndexError): logger.error("update_trips (slider): %s", exc_info=True) + except AttributeError: + logger.debug("position table is probably empty :", exc_info=True) return From 916daecaf64294e24b91811f2222dadb55096a79 Mon Sep 17 00:00:00 2001 From: jlayec Date: Sat, 1 May 2021 19:21:31 +0000 Subject: [PATCH 4/5] fix ecomix using country code when last_position is not available --- ecomix.py | 15 +++++++++------ libs/charging.py | 6 +++--- my_psacc.py | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ecomix.py b/ecomix.py index ded9671..1ff4008 100644 --- a/ecomix.py +++ b/ecomix.py @@ -48,10 +48,10 @@ class Ecomix: return None @staticmethod - def get_data_from_co2_signal(latitude, longitude): + def get_data_from_co2_signal(latitude, longitude, country_code_default): if Ecomix.co2_signal_key is not None: try: - country_code = Ecomix.get_country(latitude, longitude) + country_code = Ecomix.get_country(latitude, longitude, country_code_default) assert country_code is not None if country_code not in Ecomix._cache: Ecomix._cache[country_code] = [] @@ -90,19 +90,22 @@ class Ecomix: return mean(co2_per_kw) @staticmethod - def get_country(latitude, longitude): + def get_country(latitude, longitude, country_code_default): try: location = reverse_geocode.search([(latitude, longitude)])[0] country_code = location["country_code"] return country_code except (UnicodeDecodeError, IndexError): logger.error("Can't find country for %s %s", latitude, longitude) - return None + # return None + country_code = country_code_default + logger.warning("Using country of origin : %s (wrong co2 when traveling abroad)", country_code) + return country_code @staticmethod - def get_co2_per_kw(start: datetime, end: datetime, latitude, longitude): + def get_co2_per_kw(start: datetime, end: datetime, latitude, longitude, country_code_default): co2_per_kw = None - country_code = Ecomix.get_country(latitude, longitude) + country_code = Ecomix.get_country(latitude, longitude, country_code_default) if country_code is None: return None if Ecomix.co2_signal_key is not None: diff --git a/libs/charging.py b/libs/charging.py index 367c4e2..499ae92 100644 --- a/libs/charging.py +++ b/libs/charging.py @@ -47,7 +47,7 @@ class Charging: Database.clean_battery(conn) @staticmethod - def record_charging(car, charging_status, charge_date: datetime, level, latitude, longitude, charging_mode): + def record_charging(car, charging_status, charge_date: datetime, level, latitude, longitude, country_code, charging_mode): conn = Database.get_db() charge_date = charge_date.replace(microsecond=0) if charging_status == "InProgress": @@ -64,7 +64,7 @@ class Charging: else: conn.execute("INSERT INTO battery(start_at,start_level,charging_mode,VIN) VALUES(?,?,?,?)", (charge_date, level, charging_mode, car.vin)) - Ecomix.get_data_from_co2_signal(latitude, longitude) + Ecomix.get_data_from_co2_signal(latitude, longitude, country_code) else: try: start_at, stop_at, start_level = conn.execute( @@ -72,7 +72,7 @@ class Charging: "DESC limit 1", (car.vin,)).fetchone() in_progress = stop_at is None if in_progress: - co2_per_kw = Ecomix.get_co2_per_kw(start_at, charge_date, latitude, longitude) + co2_per_kw = Ecomix.get_co2_per_kw(start_at, charge_date, latitude, longitude, country_code) consumption_kw = (level - start_level) / 100 * car.battery_power Charging.update_chargings(conn, start_at, charge_date, level, co2_per_kw, consumption_kw, car.vin) diff --git a/my_psacc.py b/my_psacc.py index b03e81c..22d9fd9 100644 --- a/my_psacc.py +++ b/my_psacc.py @@ -476,7 +476,7 @@ class MyPSACC: try: charging_status = car.status.get_energy('Electric').charging.status charging_mode = car.status.get_energy('Electric').charging.charging_mode - Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, charging_mode) + Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, self.country_code, charging_mode) logger.debug("charging_status:%s ", charging_status) except AttributeError: logger.error("charging status not available from api") From 2cf16c94a290cb33064fcc7a52f1aec8e9f85bc5 Mon Sep 17 00:00:00 2001 From: jlayec Date: Sat, 1 May 2021 22:40:11 +0000 Subject: [PATCH 5/5] fix get_position --- my_psacc.py | 3 ++- web/views.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/my_psacc.py b/my_psacc.py index 22d9fd9..a53e35a 100644 --- a/my_psacc.py +++ b/my_psacc.py @@ -476,7 +476,8 @@ class MyPSACC: try: charging_status = car.status.get_energy('Electric').charging.status charging_mode = car.status.get_energy('Electric').charging.charging_mode - Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, self.country_code, charging_mode) + Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, self.country_code, + charging_mode) logger.debug("charging_status:%s ", charging_status) except AttributeError: logger.error("charging status not available from api") diff --git a/web/views.py b/web/views.py index 74d64b0..daf677e 100644 --- a/web/views.py +++ b/web/views.py @@ -186,7 +186,7 @@ def get_position(vin): coordinates = res.last_position.geometry.coordinates except AttributeError: return jsonify({'error': 'last_position not available from api'}) - longitude, latitude, altitude = coordinates[:2] + longitude, latitude = coordinates[:2] if len(coordinates) == 3: # altitude is not always available altitude = coordinates[2] else: