|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +############################################################################# |
| 4 | +# |
| 5 | +# OnAirScreen |
| 6 | +# Copyright (c) 2012-2025 Sascha Ludwig, astrastudio.de |
| 7 | +# All rights reserved. |
| 8 | +# |
| 9 | +# timer_manager.py |
| 10 | +# This file is part of OnAirScreen |
| 11 | +# |
| 12 | +# You may use this file under the terms of the BSD license as follows: |
| 13 | +# |
| 14 | +# "Redistribution and use in source and binary forms, with or without |
| 15 | +# modification, are permitted provided that the following conditions are |
| 16 | +# met: |
| 17 | +# * Redistributions of source code must retain the above copyright |
| 18 | +# notice, this list of conditions and the following disclaimer. |
| 19 | +# * Redistributions in binary form must reproduce the above copyright |
| 20 | +# notice, this list of conditions and the following disclaimer in |
| 21 | +# the documentation and/or other materials provided with the |
| 22 | +# distribution. |
| 23 | +# |
| 24 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 27 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 28 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 29 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 30 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 35 | +# |
| 36 | +############################################################################# |
| 37 | + |
| 38 | +""" |
| 39 | +Timer Manager for OnAirScreen |
| 40 | +
|
| 41 | +This module manages timer objects and provides timer-related functionality |
| 42 | +for AIR and LED timers. |
| 43 | +""" |
| 44 | + |
| 45 | +import logging |
| 46 | +from typing import Callable, Optional |
| 47 | + |
| 48 | +from PyQt6.QtCore import QTimer |
| 49 | + |
| 50 | +logger = logging.getLogger(__name__) |
| 51 | + |
| 52 | + |
| 53 | +class TimerManager: |
| 54 | + """ |
| 55 | + Manages timer objects for AIR and LED functionality |
| 56 | + |
| 57 | + This class handles timer creation, starting, stopping, and provides |
| 58 | + callbacks for timer events. |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__(self, main_screen): |
| 62 | + """ |
| 63 | + Initialize timer manager |
| 64 | + |
| 65 | + Args: |
| 66 | + main_screen: Reference to MainScreen instance for callbacks |
| 67 | + """ |
| 68 | + self.main_screen = main_screen |
| 69 | + self._setup_timers() |
| 70 | + |
| 71 | + def _setup_timers(self) -> None: |
| 72 | + """Setup all timer objects""" |
| 73 | + # LED timers |
| 74 | + self.timerLED1 = QTimer() |
| 75 | + self.timerLED1.timeout.connect(self.main_screen.toggle_led1) |
| 76 | + self.timerLED2 = QTimer() |
| 77 | + self.timerLED2.timeout.connect(self.main_screen.toggle_led2) |
| 78 | + self.timerLED3 = QTimer() |
| 79 | + self.timerLED3.timeout.connect(self.main_screen.toggle_led3) |
| 80 | + self.timerLED4 = QTimer() |
| 81 | + self.timerLED4.timeout.connect(self.main_screen.toggle_led4) |
| 82 | + |
| 83 | + # AIR timers |
| 84 | + self.timerAIR1 = QTimer() |
| 85 | + self.timerAIR1.timeout.connect(self.main_screen.update_air1_seconds) |
| 86 | + self.timerAIR2 = QTimer() |
| 87 | + self.timerAIR2.timeout.connect(self.main_screen.update_air2_seconds) |
| 88 | + self.timerAIR3 = QTimer() |
| 89 | + self.timerAIR3.timeout.connect(self.main_screen.update_air3_seconds) |
| 90 | + self.timerAIR4 = QTimer() |
| 91 | + self.timerAIR4.timeout.connect(self.main_screen.update_air4_seconds) |
| 92 | + |
| 93 | + def get_led_timer(self, led_num: int) -> Optional[QTimer]: |
| 94 | + """ |
| 95 | + Get LED timer for given LED number |
| 96 | + |
| 97 | + Args: |
| 98 | + led_num: LED number (1-4) |
| 99 | + |
| 100 | + Returns: |
| 101 | + QTimer instance or None if invalid |
| 102 | + """ |
| 103 | + if led_num < 1 or led_num > 4: |
| 104 | + logger.warning(f"Invalid LED number: {led_num}") |
| 105 | + return None |
| 106 | + return getattr(self, f'timerLED{led_num}') |
| 107 | + |
| 108 | + def get_air_timer(self, air_num: int) -> Optional[QTimer]: |
| 109 | + """ |
| 110 | + Get AIR timer for given AIR number |
| 111 | + |
| 112 | + Args: |
| 113 | + air_num: AIR number (1-4) |
| 114 | + |
| 115 | + Returns: |
| 116 | + QTimer instance or None if invalid |
| 117 | + """ |
| 118 | + if air_num < 1 or air_num > 4: |
| 119 | + logger.warning(f"Invalid AIR number: {air_num}") |
| 120 | + return None |
| 121 | + return getattr(self, f'timerAIR{air_num}') |
| 122 | + |
0 commit comments