@@ -37,8 +37,9 @@ def fetch_soc(id: str, pw: str, chargepoint: int) -> int:
37
37
return soc
38
38
39
39
40
- def read_token_file () -> dict :
40
+ def read_token_file () -> Union [ int , dict ] :
41
41
global tokenFile
42
+ rc = 0
42
43
try :
43
44
with open (tokenFile , "r" ) as f :
44
45
jsonstr = f .read ()
@@ -49,7 +50,8 @@ def read_token_file() -> dict:
49
50
confDict = {}
50
51
confDict ['configuration' ] = {}
51
52
confDict ['configuration' ]['token' ] = token
52
- return confDict
53
+ rc = 1
54
+ return rc , confDict
53
55
54
56
55
57
def write_token_file (confDict : dict ):
@@ -124,15 +126,16 @@ def main():
124
126
125
127
126
128
# create a new token and store it in the soc_module configuration
127
- def create_token (user_id : str , password : str ) -> str :
129
+ def create_token (user_id : str , password : str , chargepoint : int ) -> str :
128
130
global log , session , token , vehicleId , OVMS_SERVER
129
131
token_url = OVMS_SERVER + TOKEN_CMD
132
+ appl = OVMS_APPL_VALUE + str (chargepoint )
130
133
data = {
131
134
"username" : user_id ,
132
135
"password" : password
133
136
}
134
137
form_data = {
135
- OVMS_APPL_LABEL : OVMS_APPL_VALUE ,
138
+ OVMS_APPL_LABEL : appl ,
136
139
OVMS_PURPOSE_LABEL : OVMS_PURPOSE_VALUE
137
140
}
138
141
try :
@@ -155,7 +158,7 @@ def create_token(user_id: str, password: str) -> str:
155
158
156
159
# check list of token on OVMS server for unused token created by the soc mudule
157
160
# if any obsolete token are found these are deleted.
158
- def cleanup_token (user_id : str , password : str ):
161
+ def cleanup_token (user_id : str , password : str , chargepoint : int ):
159
162
global log , session , token , vehicleId , OVMS_SERVER
160
163
tokenlist_url = OVMS_SERVER + TOKEN_CMD + '?username=' + user_id + '&' + 'password=' + token
161
164
@@ -173,11 +176,12 @@ def cleanup_token(user_id: str, password: str):
173
176
else :
174
177
response = resp .text
175
178
full_tokenlist = loads (response )
179
+ appl = OVMS_APPL_VALUE + str (chargepoint )
176
180
log .debug ("cleanup_token status_code=" +
177
181
str (status_code ) + ", full_tokenlist=\n " +
178
182
dumps (full_tokenlist , indent = 4 ))
179
183
obsolete_tokenlist = list (filter (lambda token :
180
- token [OVMS_APPL_LABEL ] == OVMS_APPL_VALUE and token ["token" ] != token ,
184
+ token [OVMS_APPL_LABEL ] == appl and token ["token" ] != token ,
181
185
full_tokenlist ))
182
186
log .debug ("cleanup_token: obsolete_tokenlist=\n " +
183
187
dumps (obsolete_tokenlist , indent = 4 ))
@@ -231,19 +235,25 @@ def get_status(user_id: str) -> Union[int, dict]:
231
235
def _fetch_soc (user_id : str , password : str , chargepoint : int ) -> int :
232
236
global log , session , token , vehicleId
233
237
234
- confDict = read_token_file ()
235
- tokenstr = confDict ['configuration' ]['token' ]
236
- tokdict = loads (tokenstr )
237
- token = tokdict ['token' ]
238
- log .debug ("read token: " + token )
239
- if token is None or token == "" :
240
- token = create_token (user_id , password )
238
+ try :
239
+ rc , confDict = read_token_file ()
240
+ if rc == 0 :
241
+ tokenstr = confDict ['configuration' ]['token' ]
242
+ tokdict = loads (tokenstr )
243
+ token = tokdict ['token' ]
244
+ log .debug ("read token: " + token )
245
+ if token is None or token == "" :
246
+ token = create_token (user_id , password , chargepoint )
247
+ log .debug ("create_token: " + token )
248
+ except Exception as e :
249
+ log .info ("_fetch_soc exception:" + str (e ) + ", create new token" )
250
+ token = create_token (user_id , password , chargepoint )
241
251
log .debug ("create_token: " + token )
242
252
243
253
log .debug ("call get_status, token:" + token )
244
254
status_code , statusDict = get_status (user_id )
245
255
if status_code > 299 :
246
- token = create_token (user_id , password )
256
+ token = create_token (user_id , password , chargepoint )
247
257
status_code , statusDict = get_status (user_id )
248
258
if status_code > 299 :
249
259
raise "Authentication Problem, status_code " + str (status_code )
@@ -260,7 +270,7 @@ def _fetch_soc(user_id: str, password: str, chargepoint: int) -> int:
260
270
", km-stand=" + str (float (kms )/ 10 ) +
261
271
", soc_12v=" + str (vehicle12v ))
262
272
263
- cleanup_token (user_id , password )
273
+ cleanup_token (user_id , password , chargepoint )
264
274
265
275
return int (float (soc ))
266
276
0 commit comments