|
| 1 | + |
| 2 | +######################################################################## |
| 3 | +# # |
| 4 | +# python-OBD: A python OBD-II serial module derived from pyobd # |
| 5 | +# # |
| 6 | +# Copyright 2004 Donour Sizemore (donour@uchicago.edu) # |
| 7 | +# Copyright 2009 Secons Ltd. (www.obdtester.com) # |
| 8 | +# Copyright 2009 Peter J. Creath # |
| 9 | +# Copyright 2015 Brendan Whitfield (bcw7044@rit.edu) # |
| 10 | +# # |
| 11 | +######################################################################## |
| 12 | +# # |
| 13 | +# OBDCommand.py # |
| 14 | +# # |
| 15 | +# This file is part of python-OBD (a derivative of pyOBD) # |
| 16 | +# # |
| 17 | +# python-OBD is free software: you can redistribute it and/or modify # |
| 18 | +# it under the terms of the GNU General Public License as published by # |
| 19 | +# the Free Software Foundation, either version 2 of the License, or # |
| 20 | +# (at your option) any later version. # |
| 21 | +# # |
| 22 | +# python-OBD is distributed in the hope that it will be useful, # |
| 23 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 24 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 25 | +# GNU General Public License for more details. # |
| 26 | +# # |
| 27 | +# You should have received a copy of the GNU General Public License # |
| 28 | +# along with python-OBD. If not, see <http://www.gnu.org/licenses/>. # |
| 29 | +# # |
| 30 | +######################################################################## |
| 31 | + |
| 32 | +import re |
| 33 | +from .utils import * |
| 34 | +from .debug import debug |
| 35 | + |
| 36 | + |
| 37 | +class OBDCommand(): |
| 38 | + def __init__(self, name, desc, mode, pid, returnBytes, decoder, supported=False): |
| 39 | + self.name = name |
| 40 | + self.desc = desc |
| 41 | + self.mode = mode |
| 42 | + self.pid = pid |
| 43 | + self.bytes = returnBytes # number of bytes expected in return |
| 44 | + self.decode = decoder |
| 45 | + self.supported = supported |
| 46 | + |
| 47 | + def clone(self): |
| 48 | + return OBDCommand(self.name, |
| 49 | + self.desc, |
| 50 | + self.mode, |
| 51 | + self.pid, |
| 52 | + self.bytes, |
| 53 | + self.decode) |
| 54 | + |
| 55 | + def get_command(self): |
| 56 | + return self.mode + self.pid # the actual command transmitted to the port |
| 57 | + |
| 58 | + def get_mode_int(self): |
| 59 | + return unhex(self.mode) |
| 60 | + |
| 61 | + def get_pid_int(self): |
| 62 | + return unhex(self.pid) |
| 63 | + |
| 64 | + def __call__(self, message): |
| 65 | + |
| 66 | + # create the response object with the raw data recieved |
| 67 | + # and reference to original command |
| 68 | + r = Response(self, message) |
| 69 | + |
| 70 | + # combine the bytes back into a hex string |
| 71 | + # TODO: rewrite decoders to handle raw byte arrays |
| 72 | + _data = "" |
| 73 | + for b in message.data_bytes[2:]: |
| 74 | + h = hex(b)[2:].upper() |
| 75 | + h = "0" + h if len(h) < 2 else h |
| 76 | + _data += h |
| 77 | + |
| 78 | + # constrain number of bytes in response |
| 79 | + if (self.bytes > 0): # zero bytes means flexible response |
| 80 | + _data = constrainHex(_data, self.bytes) |
| 81 | + |
| 82 | + # decoded value into the response object |
| 83 | + d = self.decode(_data) |
| 84 | + r.value = d[0] |
| 85 | + r.unit = d[1] |
| 86 | + |
| 87 | + return r |
| 88 | + |
| 89 | + def __str__(self): |
| 90 | + return "%s%s: %s" % (self.mode, self.pid, self.desc) |
| 91 | + |
| 92 | + def __hash__(self): |
| 93 | + # needed for using commands as keys in a dict (see async.py) |
| 94 | + return hash((self.mode, self.pid)) |
| 95 | + |
| 96 | + def __eq__(self, other): |
| 97 | + if isinstance(other, OBDCommand): |
| 98 | + return (self.mode, self.pid) == (other.mode, other.pid) |
| 99 | + else: |
| 100 | + return False |
0 commit comments