mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-22 17:36:15 +00:00
Merge pull request #93 from jlayec/fix-ecomix-nolocation
fix ecomix using default country code and update slider when charging
This commit is contained in:
@@ -48,10 +48,10 @@ class Ecomix:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_data_from_co2_signal(latitude, longitude):
|
||||
def get_data_from_co2_signal(latitude, longitude, country_code_default):
|
||||
if Ecomix.co2_signal_key is not None:
|
||||
try:
|
||||
country_code = Ecomix.get_country(latitude, longitude)
|
||||
country_code = Ecomix.get_country(latitude, longitude, country_code_default)
|
||||
assert country_code is not None
|
||||
if country_code not in Ecomix._cache:
|
||||
Ecomix._cache[country_code] = []
|
||||
@@ -90,19 +90,22 @@ class Ecomix:
|
||||
return mean(co2_per_kw)
|
||||
|
||||
@staticmethod
|
||||
def get_country(latitude, longitude):
|
||||
def get_country(latitude, longitude, country_code_default):
|
||||
try:
|
||||
location = reverse_geocode.search([(latitude, longitude)])[0]
|
||||
country_code = location["country_code"]
|
||||
return country_code
|
||||
except (UnicodeDecodeError, IndexError):
|
||||
logger.error("Can't find country for %s %s", latitude, longitude)
|
||||
return None
|
||||
# return None
|
||||
country_code = country_code_default
|
||||
logger.warning("Using country of origin : %s (wrong co2 when traveling abroad)", country_code)
|
||||
return country_code
|
||||
|
||||
@staticmethod
|
||||
def get_co2_per_kw(start: datetime, end: datetime, latitude, longitude):
|
||||
def get_co2_per_kw(start: datetime, end: datetime, latitude, longitude, country_code_default):
|
||||
co2_per_kw = None
|
||||
country_code = Ecomix.get_country(latitude, longitude)
|
||||
country_code = Ecomix.get_country(latitude, longitude, country_code_default)
|
||||
if country_code is None:
|
||||
return None
|
||||
if Ecomix.co2_signal_key is not None:
|
||||
|
||||
+3
-3
@@ -47,7 +47,7 @@ class Charging:
|
||||
Database.clean_battery(conn)
|
||||
|
||||
@staticmethod
|
||||
def record_charging(car, charging_status, charge_date: datetime, level, latitude, longitude, charging_mode):
|
||||
def record_charging(car, charging_status, charge_date: datetime, level, latitude, longitude, country_code, charging_mode):
|
||||
conn = Database.get_db()
|
||||
charge_date = charge_date.replace(microsecond=0)
|
||||
if charging_status == "InProgress":
|
||||
@@ -64,7 +64,7 @@ class Charging:
|
||||
else:
|
||||
conn.execute("INSERT INTO battery(start_at,start_level,charging_mode,VIN) VALUES(?,?,?,?)",
|
||||
(charge_date, level, charging_mode, car.vin))
|
||||
Ecomix.get_data_from_co2_signal(latitude, longitude)
|
||||
Ecomix.get_data_from_co2_signal(latitude, longitude, country_code)
|
||||
else:
|
||||
try:
|
||||
start_at, stop_at, start_level = conn.execute(
|
||||
@@ -72,7 +72,7 @@ class Charging:
|
||||
"DESC limit 1", (car.vin,)).fetchone()
|
||||
in_progress = stop_at is None
|
||||
if in_progress:
|
||||
co2_per_kw = Ecomix.get_co2_per_kw(start_at, charge_date, latitude, longitude)
|
||||
co2_per_kw = Ecomix.get_co2_per_kw(start_at, charge_date, latitude, longitude, country_code)
|
||||
consumption_kw = (level - start_level) / 100 * car.battery_power
|
||||
|
||||
Charging.update_chargings(conn, start_at, charge_date, level, co2_per_kw, consumption_kw, car.vin)
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
from datetime import datetime, timezone, timedelta
|
||||
import configparser
|
||||
from statistics import mean
|
||||
from statistics import mean, StatisticsError
|
||||
|
||||
from mylogger import logger
|
||||
|
||||
@@ -61,7 +61,7 @@ class ElecPrice:
|
||||
date = date + timedelta(minutes=30)
|
||||
try:
|
||||
res = round(consumption * mean(prices), 2)
|
||||
except TypeError:
|
||||
except (TypeError, StatisticsError):
|
||||
logger.error("Can't get_price of charge, check config")
|
||||
return res
|
||||
|
||||
|
||||
+2
-1
@@ -476,7 +476,8 @@ class MyPSACC:
|
||||
try:
|
||||
charging_status = car.status.get_energy('Electric').charging.status
|
||||
charging_mode = car.status.get_energy('Electric').charging.charging_mode
|
||||
Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, charging_mode)
|
||||
Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, self.country_code,
|
||||
charging_mode)
|
||||
logger.debug("charging_status:%s ", charging_status)
|
||||
except AttributeError:
|
||||
logger.error("charging status not available from api")
|
||||
|
||||
@@ -29,7 +29,7 @@ def parse_args():
|
||||
parser.add_argument("-c", "--charge-control", help="enable charge control, default charge_config.json",
|
||||
const="charge_config.json", nargs='?', metavar='charge config file')
|
||||
parser.add_argument("-d", "--debug", help="enable debug", const=10, default=20, nargs='?',
|
||||
metavar='Debug level number', type=int)
|
||||
metavar='Debug level number or name')
|
||||
parser.add_argument("-l", "--listen", help="change server listen address", default="127.0.0.1", metavar="IP")
|
||||
parser.add_argument("-p", "--port", help="change server listen port", default="5000")
|
||||
parser.add_argument("-r", "--record", help="save vehicle data to db", action='store_true')
|
||||
|
||||
@@ -140,6 +140,15 @@ class Database:
|
||||
return None
|
||||
return res[0]
|
||||
|
||||
@staticmethod
|
||||
def get_range_timestamp():
|
||||
conn = Database.get_db()
|
||||
first = conn.execute("SELECT Timestamp FROM position ORDER BY Timestamp limit 1").fetchone()
|
||||
last = conn.execute("SELECT Timestamp FROM position ORDER BY Timestamp DESC limit 1").fetchone()
|
||||
if first is None or last is None:
|
||||
return None, None
|
||||
return first[0], last[0]
|
||||
|
||||
@staticmethod
|
||||
def set_chargings_price(conn, start_at, price):
|
||||
if isinstance(start_at, str):
|
||||
|
||||
+8
-4
@@ -186,7 +186,7 @@ def get_position(vin):
|
||||
coordinates = res.last_position.geometry.coordinates
|
||||
except AttributeError:
|
||||
return jsonify({'error': 'last_position not available from api'})
|
||||
longitude, latitude, altitude = coordinates[:2]
|
||||
longitude, latitude = coordinates[:2]
|
||||
if len(coordinates) == 3: # altitude is not always available
|
||||
altitude = coordinates[2]
|
||||
else:
|
||||
@@ -250,12 +250,14 @@ def update_trips():
|
||||
chargings = Charging.get_chargings()
|
||||
except (StopIteration, AssertionError):
|
||||
logger.debug("No trips yet")
|
||||
return
|
||||
# return
|
||||
# update for slider
|
||||
global min_date, max_date, min_millis, max_millis, step, marks
|
||||
try:
|
||||
min_date = trips[0].start_at
|
||||
max_date = trips[-1].start_at
|
||||
# min_date = trips[0].start_at
|
||||
# max_date = trips[-1].start_at
|
||||
min_date, max_date = Database.get_range_timestamp()
|
||||
logger.debug("min_date:%s - max_date:%s",min_date, max_date)
|
||||
min_millis = figures.unix_time_millis(min_date)
|
||||
max_millis = figures.unix_time_millis(max_date)
|
||||
step = (max_millis - min_millis) / 100
|
||||
@@ -263,6 +265,8 @@ def update_trips():
|
||||
cached_layout = None # force regenerate layout
|
||||
except (ValueError, IndexError):
|
||||
logger.error("update_trips (slider): %s", exc_info=True)
|
||||
except AttributeError:
|
||||
logger.debug("position table is probably empty :", exc_info=True)
|
||||
return
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user