Skip to content

Commit 30bfddd

Browse files
author
Brendan Whitfield
committed
always return tuple as a response
1 parent 071c7dc commit 30bfddd

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

obd/obd.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -299,34 +299,36 @@ def query_multi(self, *cmds, **kwargs):
299299
the car for CAN ONLY, and protects against sending
300300
unsupported commands.
301301
302-
returns a tuple of OBDCommand objects in the order
302+
returns a tuple of OBDResponse objects in the order
303303
of *cmds
304304
"""
305305
force = kwargs.pop("force", False)
306306

307+
# setup a dict with empty responses for each command
308+
responses = { cmd:OBDResponse() for cmd in cmds }
309+
310+
# helper function to convert the responses dict into a tuple
311+
response = lambda: tuple(responses[cmd] for cmd in cmds)
312+
307313
if self.status() == OBDStatus.NOT_CONNECTED:
308314
logger.warning("Query failed, no connection available")
309-
return OBDResponse()
315+
return response()
310316
elif self.interface.protocol_id() not in ["6", "7", "8", "9"]:
311-
logger.warning("Multiple PID requests are only supported in"
312-
" CAN mode")
313-
return OBDResponse()
314-
elif len(cmds) > 6:
315-
logger.warning("Query failed, too many PIDs requested")
316-
return OBDResponse()
317-
elif len(cmds) == 0:
318-
logger.warning("Query failed, zero PIDs requested")
319-
return OBDResponse()
317+
logger.warning("Multiple PID requests are only supported in CAN mode")
318+
return response()
319+
elif (len(cmds) == 0) or (len(cmds) > 6):
320+
logger.warning("query_multi accepts between 1 and 6 commands")
321+
return response()
320322

321323
# check each command for support
322324
# skip tests if forced
323325
if not force and not all([self.test_cmd(cmd) for cmd in cmds]):
324-
return
326+
return response()
325327

326328
# check that all commands are of the same mode
327329
if not all([cmd.mode == cmds[0].mode for cmd in cmds]):
328330
logger.warning("commands for query_multi() must be of the same mode")
329-
return
331+
return response()
330332

331333
# build the request
332334
cmd_string = cmds[0].command[:2] # mode part
@@ -338,7 +340,7 @@ def query_multi(self, *cmds, **kwargs):
338340

339341
if not messages:
340342
logger.info("No valid OBD Messages returned")
341-
return OBDResponse()
343+
return response()
342344

343345
# parse through the returned message finding the associated command
344346
# and how many bytes the command response is. then construct a response
@@ -347,7 +349,6 @@ def query_multi(self, *cmds, **kwargs):
347349
mode = master_blaster[0].data.pop(0) # the mode byte (ie, for mode 01 this would be 0x41)
348350

349351
cmds_by_pid = { cmd.pid:cmd for cmd in cmds }
350-
responses = { cmd:OBDResponse() for cmd in cmds }
351352

352353
for master in master_blaster:
353354
while len(master.data) > 0:
@@ -360,15 +361,15 @@ def query_multi(self, *cmds, **kwargs):
360361
if cmd is None:
361362
logger.warning("query_multi encountered unexpected PID: %s" % hex(pid)[2:])
362363
break
363-
364+
364365
l = cmd.bytes - 1 # this figure INCLUDES the PID byte
365-
366+
366367
# if the message doesn't have enough data left in it to fulfill a
367368
# PID, then abort, and proceed with whatever we've decoded so far
368369
if l > len(master.data):
369370
logger.warning("query_multi did not recieve enough data")
370371
break
371-
372+
372373
# construct a new message
373374
message = Message(master.frames) # copy of the original lines
374375
message.ecu = master.ecu
@@ -382,4 +383,4 @@ def query_multi(self, *cmds, **kwargs):
382383
master.data = master.data[l:]
383384

384385
# return responses in the order that they were specified
385-
return tuple(responses[cmd] for cmd in cmds)
386+
return response()

0 commit comments

Comments
 (0)