From 52d2850fe9601d53d25d26447b443cd63a1cf27d Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Fri, 29 Jan 2021 11:21:22 +0100 Subject: [PATCH] handle empty database --- MyPSACC.py | 72 +++++++++++++++++++++++++------------------------ README.md | 2 +- web/callback.py | 5 +++- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/MyPSACC.py b/MyPSACC.py index f1e63e6..a9e002c 100644 --- a/MyPSACC.py +++ b/MyPSACC.py @@ -506,8 +506,8 @@ class MyPSACC: else: try: start_at, stop_at, start_level = conn.execute( - "SELECT start_at, stop_at, start_level from battery WHERE VIN=? ORDER BY start_at " - "DESC limit 1", (vin,)).fetchone() + "SELECT start_at, stop_at, start_level from battery WHERE VIN=? ORDER BY start_at " + "DESC limit 1", (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) @@ -516,9 +516,10 @@ class MyPSACC: "UPDATE battery set stop_at=?, end_level=?, co2=?, kw=? WHERE start_at=? and VIN=?", (charge_date, level, co2_per_kw, kw, start_at, vin)) conn.commit() + except TypeError: + logger.debug("battery table is empty") except: logger.debug("Error when saving status " + traceback.format_exc()) - pass conn.close() @staticmethod @@ -541,41 +542,42 @@ class MyPSACC: def get_trips() -> List[Trip]: conn = get_db() res = conn.execute('SELECT * FROM position ORDER BY Timestamp').fetchall() - start = res[0] - end = res[1] trips = [] - tr = Trip() - #res = list(map(dict,res)) - for x in range(0, len(res) - 2): - next_el = res[x + 2] - if end["mileage"] - start["mileage"] == 0 or \ - (end["Timestamp"] - start["Timestamp"]).total_seconds() / 3600 > 3: - start = end - tr = Trip() - else: - distance = next_el["mileage"] - end["mileage"] # km - duration = (next_el["Timestamp"] - end["Timestamp"]).total_seconds() / 3600 - if (distance == 0 and duration > 0.08) or duration > 2: # check the speed to handle missing point - tr.distance = end["mileage"] - start["mileage"] # km - if tr.distance > 0: - tr.start_at = start["Timestamp"] - tr.end_at = end["Timestamp"] - tr.add_points(end["longitude"], end["latitude"]) - tr.duration = (end["Timestamp"] - start["Timestamp"]).total_seconds() / 3600 - tr.speed_average = tr.distance / tr.duration - diff_level = start["level"] - end["level"] - tr.consumption = diff_level / 100 * BATTERY_POWER # kw - tr.consumption_km = 100 * tr.consumption / tr.distance # kw/100 km - # logger.debug( - # f"Trip: {start['Timestamp']} {tr.distance:.1f}km {tr.duration:.2f}h {tr.speed_average:.2f} km/h {tr.consumption:.2f} kw {tr.consumption_km:.2f}kw/100km") - # filter bad value - if tr.consumption_km < 70: - trips.append(tr) - start = next_el + if len(res) > 1: + start = res[0] + end = res[1] + tr = Trip() + #res = list(map(dict,res)) + for x in range(0, len(res) - 2): + next_el = res[x + 2] + if end["mileage"] - start["mileage"] == 0 or \ + (end["Timestamp"] - start["Timestamp"]).total_seconds() / 3600 > 3: + start = end tr = Trip() else: - tr.add_points(end["longitude"], end["latitude"]) - end = next_el + distance = next_el["mileage"] - end["mileage"] # km + duration = (next_el["Timestamp"] - end["Timestamp"]).total_seconds() / 3600 + if (distance == 0 and duration > 0.08) or duration > 2: # check the speed to handle missing point + tr.distance = end["mileage"] - start["mileage"] # km + if tr.distance > 0: + tr.start_at = start["Timestamp"] + tr.end_at = end["Timestamp"] + tr.add_points(end["longitude"], end["latitude"]) + tr.duration = (end["Timestamp"] - start["Timestamp"]).total_seconds() / 3600 + tr.speed_average = tr.distance / tr.duration + diff_level = start["level"] - end["level"] + tr.consumption = diff_level / 100 * BATTERY_POWER # kw + tr.consumption_km = 100 * tr.consumption / tr.distance # kw/100 km + # logger.debug( + # f"Trip: {start['Timestamp']} {tr.distance:.1f}km {tr.duration:.2f}h {tr.speed_average:.2f} km/h {tr.consumption:.2f} kw {tr.consumption_km:.2f}kw/100km") + # filter bad value + if tr.consumption_km < 70: + trips.append(tr) + start = next_el + tr = Trip() + else: + tr.add_points(end["longitude"], end["latitude"]) + end = next_el return trips @staticmethod diff --git a/README.md b/README.md index 7c6f02f..dd03e59 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ With this app you will be able to : - get consumption statistic - visualize your trips on a map -The api is documented [here](https://developer.groupe-psa.io/webapi/b2c/quickstart/connect/#article) but it is not totally up to date, and contains some errors. +The official api is documented [here](https://developer.groupe-psa.io/webapi/b2c/quickstart/connect/#article) but it is not totally up to date, and contains some errors. ## I. Get credentials diff --git a/web/callback.py b/web/callback.py index 871bfa4..5158688 100644 --- a/web/callback.py +++ b/web/callback.py @@ -123,7 +123,6 @@ def update_trips(): try: web.db.callback_fct = update_trips update_trips() - min_date = trips[0].start_at max_date = trips[-1].start_at min_millis = figures.unix_time_millis(min_date) @@ -157,6 +156,10 @@ try: ), html.Div(id="tab-content", className="p-4"), ])]) +except (IndexError, TypeError) as e: + logger.debug("Failed to generate figure, there is probably not enough data yet") + data_div = dbc.Alert("No data to show", color="danger") + except: logger.error("Failed to generate figure, there is probably not enough data yet") logger.error(traceback.format_exc())