forked from saschaludwig/OnAirScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
executable file
·1451 lines (1207 loc) · 55.4 KB
/
start.py
File metadata and controls
executable file
·1451 lines (1207 loc) · 55.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#############################################################################
#
# OnAirScreen
# Copyright (c) 2012-2026 Sascha Ludwig, astrastudio.de
# All rights reserved.
#
# start.py
# This file is part of OnAirScreen
#
# You may use this file under the terms of the BSD license as follows:
#
# "Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
#
#############################################################################
import argparse
import logging
import re
import sys
from PyQt6.QtCore import Qt, QSettings, QCoreApplication, QTimer, pyqtSignal, QObject
from PyQt6.QtGui import QCursor, QPalette, QIcon, QPixmap, QFont, QColor
from PyQt6.QtNetwork import QNetworkInterface
from PyQt6.QtWidgets import QApplication, QWidget, QDialog, QLineEdit, QVBoxLayout, QLabel, QMessageBox
# Import resources FIRST to register them with Qt before UI files are loaded
import resources_rc # noqa: F401
from mainscreen import Ui_MainScreen
from settings_functions import Settings, versionString, distributionString
from command_handler import CommandHandler
from network import UdpServer, HttpDaemon, WebSocketDaemon
from timer_manager import TimerManager
from event_logger import EventLogger
from warning_manager import WarningManager
from settings_functions import SettingsRestorer
from timer_input import TimerInputDialog
from ntp_manager import NTPManager
from font_loader import load_fonts
from signal_handlers import setup_signal_handlers
from system_operations import SystemOperations
from status_exporter import StatusExporter
from ui_updater import UIUpdater
from hotkey_manager import HotkeyManager
from logging_config import set_log_level, get_command_line_log_level, set_command_line_log_level
from utils import settings_group
from defaults import * # noqa: F403, F405
from exceptions import WidgetAccessError, log_exception
# Logging will be configured after QApplication initialization and settings loading
logger = logging.getLogger(__name__)
class CommandSignal(QObject):
"""Signal object for thread-safe command execution"""
command_received = pyqtSignal(bytes, str)
class MainScreen(QWidget, Ui_MainScreen):
"""
Main application window for OnAirScreen
This class handles the main UI, timer management, LED/AIR controls,
network communication (UDP/HTTP), and settings management.
The class delegates specific responsibilities to specialized manager classes:
- NTPManager: NTP time synchronization checking
- UIUpdater: Periodic UI updates (date, time, backtiming)
- SystemOperations: System operations (reboot, shutdown, exit)
- StatusExporter: Status data export for API
- HotkeyManager: Keyboard shortcut management
- TimerManager: Timer object management
- WarningManager: Warning system management
"""
getTimeWindow: QDialog
textLocale: str # for text language
languages = {"English": 'en_US',
"German": 'de_DE',
"Dutch": 'nl_NL',
"French": 'fr_FR'}
def __init__(self) -> None:
"""Initialize the main screen and load settings"""
QWidget.__init__(self)
Ui_MainScreen.__init__(self)
self.setupUi(self)
self.settings = Settings()
self.restore_settings_from_config()
# Initialize event logger (needed for system operations)
self.event_logger = EventLogger()
# Initialize system operations (needed for signal connections)
self.system_operations = SystemOperations(self)
# quit app from settings window
self.settings.sigExitOAS.connect(self.system_operations.exit_oas)
self.settings.sigRebootHost.connect(self.system_operations.reboot_host)
self.settings.sigShutdownHost.connect(self.system_operations.shutdown_host)
self.settings.sigConfigFinished.connect(self.config_finished)
self.settings.sigConfigClosed.connect(self.config_closed)
# Store MQTT settings when settings dialog opens to compare on apply
self._mqtt_settings_before_edit = {}
# Initialize command handler
self.command_handler = CommandHandler(self)
# Initialize event logger
self.event_logger = EventLogger()
# Initialize status exporter
self.status_exporter = StatusExporter(self)
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
with settings_group(settings, "General"):
if settings.value('fullscreen', True, type=bool):
self.showFullScreen()
app.setOverrideCursor(QCursor(Qt.CursorShape.BlankCursor))
logger.info(f"Loaded settings from: {settings.fileName()}")
self.labelWarning.hide()
# Initialize warning manager
self.warning_manager = WarningManager(
self.labelWarning,
self.labelCurrentSong,
self.labelNews,
self.event_logger,
self._publish_mqtt_status
)
# Keep warnings attribute for backward compatibility (used in get_status_json)
self.warnings = self.warning_manager.warnings
# Initialize hotkey manager
self.hotkey_manager = HotkeyManager(self)
self.statusLED1 = False
self.statusLED2 = False
self.statusLED3 = False
self.statusLED4 = False
self.LED1on = False
self.LED2on = False
self.LED3on = False
self.LED4on = False
# Initialize UI updater
self.ui_updater = UIUpdater(self)
# Setup and start constant update timer
self.ctimer = QTimer()
self.ctimer.timeout.connect(self.ui_updater.constant_update)
self.ctimer.start(100)
# Initialize timer manager
self.timer_manager = TimerManager(self)
# AIR timer state
self.Air1Seconds = 0
self.statusAIR1 = False
self.Air2Seconds = 0
self.statusAIR2 = False
self.Air3Seconds = 0
self.statusAIR3 = False
self.radioTimerMode = 0 # count up mode
self.Air4Seconds = 0
self.statusAIR4 = False
self.streamTimerMode = 0 # count up mode
# Expose timer objects for backward compatibility
self.timerLED1 = self.timer_manager.timerLED1
self.timerLED2 = self.timer_manager.timerLED2
self.timerLED3 = self.timer_manager.timerLED3
self.timerLED4 = self.timer_manager.timerLED4
self.timerAIR1 = self.timer_manager.timerAIR1
self.timerAIR2 = self.timer_manager.timerAIR2
self.timerAIR3 = self.timer_manager.timerAIR3
self.timerAIR4 = self.timer_manager.timerAIR4
# Initialize NTP manager
self.ntp_manager = NTPManager(self)
self.replacenowTimer = QTimer()
self.replacenowTimer.timeout.connect(self.replace_now_next)
# Setup command signal for thread-safe HTTP command execution
self.command_signal = CommandSignal()
self.command_signal.command_received.connect(self._parse_cmd_with_source)
# Setup UDP Server with source tracking
def udp_command_callback(data: bytes) -> None:
self._parse_cmd_with_source(data, "udp")
self.udp_server = UdpServer(udp_command_callback)
# Setup HTTP Server with reference to MainScreen for status API and command signal
self.httpd = HttpDaemon(self, self.command_signal)
self.httpd.start()
# Setup WebSocket Server for real-time status updates
self.wsd = WebSocketDaemon(self)
self.wsd.start()
# Setup MQTT Client
try:
from mqtt_client import MqttClient
self.mqtt_client = MqttClient(self)
self.mqtt_client.start()
except Exception as e:
logger.warning(f"Failed to initialize MQTT client: {e}")
self.mqtt_client = None
# Log application start
self.event_logger.log_system_event("Application started")
# display all host addresses
self.display_all_hostaddresses()
# NTP warning is already initialized in NTPManager
# do initial update check
self.settings.sigCheckForUpdate.emit()
def quit_oas(self) -> None:
"""
Quit the application with cleanup
Stops NTP check thread, HTTP server, and quits the application.
"""
logger.info("Quitting, cleaning up...")
self.event_logger.log_system_event("Application quit")
self.ntp_manager.stop()
self.httpd.stop()
if hasattr(self, 'wsd') and self.wsd:
self.wsd.stop()
if hasattr(self, 'mqtt_client') and self.mqtt_client:
self.mqtt_client.stop()
QCoreApplication.instance().quit()
def radio_timer_start_stop(self) -> None:
"""
Start or stop the radio timer (AIR3)
Toggles the radio timer between running and stopped states.
"""
self.start_stop_air3()
def radio_timer_reset(self) -> None:
"""Reset radio timer"""
self._reset_timer('radio', 3)
def _reset_timer(self, timer_type: str, air_num: int) -> None:
"""
Generic method to reset timer
Args:
timer_type: Type of timer ('radio' or 'stream')
air_num: AIR number (3 or 4)
"""
if timer_type == 'radio':
self.reset_air3()
self.radioTimerMode = 0 # count up mode
elif timer_type == 'stream':
self.reset_air4()
self.streamTimerMode = 0 # count up mode
else:
logger.warning(f"Invalid timer type: {timer_type}")
def radio_timer_set(self, seconds: int) -> None:
"""
Set radio timer duration in seconds
Args:
seconds: Timer duration in seconds (0 for count-up mode, >0 for count-down mode)
"""
self.Air3Seconds = seconds
if seconds > 0:
self.radioTimerMode = 1 # count down mode
mode = "count_down"
else:
self.radioTimerMode = 0 # count up mode
mode = "count_up"
self.AirLabel_3.setText(f"Timer\n{int(self.Air3Seconds / 60)}:{int(self.Air3Seconds % 60):02d}")
# Log timer set event
self.event_logger.log_timer_set(3, seconds, mode)
def get_timer_dialog(self) -> None:
"""
Generate and display timer input dialog window
Creates a dialog window for entering timer values in formats:
- "2,10" or "2.10" for 2 minutes 10 seconds
- "30" for 30 seconds only
"""
if not hasattr(self, 'timer_input_dialog') or self.timer_input_dialog is None:
self.timer_input_dialog = TimerInputDialog(self)
self.timer_input_dialog.timer_set.connect(self.radio_timer_set)
self.timer_input_dialog.show()
def stream_timer_start_stop(self) -> None:
"""
Start or stop the stream timer (AIR4)
Toggles the stream timer between running and stopped states.
"""
self.start_stop_air4()
def stream_timer_reset(self) -> None:
"""
Reset stream timer (AIR4)
Resets the stream timer to 0 and sets it to count-up mode.
"""
self._reset_timer('stream', 4)
def _ensure_air_icons_are_set(self) -> None:
"""Helper method to ensure all AIR icons are set correctly"""
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
with settings_group(settings, "AIR"):
air_configs = [
(1, 'air1iconpath', ':/mic_icon/images/mic_icon.png'),
(2, 'air2iconpath', ':/phone_icon/images/phone_icon.png'),
(3, 'air3iconpath', ':/timer_icon/images/timer_icon.png'),
(4, 'air4iconpath', ':/stream_icon/images/antenna2.png')
]
for air_num, icon_key, default_path in air_configs:
icon_path = settings.value(icon_key, default_path)
if icon_path:
pixmap = QPixmap(icon_path)
icon_widget = getattr(self, f'AirIcon_{air_num}')
icon_widget.setPixmap(pixmap)
icon_widget.update()
def _set_air_state(self, air_num: int, action: bool) -> None:
"""
Generic method to set AIR state (active/inactive)
Args:
air_num: AIR number (1-4)
action: True for active, False for inactive
"""
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
air_configs = {
1: {
'label': 'TimerAIR1Text',
'label_default': 'Mic',
'icon_key': 'air1iconpath',
'icon_default': ':/mic_icon/images/mic_icon.png',
'active_text_color': 'AIR1activetextcolor',
'active_bg_color': 'AIR1activebgcolor',
'seconds_attr': 'Air1Seconds',
'status_attr': 'statusAIR1',
'timer_attr': 'timerAIR1',
'label_widget': 'AirLabel_1',
'icon_widget': 'AirIcon_1',
'reset_seconds': True
},
2: {
'label': 'TimerAIR2Text',
'label_default': 'Phone',
'icon_key': 'air2iconpath',
'icon_default': ':/phone_icon/images/phone_icon.png',
'active_text_color': 'AIR2activetextcolor',
'active_bg_color': 'AIR2activebgcolor',
'seconds_attr': 'Air2Seconds',
'status_attr': 'statusAIR2',
'timer_attr': 'timerAIR2',
'label_widget': 'AirLabel_2',
'icon_widget': 'AirIcon_2',
'reset_seconds': True
},
3: {
'label': 'TimerAIR3Text',
'label_default': 'Timer',
'icon_key': 'air3iconpath',
'icon_default': ':/timer_icon/images/timer_icon.png',
'active_text_color': 'AIR3activetextcolor',
'active_bg_color': 'AIR3activebgcolor',
'seconds_attr': 'Air3Seconds',
'status_attr': 'statusAIR3',
'timer_attr': 'timerAIR3',
'label_widget': 'AirLabel_3',
'icon_widget': 'AirIcon_3',
'reset_seconds': False,
'special_mode': 'radioTimerMode'
},
4: {
'label': 'TimerAIR4Text',
'label_default': 'Stream',
'icon_key': 'air4iconpath',
'icon_default': ':/stream_icon/images/antenna2.png',
'active_text_color': 'AIR4activetextcolor',
'active_bg_color': 'AIR4activebgcolor',
'seconds_attr': 'Air4Seconds',
'status_attr': 'statusAIR4',
'timer_attr': 'timerAIR4',
'label_widget': 'AirLabel_4',
'icon_widget': 'AirIcon_4',
'reset_seconds': False,
'special_mode': 'streamTimerMode'
}
}
if air_num not in air_configs:
logger.warning(f"Invalid AIR number: {air_num}, must be 1-4")
return
config = air_configs[air_num]
label_widget = getattr(self, config['label_widget'])
icon_widget = getattr(self, config['icon_widget'])
seconds_attr = config['seconds_attr']
status_attr = config['status_attr']
timer_attr = config['timer_attr']
if action:
with settings_group(settings, "Timers"):
if config.get('reset_seconds', False):
setattr(self, seconds_attr, 0)
# Set active styles
active_text_color = settings.value(config['active_text_color'], DEFAULT_TIMER_AIR_ACTIVE_TEXT_COLOR)
active_bg_color = settings.value(config['active_bg_color'], DEFAULT_TIMER_AIR_ACTIVE_BG_COLOR)
label_widget.setStyleSheet(f"color:{active_text_color};background-color:{active_bg_color}")
# Set icon with active styles
with settings_group(settings, "AIR"):
icon_path = settings.value(config['icon_key'], config['icon_default'])
if icon_path:
pixmap = QPixmap(icon_path)
icon_widget.setStyleSheet(f"color:{active_text_color};background-color:{active_bg_color}")
icon_widget.setPixmap(pixmap)
icon_widget.update()
# Set label text
label_text = settings.value(config['label'], config['label_default'])
seconds = getattr(self, seconds_attr)
label_widget.setText(f"{label_text}\n{int(seconds/60)}:{seconds%60:02d}")
# Set status and start timer
setattr(self, status_attr, True)
timer = getattr(self, timer_attr)
timer.start(1000)
# Log AIR started event
self.event_logger.log_air_started(air_num, "manual")
# Publish MQTT status immediately after AIR start
self._publish_mqtt_status(f"air{air_num}")
# Special handling for AIR3 and AIR4 countdown mode
if 'special_mode' in config:
mode_attr = config['special_mode']
mode = getattr(self, mode_attr, 0)
if mode == 1 and seconds > 1:
update_method = getattr(self, f'update_air{air_num}_seconds')
update_method()
else:
with settings_group(settings, "LEDS"):
inactive_text_color = settings.value('inactivetextcolor', '#555555')
inactive_bg_color = settings.value('inactivebgcolor', '#222222')
# Save icon before setStyleSheet to prevent flickering
with settings_group(settings, "AIR"):
icon_path = settings.value(config['icon_key'], config['icon_default'])
icon_pixmap = QPixmap(icon_path) if icon_path else None
# Set inactive styles
label_widget.setStyleSheet(f"color:{inactive_text_color};background-color:{inactive_bg_color}")
icon_widget.setStyleSheet(f"color:{inactive_text_color};background-color:{inactive_bg_color}")
# Restore icon immediately after styleSheet change to prevent flickering
if icon_pixmap and not icon_pixmap.isNull():
icon_widget.setPixmap(icon_pixmap)
icon_widget.update()
# Set status and stop timer
setattr(self, status_attr, False)
timer = getattr(self, timer_attr)
timer.stop()
# Log AIR stopped event
self.event_logger.log_air_stopped(air_num, "manual")
# Publish MQTT status immediately after AIR state change
self._publish_mqtt_status(f"air{air_num}")
def _update_air_seconds(self, air_num: int) -> None:
"""
Generic method to update AIR seconds display
Args:
air_num: AIR number (1-4)
"""
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
air_configs = {
1: {'label': 'TimerAIR1Text', 'label_default': 'Mic', 'seconds_attr': 'Air1Seconds', 'label_widget': 'AirLabel_1'},
2: {'label': 'TimerAIR2Text', 'label_default': 'Phone', 'seconds_attr': 'Air2Seconds', 'label_widget': 'AirLabel_2'},
3: {'label': 'TimerAIR3Text', 'label_default': 'Timer', 'seconds_attr': 'Air3Seconds', 'label_widget': 'AirLabel_3', 'mode_attr': 'radioTimerMode'},
4: {'label': 'TimerAIR4Text', 'label_default': 'Stream', 'seconds_attr': 'Air4Seconds', 'label_widget': 'AirLabel_4', 'mode_attr': 'streamTimerMode'}
}
config = air_configs[air_num]
seconds_attr = config['seconds_attr']
label_widget = getattr(self, config['label_widget'])
# Handle countdown mode for AIR3 and AIR4
if 'mode_attr' in config:
mode_attr = config['mode_attr']
mode = getattr(self, mode_attr, 0)
if mode == 0: # count up mode
setattr(self, seconds_attr, getattr(self, seconds_attr) + 1)
else: # countdown mode
current_seconds = getattr(self, seconds_attr)
setattr(self, seconds_attr, current_seconds - 1)
if getattr(self, seconds_attr) < 1:
stop_method = getattr(self, f'stop_air{air_num}')
stop_method()
# Reset the correct mode attribute
setattr(self, mode_attr, 0)
else:
# Simple count up for AIR1 and AIR2
setattr(self, seconds_attr, getattr(self, seconds_attr) + 1)
# Update label text
with settings_group(settings, "Timers"):
label_text = settings.value(config['label'], config['label_default'])
seconds = getattr(self, seconds_attr)
label_widget.setText(f"{label_text}\n{int(seconds/60)}:{seconds%60:02d}")
def show_settings(self) -> None:
"""
Show settings dialog window
Restores mouse cursor, ensures AIR icons are set, and displays settings window.
"""
global app
# un-hide mouse cursor
app.setOverrideCursor(QCursor(Qt.CursorShape.ArrowCursor))
# Set icons BEFORE opening dialog to prevent flickering
self._ensure_air_icons_are_set()
self.settings.show_settings()
def display_all_hostaddresses(self) -> None:
"""
Display all local network IP addresses in NOW and NEXT text fields
Retrieves all non-loopback IPv4 and IPv6 addresses from network interfaces
and displays them in the UI. Starts a timer to replace with configured text
after 10 seconds if replacenow setting is enabled.
"""
v4addrs = list()
v6addrs = list()
# Get all network interfaces
for interface in QNetworkInterface.allInterfaces():
# Skip loopback interfaces
if interface.flags() & QNetworkInterface.InterfaceFlag.IsLoopBack:
continue
# Get all address entries for this interface
for entry in interface.addressEntries():
address = entry.ip()
addr_str = address.toString()
# Check if it's IPv4 or IPv6 using toIPv4Address/toIPv6Address
# Both methods return a tuple: (address_value, is_valid) for IPv4, (16 bytes) for IPv6
ipv4_result = address.toIPv4Address()
ipv6_result = address.toIPv6Address()
if len(ipv4_result) == 2 and ipv4_result[1]: # IPv4 and valid
# Skip localhost addresses
if not addr_str.startswith('127.'):
v4addrs.append(addr_str)
elif len(ipv6_result) == 16: # IPv6 (returns 16-byte tuple)
# Skip IPv6 localhost (::1) and link-local addresses (fe80::)
if addr_str != '::1' and not addr_str.startswith('fe80::'):
v6addrs.append(addr_str)
self.set_current_song_text(", ".join([str(addr) for addr in v4addrs]))
self.set_news_text(", ".join([str(addr) for addr in v6addrs]))
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
with settings_group(settings, "General"):
if settings.value('replacenow', True, type=bool):
self.replacenowTimer.setSingleShot(True)
self.replacenowTimer.start(10000)
def _parse_cmd_with_source(self, data: bytes, source: str = "udp") -> None:
"""
Parse and execute a command with source tracking
Args:
data: Command string in format "COMMAND:VALUE"
source: Source of command ('udp' or 'http')
"""
try:
command_str = data.decode('utf-8') if isinstance(data, bytes) else str(data)
if ':' in command_str:
command, value = command_str.split(':', 1)
# Log command received event with source
self.event_logger.log_command_received(command, value, source)
except (UnicodeDecodeError, ValueError, AttributeError):
pass
# Forward to actual command handler
self.command_handler.parse_cmd(data if isinstance(data, bytes) else data.encode())
def parse_cmd(self, data: bytes) -> bool:
"""
Parse and execute a command from UDP/HTTP input
Args:
data: Command string in format "COMMAND:VALUE"
Returns:
True if command was parsed successfully, False otherwise
"""
# Default to unknown source for backward compatibility
self._parse_cmd_with_source(data, "unknown")
return True
def manual_toggle_led1(self) -> None:
"""Toggle LED1 using led_logic (manual toggle)"""
self._manual_toggle_led(1)
def manual_toggle_led2(self) -> None:
"""Toggle LED2 using led_logic (manual toggle)"""
self._manual_toggle_led(2)
def manual_toggle_led3(self) -> None:
"""Toggle LED3 using led_logic (manual toggle)"""
self._manual_toggle_led(3)
def manual_toggle_led4(self) -> None:
"""Toggle LED4 using led_logic (manual toggle)"""
self._manual_toggle_led(4)
def _manual_toggle_led(self, led_num: int) -> None:
"""Generic method to toggle LED using led_logic"""
led_on_attr = f'LED{led_num}on'
current_state = getattr(self, led_on_attr, False)
self.led_logic(led_num, not current_state)
def toggle_led1(self) -> None:
"""Toggle LED1 using set_led1"""
self._toggle_led(1)
def toggle_led2(self) -> None:
"""Toggle LED2 using set_led2"""
self._toggle_led(2)
def toggle_led3(self) -> None:
"""Toggle LED3 using set_led3"""
self._toggle_led(3)
def toggle_led4(self) -> None:
"""Toggle LED4 using set_led4"""
self._toggle_led(4)
def _toggle_led(self, led_num: int) -> None:
"""Generic method to toggle LED using led_logic"""
# Use LED{num}on for logical status, not statusLED{num} which is the visual blinking state
led_on_attr = f'LED{led_num}on'
current_state = getattr(self, led_on_attr, False)
self.led_logic(led_num, not current_state)
def toggle_air1(self) -> None:
"""Toggle AIR1"""
self._toggle_air(1)
def toggle_air2(self) -> None:
"""Toggle AIR2"""
self._toggle_air(2)
def toggle_air4(self) -> None:
"""Toggle AIR4"""
self._toggle_air(4)
def _toggle_air(self, air_num: int) -> None:
"""Generic method to toggle AIR"""
status_attr = f'statusAIR{air_num}'
current_state = getattr(self, status_attr, False)
set_air_method = getattr(self, f'set_air{air_num}')
set_air_method(not current_state)
def display_ips(self) -> None:
"""Display all host IP addresses"""
self.display_all_hostaddresses()
self.replacenowTimer.setSingleShot(True)
self.replacenowTimer.start(10000)
def unset_led1(self) -> None:
"""Turn off LED1"""
self._unset_led(1)
def unset_led2(self) -> None:
"""Turn off LED2"""
self._unset_led(2)
def unset_led3(self) -> None:
"""Turn off LED3"""
self._unset_led(3)
def unset_led4(self) -> None:
"""Turn off LED4"""
self._unset_led(4)
def _unset_led(self, led_num: int) -> None:
"""Generic method to turn off LED"""
self.led_logic(led_num, False)
def led_logic(self, led: int, state: bool) -> None:
"""
Handle LED logic (on/off, autoflash, timedflash)
Args:
led: LED number (1-4)
state: True to turn on, False to turn off
"""
if led < 1 or led > 4:
logger.warning(f"Invalid LED number: {led}")
return
# Get LED-specific attributes
timer_attr = f'timerLED{led}'
set_led_attr = f'set_led{led}'
unset_led_attr = f'unset_led{led}'
led_on_attr = f'LED{led}on'
autoflash_attr = f'LED{led}Autoflash'
timedflash_attr = f'LED{led}Timedflash'
# Determine source for logging
source = "manual"
if state:
timer = getattr(self, timer_attr)
autoflash = getattr(self.settings, autoflash_attr)
timedflash = getattr(self.settings, timedflash_attr)
if autoflash.isChecked() and timer.isActive():
source = "autoflash"
elif timedflash.isChecked():
source = "timedflash"
if state:
# Turn LED on
timer = getattr(self, timer_attr)
autoflash = getattr(self.settings, autoflash_attr)
timedflash = getattr(self.settings, timedflash_attr)
if autoflash.isChecked():
timer.start(500)
if timedflash.isChecked():
timer.start(500)
QTimer.singleShot(20000, getattr(self, unset_led_attr))
set_led_method = getattr(self, set_led_attr)
set_led_method(state)
setattr(self, led_on_attr, state)
else:
# Turn LED off
set_led_method = getattr(self, set_led_attr)
set_led_method(state)
timer = getattr(self, timer_attr)
timer.stop()
setattr(self, led_on_attr, state)
# Log LED change event
self.event_logger.log_led_changed(led, state, source)
# Publish MQTT status immediately after LED change
self._publish_mqtt_status(f"led{led}")
# Broadcast WebSocket status immediately after LED change
self._broadcast_web_status()
def set_station_color(self, newcolor: QColor) -> None:
"""
Set the station label color
Args:
newcolor: QColor object for the station name label
"""
self._set_label_color(self.labelStation, newcolor)
def set_slogan_color(self, newcolor: QColor) -> None:
"""
Set the slogan label color
Args:
newcolor: QColor object for the slogan label
"""
self._set_label_color(self.labelSlogan, newcolor)
def _set_label_color(self, widget: QLabel, color: QColor) -> None:
"""
Generic method to set label color
Args:
widget: The label widget to set color for
color: QColor object to set as text color
"""
palette = widget.palette()
palette.setColor(QPalette.ColorRole.WindowText, color)
widget.setPalette(palette)
def restore_settings_from_config(self) -> None:
"""Restore all settings from configuration"""
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
settings_restorer = SettingsRestorer(self, self.settings)
settings_restorer.restore_all(settings)
def constant_update(self):
"""Slot for constant timer timeout - delegates to UI updater"""
try:
if self.ui_updater:
self.ui_updater.constant_update()
except (AttributeError, RuntimeError):
# Fallback if ui_updater not yet initialized
from ui_updater import UIUpdater
self.ui_updater = UIUpdater(self)
self.ui_updater.constant_update()
def update_date(self):
"""Update the date display - delegates to UI updater"""
try:
if self.ui_updater:
self.ui_updater.update_date()
except (AttributeError, RuntimeError):
from ui_updater import UIUpdater
self.ui_updater = UIUpdater(self)
self.ui_updater.update_date()
def update_backtiming_text(self) -> None:
"""Update the text clock display - delegates to UI updater"""
try:
if self.ui_updater:
self.ui_updater.update_backtiming_text()
except (AttributeError, RuntimeError):
from ui_updater import UIUpdater
self.ui_updater = UIUpdater(self)
self.ui_updater.update_backtiming_text()
def update_backtiming_seconds(self):
"""Update backtiming seconds - delegates to UI updater"""
try:
if self.ui_updater:
self.ui_updater.update_backtiming_seconds()
except (AttributeError, RuntimeError):
from ui_updater import UIUpdater
self.ui_updater = UIUpdater(self)
self.ui_updater.update_backtiming_seconds()
def update_ntp_status(self):
"""Update NTP status warning (priority -1)"""
try:
if self.ntp_manager:
self.ntp_manager.update_ntp_status()
except (AttributeError, RuntimeError):
# Fallback if ntp_manager not yet initialized
from ntp_manager import NTPManager
self.ntp_manager = NTPManager(self)
self.ntp_manager.update_ntp_status()
def toggle_full_screen(self):
global app
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
with settings_group(settings, "General"):
if not settings.value('fullscreen', True, type=bool):
self.showFullScreen()
app.setOverrideCursor(QCursor(Qt.CursorShape.BlankCursor))
settings.setValue('fullscreen', True)
else:
self.showNormal()
app.setOverrideCursor(QCursor(Qt.CursorShape.ArrowCursor))
settings.setValue('fullscreen', False)
def set_air1(self, action: bool) -> None:
"""Set AIR1 state (active/inactive)"""
self._set_air_state(1, action)
def update_air1_seconds(self) -> None:
"""Update AIR1 seconds display"""
self._update_air_seconds(1)
def set_air2(self, action: bool) -> None:
"""Set AIR2 state (active/inactive)"""
self._set_air_state(2, action)
def update_air2_seconds(self) -> None:
"""Update AIR2 seconds display"""
self._update_air_seconds(2)
def reset_air3(self) -> None:
"""Reset AIR3 timer"""
self._reset_air(3)
def _reset_air(self, air_num: int) -> None:
"""
Generic method to reset AIR timer
Args:
air_num: AIR number (3 or 4)
"""
if air_num not in [3, 4]:
logger.warning(f"Invalid AIR number for reset: {air_num}")
return
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
with settings_group(settings, "Timers"):
timer_attr = f'timerAIR{air_num}'
seconds_attr = f'Air{air_num}Seconds'
status_attr = f'statusAIR{air_num}'
label_attr = f'AirLabel_{air_num}'
default_texts = {3: 'Timer', 4: 'Stream'}
text_key = f'TimerAIR{air_num}Text'
timer = getattr(self, timer_attr)
timer.stop()
setattr(self, seconds_attr, 0)
label_text = settings.value(text_key, default_texts[air_num])
label_widget = getattr(self, label_attr)
seconds = getattr(self, seconds_attr)
label_widget.setText(f"{label_text}\n{int(seconds/60)}:{seconds%60:02d}")
# Log AIR reset event
self.event_logger.log_air_reset(air_num, "manual")
if getattr(self, status_attr):
timer.start(1000)
def set_air3(self, action: bool) -> None:
"""Set AIR3 state (active/inactive)"""
self._set_air_state(3, action)
def start_stop_air3(self) -> None:
"""Toggle AIR3 start/stop"""
self._start_stop_air(3)
def _start_stop_air(self, air_num: int) -> None:
"""
Generic method to toggle AIR start/stop
Args:
air_num: AIR number (3 or 4)
"""
if air_num not in [3, 4]:
logger.warning(f"Invalid AIR number for start_stop: {air_num}")
return
status_attr = f'statusAIR{air_num}'
current_state = getattr(self, status_attr, False)
if not current_state:
getattr(self, f'start_air{air_num}')()
else:
getattr(self, f'stop_air{air_num}')()
def start_air3(self) -> None:
"""Start AIR3"""
self.set_air3(True)
def stop_air3(self) -> None:
"""Stop AIR3"""
self.set_air3(False)
def update_air3_seconds(self) -> None:
"""Update AIR3 seconds display"""
self._update_air_seconds(3)
def reset_air4(self) -> None:
"""Reset AIR4 timer"""
self._reset_air(4)