forked from peter/volvo2mqtt
Reduce api calls in force update mode
This commit is contained in:
+4
-3
@@ -228,7 +228,7 @@ def api_call(url, method, vin, sensor_id=None, force_update=False):
|
|||||||
|
|
||||||
def cached_request(url, method, vin, force_update=False):
|
def cached_request(url, method, vin, force_update=False):
|
||||||
global cached_requests
|
global cached_requests
|
||||||
if not keys_exists(cached_requests, vin + "_" + url) or force_update:
|
if not keys_exists(cached_requests, vin + "_" + url):
|
||||||
# No API Data cached, get fresh data from API
|
# No API Data cached, get fresh data from API
|
||||||
print("Starting " + method + " call against " + url)
|
print("Starting " + method + " call against " + url)
|
||||||
try:
|
try:
|
||||||
@@ -240,8 +240,9 @@ def cached_request(url, method, vin, force_update=False):
|
|||||||
data = {"response": response, "last_update": datetime.now()}
|
data = {"response": response, "last_update": datetime.now()}
|
||||||
cached_requests[vin + "_" + url] = data
|
cached_requests[vin + "_" + url] = data
|
||||||
else:
|
else:
|
||||||
if (datetime.now() - cached_requests[vin + "_" + url]["last_update"]).total_seconds() >= settings["updateInterval"]:
|
if (datetime.now() - cached_requests[vin + "_" + url]["last_update"]).total_seconds() >= settings["updateInterval"] \
|
||||||
# Old Data in Cache, updating
|
or (force_update and (datetime.now() - cached_requests[vin + "_" + url]["last_update"]).total_seconds() >= 2):
|
||||||
|
# Old Data in Cache, or force mode active, updating
|
||||||
print("Starting " + method + " call against " + url)
|
print("Starting " + method + " call against " + url)
|
||||||
try:
|
try:
|
||||||
response = session.get(url.format(vin), timeout=15)
|
response = session.get(url.format(vin), timeout=15)
|
||||||
|
|||||||
Reference in New Issue
Block a user