6#include "schedulermodulestate.h"
7#include "schedulerjob.h"
8#include <ekos_scheduler_debug.h>
9#include "schedulerprocess.h"
10#include "schedulerjob.h"
11#include "kstarsdata.h"
15#define MAX_FAILURE_ATTEMPTS 5
20QDateTime SchedulerModuleState::m_Dawn, SchedulerModuleState::m_Dusk, SchedulerModuleState::m_PreDawnDateTime;
21GeoLocation *SchedulerModuleState::storedGeo =
nullptr;
23SchedulerModuleState::SchedulerModuleState() {}
25void SchedulerModuleState::init()
30 enablePreemptiveShutdown(SchedulerModuleState::getLocalTime());
32 setIterationSetup(
false);
33 setupNextIteration(RUN_WAKEUP, 10);
36void SchedulerModuleState::setCurrentProfile(
const QString &newName,
bool signal)
38 bool changed = (newName != m_currentProfile);
41 m_currentProfile = newName;
44 changed = (m_currentProfile != m_profiles.
first());
45 m_currentProfile = m_profiles.
first();
48 if (signal && changed)
49 emit currentProfileChanged();
52void SchedulerModuleState::updateProfiles(
const QStringList &newProfiles)
54 QString selected = currentProfile();
57 allProfiles.append(newProfiles);
59 m_profiles = allProfiles;
61 setCurrentProfile(selected,
false);
62 emit profilesChanged();
65void SchedulerModuleState::setActiveJob(SchedulerJob *newActiveJob)
67 m_activeJob = newActiveJob;
70void SchedulerModuleState::updateJobStage(SchedulerJobStage stage)
72 if (activeJob() ==
nullptr)
74 emit jobStageChanged(SCHEDSTAGE_IDLE);
78 activeJob()->setStage(stage);
79 emit jobStageChanged(stage);
87 for (
const auto &oneJob : jobs())
88 jobArray.append(oneJob->toJson());
93void SchedulerModuleState::setSchedulerState(
const SchedulerState &newState)
95 m_schedulerState = newState;
96 emit schedulerStateChanged(newState);
99void SchedulerModuleState::setCurrentPosition(
int newCurrentPosition)
101 m_currentPosition = newCurrentPosition;
102 emit currentPositionChanged(newCurrentPosition);
105void SchedulerModuleState::setStartupState(StartupState state)
107 if (m_startupState != state)
109 m_startupState = state;
110 emit startupStateChanged(state);
114void SchedulerModuleState::setShutdownState(ShutdownState state)
116 if (m_shutdownState != state)
118 m_shutdownState = state;
119 emit shutdownStateChanged(state);
123void SchedulerModuleState::setParkWaitState(ParkWaitState state)
125 if (m_parkWaitState != state)
127 m_parkWaitState = state;
128 emit parkWaitStateChanged(state);
132bool SchedulerModuleState::removeJob(
const int currentRow)
139 SchedulerJob *
const job = jobs().
at(currentRow);
142 if (job == m_activeJob)
144 emit newLog(
i18n(
"Cannot delete currently running job '%1'.", job->getName()));
147 else if (job ==
nullptr || (activeJob() ==
nullptr && schedulerState() != SCHEDULER_IDLE))
150 emit newLog(
i18n(
"Cannot delete job. Scheduler state: %1",
151 getSchedulerStatusString(schedulerState(),
true)));
155 qCDebug(KSTARS_EKOS_SCHEDULER) <<
QString(
"Job '%1' at row #%2 is being deleted.").
arg(job->getName()).
arg(currentRow + 1);
162 if (currentPosition() >= jobs().
count())
163 setCurrentPosition(jobs().
count() - 1);
170void SchedulerModuleState::enablePreemptiveShutdown(
const QDateTime &wakeupTime)
172 m_preemptiveShutdownWakeupTime = wakeupTime;
175void SchedulerModuleState::disablePreemptiveShutdown()
177 m_preemptiveShutdownWakeupTime =
QDateTime();
180const QDateTime &SchedulerModuleState::preemptiveShutdownWakeupTime()
const
182 return m_preemptiveShutdownWakeupTime;
185bool SchedulerModuleState::preemptiveShutdown()
const
187 return m_preemptiveShutdownWakeupTime.
isValid();
190void SchedulerModuleState::setEkosState(EkosState state)
192 if (m_ekosState != state)
194 qCDebug(KSTARS_EKOS_SCHEDULER) <<
"EKOS state changed from" << m_ekosState <<
"to" << state;
196 emit ekosStateChanged(state);
200bool SchedulerModuleState::increaseEkosConnectFailureCount()
202 return (++m_ekosConnectFailureCount <= MAX_FAILURE_ATTEMPTS);
205bool SchedulerModuleState::increaseParkingCapFailureCount()
207 return (++m_parkingCapFailureCount <= MAX_FAILURE_ATTEMPTS);
210bool SchedulerModuleState::increaseParkingMountFailureCount()
212 return (++m_parkingMountFailureCount <= MAX_FAILURE_ATTEMPTS);
215bool SchedulerModuleState::increaseParkingDomeFailureCount()
217 return (++m_parkingDomeFailureCount <= MAX_FAILURE_ATTEMPTS);
220void SchedulerModuleState::resetFailureCounters()
222 resetIndiConnectFailureCount();
223 resetEkosConnectFailureCount();
224 resetFocusFailureCount();
225 resetGuideFailureCount();
226 resetAlignFailureCount();
227 resetCaptureFailureCount();
230bool SchedulerModuleState::increaseIndiConnectFailureCount()
232 return (++m_indiConnectFailureCount <= MAX_FAILURE_ATTEMPTS);
235bool SchedulerModuleState::increaseCaptureFailureCount()
237 return (++m_captureFailureCount <= MAX_FAILURE_ATTEMPTS);
240bool SchedulerModuleState::increaseFocusFailureCount()
242 return (++m_focusFailureCount <= MAX_FAILURE_ATTEMPTS);
245bool SchedulerModuleState::increaseGuideFailureCount()
247 return (++m_guideFailureCount <= MAX_FAILURE_ATTEMPTS);
250bool SchedulerModuleState::increaseAlignFailureCount()
252 return (++m_alignFailureCount <= MAX_FAILURE_ATTEMPTS);
255void SchedulerModuleState::setIndiState(INDIState state)
257 if (m_indiState != state)
259 qCDebug(KSTARS_EKOS_SCHEDULER) <<
"INDI state changed from" << m_indiState <<
"to" << state;
261 emit indiStateChanged(state);
265qint64 SchedulerModuleState::getCurrentOperationMsec()
const
267 if (!currentOperationTimeStarted)
return 0;
268 return currentOperationTime.
msecsTo(KStarsData::Instance()->ut());
271void SchedulerModuleState::startCurrentOperationTimer()
273 currentOperationTimeStarted =
true;
274 currentOperationTime = KStarsData::Instance()->
ut();
277void SchedulerModuleState::cancelGuidingTimer()
279 m_restartGuidingInterval = -1;
283bool SchedulerModuleState::isGuidingTimerActive()
285 return (m_restartGuidingInterval > 0 &&
286 m_restartGuidingTime.
msecsTo(KStarsData::Instance()->ut()) >= 0);
289void SchedulerModuleState::startGuidingTimer(
int milliseconds)
291 m_restartGuidingInterval = milliseconds;
292 m_restartGuidingTime = KStarsData::Instance()->
ut();
301 return *storedLocalTime;
302 return KStarsData::Instance()->
geo()->UTtoLT(KStarsData::Instance()->clock()->utc());
310 startup = getLocalTime();
316 QDateTime dawn = startup, dusk = startup;
319 for ( ; dawn <= startup || dusk <= startup ; midnight = midnight.
addDays(1))
324 KSAlmanac const ksal(midnight, getGeo());
327 dawn = getGeo()->UTtoLT(ksal.getDate().addSecs((ksal.getDawnAstronomicalTwilight() * 24.0 + Options::dawnOffset()) *
331 dusk = getGeo()->UTtoLT(ksal.getDate().addSecs((ksal.getDuskAstronomicalTwilight() * 24.0 + Options::duskOffset()) *
337 getGeo()->lng()->Degrees());
341 if (almanacMap.
size() > 5)
344 qDeleteAll(almanacMap);
347 ksal =
new KSAlmanac(midnight, getGeo());
348 almanacMap[key] = ksal;
353 dawn = getGeo()->UTtoLT(ksal->getDate().
addSecs((ksal->getDawnAstronomicalTwilight() * 24.0 + Options::dawnOffset()) *
358 dusk = getGeo()->UTtoLT(ksal->getDate().
addSecs((ksal->getDuskAstronomicalTwilight() * 24.0 + Options::duskOffset()) *
370void SchedulerModuleState::calculateDawnDusk()
372 calculateDawnDusk(
QDateTime(), m_Dawn, m_Dusk);
374 m_PreDawnDateTime = m_Dawn.
addSecs(-60.0 * abs(Options::preDawnTime()));
375 emit updateNightTime();
382 return KStarsData::Instance()->
geo();
385bool SchedulerModuleState::hasGeo()
387 return storedGeo !=
nullptr;
390void SchedulerModuleState::setupNextIteration(SchedulerTimerState nextState)
392 setupNextIteration(nextState, m_UpdatePeriodMs);
395void SchedulerModuleState::setupNextIteration(SchedulerTimerState nextState,
int milliseconds)
397 if (iterationSetup())
399 qCDebug(KSTARS_EKOS_SCHEDULER)
400 <<
QString(
"Multiple setupNextIteration calls: current %1 %2, previous %3 %4")
401 .
arg(nextState).
arg(milliseconds).
arg(timerState()).
arg(timerInterval());
403 setTimerState(nextState);
405 if (iterationTimer().isActive())
409 iterationTimer().
stop();
410 setTimerInterval(std::max(0, milliseconds - remaining));
411 iterationTimer().
start(timerInterval());
416 setTimerInterval(milliseconds);
418 setIterationSetup(
true);
421uint SchedulerModuleState::maxFailureAttempts()
423 return MAX_FAILURE_ATTEMPTS;
426void SchedulerModuleState::clearLog()
432bool SchedulerModuleState::checkRepeatSequence()
434 return (!Options::rememberJobProgress() && Options::schedulerRepeatEverything() &&
435 (Options::schedulerExecutionSequencesLimit() == 0
436 || sequenceExecutionCounter()) < Options::schedulerExecutionSequencesLimit());
Contains all relevant information for specifying a location on Earth: City Name, State/Province name,...
A class that implements methods to find sun rise, sun set, twilight begin / end times,...
const KStarsDateTime & ut() const
Extension of QDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day,...
KStarsDateTime addSecs(double s) const
The SchedulerState class holds all attributes defining the scheduler's state.
QString i18n(const char *text, const TYPE &arg...)
Ekos is an advanced Astrophotography tool for Linux.
QDateTime addDays(qint64 ndays) const const
QDateTime addSecs(qint64 s) const const
bool isValid() const const
qint64 msecsTo(const QDateTime &other) const const
const_reference at(qsizetype i) const const
qsizetype count() const const
bool removeOne(const AT &t)
size_type size() const const
T value(const Key &key, const T &defaultValue) const const
QString arg(Args &&... args) const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const