diff --git a/scripts/python/API/api.py b/scripts/python/API/api.py index 60a97f3f..8f2e6adb 100644 --- a/scripts/python/API/api.py +++ b/scripts/python/API/api.py @@ -157,17 +157,7 @@ class API: except Exception as e: # Log the error but don't crash the update process self.logger.error(f"Error in callback: {e}") - - def register_on_update_callback(self, callback): - """ - Register a callback function to be called on each update. - - Args: - callback (function): The function to call on update. Can be sync or async. - The function should accept a single argument, which is the API instance. - """ - self.on_update_callback = callback - + async def _run_async(self): """ Async implementation of the API service loop. @@ -203,6 +193,16 @@ class API: finally: self.logger.info("API stopped") self.running = False + + def register_on_update_callback(self, callback): + """ + Register a callback function to be called on each update. + + Args: + callback (function): The function to call on update. Can be sync or async. + The function should accept a single argument, which is the API instance. + """ + self.on_update_callback = callback def unregister_on_update_callback(self): """ diff --git a/scripts/python/API/example_disembarked_infantry.py b/scripts/python/API/example_disembarked_infantry.py index 02c5bada..4f288bad 100644 --- a/scripts/python/API/example_disembarked_infantry.py +++ b/scripts/python/API/example_disembarked_infantry.py @@ -12,7 +12,7 @@ formatter = logging.Formatter('[%(asctime)s] %(name)s - %(levelname)s - %(messag handler.setFormatter(formatter) logger.addHandler(handler) -units_to_delete = None +units_to_delete = [] ############################################################################################# # This class represents a disembarked infantry unit that will engage in combat @@ -28,7 +28,7 @@ class DisembarkedInfantry(Unit): with the closest enemy unit. """ logger.info(f"Unit {self.unit_id} is now fighting.") - + # Pick a random target target = self.pick_random_target() @@ -86,7 +86,7 @@ class DisembarkedInfantry(Unit): # Simulate a firefight in the direction of the enemy firefight_destination = self.position.project_with_bearing_and_distance(30, bearing_to_enemy) - self.simulate_fire_fight(firefight_destination.lat, firefight_destination.lng, firefight_destination.alt + 1) + self.simulate_fire_fight(firefight_destination, firefight_destination.alt + 1) await asyncio.sleep(10) # Simulate some time spent in firefight self.start_fighting() # Restart the fighting process @@ -109,8 +109,8 @@ def on_api_startup(api: API): if unit.alive and not unit.human and unit.coalition == "blue": units_to_delete.append(unit) try: - unit.delete_unit(False, "", True) unit.register_on_property_change_callback("alive", on_unit_alive_change) + unit.delete_unit(False, "", True) logger.info(f"Deleted unit: {unit}") except Exception as e: @@ -175,7 +175,7 @@ def on_api_update(api: API): new_unit.__class__ = DisembarkedInfantry new_unit.start_fighting() - api.spawn_ground_units([spawn_table], unit.coalition, "", True, 0, lambda new_group_ID: execution_callback(new_group_ID)) + api.spawn_ground_units([spawn_table], unit.coalition, "", True, 0, execution_callback) logger.info(f"Spawned new unit succesfully at {spawn_position} with heading {unit.heading}") break diff --git a/scripts/python/API/example_voice_control.py b/scripts/python/API/example_voice_control.py index ecb5a80c..0a073282 100644 --- a/scripts/python/API/example_voice_control.py +++ b/scripts/python/API/example_voice_control.py @@ -1,5 +1,6 @@ from math import pi +import os from api import API, UnitSpawnTable from radio.radio_listener import RadioListener @@ -70,12 +71,15 @@ def on_message_received(recognized_text: str, unit_id: str, api: API, listener: message_filename = api.generate_audio_message("I did not understand") listener.transmit_on_frequency(message_filename, listener.frequency, listener.modulation, listener.encryption) + # Delete the message file after processing + os.remove(message_filename) + if __name__ == "__main__": api = API() logger.info("API initialized") listener = api.create_radio_listener() listener.start(frequency=251.000e6, modulation=0, encryption=0) - listener.register_message_callback(lambda wav_filename, unit_id, api=api, listener=listener: on_message_received(wav_filename, unit_id, api, listener)) + listener.register_message_callback(lambda recognized_text, unit_id, api=api, listener=listener: on_message_received(recognized_text, unit_id, api, listener)) api.run() \ No newline at end of file