mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-26 10:17:18 +00:00
update api, update battery record
This commit is contained in:
+12
-6
@@ -16,12 +16,15 @@ from web.app import app, dash_app, myp, chc
|
||||
import web.db
|
||||
|
||||
trips = None
|
||||
chargings = None
|
||||
|
||||
|
||||
@dash_app.callback(Output('trips_map', 'figure'),
|
||||
Output('consumption_fig', 'figure'),
|
||||
Output('consumption_fig_by_speed', 'figure'),
|
||||
Output('consumption', 'children'),
|
||||
Output('tab_trips', 'children'),
|
||||
Output('tab_battery', 'children'),
|
||||
Input('date-slider', 'value'))
|
||||
def display_value(value):
|
||||
min = datetime.fromtimestamp(value[0], tz=timezone.utc)
|
||||
@@ -30,7 +33,8 @@ def display_value(value):
|
||||
for trip in trips:
|
||||
if min <= trip.start_at <= max:
|
||||
filtered_trips.append(trip)
|
||||
figures.get_figures(filtered_trips)
|
||||
filtered_chargings = MyPSACC.get_chargings(min,max)
|
||||
figures.get_figures(filtered_trips,filtered_chargings)
|
||||
consumption = "Average consumption: {:.1f} kW/100km".format(float(figures.consumption_df.mean(numeric_only=True)))
|
||||
return figures.trips_map, figures.consumption_fig, figures.consumption_fig_by_speed, consumption, figures.table_fig
|
||||
|
||||
@@ -107,23 +111,25 @@ def after_request(response):
|
||||
|
||||
|
||||
def update_trips():
|
||||
global trips
|
||||
logger.info("update_trips")
|
||||
global trips, chargings
|
||||
logger.info("update_data")
|
||||
try:
|
||||
trips = MyPSACC.get_trips()
|
||||
chargings = MyPSACC.get_chargings()
|
||||
except:
|
||||
logger.error("update_trips: "+traceback.format_exc())
|
||||
logger.error("update_trips: " + traceback.format_exc())
|
||||
|
||||
|
||||
try:
|
||||
web.db.callback_fct = update_trips
|
||||
update_trips()
|
||||
|
||||
min_date = trips[0].start_at
|
||||
max_date = trips[-1].start_at
|
||||
min_millis = figures.unix_time_millis(min_date)
|
||||
max_millis = figures.unix_time_millis(max_date)
|
||||
step = (max_millis - min_millis) / 100
|
||||
figures.get_figures(trips)
|
||||
figures.get_figures(trips, chargings)
|
||||
data_div = html.Div([dcc.RangeSlider(
|
||||
id='date-slider',
|
||||
min=min_millis,
|
||||
@@ -142,6 +148,7 @@ try:
|
||||
dcc.Graph(figure=figures.consumption_fig_by_speed, id="consumption_fig_by_speed")
|
||||
]),
|
||||
dbc.Tab(label="Trips", tab_id="trips", id="tab_trips", children=[figures.table_fig]),
|
||||
dbc.Tab(label="Battery", tab_id="battery", id="tab_battery", children=[figures.battery_info]),
|
||||
dbc.Tab(label="Map", tab_id="map", children=[
|
||||
dcc.Graph(figure=figures.trips_map, id="trips_map", style={"height": '90vh'})]),
|
||||
],
|
||||
@@ -159,4 +166,3 @@ dash_app.layout = dbc.Container(fluid=True, children=[
|
||||
html.H1('My car info'),
|
||||
data_div
|
||||
])
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ def get_db():
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS position (Timestamp DATETIME PRIMARY KEY, VIN TEXT, longitude REAL, "
|
||||
"latitude REAL, mileage REAL, level INTEGER);")
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS battery (start_at DATETIME PRIMARY KEY,stop_at DATETIME, start_level, "
|
||||
"end_level)")
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS battery (start_at DATETIME PRIMARY KEY,stop_at DATETIME,VIN TEXT, "
|
||||
"start_level INTEGER, end_level INTEGER, co2 INTEGER, kw INTEGER);")
|
||||
conn.create_function("update_trips", 0, update_callback)
|
||||
conn.execute("CREATE TEMP TRIGGER IF NOT EXISTS update_trigger AFTER INSERT ON position BEGIN "
|
||||
"SELECT update_trips(); END;")
|
||||
|
||||
+16
-4
@@ -1,6 +1,7 @@
|
||||
from copy import deepcopy
|
||||
from typing import List
|
||||
|
||||
import dash_bootstrap_components as dbc
|
||||
import dash_table
|
||||
import numpy as np
|
||||
from dash_table.Format import Format, Scheme, Symbol
|
||||
@@ -46,10 +47,11 @@ consumption_fig_by_speed = None
|
||||
table_fig = None
|
||||
pandas_options.display.float_format = '${:.2f}'.format
|
||||
info = ""
|
||||
battery_info = dbc.Alert("No data to show", color="danger")
|
||||
|
||||
|
||||
def get_figures(trips: List[Trip]):
|
||||
global consumption_fig, consumption_df, trips_map, consumption_fig_by_speed, table_fig, info
|
||||
def get_figures(trips: List[Trip], charging: List[dict]):
|
||||
global consumption_fig, consumption_df, trips_map, consumption_fig_by_speed, table_fig, info, battery_info
|
||||
lats = []
|
||||
lons = []
|
||||
names = []
|
||||
@@ -94,5 +96,15 @@ def get_figures(trips: List[Trip]):
|
||||
go.Scatter(mode="markers", x=consum_df_by_speed["speed"], y=consum_df_by_speed["consumption"],
|
||||
name="Trips"))
|
||||
consumption_fig_by_speed.update_layout(xaxis_title="average Speed km/h", yaxis_title="Consumption kWh/100Km")
|
||||
info = "Average consumption: {:.1f} kW/100km".format(
|
||||
float(consumption_df.mean(numeric_only=True)))
|
||||
kw_per_km = float(consumption_df.mean(numeric_only=True))
|
||||
info = "Average consumption: {:.1f} kW/100km".format(kw_per_km)
|
||||
|
||||
# charging
|
||||
charging_data = DataFrame.from_records(charging)
|
||||
co2_per_kw = charging_data["co2"].sum() / charging_data["kw"].sum()
|
||||
co2_per_km = co2_per_kw * kw_per_km / 100
|
||||
charge_speed = 3600 * charging_data["kw"].mean() / \
|
||||
(charging_data["stop_at"] - charging_data["start_at"]).mean().total_seconds()
|
||||
battery_info = "Average gC02/kW: {:.1f}\n" \
|
||||
"Average gC02/km: {:1f}\n" \
|
||||
"Average Charge SPEED {:1f} gC02/kW".format(co2_per_kw, co2_per_km, charge_speed)
|
||||
|
||||
Reference in New Issue
Block a user