Skip to content

Commit

Permalink
Add daylight sensor config.sunriseoffset and config.sunsetoffset
Browse files Browse the repository at this point in the history
  • Loading branch information
manup committed Mar 15, 2018
1 parent 3679d1c commit 49c7ec0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
4 changes: 4 additions & 0 deletions database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,10 @@ static int sqliteLoadAllSensorsCallback(void *user, int ncols, char **colval , c
d->daylightSensorId = sensor.id();
sensor.removeItem(RConfigReachable);
sensor.addItem(DataTypeBool, RConfigConfigured);
item = sensor.addItem(DataTypeInt8, RConfigSunriseOffset);
item->setValue(30);
item =sensor.addItem(DataTypeInt8, RConfigSunsetOffset);
item->setValue(-30);
sensor.addItem(DataTypeString, RConfigLat);
sensor.addItem(DataTypeString, RConfigLong);
sensor.addItem(DataTypeBool, RStateDaylight);
Expand Down
60 changes: 39 additions & 21 deletions rest_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,12 @@ void DeRestPluginPrivate::initTimezone()
dl.setName(QLatin1String("Daylight"));
item = dl.addItem(DataTypeBool, RConfigConfigured);
item->setValue(false);
item = dl.addItem(DataTypeInt8, RConfigSunriseOffset);
item->setValue(30);
item = dl.addItem(DataTypeInt8, RConfigSunsetOffset);
item->setValue(-30);
item = dl.addItem(DataTypeBool, RStateDaylight);
item->setValue(QVariant());

item = dl.addItem(DataTypeInt32, RStateStatus);
item->setValue(QVariant());

Expand Down Expand Up @@ -3145,8 +3148,10 @@ void DeRestPluginPrivate::daylightTimerFired()

ResourceItem *daylight = sensor->item(RStateDaylight);
ResourceItem *status = sensor->item(RStateStatus);
DBG_Assert(daylight && status);
if (!daylight || !status)
ResourceItem *sunriseOffset = sensor->item(RConfigSunriseOffset);
ResourceItem *sunsetOffset = sensor->item(RConfigSunsetOffset);
DBG_Assert(daylight && status && sunriseOffset && sunsetOffset);
if (!daylight || !status || !sunriseOffset || !sunsetOffset)
{
return;
}
Expand All @@ -3158,6 +3163,8 @@ void DeRestPluginPrivate::daylightTimerFired()

const char *curName = 0;
int cur = 0;
quint64 sunrise = 0;
quint64 sunset = 0;

for (const DL_Result &r : daylightTimes)
{
Expand All @@ -3168,32 +3175,43 @@ void DeRestPluginPrivate::daylightTimerFired()
curName = r.name;
cur = r.weight;
}

if (r.weight == DL_SUNRISE_START) { sunrise = r.msecsSinceEpoch; }
else if (r.weight == DL_SUNSET_END) { sunset = r.msecsSinceEpoch; }
}

if (cur)
bool dl = false;
if (sunrise > 0 && sunset > 0)
{
bool dl = (cur > DL_SUNRISE_END && cur < DL_SUNSET_START) ? true : false;
if (!daylight->lastSet().isValid() || daylight->toBool() != dl)
{
daylight->setValue(dl);
Event e(RSensors, RStateStatus, sensor->id(), status);
enqueueEvent(e);
sensor->updateStateTimestamp();
sensor->setNeedSaveDatabase(true);
saveDatabaseItems |= DB_SENSORS;
}
sunrise += (sunriseOffset->toNumber() * 60 * 1000);
sunset += (sunsetOffset->toNumber() * 60 * 1000);

if (cur != status->toNumber())
if (nowMs > sunrise && nowMs < sunset)
{
status->setValue(cur);
Event e(RSensors, RStateStatus, sensor->id(), status);
enqueueEvent(e);
sensor->updateStateTimestamp();
sensor->setNeedSaveDatabase(true);
saveDatabaseItems |= DB_SENSORS;
dl = true;
}
}

if (!daylight->lastSet().isValid() || daylight->toBool() != dl)
{
daylight->setValue(dl);
Event e(RSensors, RStateStatus, sensor->id(), status);
enqueueEvent(e);
sensor->updateStateTimestamp();
sensor->setNeedSaveDatabase(true);
saveDatabaseItems |= DB_SENSORS;
}

if (cur && cur != status->toNumber())
{
status->setValue(cur);
Event e(RSensors, RStateStatus, sensor->id(), status);
enqueueEvent(e);
sensor->updateStateTimestamp();
sensor->setNeedSaveDatabase(true);
saveDatabaseItems |= DB_SENSORS;
}

if (curName)
{
DBG_Printf(DBG_INFO, "Daylight now: %s, status: %d\n", curName, cur);
Expand Down

0 comments on commit 49c7ec0

Please sign in to comment.