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;
73 for (
auto job : jobs())
83 for (
auto job : jobs())
90void SchedulerModuleState::updateJobStage(SchedulerJobStage stage)
92 if (activeJob() ==
nullptr)
94 emit jobStageChanged(SCHEDSTAGE_IDLE);
98 activeJob()->setStage(stage);
99 emit jobStageChanged(stage);
107 for (
const auto &oneJob : jobs())
108 jobArray.
append(oneJob->toJson());
113void SchedulerModuleState::setSchedulerState(
const SchedulerState &newState)
115 m_schedulerState = newState;
116 emit schedulerStateChanged(newState);
119void SchedulerModuleState::setCurrentPosition(
int newCurrentPosition)
121 m_currentPosition = newCurrentPosition;
122 emit currentPositionChanged(newCurrentPosition);
125void SchedulerModuleState::setStartupState(StartupState state)
127 if (m_startupState != state)
129 m_startupState = state;
130 emit startupStateChanged(state);
134void SchedulerModuleState::setShutdownState(ShutdownState state)
136 if (m_shutdownState != state)
138 m_shutdownState = state;
139 emit shutdownStateChanged(state);
143void SchedulerModuleState::setParkWaitState(ParkWaitState state)
145 if (m_parkWaitState != state)
147 m_parkWaitState = state;
148 emit parkWaitStateChanged(state);
152bool SchedulerModuleState::removeJob(
const int currentRow)
159 SchedulerJob *
const job = jobs().
at(currentRow);
162 if (job == m_activeJob)
164 emit newLog(
i18n(
"Cannot delete currently running job '%1'.", job->getName()));
167 else if (job ==
nullptr || (activeJob() ==
nullptr && schedulerState() != SCHEDULER_IDLE))
170 emit newLog(
i18n(
"Cannot delete job. Scheduler state: %1",
171 getSchedulerStatusString(schedulerState(),
true)));
175 qCDebug(KSTARS_EKOS_SCHEDULER) <<
QString(
"Job '%1' at row #%2 is being deleted.").
arg(job->getName()).
arg(currentRow + 1);
178 if (job->isLead() && job->followerJobs().count() > 0)
181 SchedulerJob *newLead = findLead(currentRow - 1);
183 if (newLead ==
nullptr)
190 for (
auto follower : job->followerJobs())
192 follower->setLeadJob(newLead);
193 newLead->followerJobs().append(follower);
203 if (currentPosition() >= jobs().count())
204 setCurrentPosition(jobs().count() - 1);
211void SchedulerModuleState::refreshFollowerLists()
214 for (
auto job : m_jobs)
215 job->followerJobs().clear();
218 for (
auto job : m_jobs)
220 SchedulerJob *lead = job->leadJob();
221 if (job->isLead() ==
false && lead !=
nullptr)
223 lead->followerJobs().append(job);
224 lead->updateSharedFollowerAttributes();
229SchedulerJob *SchedulerModuleState::findLead(
int position,
bool upward)
231 auto start = std::min(position,
static_cast<int>(jobs().count()));
235 for (
int i = start; i >= 0; i--)
236 if (jobs().at(i)->isLead())
241 for (
int i = start; i < jobs().
count(); i++)
242 if (jobs().at(i)->isLead())
250void SchedulerModuleState::enablePreemptiveShutdown(
const QDateTime &wakeupTime)
252 m_preemptiveShutdownWakeupTime = wakeupTime;
255void SchedulerModuleState::disablePreemptiveShutdown()
257 m_preemptiveShutdownWakeupTime =
QDateTime();
260const QDateTime &SchedulerModuleState::preemptiveShutdownWakeupTime()
const
262 return m_preemptiveShutdownWakeupTime;
265bool SchedulerModuleState::preemptiveShutdown()
const
267 return m_preemptiveShutdownWakeupTime.
isValid();
270void SchedulerModuleState::setEkosState(EkosState state)
272 if (m_ekosState != state)
274 qCDebug(KSTARS_EKOS_SCHEDULER) <<
"EKOS state changed from" << m_ekosState <<
"to" << state;
276 emit ekosStateChanged(state);
280bool SchedulerModuleState::increaseEkosConnectFailureCount()
282 return (++m_ekosConnectFailureCount <= MAX_FAILURE_ATTEMPTS);
285bool SchedulerModuleState::increaseParkingCapFailureCount()
287 return (++m_parkingCapFailureCount <= MAX_FAILURE_ATTEMPTS);
290bool SchedulerModuleState::increaseParkingMountFailureCount()
292 return (++m_parkingMountFailureCount <= MAX_FAILURE_ATTEMPTS);
295bool SchedulerModuleState::increaseParkingDomeFailureCount()
297 return (++m_parkingDomeFailureCount <= MAX_FAILURE_ATTEMPTS);
300void SchedulerModuleState::resetFailureCounters()
302 resetIndiConnectFailureCount();
303 resetEkosConnectFailureCount();
304 resetFocusFailureCount();
305 resetGuideFailureCount();
306 resetAlignFailureCount();
307 resetCaptureFailureCount();
310bool SchedulerModuleState::increaseIndiConnectFailureCount()
312 return (++m_indiConnectFailureCount <= MAX_FAILURE_ATTEMPTS);
315bool SchedulerModuleState::increaseCaptureFailureCount()
317 return (++m_captureFailureCount <= MAX_FAILURE_ATTEMPTS);
320bool SchedulerModuleState::increaseFocusFailureCount()
322 return (++m_focusFailureCount <= MAX_FAILURE_ATTEMPTS);
325bool SchedulerModuleState::increaseGuideFailureCount()
327 return (++m_guideFailureCount <= MAX_FAILURE_ATTEMPTS);
330bool SchedulerModuleState::increaseAlignFailureCount()
332 return (++m_alignFailureCount <= MAX_FAILURE_ATTEMPTS);
335void SchedulerModuleState::setIndiState(INDIState state)
337 if (m_indiState != state)
339 qCDebug(KSTARS_EKOS_SCHEDULER) <<
"INDI state changed from" << m_indiState <<
"to" << state;
341 emit indiStateChanged(state);
345qint64 SchedulerModuleState::getCurrentOperationMsec()
const
347 if (!currentOperationTimeStarted)
return 0;
348 return currentOperationTime.
msecsTo(KStarsData::Instance()->ut());
351void SchedulerModuleState::startCurrentOperationTimer()
353 currentOperationTimeStarted =
true;
354 currentOperationTime = KStarsData::Instance()->
ut();
357void SchedulerModuleState::cancelGuidingTimer()
359 m_restartGuidingInterval = -1;
363bool SchedulerModuleState::isGuidingTimerActive()
365 return (m_restartGuidingInterval > 0 &&
366 m_restartGuidingTime.
msecsTo(KStarsData::Instance()->ut()) >= 0);
369void SchedulerModuleState::startGuidingTimer(
int milliseconds)
371 m_restartGuidingInterval = milliseconds;
372 m_restartGuidingTime = KStarsData::Instance()->
ut();
381 return *storedLocalTime;
382 return KStarsData::Instance()->
geo()->UTtoLT(KStarsData::Instance()->clock()->utc());
390 startup = getLocalTime();
396 QDateTime dawn = startup, dusk = startup;
399 for ( ; dawn <= startup || dusk <= startup ; midnight = midnight.
addDays(1))
404 KSAlmanac const ksal(midnight, getGeo());
407 dawn = getGeo()->UTtoLT(ksal.getDate().addSecs((ksal.getDawnAstronomicalTwilight() * 24.0 + Options::dawnOffset()) *
411 dusk = getGeo()->UTtoLT(ksal.getDate().addSecs((ksal.getDuskAstronomicalTwilight() * 24.0 + Options::duskOffset()) *
417 getGeo()->lng()->Degrees());
421 if (almanacMap.
size() > 5)
424 qDeleteAll(almanacMap);
427 ksal =
new KSAlmanac(midnight, getGeo());
428 almanacMap[key] = ksal;
433 dawn = getGeo()->UTtoLT(ksal->getDate().
addSecs((ksal->getDawnAstronomicalTwilight() * 24.0 + Options::dawnOffset()) *
438 dusk = getGeo()->UTtoLT(ksal->getDate().
addSecs((ksal->getDuskAstronomicalTwilight() * 24.0 + Options::duskOffset()) *
450void SchedulerModuleState::calculateDawnDusk()
452 calculateDawnDusk(
QDateTime(), m_Dawn, m_Dusk);
454 m_PreDawnDateTime = m_Dawn.
addSecs(-60.0 * abs(Options::preDawnTime()));
455 emit updateNightTime();
462 return KStarsData::Instance()->
geo();
465bool SchedulerModuleState::hasGeo()
467 return storedGeo !=
nullptr;
470void SchedulerModuleState::setupNextIteration(SchedulerTimerState nextState)
472 setupNextIteration(nextState, m_UpdatePeriodMs);
475void SchedulerModuleState::setupNextIteration(SchedulerTimerState nextState,
int milliseconds)
477 if (iterationSetup())
479 qCDebug(KSTARS_EKOS_SCHEDULER)
480 <<
QString(
"Multiple setupNextIteration calls: current %1 %2, previous %3 %4")
481 .
arg(nextState).
arg(milliseconds).
arg(timerState()).
arg(timerInterval());
483 setTimerState(nextState);
485 if (iterationTimer().isActive())
489 iterationTimer().
stop();
490 setTimerInterval(std::max(0, milliseconds - remaining));
491 iterationTimer().
start(timerInterval());
496 setTimerInterval(milliseconds);
498 setIterationSetup(
true);
501uint SchedulerModuleState::maxFailureAttempts()
503 return MAX_FAILURE_ATTEMPTS;
506void SchedulerModuleState::clearLog()
512bool SchedulerModuleState::checkRepeatSequence()
514 return (!Options::rememberJobProgress() && Options::schedulerRepeatEverything() &&
515 (Options::schedulerExecutionSequencesLimit() == 0
516 || 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
void append(const QJsonValue &value)
void append(QList< T > &&value)
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