diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index dca881e2..b8505b55 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -1260,9 +1260,6 @@ DeviceManager::DeviceError DeviceManager::verifyParam(const QList par DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType ¶mType, const Param ¶m) { if (paramType.name() == param.name()) { - qCDebug(dcDeviceManager) << paramType; - qCDebug(dcDeviceManager) << param; - qCDebug(dcDeviceManager) << QVariant::typeToName(paramType.type()) << param.value() << param.value().convert(paramType.type()); if (!param.value().canConvert(paramType.type())) { qCWarning(dcDeviceManager) << "Wrong parameter type for param" << param.name() << " Got:" << param.value() << " Expected:" << QVariant::typeToName(paramType.type()); diff --git a/plugins/deviceplugins/datetime/alarm.cpp b/plugins/deviceplugins/datetime/alarm.cpp index 66a9cab0..0eeb87d1 100644 --- a/plugins/deviceplugins/datetime/alarm.cpp +++ b/plugins/deviceplugins/datetime/alarm.cpp @@ -200,14 +200,7 @@ QDateTime Alarm::getAlertTime() const QDateTime Alarm::calculateOffsetTime(const QDateTime &dateTime) const { - QDateTime offsetTime = QDateTime(dateTime).addSecs(m_offset * 60); - - qCDebug(dcDateTime) << name() << "original time:" << dateTime.toString(); - qCDebug(dcDateTime) << name() << "offset:" << m_offset << "minutes"; - qCDebug(dcDateTime) << name() << "new time:" << offsetTime.toString(); - qCDebug(dcDateTime) << name() << "--------------------------------------------"; - - return offsetTime; + return QDateTime(dateTime).addSecs(m_offset * 60); } bool Alarm::checkDayOfWeek(const QDateTime &dateTime) @@ -297,27 +290,17 @@ void Alarm::validate(const QDateTime &dateTime) if (m_timeType != TimeTypeTime) return; - qCDebug(dcDateTime) << name() << "validate..."; - - if (!checkDayOfWeek(dateTime)) { - qCDebug(dcDateTime) << name() << "check day...FAIL"; + if (!checkDayOfWeek(dateTime)) return; - } - qCDebug(dcDateTime) << name() << "check day...OK"; // check if should use the given time - if (!checkHour(dateTime)) { - qCDebug(dcDateTime) << name() << "check hour...FAIL"; + if (!checkHour(dateTime)) return; - } - qCDebug(dcDateTime) << name() << "check hour...OK"; - if (!checkMinute(dateTime)) { - qCDebug(dcDateTime) << name() << "check minute...FAIL"; + if (!checkMinute(dateTime)) return; - } - qCDebug(dcDateTime) << name() << "time match" << dateTime.time().toString() << QTime(hours(), minutes()).toString("hh:mm") << "with offset" << m_offset; + qCDebug(dcDateTime) << name() << "time match" << dateTime.time().toString("hh:mm") << QTime(hours(), minutes()).toString("hh:mm") << "with offset" << m_offset; emit alarm(); } diff --git a/plugins/deviceplugins/datetime/countdown.cpp b/plugins/deviceplugins/datetime/countdown.cpp new file mode 100644 index 00000000..77d98110 --- /dev/null +++ b/plugins/deviceplugins/datetime/countdown.cpp @@ -0,0 +1,104 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2015 Simon Stuerz * + * * + * This file is part of guh. * + * * + * Guh 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. * + * * + * Guh 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 guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "countdown.h" +#include "loggingcategories.h" + +Countdown::Countdown(const QString &name, const QTime &time, const bool &repeating, QObject *parent) : + QObject(parent), + m_name(name), + m_time(time), + m_currentTime(time), + m_repeating(repeating) +{ + m_timer = new QTimer(this); + m_timer->setInterval(1000); + m_timer->setSingleShot(false); + + connect(m_timer, &QTimer::timeout, this, &Countdown::onTimeout); +} + +void Countdown::start() +{ + qCDebug(dcDateTime) << name() << "start" << m_currentTime.toString(); + m_currentTime = m_time; + m_timer->start(); + m_running = true; + emit runningStateChanged(true); +} + +void Countdown::stop() +{ + m_timer->stop(); + qCDebug(dcDateTime) << name() << "stop" << m_currentTime.toString(); + m_running = false; + emit runningStateChanged(false); +} + +void Countdown::restart() +{ + m_currentTime = m_time; + qCDebug(dcDateTime) << name() << "restart" << m_currentTime.toString(); + m_timer->start(); + m_running = true; + emit runningStateChanged(true); +} + +QString Countdown::name() const +{ + return m_name; +} + +bool Countdown::running() const +{ + return m_running; +} + +bool Countdown::repeating() const +{ + return m_repeating; +} + +QTime Countdown::time() const +{ + return m_time; +} + +QTime Countdown::currentTime() const +{ + return m_currentTime; +} + +void Countdown::onTimeout() +{ + m_currentTime = m_currentTime.addSecs(-1); + qCDebug(dcDateTime) << name() << m_currentTime.toString(); + if (m_currentTime == QTime(0,0)) { + qCDebug(dcDateTime) << name() << "countdown timeout."; + if (m_repeating) { + m_currentTime = m_time; + } else { + m_timer->stop(); + m_running = false; + emit runningStateChanged(false); + } + emit countdownTimeout(); + } +} diff --git a/plugins/deviceplugins/datetime/countdown.h b/plugins/deviceplugins/datetime/countdown.h new file mode 100644 index 00000000..c7a74c42 --- /dev/null +++ b/plugins/deviceplugins/datetime/countdown.h @@ -0,0 +1,62 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2015 Simon Stuerz * + * * + * This file is part of guh. * + * * + * Guh 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. * + * * + * Guh 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 guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef COUNTDOWN_H +#define COUNTDOWN_H + +#include +#include +#include + +class Countdown : public QObject +{ + Q_OBJECT +public: + explicit Countdown(const QString &name, const QTime &time, const bool &repeating, QObject *parent = 0); + + void start(); + void stop(); + void restart(); + + QString name() const; + bool running() const; + bool repeating() const; + + QTime time() const; + QTime currentTime() const; + +private: + QString m_name; + QTime m_time; + QTime m_currentTime; + QTimer *m_timer; + bool m_repeating; + bool m_running; + +signals: + void countdownTimeout(); + void runningStateChanged(const bool &running); + +private slots: + void onTimeout(); + +}; + +#endif // COUNTDOWN_H diff --git a/plugins/deviceplugins/datetime/datetime.pro b/plugins/deviceplugins/datetime/datetime.pro index e36fccca..241dfb63 100644 --- a/plugins/deviceplugins/datetime/datetime.pro +++ b/plugins/deviceplugins/datetime/datetime.pro @@ -4,9 +4,11 @@ TARGET = $$qtLibraryTarget(guh_deviceplugindatetime) SOURCES += \ deviceplugindatetime.cpp \ - alarm.cpp + alarm.cpp \ + countdown.cpp HEADERS += \ deviceplugindatetime.h \ - alarm.h + alarm.h \ + countdown.h diff --git a/plugins/deviceplugins/datetime/deviceplugindatetime.cpp b/plugins/deviceplugins/datetime/deviceplugindatetime.cpp index 8a8dc029..cc4760d7 100644 --- a/plugins/deviceplugins/datetime/deviceplugindatetime.cpp +++ b/plugins/deviceplugins/datetime/deviceplugindatetime.cpp @@ -160,12 +160,29 @@ DeviceManager::DeviceSetupStatus DevicePluginDateTime::setupDevice(Device *devic alarm->setHours(device->paramValue("hours").toInt()); alarm->setTimeType(device->paramValue("time type").toString()); alarm->setOffset(device->paramValue("offset").toInt()); + alarm->setDusk(m_dusk); + alarm->setSunrise(m_sunrise); + alarm->setNoon(m_noon); + alarm->setDawn(m_dawn); + alarm->setSunset(m_sunset); connect(alarm, &Alarm::alarm, this, &DevicePluginDateTime::onAlarm); m_alarms.insert(device, alarm); } + if (device->deviceClassId() == countdownDeviceClassId) { + Countdown *countdown = new Countdown(device->paramValue("name").toString(), + QTime(device->paramValue("hours").toInt(), + device->paramValue("minutes").toInt(), + device->paramValue("seconds").toInt()), + device->paramValue("repeating").toBool()); + + connect(countdown, &Countdown::countdownTimeout, this, &DevicePluginDateTime::onCountdownTimeout); + qCDebug(dcDateTime) << "setup countdown" << countdown->name() << countdown->time().toString(); + m_countdowns.insert(device, countdown); + } + m_timer->start(); return DeviceManager::DeviceSetupStatusSuccess; @@ -198,9 +215,35 @@ void DevicePluginDateTime::deviceRemoved(Device *device) Alarm *alarm = m_alarms.take(device); alarm->deleteLater(); } + + // countdown + if (device->deviceClassId() == countdownDeviceClassId) { + Countdown *countdown = m_countdowns.take(device); + countdown->deleteLater(); + } + startMonitoringAutoDevices(); } +DeviceManager::DeviceError DevicePluginDateTime::executeAction(Device *device, const Action &action) +{ + if (device->deviceClassId() == countdownDeviceClassId) { + Countdown *countdown = m_countdowns.value(device); + if (action.actionTypeId() == startActionTypeId) { + countdown->start(); + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == restartActionTypeId) { + countdown->restart(); + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == stopActionTypeId) { + countdown->stop(); + return DeviceManager::DeviceErrorNoError; + } + return DeviceManager::DeviceErrorActionTypeNotFound; + } + return DeviceManager::DeviceErrorNoError; +} + void DevicePluginDateTime::networkManagerReplyReady(QNetworkReply *reply) { int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -354,11 +397,25 @@ void DevicePluginDateTime::onAlarm() Alarm *alarm = static_cast(sender()); Device *device = m_alarms.key(alarm); - qCDebug(dcDateTime) << alarm->name() << "alarm!"; - emit emitEvent(Event(alarmEventTypeId, device->id())); } +void DevicePluginDateTime::onCountdownTimeout() +{ + Countdown *countdown = static_cast(sender()); + Device *device = m_countdowns.key(countdown); + + emit emitEvent(Event(timeoutEventTypeId, device->id())); +} + +void DevicePluginDateTime::onCountdownRunningChanged(const bool &running) +{ + Countdown *countdown = static_cast(sender()); + Device *device = m_countdowns.key(countdown); + + device->setStateValue(runningStateTypeId, running); +} + void DevicePluginDateTime::onSecondChanged() { QDateTime currentTime = QDateTime::currentDateTime().toTimeZone(m_timeZone); diff --git a/plugins/deviceplugins/datetime/deviceplugindatetime.h b/plugins/deviceplugins/datetime/deviceplugindatetime.h index f86170f5..1b82bca6 100644 --- a/plugins/deviceplugins/datetime/deviceplugindatetime.h +++ b/plugins/deviceplugins/datetime/deviceplugindatetime.h @@ -23,6 +23,7 @@ #include "plugin/deviceplugin.h" #include "alarm.h" +#include "countdown.h" #include #include @@ -45,6 +46,7 @@ public: void postSetupDevice(Device *device) override; void deviceRemoved(Device *device) override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; void networkManagerReplyReady(QNetworkReply *reply) override; void startMonitoringAutoDevices() override; @@ -56,6 +58,7 @@ private: QDateTime m_currentDateTime; QHash m_alarms; + QHash m_countdowns; QDateTime m_dusk; QDateTime m_sunrise; @@ -81,6 +84,8 @@ signals: private slots: void onAlarm(); + void onCountdownTimeout(); + void onCountdownRunningChanged(const bool &running); void onSecondChanged(); void onMinuteChanged(const QDateTime &dateTime); void onHourChanged(const QDateTime &dateTime); diff --git a/plugins/deviceplugins/datetime/deviceplugindatetime.json b/plugins/deviceplugins/datetime/deviceplugindatetime.json index da00f0d0..affab415 100644 --- a/plugins/deviceplugins/datetime/deviceplugindatetime.json +++ b/plugins/deviceplugins/datetime/deviceplugindatetime.json @@ -238,6 +238,81 @@ "defaultValue": 0 } ] + }, + { + "deviceClassId": "805c8948-e663-4ba6-aa67-df7446ed7098", + "idName": "countdown", + "name": "Countdown", + "createMethods": ["user"], + "paramTypes": [ + { + "name": "name", + "type": "QString", + "inputType": "TextLine" + }, + { + "name": "seconds", + "type": "int", + "unit": "Seconds", + "minValue": 0, + "maxValue": 60, + "defaultValue": 0 + }, + { + "name": "minutes", + "type": "int", + "unit": "Minutes", + "minValue": 0, + "maxValue": 60, + "defaultValue": 0 + }, + { + "name": "hours", + "type": "int", + "unit": "Hours", + "minValue": 0, + "maxValue": 24, + "defaultValue": 0 + }, + { + "name": "repeating", + "type": "bool", + "defaultValue": false + } + ], + "eventTypes": [ + { + "id": "1db11351-ad67-448e-a784-216741a06a58", + "idName": "timeout", + "name": "timeout" + } + ], + "stateTypes": [ + { + "id": "b21ccd53-1ebd-41a0-a2a3-662874e79837", + "idName": "running", + "name": "running", + "type": "bool", + "defaultValue": false + } + ], + "actionTypes": [ + { + "id": "436e9923-6eff-444e-bde7-a61228a4d748", + "idName": "start", + "name": "start" + }, + { + "id": "bf4d1def-4159-4254-a76d-fd31aaa84e2d", + "idName": "stop", + "name": "stop" + }, + { + "id": "14aad613-b972-411f-93c9-f00bfd254285", + "idName": "restart", + "name": "restart" + } + ] } ] }