Skip to content

Commit

Permalink
soc_smarteq: fix token comparison, handle missing store file (#2569)
Browse files Browse the repository at this point in the history
Co-authored-by: rleidner <[email protected]>
  • Loading branch information
rleidner and rleidner authored Jan 3, 2023
1 parent 917d3db commit e52f25d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions modules/soc_smarteq/soc_smarteq.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pkce
import logging
import pickle
import copy

# Constants
BASE_URL = "https://id.mercedes-benz.com"
Expand Down Expand Up @@ -70,16 +71,12 @@ def __init__(self, storeFile: str):
# currently is contains:
# Tokens: refresh- and access-tokens of OAUTH
# refresh_timestamp: epoch of last refresh_tokens.
self.store = {}
self.store['Tokens'] = {}
self.store['refresh_timestamp'] = int(0)

self.session = requests.session()

self.load_store()
self.oldTokens = self.store['Tokens']
self.oldTokens = copy.deepcopy(self.store['Tokens'])
self.init = True
self.init = False # test!!

def load_store(self):
try:
Expand All @@ -89,8 +86,14 @@ def load_store(self):
self.store['Tokens'] = {}
self.store['refresh_timestamp'] = int(0)
tf.close()
except FileNotFoundError:
self.log.warning("init: no store file found, full reconnect required")
self.store = {}
self.store['Tokens'] = {}
self.store['refresh_timestamp'] = int(0)
except Exception as e:
self.log.debug("init: loading stored data failed, file: " + self.storeFile)
self.store = {}
self.store['Tokens'] = {}
self.store['refresh_timestamp'] = int(0)

Expand Down Expand Up @@ -416,8 +419,8 @@ def fetch_soc(self) -> int:

if self.store['Tokens'] != self.oldTokens:
self.log.debug("reconnect: tokens changed, store token file")
self.write_store()

self.write_store()
return soc


Expand Down

0 comments on commit e52f25d

Please sign in to comment.