From 4db0350dfe0d219837b9b419015dfa2285d065cc Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Tue, 22 Aug 2023 18:55:00 +0300 Subject: [PATCH] - fixes --- src/agent_gsm.py | 6 +++--- src/utils.py | 7 +++++++ src/utils_logcat.py | 6 +++--- src/utils_mcon.py | 10 +++++----- src/utils_types.py | 9 +++------ 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/agent_gsm.py b/src/agent_gsm.py index 8de470b..47dc37f 100644 --- a/src/agent_gsm.py +++ b/src/agent_gsm.py @@ -384,10 +384,10 @@ def run_probe(): break # Process tasks and measure spent time - start_time = time.monotonic() + start_time = utils.get_monotonic_time() for t in TASK_LIST.tasks: - if t["scheduled_time"] <= time.monotonic(): + if t["scheduled_time"] <= utils.get_monotonic_time(): if t["command"] == "call": try: # Remove sheduled time @@ -405,7 +405,7 @@ def run_probe(): except Exception as err: utils.log_error(message="Unexpected error.", err=err) - spent_time = time.monotonic() - start_time + spent_time = utils.get_monotonic_time() - start_time # Wait 1 minute if spent_time < 60: diff --git a/src/utils.py b/src/utils.py index 9aa84f6..9499591 100644 --- a/src/utils.py +++ b/src/utils.py @@ -10,6 +10,7 @@ import smtplib import socket import sox import io +import time from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication @@ -28,6 +29,12 @@ the_log = None # 1 minute network timeout NETWORK_TIMEOUT = 15 +start_system_time = time.time() +start_monotonic_time = time.monotonic() + +def get_monotonic_time(): + return time.monotonic() - start_monotonic_time + start_system_time + def open_log_file(path: str, mode: str): global the_log diff --git a/src/utils_logcat.py b/src/utils_logcat.py index 8a75bd3..648a520 100644 --- a/src/utils_logcat.py +++ b/src/utils_logcat.py @@ -39,14 +39,14 @@ class LogcatEventSource(multiprocessing.Process): process_poll.register(process_logcat.stdout, select.POLLIN) # Monitoring start time - current_timestamp = time.monotonic() + current_timestamp = utils.get_monotonic_time() # Read logcat output line by line while self.terminate_flag.value == 0: # Check if time limit is hit - if time.monotonic() - current_timestamp > self.timelimit: + if utils.get_monotonic_time() - current_timestamp > self.timelimit: break - current_timestamp = time.monotonic() + current_timestamp = utils.get_monotonic_time() # Look for available data on stdout try: diff --git a/src/utils_mcon.py b/src/utils_mcon.py index d6587e5..3f451c5 100644 --- a/src/utils_mcon.py +++ b/src/utils_mcon.py @@ -280,12 +280,12 @@ def gsm_monitor(file_to_play: str, file_to_record: str, on_start, on_finish, on_ utils.log(f'Playing device resolved to {AUDIO_DEV_PLAY}') # Monitoring start time - timestamp_start = time.monotonic() + timestamp_start = utils.get_monotonic_time() # Call start time timestamp_call = None if ROLE == Role.Caller: - timestamp_call = time.monotonic() + timestamp_call = utils.get_monotonic_time() # Should call to be stopped ? force_call_stop = False @@ -295,7 +295,7 @@ def gsm_monitor(file_to_play: str, file_to_record: str, on_start, on_finish, on_ # Read logcat output line by line while True: # Check if time limit is hit - if time.monotonic() - timestamp_start > TIME_LIMIT_MONITORING: + if utils.get_monotonic_time() - timestamp_start > TIME_LIMIT_MONITORING: break # Check if limit of calls hit @@ -304,7 +304,7 @@ def gsm_monitor(file_to_play: str, file_to_record: str, on_start, on_finish, on_ # Check if call hit maximum length - smth goes weird, exit from the script if timestamp_call: - if time.monotonic() - timestamp_call > TIME_LIMIT_CALL: + if util.get_monotonic_time() - timestamp_call > TIME_LIMIT_CALL: utils.log_verbose(f'Call time limit ({TIME_LIMIT_CALL}s). Stop the call.') timestamp_call = None @@ -419,7 +419,7 @@ def gsm_monitor(file_to_play: str, file_to_record: str, on_start, on_finish, on_ utils.log_verbose(f'Detected call start notification from the mobile helper app, trying to start audio.') # Save call start time - timestamp_call = time.monotonic() + timestamp_call = utils.get_monotonic_time() # Is audio failed audio_failed = False diff --git a/src/utils_types.py b/src/utils_types.py index b94f566..3bdc64e 100644 --- a/src/utils_types.py +++ b/src/utils_types.py @@ -6,9 +6,6 @@ import utils import json from crontab import CronTab -start_system_time = time.time() -start_monotonic_time = time.monotonic() - class Phone: identifier: int = 0 @@ -112,20 +109,20 @@ class TaskList: # https://crontab.guru is good for crontab strings generation # Use monotonic time source! - current_time = time.monotonic() + current_time = utils.get_monotonic_time() for task in self.tasks: if 'scheduled_time' not in task and 'schedule' in task: # No schedule flag, so time to schedule try: cron_string = task['schedule'].strip() if cron_string == '* * * * *': - task['scheduled_time'] = time.monotonic() - 0.001 # To ensure further comparison will not be affected by precision errors + task['scheduled_time'] = utils.get_monotonic_time() - 0.001 # To ensure further comparison will not be affected by precision errors else: cron = CronTab(task['schedule']) task['scheduled_time'] = current_time + cron.next(default_utc=True) # Just to help in further log reading & debugging - show the scheduled time in readable form - task['scheduled_time_str'] = time.ctime(task['scheduled_time'] - start_monotonic_time + start_system_time) + task['scheduled_time_str'] = time.ctime(task['scheduled_time']) except: utils.log_error("Error", sys.exc_info()[0])