Add ScriptAlarm
This commit is contained in:
parent
9ecb2d0af4
commit
dd70129a4d
@ -28,6 +28,7 @@ HEADERS += nymeacore.h \
|
||||
ruleengine/ruleactionparam.h \
|
||||
scriptengine/script.h \
|
||||
scriptengine/scriptaction.h \
|
||||
scriptengine/scriptalarm.h \
|
||||
scriptengine/scriptengine.h \
|
||||
scriptengine/scriptevent.h \
|
||||
scriptengine/scriptstate.h \
|
||||
@ -108,6 +109,7 @@ SOURCES += nymeacore.cpp \
|
||||
ruleengine/ruleactionparam.cpp \
|
||||
scriptengine/script.cpp \
|
||||
scriptengine/scriptaction.cpp \
|
||||
scriptengine/scriptalarm.cpp \
|
||||
scriptengine/scriptengine.cpp \
|
||||
scriptengine/scriptevent.cpp \
|
||||
scriptengine/scriptstate.cpp \
|
||||
|
||||
142
libnymea-core/scriptengine/scriptalarm.cpp
Normal file
142
libnymea-core/scriptengine/scriptalarm.cpp
Normal file
@ -0,0 +1,142 @@
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* *
|
||||
* Copyright (C) 2019 Michael Zanetti <michael.zanetti@nymea.io> *
|
||||
* *
|
||||
* This file is part of nymea. *
|
||||
* *
|
||||
* nymea is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, version 2 of the License. *
|
||||
* *
|
||||
* nymea is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with nymea. If not, see <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#include "scriptalarm.h"
|
||||
#include "loggingcategories.h"
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
ScriptAlarm::ScriptAlarm(QObject *parent) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QTime ScriptAlarm::time() const
|
||||
{
|
||||
return m_time;
|
||||
}
|
||||
|
||||
void ScriptAlarm::setTime(const QTime &time)
|
||||
{
|
||||
qCDebug(dcScriptEngine()) << "Blablabla" << time;
|
||||
if (m_time != time) {
|
||||
m_time = time;
|
||||
emit timeChanged();
|
||||
|
||||
if (!time.isValid()) {
|
||||
qCWarning(dcScriptEngine()) << "Invalid time:" << time;
|
||||
}
|
||||
|
||||
if (time.isValid() && m_timerId == 0) {
|
||||
m_timerId = startTimer(1000, Qt::VeryCoarseTimer);
|
||||
} else if (!time.isValid() && m_timerId != 0) {
|
||||
killTimer(m_timerId);
|
||||
}
|
||||
|
||||
updateActive();
|
||||
}
|
||||
}
|
||||
|
||||
QTime ScriptAlarm::endTime() const
|
||||
{
|
||||
return m_endTime;
|
||||
}
|
||||
|
||||
void ScriptAlarm::setEndTime(const QTime &endTime)
|
||||
{
|
||||
if (m_endTime != endTime) {
|
||||
m_endTime = endTime;
|
||||
emit endTimeChanged();
|
||||
|
||||
updateActive();
|
||||
}
|
||||
}
|
||||
|
||||
ScriptAlarm::WeekDays ScriptAlarm::weekDays() const
|
||||
{
|
||||
return m_weekDays;
|
||||
}
|
||||
|
||||
void ScriptAlarm::setWeekDays(const WeekDays &weekDays)
|
||||
{
|
||||
if (m_weekDays != weekDays) {
|
||||
m_weekDays = weekDays;
|
||||
emit weekDaysChanged();
|
||||
|
||||
updateActive();
|
||||
}
|
||||
}
|
||||
|
||||
bool ScriptAlarm::active() const
|
||||
{
|
||||
return m_active;
|
||||
}
|
||||
|
||||
void ScriptAlarm::timerEvent(QTimerEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
QTime now = QTime::currentTime();
|
||||
|
||||
updateActive();
|
||||
|
||||
if (!m_weekDays.testFlag(today())) {
|
||||
return;
|
||||
}
|
||||
if (!m_weekDays.testFlag(today())) {
|
||||
return;
|
||||
}
|
||||
if (m_time.hour() != now.hour()) {
|
||||
return;
|
||||
}
|
||||
if (m_time.minute() != now.minute()) {
|
||||
return;
|
||||
}
|
||||
if (m_time.second() != now.second()) {
|
||||
return;
|
||||
}
|
||||
|
||||
emit triggered();
|
||||
}
|
||||
|
||||
ScriptAlarm::WeekDay ScriptAlarm::today() const
|
||||
{
|
||||
QList<WeekDay> allDays = {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
|
||||
return allDays.at(QDateTime::currentDateTime().date().dayOfWeek() - 1);
|
||||
}
|
||||
|
||||
void ScriptAlarm::updateActive()
|
||||
{
|
||||
QTime now = QTime::currentTime();
|
||||
|
||||
bool active = m_endTime.isValid() && m_weekDays.testFlag(today());
|
||||
|
||||
if (active) {
|
||||
bool beforeStart = now < m_time;
|
||||
bool afterEnd = now > m_endTime;
|
||||
if (m_time < m_endTime) {
|
||||
active = !beforeStart && !afterEnd;
|
||||
} else {
|
||||
active = beforeStart || afterEnd;
|
||||
}
|
||||
}
|
||||
if (active != m_active) {
|
||||
m_active = active;
|
||||
emit activeChanged();
|
||||
}
|
||||
}
|
||||
90
libnymea-core/scriptengine/scriptalarm.h
Normal file
90
libnymea-core/scriptengine/scriptalarm.h
Normal file
@ -0,0 +1,90 @@
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* *
|
||||
* Copyright (C) 2019 Michael Zanetti <michael.zanetti@nymea.io> *
|
||||
* *
|
||||
* This file is part of nymea. *
|
||||
* *
|
||||
* nymea is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, version 2 of the License. *
|
||||
* *
|
||||
* nymea is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with nymea. If not, see <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef SCRIPTALARM_H
|
||||
#define SCRIPTALARM_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDateTime>
|
||||
#include <QTimer>
|
||||
|
||||
class ScriptAlarm : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QTime time READ time WRITE setTime NOTIFY timeChanged)
|
||||
Q_PROPERTY(QTime endTime READ endTime WRITE setEndTime NOTIFY endTimeChanged)
|
||||
Q_PROPERTY(WeekDays weekDays READ weekDays WRITE setWeekDays NOTIFY weekDaysChanged)
|
||||
|
||||
Q_PROPERTY(bool active READ active NOTIFY activeChanged)
|
||||
|
||||
public:
|
||||
enum WeekDay {
|
||||
Monday = 0x01,
|
||||
Tuesday = 0x02,
|
||||
Wednesday = 0x04,
|
||||
Thursday = 0x08,
|
||||
Friday = 0x10,
|
||||
Saturday = 0x20,
|
||||
Sunday = 0x40,
|
||||
AllDays = 0xFF
|
||||
};
|
||||
Q_ENUM(WeekDay)
|
||||
Q_DECLARE_FLAGS(WeekDays, WeekDay)
|
||||
Q_FLAG(WeekDays)
|
||||
|
||||
explicit ScriptAlarm(QObject *parent = nullptr);
|
||||
|
||||
QTime time() const;
|
||||
void setTime(const QTime &time);
|
||||
|
||||
QTime endTime() const;
|
||||
void setEndTime(const QTime &endTime);
|
||||
|
||||
WeekDays weekDays() const;
|
||||
void setWeekDays(const WeekDays &weekDays);
|
||||
|
||||
bool active() const;
|
||||
|
||||
signals:
|
||||
void timeChanged();
|
||||
void endTimeChanged();
|
||||
void weekDaysChanged();
|
||||
|
||||
void triggered();
|
||||
void activeChanged();
|
||||
|
||||
protected:
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
|
||||
private:
|
||||
WeekDay today() const;
|
||||
|
||||
void updateActive();
|
||||
|
||||
private:
|
||||
QTime m_time;
|
||||
QTime m_endTime;
|
||||
WeekDays m_weekDays = AllDays;
|
||||
|
||||
bool m_active = false;
|
||||
int m_timerId = 0;
|
||||
};
|
||||
|
||||
#endif // SCRIPTALARM_H
|
||||
@ -24,6 +24,7 @@
|
||||
#include "scriptaction.h"
|
||||
#include "scriptevent.h"
|
||||
#include "scriptstate.h"
|
||||
#include "scriptalarm.h"
|
||||
|
||||
#include "nymeasettings.h"
|
||||
|
||||
@ -49,6 +50,7 @@ ScriptEngine::ScriptEngine(DeviceManager *deviceManager, QObject *parent) : QObj
|
||||
qmlRegisterType<ScriptEvent>("nymea", 1, 0, "DeviceEvent");
|
||||
qmlRegisterType<ScriptAction>("nymea", 1, 0, "DeviceAction");
|
||||
qmlRegisterType<ScriptState>("nymea", 1, 0, "DeviceState");
|
||||
qmlRegisterType<ScriptAlarm>("nymea", 1, 0, "Alarm");
|
||||
|
||||
m_engine = new QQmlEngine(this);
|
||||
m_engine->setProperty("deviceManager", reinterpret_cast<quint64>(m_deviceManager));
|
||||
|
||||
Reference in New Issue
Block a user