fix offset time and validate params

pull/135/head
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)
{
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());
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();
return DeviceErrorInvalidParameter;
}
if (paramType.minValue().isValid() && param.value() < paramType.minValue()) {
qCWarning(dcDeviceManager) << "Value out of range for param" << param.name() << " Got:" << param.value().convert(paramType.type()) << " Min:" << paramType.minValue().convert(paramType.type());
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() << " Min:" << paramType.minValue().convert(paramType.type());
return DeviceErrorInvalidParameter;
}
if (!paramType.allowedValues().isEmpty() && !paramType.allowedValues().contains(param.value())) {

View File

@ -22,7 +22,12 @@
#include "loggingcategories.h"
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;
} else if (timeType == "sunrise") {
m_timeType = TimeTypeSunrise;
} else if (timeType == "noon") {
} else if (timeType == "sunnoon") {
m_timeType = TimeTypeNoon;
} else if (timeType == "sunset") {
m_timeType = TimeTypeSunset;
@ -188,6 +193,11 @@ Alarm::TimeType Alarm::timeType() const
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 offsetTime = QDateTime(dateTime).addSecs(m_offset * 60);
@ -201,10 +211,8 @@ QDateTime Alarm::calculateOffsetTime(const QDateTime &dateTime) const
}
bool Alarm::checkDayOfWeek(const QDateTime &dateTime)
{
QDateTime offsetTime = calculateOffsetTime(dateTime);
switch (offsetTime.date().dayOfWeek()) {
{
switch (dateTime.date().dayOfWeek()) {
case Qt::Monday:
return monday();
case Qt::Tuesday:
@ -226,22 +234,18 @@ bool Alarm::checkDayOfWeek(const QDateTime &dateTime)
bool Alarm::checkHour(const QDateTime &dateTime)
{
QDateTime offsetTime = calculateOffsetTime(dateTime);
if (offsetTime.time().hour() != m_hours) {
return false;
if (getAlertTime().time().hour() == dateTime.time().hour()) {
return true;
}
return true;
return false;
}
bool Alarm::checkMinute(const QDateTime &dateTime)
{
QDateTime offsetTime = calculateOffsetTime(dateTime);
if (offsetTime.time().minute() != m_minutes) {
return false;
if (getAlertTime().time().minute() == dateTime.time().minute()) {
return true;
}
return true;
return false;
}
bool Alarm::checkTimeTypes(const QDateTime &dateTime)
@ -313,7 +317,7 @@ void Alarm::validate(const QDateTime &dateTime)
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();
}

View File

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

View File

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

View File

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

View File

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