Skip to content

Commit bccff6d

Browse files
author
Brendan Whitfield
committed
Async merge fixes
1 parent 695e624 commit bccff6d

5 files changed

Lines changed: 42 additions & 21 deletions

File tree

obd/async.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import threading
3434
from utils import Response
3535
from commands import OBDCommand
36+
from debug import debug
37+
3638

3739

3840
class Async(obd.OBD):
@@ -45,40 +47,50 @@ def __init__(self, portstr=None):
4547
self.running = False
4648
self.start()
4749

50+
4851
def start(self):
49-
self.running = True
5052
if self.is_connected():
53+
debug("Starting async thread")
54+
self.running = True
5155
self.thread = threading.Thread(target=self.run)
5256
self.thread.daemon = True
5357
self.thread.start()
58+
else:
59+
debug("Async thread not started because no connection was made")
60+
5461

5562
def stop(self):
56-
self.running = False
5763
if self.thread is not None:
64+
debug("Stopping async thread...")
65+
self.running = False
5866
self.thread.join()
5967
self.thread = None
68+
debug("Async thread stopped")
69+
6070

6171
def close(self):
6272
self.stop()
6373
self.close()
6474

65-
def watch(self, c):
6675

67-
if not isinstance(c, OBDCommand):
68-
return False
76+
def watch(self, c, force=False):
6977

70-
if not self.has_command(c):
78+
if not (self.has_command(c) or force):
79+
debug("'%s' is not supported" % str(c), True)
7180
return False
7281

7382
if not self.commands.has_key(c):
83+
debug("Watching command: %s" % str(c))
7484
self.commands[c] = Response() # give it an initial value
7585

7686
return True
7787

88+
7889
def unwatch(self, c):
90+
debug("Unwatching command: %s" % str(c))
7991
self.commands.pop(c, None)
8092

81-
def get(self, c):
93+
def query(self, c):
8294
if self.commands.has_key(c):
8395
return self.commands[c]
8496
else:
@@ -91,6 +103,6 @@ def run(self):
91103
if len(self.commands) > 0:
92104
# loop over the requested commands, and collect the result
93105
for c in self.commands:
94-
self.commands[c] = self.query(c)
106+
self.commands[c] = self.send(c)
95107
else:
96108
time.sleep(1)

obd/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def set_supported(self, mode, pid, v):
293293
if (mode < len(self.modes)) and (pid < len(self.modes[mode])):
294294
self.modes[mode][pid].supported = v
295295
else:
296-
debug("set_supported only accepts boolean values", True)
296+
debug("set_supported() only accepts boolean values", True)
297297

298298
# checks for existance of int mode and int pid
299299
def has(self, mode, pid):

obd/debug.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030

3131
class Debug():
3232
def __init__(self):
33-
self.console = True
33+
self.console = False
3434
self.handler = None
3535

3636
def __call__(self, msg, forcePrint=False):
3737

3838
if self.console or forcePrint:
39-
print msg
39+
print(msg)
4040

4141
if hasattr(self.handler, '__call__'):
4242
self.handler(msg)

obd/obd.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def connect(self, portstr=None):
7777

7878
def close(self):
7979
if self.is_connected():
80+
debug("Closing connection")
8081
self.port.close()
8182
self.port = None
8283

@@ -108,7 +109,7 @@ def load_commands(self):
108109
if not self.has_command(get):
109110
continue
110111

111-
response = self.query(get) # ask nicely
112+
response = self.send(get) # ask nicely
112113

113114
if response.is_null():
114115
continue
@@ -137,26 +138,34 @@ def print_commands(self):
137138
for c in self.supported_commands:
138139
print str(c)
139140

141+
140142
def has_command(self, c):
141143
return commands.has(c.get_mode_int(), c.get_pid_int()) and c.supported
142144

143-
def query(self, command, force=False):
144-
""" send the given command, retrieve response, and parse response """
145+
146+
def send(self, c):
147+
""" send the given command, retrieve and parse response """
145148

146149
# check for a connection
147150
if not self.is_connected():
148151
debug("Query failed, no connection available", True)
149152
return Response() # return empty response
150153

154+
# send the query
155+
debug("Sending command: %s" % str(c))
156+
self.port.send(c.get_command()) # send command to the port
157+
return c.compute(self.port.get()) # get the data, and compute a response object
158+
159+
160+
def query(self, c, force=False):
161+
151162
# check that the command is supported
152-
if not (self.has_command(command) or force):
153-
debug("'%s' is not supported" % str(command), True)
163+
if not (self.has_command(c) or force):
164+
debug("'%s' is not supported" % str(c), True)
154165
return Response() # return empty response
166+
else:
167+
return self.send(c)
155168

156-
# send the query
157-
debug("Sending command: %s" % str(command))
158-
self.port.send(command.get_command()) # send command to the port
159-
return command.compute(self.port.get()) # get the data, and compute a response object
160169

161170
'''
162171
def query_DTC(self):

obd/port.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def send(self, cmd):
140140
def get(self):
141141
"""Internal use only: not a public interface"""
142142

143-
attempts = 1
143+
attempts = 2
144144
result = ""
145145

146146
if self.port is not None:

0 commit comments

Comments
 (0)