Skip to content

Commit d09e3e9

Browse files
authored
Merge pull request #2853 from rleidner/soc_ovms_p1
soc_ovms: fix initial token creation, add cp to token appl
2 parents 4413b87 + 39bd35b commit d09e3e9

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

modules/soc_ovms/soc_ovms.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def fetch_soc(id: str, pw: str, chargepoint: int) -> int:
3737
return soc
3838

3939

40-
def read_token_file() -> dict:
40+
def read_token_file() -> Union[int, dict]:
4141
global tokenFile
42+
rc = 0
4243
try:
4344
with open(tokenFile, "r") as f:
4445
jsonstr = f.read()
@@ -49,7 +50,8 @@ def read_token_file() -> dict:
4950
confDict = {}
5051
confDict['configuration'] = {}
5152
confDict['configuration']['token'] = token
52-
return confDict
53+
rc = 1
54+
return rc, confDict
5355

5456

5557
def write_token_file(confDict: dict):
@@ -124,15 +126,16 @@ def main():
124126

125127

126128
# 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:
128130
global log, session, token, vehicleId, OVMS_SERVER
129131
token_url = OVMS_SERVER + TOKEN_CMD
132+
appl = OVMS_APPL_VALUE + str(chargepoint)
130133
data = {
131134
"username": user_id,
132135
"password": password
133136
}
134137
form_data = {
135-
OVMS_APPL_LABEL: OVMS_APPL_VALUE,
138+
OVMS_APPL_LABEL: appl,
136139
OVMS_PURPOSE_LABEL: OVMS_PURPOSE_VALUE
137140
}
138141
try:
@@ -155,7 +158,7 @@ def create_token(user_id: str, password: str) -> str:
155158

156159
# check list of token on OVMS server for unused token created by the soc mudule
157160
# 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):
159162
global log, session, token, vehicleId, OVMS_SERVER
160163
tokenlist_url = OVMS_SERVER + TOKEN_CMD + '?username=' + user_id + '&' + 'password=' + token
161164

@@ -173,11 +176,12 @@ def cleanup_token(user_id: str, password: str):
173176
else:
174177
response = resp.text
175178
full_tokenlist = loads(response)
179+
appl = OVMS_APPL_VALUE + str(chargepoint)
176180
log.debug("cleanup_token status_code=" +
177181
str(status_code) + ", full_tokenlist=\n" +
178182
dumps(full_tokenlist, indent=4))
179183
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,
181185
full_tokenlist))
182186
log.debug("cleanup_token: obsolete_tokenlist=\n" +
183187
dumps(obsolete_tokenlist, indent=4))
@@ -231,19 +235,25 @@ def get_status(user_id: str) -> Union[int, dict]:
231235
def _fetch_soc(user_id: str, password: str, chargepoint: int) -> int:
232236
global log, session, token, vehicleId
233237

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)
241251
log.debug("create_token: " + token)
242252

243253
log.debug("call get_status, token:" + token)
244254
status_code, statusDict = get_status(user_id)
245255
if status_code > 299:
246-
token = create_token(user_id, password)
256+
token = create_token(user_id, password, chargepoint)
247257
status_code, statusDict = get_status(user_id)
248258
if status_code > 299:
249259
raise "Authentication Problem, status_code " + str(status_code)
@@ -260,7 +270,7 @@ def _fetch_soc(user_id: str, password: str, chargepoint: int) -> int:
260270
", km-stand=" + str(float(kms)/10) +
261271
", soc_12v=" + str(vehicle12v))
262272

263-
cleanup_token(user_id, password)
273+
cleanup_token(user_id, password, chargepoint)
264274

265275
return int(float(soc))
266276

0 commit comments

Comments
 (0)