-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstatus_exporter.py
More file actions
211 lines (186 loc) · 8.45 KB
/
Copy pathstatus_exporter.py
File metadata and controls
211 lines (186 loc) · 8.45 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#############################################################################
#
# OnAirScreen
# Copyright (c) 2012-2026 Sascha Ludwig, astrastudio.de
# All rights reserved.
#
# status_exporter.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."
#
#############################################################################
"""
Status Exporter for OnAirScreen
This module handles exporting the current application status as JSON.
"""
import logging
from typing import TYPE_CHECKING
from PyQt6.QtCore import QSettings
from exceptions import WidgetAccessError, log_exception
from settings_functions import versionString, distributionString
from utils import settings_group
if TYPE_CHECKING:
from start import MainScreen
logger = logging.getLogger(__name__)
class StatusExporter:
"""
Exports current application status as JSON-serializable dictionary
This class collects status information from various components
and formats it for API responses.
"""
def __init__(self, main_screen: "MainScreen"):
"""
Initialize status exporter
Args:
main_screen: Reference to MainScreen instance
"""
self.main_screen = main_screen
def _is_led_setting_checked(self, led_num: int, setting_suffix: str) -> bool:
"""Return True if LED{n}{suffix} checkbox (e.g. Autoflash/Timedflash) is checked."""
try:
settings = getattr(self.main_screen, 'settings', None)
if not settings:
return False
widget_attr = f'LED{led_num}{setting_suffix}'
if not hasattr(settings, widget_attr):
return False
return bool(getattr(settings, widget_attr).isChecked())
except (AttributeError, RuntimeError) as e:
logger.debug(f"Could not access {setting_suffix} status for LED{led_num}: {e}")
return False
def get_status_json(self) -> dict:
"""
Get current status as JSON-serializable dictionary
Returns:
Dictionary containing current LED, AIR timer status, and text fields
"""
settings = QSettings(QSettings.Scope.UserScope, "astrastudio", "OnAirScreen")
# Get LED status
leds = {}
for led_num in range(1, 5):
# IMPORTANT: Use LED{num}on for logical status, not statusLED{num}
# statusLED{num} reflects the visual blinking state (changes between True/False)
# LED{num}on reflects the logical state (True if LED is on, even if blinking)
led_on_attr = f'LED{led_num}on'
with settings_group(settings, f"LED{led_num}"):
led_text = settings.value('text', f'LED{led_num}')
# Get logical LED status (True if LED is on, regardless of blinking state)
led_status = getattr(self.main_screen, led_on_attr, False)
# Get flash settings (web UI blinks locally when either is enabled)
autoflash_enabled = self._is_led_setting_checked(led_num, 'Autoflash')
timedflash_enabled = self._is_led_setting_checked(led_num, 'Timedflash')
leds[led_num] = {
'status': led_status, # Use logical status (LED{num}on), not visual status (statusLED{num})
'text': led_text,
'autoflash': autoflash_enabled,
'timedflash': timedflash_enabled,
}
# Get AIR timer status
air = {}
for air_num in range(1, 5):
status_attr = f'statusAIR{air_num}'
seconds_attr = f'Air{air_num}Seconds'
with settings_group(settings, "Timers"):
air_text = settings.value(f'TimerAIR{air_num}Text', f'AIR{air_num}')
air[air_num] = {
'status': getattr(self.main_screen, status_attr, False),
'seconds': getattr(self.main_screen, seconds_attr, 0),
'text': air_text,
'topOfHour': False,
}
if air_num == 3:
try:
air[air_num]['topOfHour'] = bool(
getattr(self.main_screen, 'topOfHourActive', False)
)
except RuntimeError:
# Uninitialized Qt object (e.g. in unit tests)
air[air_num]['topOfHour'] = False
# Get text field values
now_text = ""
next_text = ""
warn_text = ""
if hasattr(self.main_screen, 'labelCurrentSong') and self.main_screen.labelCurrentSong:
try:
now_text = self.main_screen.labelCurrentSong.text() or ""
except (AttributeError, RuntimeError) as e:
error = WidgetAccessError(
f"Error accessing labelCurrentSong.text(): {e}",
widget_name="labelCurrentSong",
attribute="text"
)
log_exception(logger, error, use_exc_info=False)
now_text = ""
if hasattr(self.main_screen, 'labelNews') and self.main_screen.labelNews:
try:
next_text = self.main_screen.labelNews.text() or ""
except (AttributeError, RuntimeError) as e:
error = WidgetAccessError(
f"Error accessing labelNews.text(): {e}",
widget_name="labelNews",
attribute="text"
)
log_exception(logger, error, use_exc_info=False)
next_text = ""
if hasattr(self.main_screen, 'labelWarning') and self.main_screen.labelWarning:
try:
warn_text = self.main_screen.labelWarning.text() or ""
except (AttributeError, RuntimeError) as e:
error = WidgetAccessError(
f"Error accessing labelWarning.text(): {e}",
widget_name="labelWarning",
attribute="text"
)
log_exception(logger, error, use_exc_info=False)
warn_text = ""
# Get all warnings with priorities
warnings = []
try:
if hasattr(self.main_screen, 'warning_manager') and self.main_screen.warning_manager:
warnings = self.main_screen.warning_manager.get_warnings()
except (AttributeError, RuntimeError) as e:
# If warning_manager doesn't exist or can't be accessed, use empty list
error = WidgetAccessError(
f"Error accessing warning_manager: {e}",
widget_name="MainScreen",
attribute="warning_manager"
)
log_exception(logger, error, use_exc_info=False)
pass
return {
'leds': leds,
'air': air,
'texts': {
'now': now_text,
'next': next_text,
'warn': warn_text # Keep for backward compatibility
},
'warnings': warnings, # New: all warnings with priorities
'version': versionString,
'distribution': distributionString
}