From dd70129a4d5ba06e5db5b7d994a1f95ba56563fa Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 9 Dec 2019 13:17:35 +0100 Subject: [PATCH] Add ScriptAlarm --- libnymea-core/libnymea-core.pro | 2 + libnymea-core/scriptengine/scriptalarm.cpp | 142 ++++++++++++++++++++ libnymea-core/scriptengine/scriptalarm.h | 90 +++++++++++++ libnymea-core/scriptengine/scriptengine.cpp | 2 + 4 files changed, 236 insertions(+) create mode 100644 libnymea-core/scriptengine/scriptalarm.cpp create mode 100644 libnymea-core/scriptengine/scriptalarm.h diff --git a/libnymea-core/libnymea-core.pro b/libnymea-core/libnymea-core.pro index 8b0696fc..090642c1 100644 --- a/libnymea-core/libnymea-core.pro +++ b/libnymea-core/libnymea-core.pro @@ -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 \ diff --git a/libnymea-core/scriptengine/scriptalarm.cpp b/libnymea-core/scriptengine/scriptalarm.cpp new file mode 100644 index 00000000..e2facfae --- /dev/null +++ b/libnymea-core/scriptengine/scriptalarm.cpp @@ -0,0 +1,142 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * 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 . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "scriptalarm.h" +#include "loggingcategories.h" + +#include + +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 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(); + } +} diff --git a/libnymea-core/scriptengine/scriptalarm.h b/libnymea-core/scriptengine/scriptalarm.h new file mode 100644 index 00000000..bb82a4bf --- /dev/null +++ b/libnymea-core/scriptengine/scriptalarm.h @@ -0,0 +1,90 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * 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 . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef SCRIPTALARM_H +#define SCRIPTALARM_H + +#include +#include +#include + +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 diff --git a/libnymea-core/scriptengine/scriptengine.cpp b/libnymea-core/scriptengine/scriptengine.cpp index 1c557f1e..d75a9239 100644 --- a/libnymea-core/scriptengine/scriptengine.cpp +++ b/libnymea-core/scriptengine/scriptengine.cpp @@ -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("nymea", 1, 0, "DeviceEvent"); qmlRegisterType("nymea", 1, 0, "DeviceAction"); qmlRegisterType("nymea", 1, 0, "DeviceState"); + qmlRegisterType("nymea", 1, 0, "Alarm"); m_engine = new QQmlEngine(this); m_engine->setProperty("deviceManager", reinterpret_cast(m_deviceManager));