Add mileage to the charge log

This commit is contained in:
stevoh6
2023-01-13 08:55:17 +01:00
parent a56aea68e9
commit a18700b100
7 changed files with 27 additions and 20 deletions
@@ -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()
@@ -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")
+2 -1
View File
@@ -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
+4 -3
View File
@@ -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=?",
+3 -1
View File
@@ -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=[
+8 -6
View File
@@ -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}
+5 -5
View File
@@ -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):