From d998b72c42e0c07538cc0dc0e116217cc83ed83d Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Tue, 19 Apr 2022 20:59:05 +0200 Subject: [PATCH] feat: add energy efficiency & custom long trips min dist & fix last postition --- config.ini | 14 ++-- .../psacc/repository/config_repository.py | 16 ++-- psa_car_controller/web/assets/clientside.js | 80 +++++++++++-------- psa_car_controller/web/tools/figurefilter.py | 6 +- psa_car_controller/web/view/views.py | 3 +- tests/test_unit.py | 2 +- 6 files changed, 73 insertions(+), 48 deletions(-) diff --git a/config.ini b/config.ini index 2f21f22..1b16fcd 100644 --- a/config.ini +++ b/config.ini @@ -3,15 +3,17 @@ currency = € # minimum trip length in km so it's added to stats and map in website minimum trip length = 10 +length unit = km [Electricity config] # price by kw/h day price = 0.15 -night price = +night price # ex: 22h30 -night hour start = +night hour start # ex: 6h00 -night hour end = -dc charge price = 0.4 -high speed dc charge price = 0.6 +night hour end +dc charge price +high speed dc charge price # minimum power in kW that should be delivered during a charge so it can be considered as a high speed charger -high speed dc charge threshold = 60 \ No newline at end of file +high speed dc charge threshold +charger efficiency = 0.8942 diff --git a/psa_car_controller/psacc/repository/config_repository.py b/psa_car_controller/psacc/repository/config_repository.py index 31fe3fd..6f0b83e 100644 --- a/psa_car_controller/psacc/repository/config_repository.py +++ b/psa_car_controller/psacc/repository/config_repository.py @@ -29,6 +29,7 @@ dc charge price = high speed dc charge price = # minimum power in kW that should be delivered during a charge so it can be considered as a high speed charger high speed dc charge threshold = +charger efficiency = """ @@ -91,6 +92,7 @@ class ElectricityPriceConfig(BaseModel): dc_charge_price: float = None high_speed_dc_charge_price: float = None high_speed_dc_charge_threshold: float = None + charger_efficiency: float = 0.8942 @staticmethod def compare_hour(date: datetime, hour, minute): @@ -128,7 +130,7 @@ class ElectricityPriceConfig(BaseModel): prices.append(self.get_instant_price(date)) date = date + timedelta(minutes=30) try: - res = round(consumption * mean(prices), 2) + res = round(consumption * mean(prices) / self.charger_efficiency, 2) except (TypeError, StatisticsError): logger.error("Can't get_price of charge, check config") return res @@ -157,15 +159,17 @@ class ConfigRepository(BaseModel): def read_config(name=None) -> 'ConfigRepository': try: config_str = ConfigRepository._read_file(name) - config = ConfigUpdater() + config = ConfigUpdater(allow_no_value=True) config.read_string(config_str) + return ConfigRepository.config_file_to_dto(config) except FileNotFoundError: - config = ConfigRepository.get_default_config() - return ConfigRepository.config_file_to_dto(config) + config = ConfigRepository.config_file_to_dto(ConfigRepository.get_default_config()) + config.write_config() + return config @staticmethod def get_default_config(): - config = ConfigUpdater() + config = ConfigUpdater(allow_no_value=True) config.read_string(DEFAULT_CONFIG) return config @@ -198,7 +202,7 @@ class ConfigRepository(BaseModel): for option in config[section]: new_option = option.replace(" ", "_") value = config[section][option].value - if len(value) > 0: + if value and len(value) > 0: new_dict[new_section][new_option] = value config_obj = ConfigRepository(**new_dict) diff --git a/psa_car_controller/web/assets/clientside.js b/psa_car_controller/web/assets/clientside.js index 8a1d155..ff47b23 100644 --- a/psa_car_controller/web/assets/clientside.js +++ b/psa_car_controller/web/assets/clientside.js @@ -70,9 +70,9 @@ function filterDataset (data, range) { return res } -function filterShortTrip (data) { +function filterShortTrip (data, minimumLength) { const longTrips = { - trips: data.trips.filter(line => line.distance > 10), + trips: data.trips.filter(line => line.distance > minimumLength), chargings: data.chargings } console.log('long trips:', longTrips) @@ -87,31 +87,41 @@ function updateFigures (data, oldFigure, x, y) { const xLabel = x[i] const figure = Object.assign({}, oldFigure[i]) i++ - if ('mapbox' in figure.layout) { - figure.data[0].lat = [] - figure.data[0].lon = [] - figure.data[0].hovertext = [] - let trip = null - for (trip of trips) { - const xPos = trip.positions[xLabel] - figure.data[0].lat.push(...xPos, null) - figure.data[0].lon.push(...trip.positions[yLabel[0]]) - figure.data[0].hovertext.push(...Array(xPos.length).fill(trip[yLabel[1]]), null) - } - if (trip) { - const lastPos = trip.positions[yLabel[0]].length - 1 - figure.layout.mapbox.center.lat = trip.positions[xLabel][lastPos] - figure.layout.mapbox.center.lon = trip.positions[yLabel[0]][lastPos] - figure.data[1].lat = [figure.layout.mapbox.center.lat] - figure.data[1].lon = [figure.layout.mapbox.center.lon] - } - } else { - const xValues = trips.map(a => a[xLabel]) - // for each y label - for (let j = 0; j < yLabel.length; j++) { - figure.data[j].y = trips.map(a => a[yLabel[j]]) - figure.data[j].x = xValues - } + const xValues = trips.map(a => a[xLabel]) + // for each y label + for (let j = 0; j < yLabel.length; j++) { + figure.data[j].y = trips.map(a => a[yLabel[j]]) + figure.data[j].x = xValues + } + console.log(xLabel, figure) + figures.push(figure) + }) + return figures +} + +function updateMap (data, oldFigure, x, y, lastPos) { + const trips = data.trips + const figures = [] + let i = 0 + y.forEach(function (yLabel) { + const xLabel = x[i] + const figure = Object.assign({}, oldFigure[i]) + i++ + figure.data[0].lat = [] + figure.data[0].lon = [] + figure.data[0].hovertext = [] + let trip = null + for (trip of trips) { + const xPos = trip.positions[xLabel] + figure.data[0].lat.push(...xPos, null) + figure.data[0].lon.push(...trip.positions[yLabel[0]]) + figure.data[0].hovertext.push(...Array(xPos.length).fill(trip[yLabel[1]]), null) + } + if (trip) { + figure.layout.mapbox.center.lat = lastPos.lat + figure.layout.mapbox.center.lon = lastPos.lon + figure.data[1].lat = [lastPos.lat] + figure.data[1].lon = [lastPos.lon] } console.log(xLabel, figure) figures.push(figure) @@ -215,7 +225,14 @@ function sortMultipleTable (sortParams, data, tables) { } } -function filterAndSort (data, range, figures, p, log, sort) { // eslint-disable-line no-unused-vars +function getLastPosition (trips) { + const lastPos = {} + lastPos.lat = trips.at(-1).positions.lat[0] + lastPos.lon = trips.at(-1).positions.long[0] + return lastPos +} + +function filterAndSort (data, range, figures, p, log, sort, config) { // eslint-disable-line no-unused-vars if (log > 10) { logger.disableLogger() } @@ -236,12 +253,11 @@ function filterAndSort (data, range, figures, p, log, sort) { // eslint-disable- dataFiltered = filterDataset(data, range) sortMultipleTable(sort, dataFiltered, p.table_src) outFigures.push(...updateTables(dataFiltered, p.table_src)) - console.log(dataFiltered.trips.length) - const longTrips = filterShortTrip(dataFiltered) - console.log('trips', dataFiltered.trips.length) + const longTrips = filterShortTrip(dataFiltered, config.minimumLength) + console.log('trips', dataFiltered.trips) console.log('longTrips', longTrips.trips.length) outFigures.push(...updateFigures(longTrips, figures.graph, p.graph_x_label, p.graph_y_label)) - outFigures.push(...updateFigures(dataFiltered, figures.maps, p.map_x_label, p.map_y_label)) + outFigures.push(...updateMap(dataFiltered, figures.maps, p.map_x_label, p.map_y_label, getLastPosition(dataFiltered.trips))) updateCardsValue(longTrips) } return outFigures diff --git a/psa_car_controller/web/tools/figurefilter.py b/psa_car_controller/web/tools/figurefilter.py index 83cb585..63231c4 100644 --- a/psa_car_controller/web/tools/figurefilter.py +++ b/psa_car_controller/web/tools/figurefilter.py @@ -112,9 +112,10 @@ class FigureFilter: }, indent=4) return params - def set_clientside_callback(self, dash_app): + def set_clientside_callback(self, dash_app, config: dict): callback_id = create_callback_id(self.__get_output()) if callback_id not in dash_app.callback_map: + config_str = json.dumps(config) if logger.isEnabledFor(DEBUG): log_level = 10 else: @@ -122,7 +123,8 @@ class FigureFilter: fct_def = f"""function(data,range, figures, {self.gen_sort_variable()}) {{ const params={self.get_params()} const logLevel={log_level} - return filterAndSort(data, range, figures, params, logLevel, {self.__gen_sort_dict()}) + return filterAndSort(data, range, figures, params, logLevel, + {self.__gen_sort_dict()}, {config_str}) }}""" dash_app.clientside_callback(fct_def, *self.__get_output(), diff --git a/psa_car_controller/web/view/views.py b/psa_car_controller/web/view/views.py index 5bd4841..ac344a0 100644 --- a/psa_car_controller/web/view/views.py +++ b/psa_car_controller/web/view/views.py @@ -327,7 +327,7 @@ def serve_layout(): fig_filter.add_table("trips", figures.table_fig) fig_filter.add_table("chargings", figures.battery_table) fig_filter.src = {"trips": trips.get_trips_as_dict(), "chargings": chargings} - fig_filter.set_clientside_callback(dash_app) + fig_filter.set_clientside_callback(dash_app, {"minimumLength": APP.config.General.minimum_trip_length}) create_callback() except (IndexError, TypeError, NameError, AssertionError, NameError, AttributeError): summary_tab = figures.ERROR_DIV @@ -398,6 +398,7 @@ try: if APP.is_good: Charging.set_default_price(APP.myp.vehicles_list) Database.set_db_callback(update_trips) + figures.CURRENCY = APP.config.General.currency update_trips() except (IndexError, TypeError): logger.debug("Failed to get trips, there is probably not enough data yet:", exc_info=True) diff --git a/tests/test_unit.py b/tests/test_unit.py index f16684c..fc52e6a 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -203,7 +203,7 @@ class TestUnit(unittest.TestCase): 'end_level': 85, 'co2': co2, 'kw': 20.7, - 'price': 3.84, + 'price': 4.29, 'charging_mode': 'slow'}]) assert get_figures(car) row = {"start_at": date0.strftime('%Y-%m-%dT%H:%M:%S.000Z'),