Skip to content
Newer
Older
100644 157 lines (119 sloc) 4.68 KB
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 16, 2012
1 import elm
0b871ee @djs move elm327 test fixtures to json
authored Jun 16, 2012
2 import json
3 import os
4 import re
5
6 import unittest
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
7 import pytest
8
9 class MockElm327(object):
10 COMMAND_RECEIVED = re.compile('^(.*?)\r', re.M)
11
3b078aa @djs clean up elm test
authored Jul 15, 2012
12 class State(object):
13 RESET = 0
14 NORMAL = 1
15
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
16 def __init__(self):
17 self.in_data = ""
18 self.out_data = ""
19 self.echo = True
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 9, 2013
20 self.response = True
21 self.spaces = True
3b078aa @djs clean up elm test
authored Jul 16, 2012
22 self.state = self.State.RESET
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
23
499cae5 @djs Clean up filehandle pattern
authored May 25, 2015
24 with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fixtures', 'elm.json'), 'r') as fh:
25 self.command_table_h0 = json.load(fh)
0b871ee @djs move elm327 test fixtures to json
authored Jun 17, 2012
26
499cae5 @djs Clean up filehandle pattern
authored May 25, 2015
27 with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fixtures', 'elm_h1.json'), 'r') as fh:
28 self.command_table_h1 = json.load(fh)
5dfe703 @djs Start work on full CAN-bus mode support
authored Aug 19, 2012
29
30 self._set_h0()
31
32 def _set_h1(self):
33 self.headers = True
34 self.command_table = self.command_table_h1
35
36 def _set_h0(self):
37 self.headers = False
38 self.command_table = self.command_table_h0
39
40 def open(self):
41 pass
42
43 def inWaiting(self):
44 return 0
45
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
46 def write(self, data):
47 self.in_data = self.in_data + data
48
49 while True:
50 m = self.COMMAND_RECEIVED.search(self.in_data)
51 if m:
3b078aa @djs clean up elm test
authored Jul 16, 2012
52 command = m.group(1).replace(' ', '').upper()
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
53 #print "old in_data = " + repr(self.in_data)
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
54 self.in_data = self.COMMAND_RECEIVED.sub('', self.in_data)
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
55 #print "new in_data = " + repr(self.in_data)
5dfe703 @djs Start work on full CAN-bus mode support
authored Aug 19, 2012
56
57 if command == 'ATH0':
58 self._set_h0()
59 elif command == 'ATH1':
60 self._set_h1()
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
61 elif command == 'ATD':
62 self._set_h0()
63 elif command == 'ATE1':
64 self.echo = True
65 elif command == 'ATE0':
66 self.echo = False
67 elif command == 'ATR0':
68 self.response = False
69 elif command == 'ATR1':
70 self.response = True
71 elif command == 'ATS0':
72 self.spaces = False
73 elif command == 'ATS1':
74 self.spaces = True
75
76 if self.response:
77 if command in self.command_table:
78 if self.echo:
79 self.out_data = self.out_data + command + '\r'
80
81 if self.spaces:
82 self.out_data = self.out_data + self.command_table[command] + '\r\r>'
83 else:
84 self.out_data = self.out_data + self.command_table[command].strip(' ') + '\r\r>'
85 else:
86 self.out_data = self.out_data + '?' + '\r\r>'
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
87 else:
88 break
89
90
91 def read(self, count):
92 rd = self.out_data[:count]
93 self.out_data = self.out_data[count:]
94
95 return rd
96
97
98 class ElmTests(unittest.TestCase):
99
100 def setup_method(self, method):
101 self.mock = MockElm327()
102 self.elm = elm.Elm(self.mock)
103
104 def test_reset(self):
105 # supported device
106 self.elm.reset()
107
108 # unsupported device
109 self.mock.command_table['ATZ'] = '\r\rELM328 v1.1a'
110 with pytest.raises(Exception):
111 self.elm.reset()
112
113 def test_warm_reset(self):
114 # supported device
115 self.elm.warm_reset()
116
117 # unsupported device
118 self.mock.command_table['ATWS'] = '\r\rELM328 v1.1a'
119 with pytest.raises(Exception):
120 self.elm.warm_reset()
121
122 def test_connect(self):
123 self.elm._connect()
124
125
126 def test_read_voltage(self):
127 data = self.elm.read_voltage()
128 assert data == '12.3V'
129
130 def test_get_device_info(self):
131 self.elm.get_device_info()
132
21c68f2 @djs basic single line obd-ii commands
authored Jun 16, 2012
133 def test_send_obdii_command(self):
134 expected_response = [0x41, 0x0c, 0x1a, 0xf8]
135
3c31da7 @djs WIP: PIDs
authored Jun 18, 2012
136 data = self.elm.send_obdii_command([0x01, 0x0c])
21c68f2 @djs basic single line obd-ii commands
authored Jun 17, 2012
137 assert data == expected_response
138
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
139 class ElmFullTests(unittest.TestCase):
140 def setup_method(self, method):
141 self.mock = MockElm327()
142 self.elm = elm.ElmFull(self.mock)
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
143
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
144 def test_send_obdii_command(self):
145 expected_response = {0x7e8: [0x41, 0x0c, 0x0b, 0xa4, 0x0, 0x0, 0x0],
146 0x7e9: [0x41, 0x0c, 0x0b, 0xa0, 0x0, 0x0, 0x0]}
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
147
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
148 data = self.elm.send_obdii_command([0x01, 0x0c])
149 assert data == expected_response
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
150
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
151 def test_send_obdii_command_with_ml_resp(self):
152 expected_response = {0x7e8: [0x49, 0x02, 0x01, 0x31, 0x46, 0x41, 0x48, 0x50, 0x33, 0x4A, 0x32, 0x31, 0x43, 0x4c, 0x32, 0x31, 0x32, 0x39, 0x37, 0x38]}
d2a958b @djs Add more elm327 commands and add unit tests with mock elm327
authored Jun 17, 2012
153
e710cb7 @djs Add initial support for multi-ecu response/header mode
authored Jun 10, 2013
154 data = self.elm.send_obdii_command([0x09, 0x02])
155 assert data == expected_response
156 print ''.join(chr(i) for i in data[0x7e8][3:])
Something went wrong with that request. Please try again.