Skip to content

Commit 827764b

Browse files
committed
Extract timer manager to separate module
1 parent a0e190d commit 827764b

File tree

2 files changed

+139
-23
lines changed

2 files changed

+139
-23
lines changed

start.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from settings_functions import Settings, versionString
5656
from command_handler import CommandHandler
5757
from network import UdpServer, HttpDaemon
58+
from timer_manager import TimerManager
5859
from utils import settings_group
5960

6061
# Configure logging
@@ -152,42 +153,35 @@ def __init__(self) -> None:
152153
self.LED3on = False
153154
self.LED4on = False
154155

155-
# Setup and start timers
156+
# Setup and start constant update timer
156157
self.ctimer = QTimer()
157158
self.ctimer.timeout.connect(self.constant_update)
158159
self.ctimer.start(100)
159-
# LED timers
160-
self.timerLED1 = QTimer()
161-
self.timerLED1.timeout.connect(self.toggle_led1)
162-
self.timerLED2 = QTimer()
163-
self.timerLED2.timeout.connect(self.toggle_led2)
164-
self.timerLED3 = QTimer()
165-
self.timerLED3.timeout.connect(self.toggle_led3)
166-
self.timerLED4 = QTimer()
167-
self.timerLED4.timeout.connect(self.toggle_led4)
168-
169-
# Setup OnAir Timers
170-
self.timerAIR1 = QTimer()
171-
self.timerAIR1.timeout.connect(self.update_air1_seconds)
160+
161+
# Initialize timer manager
162+
self.timer_manager = TimerManager(self)
163+
164+
# AIR timer state
172165
self.Air1Seconds = 0
173166
self.statusAIR1 = False
174-
175-
self.timerAIR2 = QTimer()
176-
self.timerAIR2.timeout.connect(self.update_air2_seconds)
177167
self.Air2Seconds = 0
178168
self.statusAIR2 = False
179-
180-
self.timerAIR3 = QTimer()
181-
self.timerAIR3.timeout.connect(self.update_air3_seconds)
182169
self.Air3Seconds = 0
183170
self.statusAIR3 = False
184171
self.radioTimerMode = 0 # count up mode
185-
186-
self.timerAIR4 = QTimer()
187-
self.timerAIR4.timeout.connect(self.update_air4_seconds)
188172
self.Air4Seconds = 0
189173
self.statusAIR4 = False
190174
self.streamTimerMode = 0 # count up mode
175+
176+
# Expose timer objects for backward compatibility
177+
self.timerLED1 = self.timer_manager.timerLED1
178+
self.timerLED2 = self.timer_manager.timerLED2
179+
self.timerLED3 = self.timer_manager.timerLED3
180+
self.timerLED4 = self.timer_manager.timerLED4
181+
self.timerAIR1 = self.timer_manager.timerAIR1
182+
self.timerAIR2 = self.timer_manager.timerAIR2
183+
self.timerAIR3 = self.timer_manager.timerAIR3
184+
self.timerAIR4 = self.timer_manager.timerAIR4
191185

192186
# Setup NTP Check Thread
193187
self.checkNTPOffset = CheckNTPOffsetThread(self)

timer_manager.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)