fix offset time and validate params

This commit is contained in:
Simon Stürz 2015-06-28 18:43:38 +02:00 committed by Michael Zanetti
parent 23f3a35a83
commit ccf0410411
6 changed files with 91 additions and 79 deletions

View File

@ -1260,6 +1260,10 @@ DeviceManager::DeviceError DeviceManager::verifyParam(const QList<ParamType> par
DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType &paramType, const Param &param) DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType &paramType, const Param &param)
{ {
if (paramType.name() == param.name()) { 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())) { if (!param.value().canConvert(paramType.type())) {
qCWarning(dcDeviceManager) << "Wrong parameter type for param" << param.name() << " Got:" << param.value() << " Expected:" << QVariant::typeToName(paramType.type()); qCWarning(dcDeviceManager) << "Wrong parameter type for param" << param.name() << " Got:" << param.value() << " Expected:" << QVariant::typeToName(paramType.type());
return DeviceErrorInvalidParameter; return DeviceErrorInvalidParameter;
@ -1269,8 +1273,8 @@ DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType &paramType
qCWarning(dcDeviceManager) << "Value out of range for param" << param.name() << " Got:" << param.value() << " Max:" << paramType.maxValue(); qCWarning(dcDeviceManager) << "Value out of range for param" << param.name() << " Got:" << param.value() << " Max:" << paramType.maxValue();
return DeviceErrorInvalidParameter; return DeviceErrorInvalidParameter;
} }
if (paramType.minValue().isValid() && param.value() < paramType.minValue()) { if (paramType.minValue().isValid() && param.value().convert(paramType.type()) < paramType.minValue().convert(paramType.type())) {
qCWarning(dcDeviceManager) << "Value out of range for param" << param.name() << " Got:" << param.value().convert(paramType.type()) << " Min:" << paramType.minValue().convert(paramType.type()); qCWarning(dcDeviceManager) << "Value out of range for param" << param.name() << " Got:" << param.value() << " Min:" << paramType.minValue().convert(paramType.type());
return DeviceErrorInvalidParameter; return DeviceErrorInvalidParameter;
} }
if (!paramType.allowedValues().isEmpty() && !paramType.allowedValues().contains(param.value())) { if (!paramType.allowedValues().isEmpty() && !paramType.allowedValues().contains(param.value())) {

View File

@ -22,7 +22,12 @@
#include "loggingcategories.h" #include "loggingcategories.h"
Alarm::Alarm(QObject *parent) : Alarm::Alarm(QObject *parent) :
QObject(parent) QObject(parent),
m_duskOffset(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_sunriseOffset(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_noonOffset(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_sunsetOffset(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_dawnOffset(QDateTime(QDate(1970, 1, 1), QTime(0,0,0)))
{ {
} }
@ -174,7 +179,7 @@ void Alarm::setTimeType(const QString &timeType)
m_timeType = TimeTypeDusk; m_timeType = TimeTypeDusk;
} else if (timeType == "sunrise") { } else if (timeType == "sunrise") {
m_timeType = TimeTypeSunrise; m_timeType = TimeTypeSunrise;
} else if (timeType == "noon") { } else if (timeType == "sunnoon") {
m_timeType = TimeTypeNoon; m_timeType = TimeTypeNoon;
} else if (timeType == "sunset") { } else if (timeType == "sunset") {
m_timeType = TimeTypeSunset; m_timeType = TimeTypeSunset;
@ -188,6 +193,11 @@ Alarm::TimeType Alarm::timeType() const
return m_timeType; return m_timeType;
} }
QDateTime Alarm::getAlertTime() const
{
return QDateTime(QDate::currentDate(), QTime(hours(), minutes())).addSecs(m_offset * 60);
}
QDateTime Alarm::calculateOffsetTime(const QDateTime &dateTime) const QDateTime Alarm::calculateOffsetTime(const QDateTime &dateTime) const
{ {
QDateTime offsetTime = QDateTime(dateTime).addSecs(m_offset * 60); QDateTime offsetTime = QDateTime(dateTime).addSecs(m_offset * 60);
@ -201,10 +211,8 @@ QDateTime Alarm::calculateOffsetTime(const QDateTime &dateTime) const
} }
bool Alarm::checkDayOfWeek(const QDateTime &dateTime) bool Alarm::checkDayOfWeek(const QDateTime &dateTime)
{ {
QDateTime offsetTime = calculateOffsetTime(dateTime); switch (dateTime.date().dayOfWeek()) {
switch (offsetTime.date().dayOfWeek()) {
case Qt::Monday: case Qt::Monday:
return monday(); return monday();
case Qt::Tuesday: case Qt::Tuesday:
@ -226,22 +234,18 @@ bool Alarm::checkDayOfWeek(const QDateTime &dateTime)
bool Alarm::checkHour(const QDateTime &dateTime) bool Alarm::checkHour(const QDateTime &dateTime)
{ {
QDateTime offsetTime = calculateOffsetTime(dateTime); if (getAlertTime().time().hour() == dateTime.time().hour()) {
return true;
if (offsetTime.time().hour() != m_hours) {
return false;
} }
return true; return false;
} }
bool Alarm::checkMinute(const QDateTime &dateTime) bool Alarm::checkMinute(const QDateTime &dateTime)
{ {
QDateTime offsetTime = calculateOffsetTime(dateTime); if (getAlertTime().time().minute() == dateTime.time().minute()) {
return true;
if (offsetTime.time().minute() != m_minutes) {
return false;
} }
return true; return false;
} }
bool Alarm::checkTimeTypes(const QDateTime &dateTime) bool Alarm::checkTimeTypes(const QDateTime &dateTime)
@ -313,7 +317,7 @@ void Alarm::validate(const QDateTime &dateTime)
return; return;
} }
qCDebug(dcDateTime) << name() << "time match" << QTime(hours(), minutes()).toString("hh:mm") << "with offset" << m_offset; qCDebug(dcDateTime) << name() << "time match" << dateTime.time().toString() << QTime(hours(), minutes()).toString("hh:mm") << "with offset" << m_offset;
emit alarm(); emit alarm();
} }

View File

@ -103,6 +103,7 @@ private:
QDateTime m_sunsetOffset; QDateTime m_sunsetOffset;
QDateTime m_dawnOffset; QDateTime m_dawnOffset;
QDateTime getAlertTime() const;
QDateTime calculateOffsetTime(const QDateTime &dateTime) const; QDateTime calculateOffsetTime(const QDateTime &dateTime) const;
bool checkDayOfWeek(const QDateTime &dateTime); bool checkDayOfWeek(const QDateTime &dateTime);

View File

@ -90,7 +90,12 @@
DevicePluginDateTime::DevicePluginDateTime() : DevicePluginDateTime::DevicePluginDateTime() :
m_timer(0), m_timer(0),
m_todayDevice(0), m_todayDevice(0),
m_timeZone(QTimeZone("Europe/Vienna")) m_timeZone(QTimeZone("Europe/Vienna")),
m_dusk(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_sunrise(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_noon(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_sunset(QDateTime(QDate(1970, 1, 1), QTime(0,0,0))),
m_dawn(QDateTime(QDate(1970, 1, 1), QTime(0,0,0)))
{ {
m_timer = new QTimer(this); m_timer = new QTimer(this);
m_timer->setInterval(1000); m_timer->setInterval(1000);
@ -98,10 +103,7 @@ DevicePluginDateTime::DevicePluginDateTime() :
m_timeZone = QTimeZone(configValue("timezone").toByteArray()); m_timeZone = QTimeZone(configValue("timezone").toByteArray());
m_currentDateTime = QDateTime(QDate::currentDate(), QTime::currentTime(), m_timeZone); m_currentDateTime = QDateTime(QDate::currentDate(), QTime::currentTime(), m_timeZone);
connect(m_timer, &QTimer::timeout, this, &DevicePluginDateTime::onTimeout); connect(m_timer, &QTimer::timeout, this, &DevicePluginDateTime::onSecondChanged);
connect(this, &DevicePluginDateTime::minuteChanged, this, &DevicePluginDateTime::onMinuteChanged);
connect(this, &DevicePluginDateTime::hourChanged, this, &DevicePluginDateTime::onHourChanged);
connect(this, &DevicePluginDateTime::dayChanged, this, &DevicePluginDateTime::onDayChanged);
connect(this, &DevicePluginDateTime::configValueChanged, this, &DevicePluginDateTime::onConfigValueChanged); connect(this, &DevicePluginDateTime::configValueChanged, this, &DevicePluginDateTime::onConfigValueChanged);
} }
@ -172,10 +174,11 @@ DeviceManager::DeviceSetupStatus DevicePluginDateTime::setupDevice(Device *devic
void DevicePluginDateTime::postSetupDevice(Device *device) void DevicePluginDateTime::postSetupDevice(Device *device)
{ {
Q_UNUSED(device) Q_UNUSED(device)
QDateTime zoneTime = QDateTime::currentDateTime().toTimeZone(m_timeZone);
updateTimes(); updateTimes();
onMinuteChanged(); onMinuteChanged(zoneTime);
onHourChanged(); onHourChanged(zoneTime);
onDayChanged(); onDayChanged(zoneTime);
} }
void DevicePluginDateTime::deviceRemoved(Device *device) void DevicePluginDateTime::deviceRemoved(Device *device)
@ -346,27 +349,6 @@ void DevicePluginDateTime::processTimesData(const QByteArray &data)
updateTimes(); updateTimes();
} }
void DevicePluginDateTime::onTimeout()
{
QDateTime zoneTime = QDateTime::currentDateTime().toTimeZone(m_timeZone);
//qCDebug(dcDateTime) << m_timeZone.id() << zoneTime.toString();
validateTimeTypes(zoneTime);
if (zoneTime.date() != m_currentDateTime.date())
emit dayChanged();
if (zoneTime.time().hour() != m_currentDateTime.time().hour())
emit hourChanged();
if (zoneTime.time().minute() != m_currentDateTime.time().minute())
emit minuteChanged();
// just store for compairing
m_currentDateTime = zoneTime;
}
void DevicePluginDateTime::onAlarm() void DevicePluginDateTime::onAlarm()
{ {
Alarm *alarm = static_cast<Alarm *>(sender()); Alarm *alarm = static_cast<Alarm *>(sender());
@ -377,42 +359,58 @@ void DevicePluginDateTime::onAlarm()
emit emitEvent(Event(alarmEventTypeId, device->id())); emit emitEvent(Event(alarmEventTypeId, device->id()));
} }
void DevicePluginDateTime::onMinuteChanged() void DevicePluginDateTime::onSecondChanged()
{ {
QDateTime zoneTime = QDateTime::currentDateTime().toTimeZone(m_timeZone); QDateTime currentTime = QDateTime::currentDateTime().toTimeZone(m_timeZone);
qCDebug(dcDateTime) << "minute changed" << zoneTime.toString(); // make shore that ms are 0
QDateTime zoneTime = QDateTime(QDate(currentTime.date()), QTime(currentTime.time().hour(), currentTime.time().minute(), currentTime.time().second(), 0));
validateTimeTypes(zoneTime);
if (zoneTime.date() != m_currentDateTime.date())
onDayChanged(zoneTime);
if (zoneTime.time().hour() != m_currentDateTime.time().hour())
onHourChanged(zoneTime);
if (zoneTime.time().minute() != m_currentDateTime.time().minute())
onMinuteChanged(zoneTime);
// just store for compairing
m_currentDateTime = zoneTime;
}
void DevicePluginDateTime::onMinuteChanged(const QDateTime &dateTime)
{
qCDebug(dcDateTime) << "minute changed" << dateTime.toString();
// validate alerts // validate alerts
foreach (Device *device, deviceManager()->findConfiguredDevices(alarmDeviceClassId)) { foreach (Alarm *alarm, m_alarms.values()) {
Alarm *alarm = m_alarms.value(device); alarm->validate(dateTime);
alarm->validate(zoneTime);
} }
} }
void DevicePluginDateTime::onHourChanged() void DevicePluginDateTime::onHourChanged(const QDateTime &dateTime)
{ {
QDateTime zoneTime = QDateTime::currentDateTime().toTimeZone(m_timeZone); qCDebug(dcDateTime) << "hour changed" << dateTime.toString();
qCDebug(dcDateTime) << "hour changed" << zoneTime.toString();
// check every hour in case we are offline in the wrong moment // check every hour in case we are offline in the wrong moment
searchGeoLocation(); searchGeoLocation();
} }
void DevicePluginDateTime::onDayChanged() void DevicePluginDateTime::onDayChanged(const QDateTime &dateTime)
{ {
QDateTime zoneTime = QDateTime::currentDateTime().toTimeZone(m_timeZone); qCDebug(dcDateTime) << "day changed" << dateTime.toString();
qCDebug(dcDateTime) << "day changed" << zoneTime.toString();
if (m_todayDevice == 0) if (m_todayDevice == 0)
return; return;
m_todayDevice->setStateValue(dayStateTypeId, zoneTime.date().day()); m_todayDevice->setStateValue(dayStateTypeId, dateTime.date().day());
m_todayDevice->setStateValue(monthStateTypeId, zoneTime.date().month()); m_todayDevice->setStateValue(monthStateTypeId, dateTime.date().month());
m_todayDevice->setStateValue(yearStateTypeId, zoneTime.date().year()); m_todayDevice->setStateValue(yearStateTypeId, dateTime.date().year());
m_todayDevice->setStateValue(weekdayStateTypeId, zoneTime.date().dayOfWeek()); m_todayDevice->setStateValue(weekdayStateTypeId, dateTime.date().dayOfWeek());
m_todayDevice->setStateValue(weekdayNameStateTypeId, zoneTime.date().longDayName(zoneTime.date().dayOfWeek())); m_todayDevice->setStateValue(weekdayNameStateTypeId, dateTime.date().longDayName(dateTime.date().dayOfWeek()));
m_todayDevice->setStateValue(monthNameStateTypeId, zoneTime.date().longMonthName(zoneTime.date().month())); m_todayDevice->setStateValue(monthNameStateTypeId, dateTime.date().longMonthName(dateTime.date().month()));
if(zoneTime.date().dayOfWeek() == 6 || zoneTime.date().dayOfWeek() == 7){ if(dateTime.date().dayOfWeek() == 6 || dateTime.date().dayOfWeek() == 7){
m_todayDevice->setStateValue(weekendStateTypeId, true); m_todayDevice->setStateValue(weekendStateTypeId, true);
}else{ }else{
m_todayDevice->setStateValue(weekendStateTypeId, false); m_todayDevice->setStateValue(weekendStateTypeId, false);
@ -474,8 +472,7 @@ void DevicePluginDateTime::validateTimeTypes(const QDateTime &dateTime)
emit emitEvent(Event(sunsetEventTypeId, m_todayDevice->id())); emit emitEvent(Event(sunsetEventTypeId, m_todayDevice->id()));
} }
foreach (Device *device, deviceManager()->findConfiguredDevices(alarmDeviceClassId)) { foreach (Alarm *alarm, m_alarms.values()) {
Alarm *alarm = m_alarms.value(device);
alarm->validateTimes(dateTime); alarm->validateTimes(dateTime);
} }
} }

View File

@ -60,9 +60,8 @@ private:
QDateTime m_dusk; QDateTime m_dusk;
QDateTime m_sunrise; QDateTime m_sunrise;
QDateTime m_noon; QDateTime m_noon;
QDateTime m_dawn;
QDateTime m_sunset; QDateTime m_sunset;
QDateTime m_dawn;
QList<QNetworkReply *> m_locationReplies; QList<QNetworkReply *> m_locationReplies;
QList<QNetworkReply *> m_timeReplies; QList<QNetworkReply *> m_timeReplies;
@ -79,16 +78,13 @@ signals:
void noon(); void noon();
void sunrise(); void sunrise();
void dawn(); void dawn();
void minuteChanged();
void hourChanged();
void dayChanged();
private slots: private slots:
void onTimeout();
void onAlarm(); void onAlarm();
void onMinuteChanged(); void onSecondChanged();
void onHourChanged(); void onMinuteChanged(const QDateTime &dateTime);
void onDayChanged(); void onHourChanged(const QDateTime &dateTime);
void onDayChanged(const QDateTime &dateTime);
void updateTimes(); void updateTimes();

View File

@ -227,6 +227,16 @@
"idName": "alarm", "idName": "alarm",
"name": "alarm" "name": "alarm"
} }
],
"stateTypes": [
{
"id": "261b79b5-b60d-4b14-af95-dc87184ec332",
"idName": "nextAlarm",
"name": "next alarm",
"unit": "UnixTime",
"type": "int",
"defaultValue": 0
}
] ]
} }
] ]