Changeset View
Changeset View
Standalone View
Standalone View
kstars/ekos/observatory/observatoryweathermodel.cpp
- This file was added.
1 | /* Ekos Observatory Module | ||||
---|---|---|---|---|---|
2 | Copyright (C) Wolfgang Reissenberger <sterne-jaeger@t-online.de> | ||||
3 | | ||||
4 | This application is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | */ | ||||
9 | | ||||
10 | #include "observatoryweathermodel.h" | ||||
11 | #include "Options.h" | ||||
12 | | ||||
13 | namespace Ekos | ||||
14 | { | ||||
15 | | ||||
16 | void ObservatoryWeatherModel::initModel(Weather *weather) | ||||
17 | { | ||||
18 | mWeather = weather; | ||||
19 | | ||||
20 | // ensure that we start the timers if required | ||||
21 | weatherChanged(status()); | ||||
22 | | ||||
23 | connect(mWeather, &Weather::ready, this, &ObservatoryWeatherModel::updateWeatherStatus); | ||||
24 | connect(mWeather, &Weather::newStatus, this, &ObservatoryWeatherModel::weatherChanged); | ||||
25 | connect(mWeather, &Weather::disconnected, this, &ObservatoryWeatherModel::disconnected); | ||||
26 | | ||||
27 | // read the default values | ||||
28 | warningActionsActive = Options::warningActionsActive(); | ||||
29 | warningActions.parkDome = Options::weatherWarningCloseDome(); | ||||
30 | warningActions.closeShutter = Options::weatherWarningCloseShutter(); | ||||
31 | warningActions.delay = Options::weatherWarningDelay(); | ||||
32 | warningActions.stopScheduler = Options::weatherAlertStopScheduler(); | ||||
33 | alertActionsActive = Options::alertActionsActive(); | ||||
34 | alertActions.parkDome = Options::weatherAlertCloseDome(); | ||||
35 | alertActions.closeShutter = Options::weatherAlertCloseShutter(); | ||||
36 | alertActions.stopScheduler = Options::weatherAlertStopScheduler(); | ||||
37 | alertActions.delay = Options::weatherAlertDelay(); | ||||
38 | | ||||
39 | warningTimer.setInterval(warningActions.delay * 1000); | ||||
40 | warningTimer.setSingleShot(true); | ||||
41 | alertTimer.setInterval(alertActions.delay * 1000); | ||||
42 | alertTimer.setSingleShot(true); | ||||
43 | | ||||
44 | connect(&warningTimer, &QTimer::timeout, [this]() { execute(warningActions); }); | ||||
45 | connect(&alertTimer, &QTimer::timeout, [this]() { execute(alertActions); }); | ||||
46 | | ||||
47 | if (mWeather->status() != ISD::Weather::WEATHER_IDLE) | ||||
48 | emit ready(); | ||||
49 | } | ||||
50 | | ||||
51 | ISD::Weather::Status ObservatoryWeatherModel::status() | ||||
52 | { | ||||
53 | if (mWeather == nullptr) | ||||
54 | return ISD::Weather::WEATHER_IDLE; | ||||
55 | | ||||
56 | return mWeather->status(); | ||||
57 | } | ||||
58 | | ||||
59 | void ObservatoryWeatherModel::setWarningActionsActive(bool active) | ||||
60 | { | ||||
61 | warningActionsActive = active; | ||||
62 | Options::setWarningActionsActive(active); | ||||
63 | | ||||
64 | // stop warning actions if deactivated | ||||
65 | if (!active && warningTimer.isActive()) | ||||
66 | warningTimer.stop(); | ||||
67 | // start warning timer if activated | ||||
68 | else if (active && !warningTimer.isActive() && mWeather->status() == ISD::Weather::WEATHER_WARNING) | ||||
69 | warningTimer.start(); | ||||
70 | } | ||||
71 | | ||||
72 | void ObservatoryWeatherModel::setAlertActionsActive(bool active) | ||||
73 | { | ||||
74 | alertActionsActive = active; | ||||
75 | Options::setAlertActionsActive(active); | ||||
76 | | ||||
77 | // stop alert actions if deactivated | ||||
78 | if (!active && alertTimer.isActive()) | ||||
79 | alertTimer.stop(); | ||||
80 | // start alert timer if activated | ||||
81 | else if (active && !alertTimer.isActive() && mWeather->status() == ISD::Weather::WEATHER_ALERT) | ||||
82 | alertTimer.start(); | ||||
83 | } | ||||
84 | | ||||
85 | void ObservatoryWeatherModel::setWarningActions(WeatherActions actions) { | ||||
86 | warningActions = actions; | ||||
87 | Options::setWeatherWarningCloseDome(actions.parkDome); | ||||
88 | Options::setWeatherWarningCloseShutter(actions.closeShutter); | ||||
89 | Options::setWeatherWarningDelay(actions.delay); | ||||
90 | warningTimer.setInterval(actions.delay * 1000); | ||||
91 | } | ||||
92 | | ||||
93 | | ||||
94 | QString ObservatoryWeatherModel::getWarningActionsStatus() | ||||
95 | { | ||||
96 | if (warningTimer.isActive()) | ||||
97 | { | ||||
98 | QString status; | ||||
99 | int remaining = warningTimer.remainingTime()/1000; | ||||
100 | return status.sprintf("%02ds remaining", remaining); | ||||
101 | } | ||||
102 | | ||||
103 | return "Status: inactive"; | ||||
104 | } | ||||
105 | | ||||
106 | void ObservatoryWeatherModel::setAlertActions(WeatherActions actions) { | ||||
107 | alertActions = actions; | ||||
108 | Options::setWeatherAlertCloseDome(actions.parkDome); | ||||
109 | Options::setWeatherAlertCloseShutter(actions.closeShutter); | ||||
110 | Options::setWeatherAlertDelay(actions.delay); | ||||
111 | alertTimer.setInterval(actions.delay * 1000); | ||||
112 | } | ||||
113 | | ||||
114 | QString ObservatoryWeatherModel::getAlertActionsStatus() | ||||
115 | { | ||||
116 | if (alertTimer.isActive()) | ||||
117 | { | ||||
118 | QString status; | ||||
119 | int remaining = alertTimer.remainingTime()/1000; | ||||
120 | return status.sprintf("%02ds remaining", remaining); | ||||
121 | } | ||||
122 | | ||||
123 | return "Status: inactive"; | ||||
124 | } | ||||
125 | | ||||
126 | void ObservatoryWeatherModel::updateWeatherStatus() | ||||
127 | { | ||||
128 | weatherChanged(status()); | ||||
129 | emit ready(); | ||||
130 | } | ||||
131 | | ||||
132 | | ||||
133 | void ObservatoryWeatherModel::weatherChanged(ISD::Weather::Status status) | ||||
134 | { | ||||
135 | switch (status) { | ||||
136 | case ISD::Weather::WEATHER_OK: | ||||
137 | warningTimer.stop(); | ||||
138 | alertTimer.stop(); | ||||
139 | break; | ||||
140 | case ISD::Weather::WEATHER_WARNING: | ||||
141 | if (warningActionsActive) | ||||
142 | warningTimer.start(); | ||||
143 | alertTimer.stop(); | ||||
144 | break; | ||||
145 | case ISD::Weather::WEATHER_ALERT: | ||||
146 | warningTimer.stop(); | ||||
147 | if (alertActionsActive) | ||||
148 | alertTimer.start(); | ||||
149 | break; | ||||
150 | default: | ||||
151 | break; | ||||
152 | } | ||||
153 | emit newStatus(status); | ||||
154 | } | ||||
155 | | ||||
156 | } // Ekos |