mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-26 10:17:18 +00:00
Merge pull request #117 from flobz/feature-client_filter
Feature client filter
This commit is contained in:
@@ -38,6 +38,12 @@ class Car:
|
||||
def is_hybrid(self) -> bool:
|
||||
return self.fuel_capacity > 0 and self.battery_power > 0
|
||||
|
||||
def has_battery(self):
|
||||
return self.battery_power > 0
|
||||
|
||||
def has_fuel(self):
|
||||
return self.fuel_capacity > 0
|
||||
|
||||
def get_status(self):
|
||||
if self.status is not None:
|
||||
return self.status
|
||||
|
||||
+2
-10
@@ -13,17 +13,9 @@ class Charging:
|
||||
elec_price: ElecPrice = ElecPrice(None)
|
||||
|
||||
@staticmethod
|
||||
def get_chargings(mini=None, maxi=None) -> List[dict]:
|
||||
def get_chargings() -> List[dict]:
|
||||
conn = Database.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()
|
||||
res = conn.execute("select * from battery ORDER BY start_at").fetchall()
|
||||
conn.close()
|
||||
return list(map(dict, res))
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
from functools import wraps
|
||||
from threading import Semaphore, Timer
|
||||
import socket
|
||||
|
||||
import requests
|
||||
|
||||
from mylogger import logger
|
||||
|
||||
|
||||
def get_temp(latitude: str, longitude: str, api_key: str) -> float:
|
||||
try:
|
||||
if not (latitude is None or longitude is None or api_key is None):
|
||||
weather_rep = requests.get("https://api.openweathermap.org/data/2.5/onecall",
|
||||
params={"lat": latitude, "lon": longitude,
|
||||
"exclude": "minutely,hourly,daily,alerts",
|
||||
"appid": api_key,
|
||||
"units": "metric"})
|
||||
temp = weather_rep.json()["current"]["temp"]
|
||||
logger.debug("Temperature :%fc", temp)
|
||||
return temp
|
||||
except ConnectionError:
|
||||
logger.error("Can't connect to openweathermap :", exc_info=True)
|
||||
except KeyError:
|
||||
logger.error("Unable to get temperature from openweathermap :", exc_info=True)
|
||||
return None
|
||||
|
||||
|
||||
def rate_limit(limit, every):
|
||||
def limit_decorator(func):
|
||||
semaphore = Semaphore(limit)
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
semaphore.acquire()
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
finally: # don't catch but ensure semaphore release
|
||||
timer = Timer(every, semaphore.release)
|
||||
timer.setDaemon(True) # allows the timer to be canceled on exit
|
||||
timer.start()
|
||||
|
||||
return wrapper
|
||||
|
||||
return limit_decorator
|
||||
|
||||
|
||||
def is_port_in_use(ip, port):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
return s.connect_ex((ip, port)) == 0
|
||||
Reference in New Issue
Block a user