diff --git a/src/agent_gsm.py b/src/agent_gsm.py index e200e08..1eb28b0 100644 --- a/src/agent_gsm.py +++ b/src/agent_gsm.py @@ -288,11 +288,10 @@ def perform_answerer(): while True: # Remove old recording record_file = f'/dev/shm/bt_record.wav' - - cmd = f'/usr/bin/python3 {DIR_THIS}/bt_call_controller.py --play-file {REFERENCE_AUDIO} --record-file {record_file} --timelimit {int(reference_length)}' - retcode = os.system(cmd) - if retcode != 0: - utils.log(f'Got non-zero exit code {retcode} from BT call controller, exiting.') + try: + bt_call_controller.run(play_file=REFERENCE_AUDIO, record_file=record_file, timelimit_seconds=int(reference_length), target=None) + except Exception as e: + utils.log(f'BT I/O failed, exiting. Error: {str(e)}') break # Call analyzer script diff --git a/src/bt_call_controller.py b/src/bt_call_controller.py index 8001203..551e2d4 100644 --- a/src/bt_call_controller.py +++ b/src/bt_call_controller.py @@ -231,26 +231,23 @@ def get_pid(name): return int(subprocess(["pidof","-s",name])) -def main(args: dict): +def run(play_file: str, record_file: str, timelimit_seconds: int, target: str): global CALL_PATH, CALL_LOCK, CALL_ADDED, CALL_REMOVED # Ensure Ctrl-C handler is default # signal.signal(signal.SIGINT, signal.SIG_DFL) # Check if input file exists - if not os.path.exists(args['play_file']): + if not os.path.exists(play_file): utils.log(f'Problem: file to play ({args["play_file"]}) doesn\'t exists.') exit(os.EX_DATAERR) - # Duration in seconds - watchdog_timeout = int(args['timelimit']) - - if watchdog_timeout == 0: + if timelimit_seconds == 0: # Use duration of played file - audio_file = soundfile.SoundFile(args['play_file']) - watchdog_timeout = int(audio_file.frames / audio_file.samplerate + 0.5) - utils.log(f'Play timeout is set to {watchdog_timeout} seconds') + audio_file = soundfile.SoundFile(play_file) + timelimit_seconds = int(audio_file.frames / audio_file.samplerate + 0.5) + utils.log(f'Play timeout is set to {timelimit_seconds} seconds') # Empty call path means 'no call started' # CALL_LOCK.acquire() @@ -269,15 +266,11 @@ def main(args: dict): PHONE.setup_dbus_loop() # Start call - if 'target' in args: - target_number = args['target'] - if target_number is not None and len(target_number) > 0: - # Make a call - dial_number(target_number, args['play_file']) - else: - answer_call(args['play_file']) + if target is not None and len(target) > 0: + # Make a call + dial_number(target, play_file) else: - answer_call(args['play_file']) + answer_call(play_file) # Don't make volume 100% - that's too much audio_volume = 50 @@ -286,14 +279,14 @@ def main(args: dict): set_headset_mic_volume(audio_volume) # Start recording - utils.log(f'Start recording with ALSA to {args["record_file"]}') - process_recording = capture_phone_alsaoutput(args['record_file']) + utils.log(f'Start recording with ALSA to {record_file}') + process_recording = capture_phone_alsaoutput(record_file) utils.log(f'Main loop PID: {os.getpid()}, TID: {threading.get_ident()}') # Wait until call is finished time_start = time.time() - while not CALL_REMOVED.value and time_start + watchdog_timeout > time.time(): + while not CALL_REMOVED.value and time_start + timelimit_seconds > time.time(): time.sleep(0.5) utils.log(f'Call {CALL_PATH} finished.') @@ -319,7 +312,7 @@ if __name__ == "__main__": retcode = 0 try: - main(args) + run(args['play_file'], args['record_file'], args['timelimit'], args['target'] if 'target' in args else None) except KeyboardInterrupt as e: print('Ctrl-C pressed, exiting') cleanup()