fix verrify param method

add countdown
This commit is contained in:
Simon Stürz 2015-06-28 22:11:14 +02:00 committed by Michael Zanetti
parent ccf0410411
commit 3738721065
8 changed files with 314 additions and 29 deletions

View File

@ -1260,9 +1260,6 @@ DeviceManager::DeviceError DeviceManager::verifyParam(const QList<ParamType> par
DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType &paramType, const Param &param)
{
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());

View File

@ -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();
}

View File

@ -0,0 +1,104 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#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();
}
}

View File

@ -0,0 +1,62 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef COUNTDOWN_H
#define COUNTDOWN_H
#include <QObject>
#include <QTime>
#include <QTimer>
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

View File

@ -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

View File

@ -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<Alarm *>(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<Countdown *>(sender());
Device *device = m_countdowns.key(countdown);
emit emitEvent(Event(timeoutEventTypeId, device->id()));
}
void DevicePluginDateTime::onCountdownRunningChanged(const bool &running)
{
Countdown *countdown = static_cast<Countdown *>(sender());
Device *device = m_countdowns.key(countdown);
device->setStateValue(runningStateTypeId, running);
}
void DevicePluginDateTime::onSecondChanged()
{
QDateTime currentTime = QDateTime::currentDateTime().toTimeZone(m_timeZone);

View File

@ -23,6 +23,7 @@
#include "plugin/deviceplugin.h"
#include "alarm.h"
#include "countdown.h"
#include <QDateTime>
#include <QTimeZone>
@ -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<Device *, Alarm *> m_alarms;
QHash<Device *, Countdown *> 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);

View File

@ -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"
}
]
}
]
}