notifier.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from threading import Thread
  2. import profiles
  3. import datetime
  4. import timetable_calendar
  5. import time
  6. from PyQt5.QtCore import *
  7. import audio_player
  8. from typing import *
  9. _thread: Union[Thread, None] = None
  10. _allowed_to_execute: bool = True
  11. _enable_logging = False
  12. def _print_logs(message: str) -> None:
  13. if _enable_logging:
  14. print(message)
  15. def _safe_behaviour_iteration():
  16. try:
  17. now = datetime.datetime.now()
  18. date = QDate(now.year, now.month, now.day)
  19. profile_id = timetable_calendar.get_profile_id(date)
  20. if profile_id is None:
  21. return
  22. profile = profiles.get(profile_id)
  23. timetable = profile['timetable']
  24. current_time = QTime(now.hour, now.minute, now.second)
  25. melody_name = timetable.get(current_time)
  26. _print_logs(f'Поток звонков: информация из файлов сохранения.')
  27. _print_logs(f'Дата сейчас: {date}.')
  28. _print_logs(f'Время сейчас: {current_time}.')
  29. _print_logs(f'Мелодия: {melody_name}.')
  30. _print_logs(f'***')
  31. if melody_name is None:
  32. return
  33. audio_player.play(melody_name)
  34. except Exception as exception:
  35. print('Ошибка в потоке звонков!')
  36. print(exception)
  37. def _thread_behaviour() -> None:
  38. global _allowed_to_execute
  39. _print_logs('Поток звонков запущен!')
  40. while _allowed_to_execute:
  41. time.sleep(1)
  42. _safe_behaviour_iteration()
  43. def restart():
  44. global _thread, _allowed_to_execute
  45. if _thread is not None:
  46. _allowed_to_execute = False
  47. _thread.join()
  48. _allowed_to_execute = True
  49. _thread = Thread(target=_thread_behaviour)
  50. _thread.start()
  51. def stop() -> None:
  52. global _thread, _allowed_to_execute
  53. if _thread is not None:
  54. _allowed_to_execute = False
  55. _thread.join()