From a18700b100fd886437c1226a152d8a4a6018b5e9 Mon Sep 17 00:00:00 2001 From: stevoh6 Date: Fri, 13 Jan 2023 08:55:17 +0100 Subject: [PATCH] Add mileage to the charge log --- psa_car_controller/psacc/application/charging.py | 7 ++++--- psa_car_controller/psacc/application/psa_client.py | 2 +- psa_car_controller/psacc/model/charge.py | 3 ++- psa_car_controller/psacc/repository/db.py | 7 ++++--- psa_car_controller/web/figures.py | 4 +++- tests/test_unit.py | 14 ++++++++------ tests/utils.py | 10 +++++----- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/psa_car_controller/psacc/application/charging.py b/psa_car_controller/psacc/application/charging.py index 336d2ec..70f59a3 100644 --- a/psa_car_controller/psacc/application/charging.py +++ b/psa_car_controller/psacc/application/charging.py @@ -60,14 +60,14 @@ class Charging: @staticmethod def record_charging(car: Car, charging_status, charge_date: datetime, level, latitude, # pylint: disable=too-many-locals - longitude, country_code, charging_mode, charging_rate, autonomy): + longitude, country_code, charging_mode, charging_rate, autonomy, mileage): conn = Database.get_db() charge_date = charge_date.replace(microsecond=0) if charging_status == "InProgress": last_charge = Database.get_last_charge(car.vin) if Charging.is_charge_ended(last_charge): - conn.execute("INSERT INTO battery(start_at,start_level,charging_mode,VIN) VALUES(?,?,?,?)", - (charge_date, level, charging_mode, car.vin)) + conn.execute("INSERT INTO battery(start_at,start_level,charging_mode,VIN,mileage) VALUES(?,?,?,?,?)", + (charge_date, level, charging_mode, car.vin, mileage)) start_at = charge_date else: start_at = last_charge.start_at @@ -93,6 +93,7 @@ class Charging: last_charge.co2 = co2_per_kw last_charge.kw = consumption_kw last_charge.stop_at = charge_date + last_charge.mileage = mileage Charging.update_chargings(conn, last_charge, car) conn.commit() conn.close() diff --git a/psa_car_controller/psacc/application/psa_client.py b/psa_car_controller/psacc/application/psa_client.py index 0dfa1c9..a3f1a6f 100644 --- a/psa_car_controller/psacc/application/psa_client.py +++ b/psa_car_controller/psacc/application/psa_client.py @@ -200,7 +200,7 @@ class PSAClient: charging_rate = car.status.get_energy('Electric').charging.charging_rate autonomy = car.status.get_energy('Electric').autonomy Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, self.country_code, - charging_mode, charging_rate, autonomy) + charging_mode, charging_rate, autonomy, mileage) logger.debug("charging_status:%s ", charging_status) except AttributeError as ex: logger.error("charging status not available from api") diff --git a/psa_car_controller/psacc/model/charge.py b/psa_car_controller/psacc/model/charge.py index 2a6e55f..2d654a7 100644 --- a/psa_car_controller/psacc/model/charge.py +++ b/psa_car_controller/psacc/model/charge.py @@ -15,7 +15,7 @@ class ChargingMode(Enum): class Charge: # pylint: disable=too-many-arguments def __init__(self, start_at: datetime, stop_at: datetime = None, vin=None, start_level=None, end_level=None, - co2=None, kw=None, price=None, charging_mode=None): + co2=None, kw=None, price=None, charging_mode=None, mileage=None): assert isinstance(start_at, datetime) self.charging_mode: ChargingMode = ChargingMode(charging_mode) self.start_at = start_at @@ -26,3 +26,4 @@ class Charge: self.co2 = co2 self.kw = kw self.price = price + self.mileage = mileage diff --git a/psa_car_controller/psacc/repository/db.py b/psa_car_controller/psacc/repository/db.py index 0236bab..f5a8121 100644 --- a/psa_car_controller/psacc/repository/db.py +++ b/psa_car_controller/psacc/repository/db.py @@ -18,7 +18,7 @@ from psa_car_controller.psacc.utils.utils import get_temp logger = logging.getLogger(__name__) -NEW_BATTERY_COLUMNS = [["price", "INTEGER"], ["charging_mode", "TEXT"]] +NEW_BATTERY_COLUMNS = [["price", "INTEGER"], ["charging_mode", "TEXT"], ["mileage", "REAL"]] NEW_POSITION_COLUMNS = [["level_fuel", "INTEGER"], ["altitude", "INTEGER"]] NEW_BATTERY_CURVE_COLUMNS = [["rate", "INTEGER"], ["autonomy", "INTEGER"]] @@ -269,8 +269,7 @@ class Database: @staticmethod def get_last_charge(vin) -> Charge: conn = Database.get_db() - res = conn.execute("SELECT start_at, stop_at, vin, start_level, end_level, co2, kw, price, charging_mode " - "FROM battery WHERE VIN=? ORDER BY start_at DESC limit 1", (vin,)).fetchone() + res = conn.execute("SELECT * FROM battery WHERE VIN=? ORDER BY start_at DESC limit 1", (vin,)).fetchone() if res: return Charge(**dict_key_to_lower_case(**res)) return None @@ -294,6 +293,8 @@ class Database: @staticmethod def update_charge(charge: Charge): + # we don't need to update mileage, since it should be inserted at beginning of charge, + # maybe in future this will be supported conn = Database.get_db() res = conn.execute( "UPDATE battery set stop_at=?, end_level=?, co2=?, kw=?, price=? WHERE start_at=? and VIN=?", diff --git a/psa_car_controller/web/figures.py b/psa_car_controller/web/figures.py index 4bb0986..af9259b 100644 --- a/psa_car_controller/web/figures.py +++ b/psa_car_controller/web/figures.py @@ -128,7 +128,9 @@ def get_figures(car: Car): {'id': 'kw', 'name': 'consumption', 'type': 'numeric', 'format': deepcopy(nb_format).symbol_suffix(" kWh").precision(2)}, {'id': 'price', 'name': 'price', 'type': 'numeric', - 'format': deepcopy(nb_format).symbol_suffix(" " + CURRENCY).precision(2), 'editable': True} + 'format': deepcopy(nb_format).symbol_suffix(" " + CURRENCY).precision(2), 'editable': True}, + {'id': 'charging_mode', 'name': 'charging mode', 'type': 'string'}, + {'id': 'mileage', 'name': 'mileage', 'type': 'numeric', 'format': nb_format}, ], data=[], style_data_conditional=[ diff --git a/tests/test_unit.py b/tests/test_unit.py index fc52e6a..b8d3186 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -188,11 +188,12 @@ class TestUnit(unittest.TestCase): Charging.elec_price = ConfigRepository.read_config(DATA_DIR + "config.ini").Electricity_config start_level = 40 end_level = 85 - Charging.record_charging(car, "InProgress", date0, start_level, latitude, longitude, None, "slow", 20, 60) - Charging.record_charging(car, "InProgress", date1, 70, latitude, longitude, "FR", "slow", 20, 60) - Charging.record_charging(car, "InProgress", date1, 70, latitude, longitude, "FR", "slow", 20, 60) - Charging.record_charging(car, "InProgress", date2, 80, latitude, longitude, "FR", "slow", 20, 60) - Charging.record_charging(car, "Stopped", date3, end_level, latitude, longitude, "FR", "slow", 20, 60) + mileage = 123456789.1 + Charging.record_charging(car, "InProgress", date0, start_level, latitude, longitude, None, "slow", 20, 60, mileage) + Charging.record_charging(car, "InProgress", date1, 70, latitude, longitude, "FR", "slow", 20, 60, mileage) + Charging.record_charging(car, "InProgress", date1, 70, latitude, longitude, "FR", "slow", 20, 60, mileage) + Charging.record_charging(car, "InProgress", date2, 80, latitude, longitude, "FR", "slow", 20, 60, mileage) + Charging.record_charging(car, "Stopped", date3, end_level, latitude, longitude, "FR", "slow", 20, 60, mileage) chargings = Charging.get_chargings() co2 = chargings[0]["co2"] assert isinstance(co2, float) @@ -204,7 +205,8 @@ class TestUnit(unittest.TestCase): 'co2': co2, 'kw': 20.7, 'price': 4.29, - 'charging_mode': 'slow'}]) + 'charging_mode': 'slow', + 'mileage': 123456789.1}]) assert get_figures(car) row = {"start_at": date0.strftime('%Y-%m-%dT%H:%M:%S.000Z'), "stop_at": date3.strftime('%Y-%m-%dT%H:%M:%S.000Z'), "start_level": start_level, "end_level": end_level} diff --git a/tests/utils.py b/tests/utils.py index 0882df5..0f49725 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -47,11 +47,11 @@ def record_position(): def record_charging(): - Charging.record_charging(car, "InProgress", date0, 50, latitude, longitude, "FR", "slow", 20, 60) - Charging.record_charging(car, "InProgress", date1, 75, latitude, longitude, "FR", "slow", 20, 60) - Charging.record_charging(car, "InProgress", date2, 85, latitude, longitude, "FR", "slow", 20, 60) - Charging.record_charging(car, "InProgress", date3, 90, latitude, longitude, "FR", "slow", 20, 60) - Charging.record_charging(car, "Stopped", date4, 91, latitude, longitude, "FR", "slow", 20, 60) + Charging.record_charging(car, "InProgress", date0, 50, latitude, longitude, "FR", "slow", 20, 60, 123456789.1) + Charging.record_charging(car, "InProgress", date1, 75, latitude, longitude, "FR", "slow", 20, 60, 123456789.1) + Charging.record_charging(car, "InProgress", date2, 85, latitude, longitude, "FR", "slow", 20, 60, 123456789.1) + Charging.record_charging(car, "InProgress", date3, 90, latitude, longitude, "FR", "slow", 20, 60, 123456789.1) + Charging.record_charging(car, "Stopped", date4, 91, latitude, longitude, "FR", "slow", 20, 60, 123456789.1) def get_date(offset):