mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-21 17:06:18 +00:00
Merge pull request #51 from jlayec/fix-nolocation
fix when last_position is not available from api
This commit is contained in:
+15
-4
@@ -514,15 +514,24 @@ class MyPSACC:
|
||||
self._record_enabled = value
|
||||
|
||||
def record_info(self, vin, status: psac.models.status.Status):
|
||||
longitude = status.last_position.geometry.coordinates[0]
|
||||
latitude = status.last_position.geometry.coordinates[1]
|
||||
date = status.last_position.properties.updated_at
|
||||
mileage = status.timed_odometer.mileage
|
||||
level = status.get_energy('Electric').level
|
||||
charging_status = status.get_energy('Electric').charging.status
|
||||
charge_date = status.get_energy('Electric').updated_at
|
||||
level_fuel = status.get_energy('Fuel').level
|
||||
moving = status.kinetic.moving
|
||||
try:
|
||||
longitude = status.last_position.geometry.coordinates[0]
|
||||
latitude = status.last_position.geometry.coordinates[1]
|
||||
date = status.last_position.properties.updated_at
|
||||
except AttributeError:
|
||||
logger.error("last_position not available from api")
|
||||
longitude = latitude = None
|
||||
date = charge_date
|
||||
try:
|
||||
moving = status.kinetic.moving
|
||||
except AttributeError:
|
||||
logger.error("kinetic not available from api")
|
||||
moving = None
|
||||
logger.debug(
|
||||
"vin:%s longitude:%s latitude:%s date:%s mileage:%s level:%s charging_status:%s charge_date:%s level_fuel:"
|
||||
"%s moving:%s", vin, longitude, latitude, date, mileage, level, charging_status, charge_date, level_fuel,
|
||||
@@ -610,6 +619,8 @@ class MyPSACC:
|
||||
res = conn.execute('SELECT * FROM position ORDER BY Timestamp')
|
||||
features_list = []
|
||||
for row in res:
|
||||
if row["longitude"] is None or row["latitude"] is None:
|
||||
continue
|
||||
feature = Feature(geometry=Point((row["longitude"], row["latitude"])),
|
||||
properties={"vin": row["vin"], "date": row["Timestamp"].strftime("%x %X"),
|
||||
"mileage": row["mileage"],
|
||||
|
||||
@@ -38,8 +38,11 @@ class Ecomix:
|
||||
|
||||
@staticmethod
|
||||
def get_co2_per_kw(start: datetime, end: datetime, latitude, longitude):
|
||||
location = reverse_geocode.search([(latitude, longitude)])[0]
|
||||
country_code = location["country_code"]
|
||||
try:
|
||||
location = reverse_geocode.search([(latitude, longitude)])[0]
|
||||
country_code = location["country_code"]
|
||||
except IndexError:
|
||||
country_code = None
|
||||
# todo implement other countries
|
||||
if country_code == 'FR':
|
||||
co2_per_kw = Ecomix.get_data_france(start, end)
|
||||
|
||||
+10
-5
@@ -84,16 +84,21 @@ def preconditioning(vin, activate):
|
||||
@app.route('/position/<string:vin>')
|
||||
def get_position(vin):
|
||||
res = myp.get_vehicle_info(vin)
|
||||
coordinates = res.last_position.geometry.coordinates
|
||||
try:
|
||||
coordinates = res.last_position.geometry.coordinates
|
||||
except AttributeError:
|
||||
coordinates = []
|
||||
if len(coordinates) == 3: # altitude is not always availlable
|
||||
longitude, latitude, altitude = coordinates
|
||||
return jsonify(
|
||||
{"longitude": longitude, "latitude": latitude, "altitude": altitude,
|
||||
"url": f"http://maps.google.com/maps?q={latitude},{longitude}"})
|
||||
longitude, latitude = coordinates
|
||||
return jsonify(
|
||||
{"longitude": longitude, "latitude": latitude,
|
||||
"url": f"http://maps.google.com/maps?q={latitude},{longitude}"})
|
||||
if len(coordinates) == 2:
|
||||
longitude, latitude = coordinates
|
||||
return jsonify(
|
||||
{"longitude": longitude, "latitude": latitude,
|
||||
"url": f"http://maps.google.com/maps?q={latitude},{longitude}"})
|
||||
return jsonify({'error':'last_position not available from api'})
|
||||
|
||||
|
||||
# Set a battery threshold and schedule an hour to stop the charge
|
||||
|
||||
Reference in New Issue
Block a user