def schedulehostdeletion(hostname, offlinedict, updatesignal, settingswidget, lock,…
- User
def schedule_host_deletion(hostname, offline_dict, update_signal, settings_widget, lock, main_window): def delete_host(): normalized_hostname = hostname.upper() with lock: if normalized_hostname in offline_dict: store_name = offline_dict[normalized_hostname]['store_name'] # Log the host coming online log_online( hostname, store_name, offline_dict, normalized_hostname, offline_dict[normalized_hostname]['inspect_status'] ) # Remove the host from the offline_dict del offline_dict[normalized_hostname] # Save the offline state save_offline_state('offline_state.json', offline_dict) # Play sound when host comes back online hostname_lower = hostname.lower() if any(fnmatch.fnmatch(hostname_lower, pattern.lower()) for pattern in settings_widget.sound_alert_patterns): if hasattr(main_window, 'sound_manager'): message = f"Big C {store_name} return to Online" audio_filename = f"{message}.mp3" audio_path = os.path.join('sounds', audio_filename) fallback_sound_file = "returnonline.mp3"
def play_sound(): if os.path.exists(audio_path): main_window.sound_manager.play(audio_path) else: print(f"Sound file '{audio_filename}' does not exist. Attempting to download...") downloaded_file = download_audio(message) if downloaded_file and os.path.exists(downloaded_file): main_window.sound_manager.play(downloaded_file) else: if os.path.exists(fallback_sound_file): print(f"Failed to download. Playing fallback sound file: {fallback_sound_file}") main_window.sound_manager.play(fallback_sound_file) else: print("Failed to download sound file and no fallback sound available.") sound_thread = threading.Thread(target=play_sound) sound_thread.start() # Remove from exception hosts if necessary if normalized_hostname in settings_widget.exception_hosts: settings_widget.exception_hosts.remove(normalized_hostname) settings_widget.update_exception_hosts_signal.emit(settings_widget.exception_hosts) else: print(f"Hostname {normalized_hostname} was not in offline_dict") # Emit the update signal to refresh the GUI update_signal.emit() # Schedule the deletion after 5 seconds timer = threading.Timer(5.0, delete_host) timer.start()make force stop play sound if the table no inspect status server up although qeueu on
- Assistant
To implement a mechanism that forces the stopping of sound playback if certain conditions aren't met (like the absence of inspect status for the server in the
offline_dict), you'll need to make some adjustments to the existing function. Specifically, you'll need to manage sound playback threads more effectively, potentially interrupting them when certain conditions are not satisfied.Here's an enhanced implementation where we add logic to enable sound playback control:
import threading import os import fnmatch def schedule_host_deletion(hostname, offline_dict, update_signal, settings_widget, lock, main_window): stop_sound_event = threading.Event() # Event object to manage sound stopping def delete_host(): normalized_hostname = hostname.upper() with lock: if normalized_hostname in offline_dict: store_name = offline_dict[normalized_hostname]['store_name'] if offline_dict[normalized_hostname]['inspect_status'] != 'up': print(f"Host {normalized_hostname} is not marked as 'up', cancelling sound.") stop_playing_sound() return # Log the host coming online log_online( hostname, store_name, offline_dict, normalized_hostname, offline_dict[normalized_hostname]['inspect_status'] ) # Remove the host from the offline_dict del offline_dict[normalized_hostname] # Save the offline state save_offline_state('offline_state.json', offline_dict) # Play sound when host comes back online hostname_lower = hostname.lower() if any(fnmatch.fnmatch(hostname_lower, pattern.lower()) for pattern in settings_widget.sound_alert_patterns): if hasattr(main_window, 'sound_manager'): message = f"Big C {store_name} return to Online" audio_filename = f"{message}.mp3" audio_path = os.path.join('sounds', audio_filename) fallback_sound_file = "returnonline.mp3" def play_sound(): if not stop_sound_event.is_set(): # Check if the event is not set if os.path.exists(audio_path): main_window.sound_manager.play(audio_path) else: print(f"Sound file '{audio_filename}' does not exist. Attempting to download...") downloaded_file = download_audio(message) if downloaded_file and os.path.exists(downloaded_file): main_window.sound_manager.play(downloaded_file) else: if os.path.exists(fallback_sound_file): print(f"Failed to download. Playing fallback sound file: {fallback_sound_file}") main_window.sound_manager.play(fallback_sound_file) else: print("Failed to download sound file and no fallback sound available.") sound_thread = threading.Thread(target=play_sound) sound_thread.start() # Remove from exception hosts if necessary if normalized_hostname in settings_widget.exception_hosts: settings_widget.exception_hosts.remove(normalized_hostname) settings_widget.update_exception_hosts_signal.emit(settings_widget.exception_hosts) else: print(f"Hostname {normalized_hostname} was not in offline_dict") # Emit the update signal to refresh the GUI update_signal.emit() def stop_playing_sound(): stop_sound_event.set() # Set the event to stop sound playback if hasattr(main_window, 'sound_manager'): main_window.sound_manager.stop() # Ensure your sound manager implementation has a stop method # Schedule the deletion after 5 seconds timer = threading.Timer(5.0, delete_host) timer.start()Key Enhancements:
- Stop Event: Introduced a
threading.Eventto control the flow of sound playback. This event, when set, can be checked in theplay_soundfunction to avoid playing sound under specific conditions. - Condition Check: Before playing any sound, the function checks if
inspect_statusis set to 'up'. If not, thestop_playing_soundfunction is called. - Sound Manager: Assumes the
sound_managerhas a stop method to stop any ongoing playback. If not, you may need to implement this feature within your sound manager.
This structure will ensure that if any pre-defined conditions are not met before the host goes online, no sound will be played, or existing sounds will be stopped promptly.
- Stop Event: Introduced a