From d75574523d3b273c40fb714532c4de27f9e6dd3e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 7 Jan 2021 18:11:46 +0100 Subject: [PATCH] Replace time.clock() with time.process_time() --- cantoolz/modules/fuzz.py | 6 +++--- cantoolz/modules/io/hw_USBtin.py | 12 ++++++------ cantoolz/modules/ping.py | 6 +++--- cantoolz/modules/replay.py | 2 +- cantoolz/modules/vircar/anti_theft_2.py | 6 +++--- cantoolz/modules/vircar/ecu_door.py | 6 +++--- cantoolz/modules/vircar/ecu_engine.py | 10 +++++----- cantoolz/modules/vircar/ecu_light.py | 6 +++--- cantoolz/replay.py | 8 ++++---- tests/modules/test_replay.py | 8 ++++---- 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/cantoolz/modules/fuzz.py b/cantoolz/modules/fuzz.py index 20cf23f..6d2364d 100644 --- a/cantoolz/modules/fuzz.py +++ b/cantoolz/modules/fuzz.py @@ -59,7 +59,7 @@ def fuzz(self, fuzz_list, idf, data, bytes_to_fuzz, level, iso_mode): def do_start(self, args): self._i = 0 - self.last = time.clock() + self.last = time.process_time() self.queue_messages = [] iso_mode = 1 if args.get('mode') in ['ISO', 'iso', 'ISOTP', 'isotp'] else 0 if 'id' in args: @@ -100,8 +100,8 @@ def do_effect(self, can_msg, args): elif not can_msg.CANData: d_time = float(args.get('delay', 0)) if d_time > 0: - if time.clock() - self.last >= d_time: - self.last = time.clock() + if time.process_time() - self.last >= d_time: + self.last = time.process_time() can_msg.CANFrame = self.queue_messages.pop() can_msg.CANData = True can_msg.bus = self._bus diff --git a/cantoolz/modules/io/hw_USBtin.py b/cantoolz/modules/io/hw_USBtin.py index 2243551..504a148 100644 --- a/cantoolz/modules/io/hw_USBtin.py +++ b/cantoolz/modules/io/hw_USBtin.py @@ -155,7 +155,7 @@ def do_start(self, params): # enable reading self.dev_write("l") self._run = True self.wait_for = False - self.last = time.clock() + self.last = time.process_time() time.sleep(1) def init_port(self): @@ -203,7 +203,7 @@ def do_init(self, params): # Get device and open serial port self._usbtin_loop = bool(params.get('usbtin_loop', False)) self._restart = bool(params.get('auto_activate', False)) self.act_time = float(params.get('auto_activate', 5.0)) - self.last = time.clock() + self.last = time.process_time() self.wait_for = False self._run = True self.do_stop({}) @@ -249,9 +249,9 @@ def do_effect(self, can_msg, args): # read full packet from serial port can_msg = self.do_read(can_msg) elif args.get('action') == 'write': # KOSTYL: workaround for BMW f10 bus - # if self._restart and self._run and (time.clock() - self.last) >= self.act_time: + # if self._restart and self._run and (time.process_time() - self.last) >= self.act_time: # self.dev_write("O") - # self.last = time.clock() + # self.last = time.process_time() self.do_write(can_msg) else: self.dprint(1, 'Command ' + args.get('action', 'NONE') + ' not implemented 8(') @@ -271,10 +271,10 @@ def do_effect(self, can_msg, args): # read full packet from serial port can_msg.debugText['please_send'] = True self.wait_for = False - elif time.clock() - self.last >= self.act_time and not self.wait_for: + elif time.process_time() - self.last >= self.act_time and not self.wait_for: self.dev_write("F") self.wait_for = True - self.last = time.clock() + self.last = time.process_time() """ return can_msg diff --git a/cantoolz/modules/ping.py b/cantoolz/modules/ping.py index a2ad269..ca022f0 100644 --- a/cantoolz/modules/ping.py +++ b/cantoolz/modules/ping.py @@ -82,7 +82,7 @@ def _get_range(data): def do_start(self, args): self.queue_messages = [] - self.last = time.clock() + self.last = time.process_time() data = [0, 0, 0, 0, 0, 0, 0, 0] if 'body' in args: @@ -127,8 +127,8 @@ def do_effect(self, can_msg, args): d_time = float(args.get('delay', 0)) if not can_msg.CANData: if d_time > 0: - if time.clock() - self.last >= d_time: - self.last = time.clock() + if time.process_time() - self.last >= d_time: + self.last = time.process_time() can_msg.CANFrame = self.do_ping(args) else: diff --git a/cantoolz/modules/replay.py b/cantoolz/modules/replay.py index 8cdc7f4..765d021 100644 --- a/cantoolz/modules/replay.py +++ b/cantoolz/modules/replay.py @@ -43,7 +43,7 @@ def cmd_load(self, name): def do_init(self, params): self.CANList = Replay() - self.last = time.clock() + self.last = time.process_time() self._last = 0 self._full = 1 self._num1 = 0 diff --git a/cantoolz/modules/vircar/anti_theft_2.py b/cantoolz/modules/vircar/anti_theft_2.py index fe45c39..eaada77 100644 --- a/cantoolz/modules/vircar/anti_theft_2.py +++ b/cantoolz/modules/vircar/anti_theft_2.py @@ -35,7 +35,7 @@ def do_init(self, params): cmds = self._status2['stop_engine'].split(':') self.can_stop = CANMessage(int(cmds[0], 16) if cmds[0][0:2] == '0x' else int(cmds[0]), int(cmds[1]), bytes.fromhex(cmds[2]), False, CANMessage.DataFrame) self._try = 0 - self.last = time.clock() + self.last = time.process_time() def do_stop(self, params): self._current = 1 # 1 - blocked, 2 - ALERT, 3 - authenticated @@ -45,12 +45,12 @@ def do_stop(self, params): # Effect (could be fuzz operation, sniff, filter or whatever) def do_effect(self, can_msg, args): - if self.stop and not can_msg.CANData and time.clock() - self.last > 1.5 and args.get('action', '') == 'write' and self._current != 3: + if self.stop and not can_msg.CANData and time.process_time() - self.last > 1.5 and args.get('action', '') == 'write' and self._current != 3: can_msg.CANData = True can_msg.CANFrame = self.can_stop if self._current != 2: self.stop = False - self.last = time.clock() + self.last = time.process_time() # FILTER elif can_msg.CANData and self._current != 3 and args.get('action', '') == 'read': diff --git a/cantoolz/modules/vircar/ecu_door.py b/cantoolz/modules/vircar/ecu_door.py index 0272538..35f27ec 100644 --- a/cantoolz/modules/vircar/ecu_door.py +++ b/cantoolz/modules/vircar/ecu_door.py @@ -37,7 +37,7 @@ def do_init(self, params): self._params = params self._params['status'] = 'Locked' self._frames = [] - self._last_sent = time.clock() + self._last_sent = time.process_time() def do_start(self, params): self._params['status'] = 'Locked' @@ -57,7 +57,7 @@ def do_effect(self, can_msg, args): self._last_sent -= self._params.get('reports_delay', 1.0) elif args['action'] == 'write' and not can_msg.CANData and self._params['status'] != 'Turned off': # Write - if (time.clock() - self._last_sent) >= self._params.get('reports_delay', 1.0): + if (time.process_time() - self._last_sent) >= self._params.get('reports_delay', 1.0): can_msg.CANFrame = CANMessage( self._params.get('id_report', 0xffff), len(self._params.get('reports', {}).get(self._params['status'], '')) / 2, @@ -66,6 +66,6 @@ def do_effect(self, can_msg, args): ) can_msg.CANData = True can_msg.bus = self._bus - self._last_sent = time.clock() + self._last_sent = time.process_time() return can_msg diff --git a/cantoolz/modules/vircar/ecu_engine.py b/cantoolz/modules/vircar/ecu_engine.py index 4805c41..20221c3 100644 --- a/cantoolz/modules/vircar/ecu_engine.py +++ b/cantoolz/modules/vircar/ecu_engine.py @@ -54,8 +54,8 @@ def do_init(self, params): self.frames = [] self.init_sess = None self.init_sess2 = None - self._last_sent = time.clock() - self.vin_gen = [time.clock(), time.clock(), 0.7, 2.9] + self._last_sent = time.process_time() + self.vin_gen = [time.process_time(), time.process_time(), 0.7, 2.9] left = len(self._vin) count = int(left / 7) + 1 @@ -93,17 +93,17 @@ def generate_rpm(self): self.rpm_down = 0 if self._status2['status'] in [2, 3, 1]: - if (time.clock() - self._last_sent) >= self._status2.get('reports_delay', 0.5): + if (time.process_time() - self._last_sent) >= self._status2.get('reports_delay', 0.5): self.frames.append( CANMessage( self._status2.get('id_report', 0xffff), 3, self._status2['status'].to_bytes(1, byteorder='big') + self._status2['rpm'].to_bytes(2, byteorder='big'), False, CANMessage.DataFrame)) - self._last_sent = time.clock() + self._last_sent = time.process_time() def generate_vin(self): - curr_t = time.clock() + curr_t = time.process_time() if self.vin_gen[0] == 0 or curr_t - self.vin_gen[0] > self.vin_gen[2]: if self.vin_gen[1] == 0 or curr_t - self.vin_gen[1] > self.vin_gen[3]: self.frames.append(CANMessage.init_data(self._status2.get('vin_id', 1), len(self.vin[self.count_part]), self.vin[self.count_part])) diff --git a/cantoolz/modules/vircar/ecu_light.py b/cantoolz/modules/vircar/ecu_light.py index c5f41c9..e1936b9 100644 --- a/cantoolz/modules/vircar/ecu_light.py +++ b/cantoolz/modules/vircar/ecu_light.py @@ -38,7 +38,7 @@ def do_init(self, params): self._params = params self._params['status'] = 'Lights OFF' self._frames = [] - self._last_sent = time.clock() + self._last_sent = time.process_time() def do_start(self, params): self._params['status'] = 'Lights OFF' @@ -61,7 +61,7 @@ def do_effect(self, can_msg, args): self._last_sent -= self._params.get('reports_delay', 1.0) elif args['action'] == 'write' and not can_msg.CANData and self._params['status'] != 'Turned off': # Write - if (time.clock() - self._last_sent) >= self._params.get('reports_delay', 1.0): + if (time.process_time() - self._last_sent) >= self._params.get('reports_delay', 1.0): can_msg.CANFrame = CANMessage( self._params.get('id_report', 0xffff), len(self._params.get('reports', {}).get(self._params['status'], '')) / 2, @@ -69,6 +69,6 @@ def do_effect(self, can_msg, args): CANMessage.DataFrame) can_msg.CANData = True can_msg.bus = self._bus - self._last_sent = time.clock() + self._last_sent = time.process_time() return can_msg diff --git a/cantoolz/replay.py b/cantoolz/replay.py index 587f05f..284e8ba 100644 --- a/cantoolz/replay.py +++ b/cantoolz/replay.py @@ -8,7 +8,7 @@ class Replay: def __init__(self): self._stream = [] - self._last = time.clock() + self._last = time.process_time() self._curr = 0 self._pre_last = 0 self._shift = 0 @@ -16,7 +16,7 @@ def __init__(self): self.add_timestamp() def reset(self): - self._last = time.clock() + self._last = time.process_time() self._curr = 0 self._pre_last = 0 @@ -25,10 +25,10 @@ def stream(self): return copy.deepcopy(self._stream) def passed_time(self): - return time.clock() + self._shift - self._last + return time.process_time() + self._shift - self._last def restart_time(self, shift=.0): - self._last = time.clock() - shift + self._last = time.process_time() - shift def append_time(self, times, can_msg): if can_msg.CANData: diff --git a/tests/modules/test_replay.py b/tests/modules/test_replay.py index 3368206..7f85068 100644 --- a/tests/modules/test_replay.py +++ b/tests/modules/test_replay.py @@ -202,7 +202,7 @@ def test_replay_timestamp(self): # Get number of loaded CAN packets from replay. num = self.CANEngine.call_module(0, 'p') self.assertTrue(0 <= num.find("Loaded packets: 11"), "Should be 11 packets") - time1 = time.clock() + time1 = time.process_time() # Replay loaded CAN packets with replay. self.CANEngine.call_module(0, 'r') while self.CANEngine.actions[0][1]._status < 100.0: @@ -210,7 +210,7 @@ def test_replay_timestamp(self): # Print table of CAN messages sniffed by analyze. ret = self.CANEngine.call_module(1, 'p') print(ret) - time2 = time.clock() + time2 = time.process_time() print("TIME: " + str(time2 - time1)) # Get current status of analyze st = self.CANEngine.call_module(1, 'S') @@ -227,7 +227,7 @@ def test_replay_timestamp2(self): # Get number of loaded CAN packets from replay. num = self.CANEngine.call_module(0, 'p') self.assertTrue(0 <= num.find("Loaded packets: 11"), "Should be 11 packets") - time1 = time.clock() + time1 = time.process_time() # Replay loaded CAN packets with replay. self.CANEngine.call_module(0, 'r') while self.CANEngine.actions[0][1]._status < 100.0: @@ -235,7 +235,7 @@ def test_replay_timestamp2(self): # Print table of CAN messages sniffed by analyze. ret = self.CANEngine.call_module(1, 'p') print(ret) - time2 = time.clock() + time2 = time.process_time() print("TIME: " + str(time2 - time1)) self.assertTrue(13.99 < time2 - time1 < 15.99, "Should be around 14 seconds") # Get current status of analyze