flobz / psa_car_controller

Control psa car with connected_car v4 API.
GNU General Public License v3.0
415 stars 220 forks source link

Error while processing new position (unsupported operand type(s) for -: 'NoneType' and 'int') #473

Closed albert0815 closed 11 months ago

albert0815 commented 1 year ago

Describe the bug I started to use the psa controller recently and until now I was not able to see the location of my car. I now updated to 3.0.8 because I was hoping that with one of the recent fixes position of the car should be available (which it was not until now). With that release I am able to receive the position with the get_vehicle request, it includes the correct possition. But still neither trips are recorded nor does the map contain the current position. I am assuming it relates to an error in the log which I post below.

To Reproduce Steps to reproduce the behavior:

  1. what command did you use: I am using the docker image with no special parameters.
  2. when the bug append ?
  3. Does it persist after restarting the program? yes
  4. Does it persist after recreate config by going to http://server_address:port/config? yes

Config file

{
    "abrp": {
        "abrp_enable_vin": [],
        "token": ""
    },
    "client_id": "xxx",
    "client_secret": "xxx",
    "co2_signal_api": null,
    "country_code": "DE",
    "customer_id": "xxx",
    "proxies": {
        "http": "",
        "https": ""
    },
    "realm": "clientsB2CPeugeot",
    "refresh_token": "xxx",
    "remote_refresh_token": "xxx",
    "weather_api": null
}

Environment (please complete the following information):

2023-01-23 11:22:05,309 :: INFO :: mqtt msg received: psa/RemoteServices/events/MPHRTServices/xxx b'{"date":"2023-01-23T11:21:04Z","etat_res_elec":0,"precond_state":{"available":1,"programs":{"program1":{"hour":34,"minute":7,"on":0,"day":[0,0,0,0,0,0,0]},"program2":{"hour":34,"minute":7,"on":0,"day":[0,0,0,0,0,0,0]},"program3":{"hour":34,"minute":7,"on":0,"day":[0,0,0,0,0,0,0]},"program4":{"hour":34,"minute":7,"on":0,"day":[0,0,0,0,0,0,0]}},"asap":0,"status":0,"aff":1},"charging_state":{"program":{"hour":0,"minute":0},"available":1,"remaining_time":635,"rate":0,"cable_detected":1,"soc_batt":34,"autonomy_zev":52,"type":0,"aff":1,"hmi_state":0,"mode":2},"stolen_state":0,"vin":"xxx","reason":6,"signal_quality":4,"sev_stop_date":"2023-01-23T11:21:03Z","fds":["NDR01","NAE01","NBM01","NCG01","NAO01","NAS01"],"sev_state":0,"obj_counter":10,"privacy_customer":0,"privacy_applicable":0,"privacy_applicable_max":2,"superlock_state":0}'
2023-01-23 11:22:05,310 :: WARNING :: charge begin but API isn't updated
2023-01-23 11:22:43,661 :: INFO :: new position recorded for xxx
2023-01-23 11:22:43,662 :: INFO :: update_data
2023-01-23 11:22:43,667 :: ERROR :: refresh_vehicle_info:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/psa_client.py", line 125, in __refresh_vehicle_info
self.get_vehicle_info(car.vin)
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/psa_client.py", line 108, in get_vehicle_info
self.record_info(car)
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/psa_client.py", line 194, in record_info
Database.record_position(self.weather_api, car.vin, mileage, latitude, longitude, altitude, date, level,
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/db.py", line 263, in record_position
conn.close()
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/db.py", line 49, in close
self.execute_callbacks()
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/db.py", line 45, in execute_callbacks
callback()
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/web/view/views.py", line 264, in update_trips
trips_by_vin = Trips.get_trips(Cars([car]))
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/trips.py", line 77, in get_trips
if TripParser.is_low_speed(speed_average, duration) or trip_parser.is_refuel(start, end, distance):
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/trip_parser.py", line 63, in __is_recharging
decharge = self.get_level_consumption(start, end)[0]
File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/trip_parser.py", line 32, in get_elec_consumption
return [start[LEVEL] - end[LEVEL], 0]
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

Additional context It seems that for some reason start or end is not available for the calculation of the consumption. Not sure, maybe this is because no trip has been recorded before?

Petapton commented 1 year ago

Hello, I think your issue is related to the car not having finished a trip (or the app not having noticed).

Problem:

I had a similar error when (re)starting my server while my car was charging. In that case I had the same error in dto_to_battery_curve.

...
    File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/battery_charge_curve.py", line 55, in dto_to_battery_curve
    speed = car.get_charge_speed(charge.end_level - charge.start_level, (stop_at - start_date).total_seconds())
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

As an example, here's the db entry I took while charging: 2023-02-18 10:14:24+00:00||VIN|30|||||No Here's the one took after charge stopped: 2023-02-18 10:14:24+00:00|2023-02-18 13:59:07+00:00|VIN|30|51||9.66|1.62|No In this case, neither stop_at nor end_level were set. After I stopped charging and restarted the server, everything went fine.

Solution(s):

  1. Discard unfinished charging sessions and trips when querying (... WHERE stop_at IS NULL OR end_level IS NULL)
  2. Catch TypeError exception and use last position and charge state to go on and compute data

I think the first is more straightforward to implement, but the second is more correct on a theoretical level. There could be also other approaches, but in the end one has to be chosen. I could submit a PR, but before starting to code I'd like to know @flobz's opinion, since I have not a deep knowledge of the project architecture.

flobz commented 11 months ago

I think this issue is fixed please open a new one if not.