From 7b7d16a49b2cf0bf246d3373ad0cdd7c07bb09b2 Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Mon, 5 Sep 2022 20:44:24 +0200 Subject: [PATCH] fix: errors --- FAQ.md | 8 +++- psa_car_controller/psa/RemoteClient.py | 4 +- .../psacc/application/car_controller.py | 2 +- .../psacc/application/charging.py | 2 +- .../psacc/application/ecomix.py | 19 ++++---- .../psacc/application/psa_client.py | 5 ++- psa_car_controller/psacc/model/car_model.py | 43 ------------------- psa_car_controller/psacc/model/car_status.py | 2 +- psa_car_controller/web/assets/clientside.js | 10 ++++- 9 files changed, 34 insertions(+), 61 deletions(-) diff --git a/FAQ.md b/FAQ.md index 16a234c..222b296 100644 --- a/FAQ.md +++ b/FAQ.md @@ -5,7 +5,13 @@ The car model need to be known by the app. To find it, the app use the first 10 If the car isn't in the list we need to add it, to do that you need to edit the file here: Go to [car_models.yml](https://github.com/flobz/psa_car_controller/blob/master/psa_car_controller/psacc/resources/car_models.yml) -and click on edit then copy cut and already existent model in the list and edit all properties that are incorrect for your model. +and click on edit then copy cut an already existent model in the list and edit all properties that are incorrect for your model: +- Model +- First ten letter of the car VIN: +- Useable Electric capacity in kw: [find it here](https://ev-database.org/cheatsheet/useable-battery-capacity-electric-car) +- Fuel capacity in liter +- abrp ref : [find it here](https://api.iternio.com/1/tlm/get_carmodels_list?api_key=32b2162f-9599-4647-8139-66e9f9528370) + Finally, click on propose change. ### 2. Error during activation {'newversion': '2.0.0', 'newversionurl': 'http://m.inwebo.com/', 'err': 'NOK:FORBIDDEN'} diff --git a/psa_car_controller/psa/RemoteClient.py b/psa_car_controller/psa/RemoteClient.py index cb68554..a5689a3 100644 --- a/psa_car_controller/psa/RemoteClient.py +++ b/psa_car_controller/psa/RemoteClient.py @@ -40,7 +40,7 @@ class RemoteClient: "accept": "application/hal+json", "User-Agent": "okhttp/4.8.0", } - self.last_request = [] + self.last_request = None self.mqtt_client = None self.otp = None @@ -140,7 +140,7 @@ class RemoteClient: logger.debug("%s %s", mqtt_request.topic, message) self.mqtt_client.publish(mqtt_request.topic, message) if store: - self.last_request = [mqtt_request] + self.last_request = mqtt_request def mqtt_request(self, vin, req_parameters, topic): return MQTTRequest(topic, vin, req_parameters, self.account_info.get_mqtt_customer_id()) diff --git a/psa_car_controller/psacc/application/car_controller.py b/psa_car_controller/psacc/application/car_controller.py index f17191f..c09df1c 100644 --- a/psa_car_controller/psacc/application/car_controller.py +++ b/psa_car_controller/psacc/application/car_controller.py @@ -75,7 +75,7 @@ class PSACarController(metaclass=Singleton): if path.isfile(self.config_name): self.myp = PSAClient.load_config(name=self.config_name) elif self.args.web_conf: - logger.error("Bad config") + logger.error("No config file") self.is_good = False return False else: diff --git a/psa_car_controller/psacc/application/charging.py b/psa_car_controller/psacc/application/charging.py index 291ada7..336d2ec 100644 --- a/psa_car_controller/psacc/application/charging.py +++ b/psa_car_controller/psacc/application/charging.py @@ -83,7 +83,7 @@ class Charging: try: last_charge = Database.get_last_charge(car.vin) charge_just_finished = last_charge.stop_at is None - except TypeError: + except (TypeError, AttributeError): logger.debug("battery table is probably empty :", exc_info=True) charge_just_finished = False if charge_just_finished: diff --git a/psa_car_controller/psacc/application/ecomix.py b/psa_car_controller/psacc/application/ecomix.py index 8314ae3..6ed9283 100644 --- a/psa_car_controller/psacc/application/ecomix.py +++ b/psa_car_controller/psacc/application/ecomix.py @@ -3,13 +3,13 @@ from datetime import datetime, timedelta from statistics import mean, StatisticsError import xml.etree.ElementTree as ElT import numbers +from typing import Union import requests import reverse_geocode from pytz import UTC from requests import RequestException - CO2_SIGNAL_REQ_INTERVAL = 600 CO2_SIGNAL_URL = "https://api.co2signal.com" @@ -21,7 +21,7 @@ class Ecomix: co2_signal_key = None @staticmethod - def get_data_france(start, end): + def get_data_france(start, end) -> Union[float, None]: start_str = start.strftime("%d/%m/%Y") end_str = end.strftime("%d/%m/%Y") try: @@ -42,13 +42,16 @@ class Ecomix: valeurs = etree.iter("valeur") co2_per_kw = [] - - valeur = next(valeurs) - while int(valeur.attrib["periode"]) != period_start: - valeur = next(valeurs) - while int(valeur.attrib["periode"]) != period_end: - co2_per_kw.append(int(valeur.text)) + try: valeur = next(valeurs) + while int(valeur.attrib["periode"]) != period_start: + valeur = next(valeurs) + while int(valeur.attrib["periode"]) != period_end: + co2_per_kw.append(int(valeur.text)) + valeur = next(valeurs) + except StopIteration: + logger.exception("Can't get CO2 value between %s %s", start, end) + return None try: return mean(co2_per_kw) except StatisticsError: diff --git a/psa_car_controller/psacc/application/psa_client.py b/psa_car_controller/psacc/application/psa_client.py index 937112e..5c68d85 100644 --- a/psa_car_controller/psacc/application/psa_client.py +++ b/psa_car_controller/psacc/application/psa_client.py @@ -175,7 +175,7 @@ class PSAClient: def set_record(self, value: bool): self._record_enabled = value - def record_info(self, car: Car): + def record_info(self, car: Car): # pylint: disable=too-many-locals mileage = car.status.timed_odometer.mileage level = car.status.get_energy('Electric').level level_fuel = car.status.get_energy('Fuel').level @@ -202,8 +202,9 @@ class PSAClient: Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, self.country_code, charging_mode, charging_rate, autonomy) logger.debug("charging_status:%s ", charging_status) - except AttributeError: + except AttributeError as ex: logger.error("charging status not available from api") + logger.debug(ex) def __iter__(self): for key, value in self.__dict__.items(): diff --git a/psa_car_controller/psacc/model/car_model.py b/psa_car_controller/psacc/model/car_model.py index e7348ad..84433be 100644 --- a/psa_car_controller/psacc/model/car_model.py +++ b/psa_car_controller/psacc/model/car_model.py @@ -33,46 +33,3 @@ class ElecModel(CarModel): @property def max_fuel_consumption(self): return 0 - -# car_models = [ -# ElecModel("e-208", 46, "peugeot:e208:20:50", r"VR3UHZKX.*"), -# ElecModel("e-2008", 46, "peugeot:e2008:20:48", r"VR3UKZKX.*"), -# ElecModel("e-Spacetourer", 46, "peugeot:etraveler:21:50:citroen", r"VF7VZZKX.*"), -# ElecModel("e-Traveller", 46, "peugeot:etraveler:21:50", r"VF3VZZKX.*"), -# ElecModel("corsa-e", 46, "opel:corsae:20:50", r"VXKUHZKX.*"), -# ElecModel("DS3 Crossback e-tense", 46, "ds:3crossback:20:48", r"VR1UJZKX.*"), # VR1UJZKXZL -# ElecModel("Mokka-e", 46, "opel:mokkae:20:48", r"VXKUKZKX.*"), # VXKUKZKXZM -# ElecModel("Zaphira-e", 68, "peugeot:etraveler:21:75:opel", r"VXEVZZKX.*"), # VXEVZZKXZMZ -# ElecModel("E-C4", 46, "citroen:ec4:21:50", r"VR7BCZKX.*"), # VR7BCZKXCM -# CarModel("SUV 3008 Hybrid 225", 13.2, 43, reg=r"VF3M4DGZ.*"), # VF3M4DGZUMS -# CarModel("SUV 3008", 0, 53, reg=r"VF3MJEHZ.*"), # VF3MJEHZRJ -# CarModel("308", 0, 56, reg=r"VF3L35GG.*"), -# CarModel("208", 0, 44, reg=r"VR3UPHN[SE].*"), # VR3UPHNSSM VR3UPHNEKM -# CarModel("2008", 0, 44, reg=r"VR3USHNS.*"), # VR3USHNSKM -# CarModel("2008 II", 0, 45, reg=r"VR3USHNK.*"), # VR3USHNKKL -# CarModel("SUV 5008 II", 0, 56, reg=r"VF3MRHNS.*"), # vf3mrhnsum -# CarModel("SUV 5008 II 2018", 0, 56, reg=r"VF3MRHNY.*"), # VF3MRHNYHH -# CarModel("N5008 GT-LINE 1.6L", 0, 60, reg=r"VF3MCBHZ.*"), # VF3MCBHZWJ -# CarModel("C5 Aircross Hybrid", 13.2, 43, reg=r"VR7A4DGZ.*"), # VR7A4DGZSM -# CarModel("DS7 Crossback E-Tense 300 4x4", 11.5, 43, reg="VR1J45GB.*"), # VR1J45GBUM VR1J45GBUL VR1J45GBUK -# CarModel("508 SW Hybrid", 11.5, 45, reg=r"VR3F4DGZ.*"), # VR3F4DGZTL -# CarModel("508 Hybrid", 11.5, 43, reg=r"VR3F3DGZ.*"), # VR3F3DGZTM -# CarModel("508 SW 2.0 HDI 163CV", 0, 72, reg=r"VF38ERHH.*"), # VF38ERHH -# CarModel("Grandland X Hybrid", 13.2, 43, reg=r"W0VZ4DGZ.*"), # W0VZ4DGZ2L -# CarModel("Grandland X Hybrid 4x4", 13.2, 43, reg=r"W0VZ45GB.*") # W0VZ45GB3L -# ] - -# fuel_model = [] -# -# import sys -# -# -# print(yaml.dump(car_models, sys.stdout)) -# with open("car_models.yml", "w") as f: -# yaml.dump(car_models, f) -# -# models_file = "/home/florian/Documents/psa/psa-car-controller/psacc/resources/car_models.yml" -# -# models -# res = yaml.load(models)[0] -# res diff --git a/psa_car_controller/psacc/model/car_status.py b/psa_car_controller/psacc/model/car_status.py index 4901346..579ba0d 100644 --- a/psa_car_controller/psacc/model/car_status.py +++ b/psa_car_controller/psacc/model/car_status.py @@ -28,7 +28,7 @@ class CarStatus(Status): if len(self.last_position.geometry.coordinates) < 3: # set altitude none self.last_position.geometry.coordinates.append(None) - except AttributeError: + except (AttributeError, TypeError): self.last_position = Position(geometry=Geometry(coordinates=[None, None, None], type="Point"), properties=PositionProperties(updated_at=None)) if self.kinetic is None: diff --git a/psa_car_controller/web/assets/clientside.js b/psa_car_controller/web/assets/clientside.js index 656f7df..df96b2e 100644 --- a/psa_car_controller/web/assets/clientside.js +++ b/psa_car_controller/web/assets/clientside.js @@ -236,8 +236,14 @@ function sortMultipleTable (sortParams, data, tables) { function getLastPosition (trips) { const lastPos = {} - lastPos.lat = trips.at(-1).positions.lat[0] - lastPos.lon = trips.at(-1).positions.long[0] + if(Array.isArray(trips) && trips.length > 0){ + lastPos.lat = trips.at(-1).positions.lat[0] + lastPos.lon = trips.at(-1).positions.long[0] + } + else { + lastPos.lat = 40 + lastPos.lon = 40 + } return lastPos }