mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-26 10:17:18 +00:00
add charge price
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
from libs.elec_price import ElecPrice
|
||||
from web.db import get_db, set_chargings_price, clean_battery
|
||||
|
||||
elec_price = ElecPrice.read_config()
|
||||
|
||||
|
||||
class Charging:
|
||||
@staticmethod
|
||||
def get_chargings(mini=None, maxi=None) -> list[dict]:
|
||||
conn = get_db()
|
||||
if mini is not None:
|
||||
if maxi is not None:
|
||||
res = conn.execute("select * from battery WHERE start_at>=? and start_at<=?", (mini, maxi)).fetchall()
|
||||
else:
|
||||
res = conn.execute("select * from battery WHERE start_at>=?", (mini,)).fetchall()
|
||||
elif maxi is not None:
|
||||
res = conn.execute("select * from battery WHERE start_at<=?", (maxi,)).fetchall()
|
||||
else:
|
||||
res = conn.execute("select * from battery").fetchall()
|
||||
conn.close()
|
||||
return list(map(dict, res))
|
||||
|
||||
@staticmethod
|
||||
def set_default_price():
|
||||
if elec_price.is_enable():
|
||||
conn = get_db()
|
||||
charge_list = list(map(dict, conn.execute("SELECT * FROM battery WHERE price IS NULL").fetchall()))
|
||||
for el in charge_list:
|
||||
el["price"] = elec_price.get_price(el["start_at"], el["stop_at"], el["kw"])
|
||||
set_chargings_price(conn, el["start_at"], el["price"])
|
||||
conn.close()
|
||||
|
||||
@staticmethod
|
||||
def update_chargings(conn, start_at, stop_at, level, co2_per_kw, kw, vin):
|
||||
price = elec_price.get_price(start_at, stop_at, kw)
|
||||
conn.execute(
|
||||
"UPDATE battery set stop_at=?, end_level=?, co2=?, kw=?, price=? WHERE start_at=? and VIN=?",
|
||||
(stop_at, level, co2_per_kw, kw, price, start_at, vin))
|
||||
clean_battery(conn)
|
||||
@@ -0,0 +1,94 @@
|
||||
from datetime import datetime, timezone, timedelta
|
||||
import configparser
|
||||
from statistics import mean
|
||||
|
||||
CONFIG_FILENAME = "config.ini"
|
||||
|
||||
|
||||
def set_number(value):
|
||||
try:
|
||||
return float(value)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def utc_to_local(utc_dt):
|
||||
return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
|
||||
|
||||
|
||||
class ElecPrice:
|
||||
currency = ""
|
||||
|
||||
def __init__(self, day_price, night_price=None, nights_hours=None):
|
||||
self.day_price = set_number(day_price)
|
||||
self.night_price = set_number(night_price)
|
||||
self.nights_hour = None
|
||||
self.set_night_hour(nights_hours)
|
||||
self.config_filename = CONFIG_FILENAME
|
||||
|
||||
def set_night_hour(self, value):
|
||||
if value is not None and isinstance(value, list):
|
||||
self.nights_hour = []
|
||||
for hours in value:
|
||||
self.nights_hour.append(list(map(int, hours)))
|
||||
|
||||
def compare_hour(self, date: datetime, hour, minute):
|
||||
if date.hour < hour:
|
||||
return False
|
||||
if date.hour == hour and date.minute < minute:
|
||||
return False
|
||||
return True
|
||||
|
||||
def get_instant_price(self, date):
|
||||
local_date = utc_to_local(date)
|
||||
if self.night_price is None:
|
||||
return self.day_price
|
||||
if self.compare_hour(local_date, self.nights_hour[0][0], self.nights_hour[0][1]) or \
|
||||
not self.compare_hour(local_date, self.nights_hour[1][0], self.nights_hour[1][1]):
|
||||
return self.night_price
|
||||
return self.day_price
|
||||
|
||||
def get_price(self, start, end, consumption):
|
||||
prices = []
|
||||
date = start
|
||||
while date < end:
|
||||
prices.append(self.get_instant_price(date))
|
||||
date = date + timedelta(minutes=30)
|
||||
return round(consumption * mean(prices), 2)
|
||||
|
||||
def is_enable(self):
|
||||
return self.day_price is not None
|
||||
|
||||
@staticmethod
|
||||
def read_config(name=CONFIG_FILENAME):
|
||||
config = configparser.ConfigParser()
|
||||
if len(config.read(name)) == 0:
|
||||
ElecPrice.write_default_config(name)
|
||||
config.read(name)
|
||||
elec_config = config["Electricity config"]
|
||||
if len(elec_config["night price"]) > 0:
|
||||
night_hours = []
|
||||
night_price = elec_config["night price"]
|
||||
for hour in [elec_config["night hour start"], elec_config["night hour end"]]:
|
||||
night_hours.append(hour.split("h"))
|
||||
else:
|
||||
night_hours = None
|
||||
night_price = None
|
||||
ElecPrice.currency = config["General"]["currency"]
|
||||
return ElecPrice(elec_config["day price"], night_price, night_hours)
|
||||
|
||||
@staticmethod
|
||||
def write_default_config(name=CONFIG_FILENAME):
|
||||
config = configparser.ConfigParser()
|
||||
config["General"] = {
|
||||
"currency": "€"
|
||||
}
|
||||
config["Electricity config"] = {
|
||||
"day price": "",
|
||||
"night price": "",
|
||||
"night hour start": "",
|
||||
"night hour end": ""
|
||||
}
|
||||
|
||||
with open(name, "w") as f:
|
||||
config.write(f)
|
||||
Reference in New Issue
Block a user