Kstars

observatoryweathermodel.cpp
1/* Ekos Observatory Module
2 SPDX-FileCopyrightText: Wolfgang Reissenberger <sterne-jaeger@t-online.de>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "observatoryweathermodel.h"
8#include "Options.h"
9#include <KLocalizedString>
10
11namespace Ekos
12{
13
14void ObservatoryWeatherModel::initModel(Weather *weather)
15{
16 weatherInterface = weather;
17
18 // ensure that we start the timers if required
19 weatherChanged(status());
20
21 connect(weatherInterface, &Weather::ready, this, [&]()
22 {
23 initialized = true;
24 emit updateWeatherStatus();
25 });
26 connect(weatherInterface, &Weather::newStatus, this, &ObservatoryWeatherModel::weatherChanged);
27 connect(weatherInterface, &Weather::newWeatherData, this, &ObservatoryWeatherModel::updateWeatherData);
28 connect(weatherInterface, &Weather::newWeatherData, this, &ObservatoryWeatherModel::newWeatherData);
29 connect(weatherInterface, &Weather::disconnected, this, [&]()
30 {
31 emit disconnected();
32 initialized = false;
33 });
34
35 // read the default values
36 warningActionsActive = Options::warningActionsActive();
37 warningActions.parkDome = Options::weatherWarningCloseDome();
38 warningActions.closeShutter = Options::weatherWarningCloseShutter();
39 warningActions.delay = Options::weatherWarningDelay();
40 alertActionsActive = Options::alertActionsActive();
41 alertActions.parkDome = Options::weatherAlertCloseDome();
42 alertActions.closeShutter = Options::weatherAlertCloseShutter();
43 alertActions.delay = Options::weatherAlertDelay();
44 m_autoScaleValues = Options::weatherAutoScaleValues();
45
46 // not implemented yet
47 warningActions.stopScheduler = false;
48 alertActions.stopScheduler = false;
49
50 warningTimer.setInterval(static_cast<int>(warningActions.delay * 1000));
51 warningTimer.setSingleShot(true);
52 alertTimer.setInterval(static_cast<int>(alertActions.delay * 1000));
53 alertTimer.setSingleShot(true);
54
55 connect(&warningTimer, &QTimer::timeout, [this]()
56 {
57 execute(warningActions);
58 });
59 connect(&alertTimer, &QTimer::timeout, [this]()
60 {
61 execute(alertActions);
62 });
63
64 if (weatherInterface->status() != ISD::Weather::WEATHER_IDLE)
65 emit ready();
66
67 initialized = true;
68}
69
70ISD::Weather::Status ObservatoryWeatherModel::status()
71{
72 if (weatherInterface == nullptr)
73 return ISD::Weather::WEATHER_IDLE;
74
75 return weatherInterface->status();
76}
77
78bool ObservatoryWeatherModel::refresh()
79{
80 return weatherInterface->refresh();
81}
82
83void ObservatoryWeatherModel::setWarningActionsActive(bool active)
84{
85 warningActionsActive = active;
86 Options::setWarningActionsActive(active);
87
88 // stop warning actions if deactivated
89 if (!active && warningTimer.isActive())
90 warningTimer.stop();
91 // start warning timer if activated
92 else if (weatherInterface->status() == ISD::Weather::WEATHER_WARNING)
93 startWarningTimer();
94}
95
96void ObservatoryWeatherModel::startWarningTimer()
97{
98 if (warningActionsActive && (warningActions.parkDome || warningActions.closeShutter || warningActions.stopScheduler))
99 {
100 if (!warningTimer.isActive())
101 warningTimer.start();
102 }
103 else if (warningTimer.isActive())
104 warningTimer.stop();
105}
106
107void ObservatoryWeatherModel::setAlertActionsActive(bool active)
108{
109 alertActionsActive = active;
110 Options::setAlertActionsActive(active);
111
112 // stop alert actions if deactivated
113 if (!active && alertTimer.isActive())
114 alertTimer.stop();
115 // start alert timer if activated
116 else if (weatherInterface->status() == ISD::Weather::WEATHER_ALERT)
117 startAlertTimer();
118}
119
120void ObservatoryWeatherModel::setAutoScaleValues(bool value)
121{
122 m_autoScaleValues = value;
123 Options::setWeatherAutoScaleValues(value);
124}
125
126void ObservatoryWeatherModel::startAlertTimer()
127{
128 if (alertActionsActive && (alertActions.parkDome || alertActions.closeShutter || alertActions.stopScheduler))
129 {
130 if (!alertTimer.isActive())
131 alertTimer.start();
132 }
133 else if (alertTimer.isActive())
134 alertTimer.stop();
135}
136
137void ObservatoryWeatherModel::setWarningActions(WeatherActions actions)
138{
139 warningActions = actions;
140 Options::setWeatherWarningCloseDome(actions.parkDome);
141 Options::setWeatherWarningCloseShutter(actions.closeShutter);
142 Options::setWeatherWarningDelay(actions.delay);
143 if (!warningTimer.isActive())
144 warningTimer.setInterval(static_cast<int>(actions.delay * 1000));
145
146 if (weatherInterface->status() == ISD::Weather::WEATHER_WARNING)
147 startWarningTimer();
148}
149
150
151QString ObservatoryWeatherModel::getWarningActionsStatus()
152{
153 if (warningTimer.isActive())
154 {
155 int remaining = warningTimer.remainingTime() / 1000;
156 return i18np("%1 second remaining", "%1 seconds remaining", remaining);
157 }
158
159 return i18n("Status: inactive");
160}
161
162void ObservatoryWeatherModel::setAlertActions(WeatherActions actions)
163{
164 alertActions = actions;
165 Options::setWeatherAlertCloseDome(actions.parkDome);
166 Options::setWeatherAlertCloseShutter(actions.closeShutter);
167 Options::setWeatherAlertDelay(actions.delay);
168 if (!alertTimer.isActive())
169 alertTimer.setInterval(static_cast<int>(actions.delay * 1000));
170
171 if (weatherInterface->status() == ISD::Weather::WEATHER_ALERT)
172 startAlertTimer();
173}
174
175QString ObservatoryWeatherModel::getAlertActionsStatus()
176{
177 if (alertTimer.isActive())
178 {
179 int remaining = alertTimer.remainingTime() / 1000;
180 return i18np("%1 second remaining", "%1 seconds remaining", remaining);
181 }
182
183 return i18n("Status: inactive");
184}
185
186void ObservatoryWeatherModel::updateWeatherStatus()
187{
188 weatherChanged(status());
189 emit ready();
190}
191
192
193void ObservatoryWeatherModel::weatherChanged(ISD::Weather::Status status)
194{
195 switch (status)
196 {
197 case ISD::Weather::WEATHER_OK:
198 warningTimer.stop();
199 alertTimer.stop();
200 break;
201 case ISD::Weather::WEATHER_WARNING:
202 alertTimer.stop();
203 startWarningTimer();
204 break;
205 case ISD::Weather::WEATHER_ALERT:
206 warningTimer.stop();
207 startAlertTimer();
208 break;
209 default:
210 break;
211 }
212 emit newStatus(status);
213}
214
215void ObservatoryWeatherModel::updateWeatherData(const std::vector<ISD::Weather::WeatherData> &data)
216{
217 // add or update all received values
218 for (auto &oneEntry : data)
219 {
220 // update if already existing
221 unsigned long pos = findWeatherData(oneEntry.name);
222 if (pos < m_WeatherData.size())
223 m_WeatherData[pos].value = oneEntry.value;
224 // new weather sensor?
225 else if (oneEntry.name.startsWith("WEATHER_"))
226 m_WeatherData.push_back({QString(oneEntry.name), QString(oneEntry.label), oneEntry.value});
227 }
228 // update UI
229 emit newStatus(status());
230}
231
232unsigned long ObservatoryWeatherModel::findWeatherData(const QString name)
233{
234 unsigned long i;
235 for (i = 0; i < m_WeatherData.size(); i++)
236 {
237 if (m_WeatherData[i].name.compare(name) == 0)
238 return i;
239 }
240 // none found
241 return i;
242}
243} // Ekos
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:83
QString name(StandardAction id)
NETWORKMANAGERQT_EXPORT NetworkManager::Status status()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
void setInterval(int msec)
bool isActive() const const
void setSingleShot(bool singleShot)
void start()
void stop()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.