Skip to content

Commit

Permalink
Improve searching for new sensors
Browse files Browse the repository at this point in the history
- fire WebSocket event 'added'
- proper handling of reanimating deleted sensors
- list new sensors in GET /sensors/new
- remove legacy code
  • Loading branch information
manup committed Apr 5, 2017
1 parent 0436dda commit 21a2102
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 102 deletions.
84 changes: 23 additions & 61 deletions de_web_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ void DeRestPluginPrivate::gpDataIndication(const deCONZ::GpDataIndication &ind)

sensorNode.setNeedSaveDatabase(true);
sensors.push_back(sensorNode);

Event e(RSensors, REventAdded, sensorNode.id());
enqueueEvent(e);
queSaveDb(DB_SENSORS , DB_SHORT_SAVE_DELAY);
}
else if (sensor && sensor->deletedState() == Sensor::StateDeleted)
Expand All @@ -817,8 +820,9 @@ void DeRestPluginPrivate::gpDataIndication(const deCONZ::GpDataIndication &ind)
sensor->setDeletedState(Sensor::StateNormal);
sensor->setNeedSaveDatabase(true);
DBG_Printf(DBG_INFO, "SensorNode %u: %s reactivated\n", sensor->id().toUInt(), qPrintable(sensor->name()));
updateEtag(sensor->etag);
updateEtag(gwConfigEtag);
updateSensorEtag(sensor);
Event e(RSensors, REventAdded, sensor->id());
enqueueEvent(e);
queSaveDb(DB_SENSORS , DB_SHORT_SAVE_DELAY);
}
}
Expand Down Expand Up @@ -1689,7 +1693,7 @@ void DeRestPluginPrivate::checkSensorNodeReachable(Sensor *sensor)
updated = true;
}

if (sensor->deletedState() == Sensor::StateDeleted && gwPermitJoinDuration > 0)
if (sensor->deletedState() == Sensor::StateDeleted && findSensorsState == FindSensorsActive)
{
DBG_Printf(DBG_INFO, "Rediscovered deleted SensorNode %s set node %s\n", qPrintable(sensor->id()), qPrintable(sensor->address().toStringExt()));
sensor->setDeletedState(Sensor::StateNormal);
Expand All @@ -1698,6 +1702,8 @@ void DeRestPluginPrivate::checkSensorNodeReachable(Sensor *sensor)
queryTime = queryTime.addSecs(5);
//sensor->setLastRead(READ_BINDING_TABLE, idleTotalCounter);
updated = true;
Event e(RSensors, REventAdded, sensor->id());
enqueueEvent(e);
}
}
else
Expand Down Expand Up @@ -2303,7 +2309,8 @@ void DeRestPluginPrivate::addSensorNode(const deCONZ::Node *node, const SensorFi

if (findSensorsState == FindSensorsActive)
{
// TODO id to list
Event e(RSensors, REventAdded, sensorNode.id());
enqueueEvent(e);
}

checkSensorBindingsForAttributeReporting(&sensors.back());
Expand Down Expand Up @@ -6289,63 +6296,6 @@ void DeRestPluginPrivate::handleOnOffClusterIndication(TaskItem &task, const deC

updateEtag(gwConfigEtag);
}

Sensor *sensorNode = getSensorNodeForAddressAndEndpoint(ind.srcAddress(), ind.srcEndpoint());

if (sensorNode && sensorNode->deletedState() == Sensor::StateDeleted && findSensorsState == FindSensorsActive)
{
// reactivate deleted switch and recover group
sensorNode->setDeletedState(Sensor::StateNormal);

std::vector<Group>::iterator g = groups.begin();
std::vector<Group>::iterator gend = groups.end();

for (; g != gend; ++g)
{
std::vector<QString> &v = g->m_deviceMemberships;

if ((std::find(v.begin(), v.end(), sensorNode->id()) != v.end()) && (g->state() == Group::StateDeleted))
{
g->setState(Group::StateNormal);
updateEtag(g->etag);
break;
}
}

sensorNode->setNeedSaveDatabase(true);
updateEtag(sensorNode->etag);

std::vector<Sensor>::iterator s = sensors.begin();
std::vector<Sensor>::iterator send = sensors.end();

for (; s != send; ++s)
{
if (s->uniqueId() == sensorNode->uniqueId() && s->id() != sensorNode->id())
{
s->setNeedSaveDatabase(true);
s->setDeletedState(Sensor::StateNormal);
updateEtag(s->etag);

std::vector<Group>::iterator g = groups.begin();
std::vector<Group>::iterator gend = groups.end();

for (; g != gend; ++g)
{
std::vector<QString> &v = g->m_deviceMemberships;

if ((std::find(v.begin(), v.end(), s->id()) != v.end()) && (g->state() == Group::StateDeleted))
{
g->setState(Group::StateNormal);
updateEtag(g->etag);
break;
}
}
}
}

updateEtag(gwConfigEtag);
queSaveDb(DB_GROUPS | DB_SENSORS, DB_SHORT_SAVE_DELAY);
}
}

/*! Handle packets related to the ZCL Commissioning cluster.
Expand Down Expand Up @@ -7037,6 +6987,18 @@ void DeRestPluginPrivate::delayedFastEnddeviceProbe()
{
if (i->address().ext() == sc->address.ext())
{
if (findSensorsState == FindSensorsActive)
{
if (i->deletedState() == Sensor::StateDeleted)
{
// reanimate
i->setDeletedState(Sensor::StateNormal);
i->setNeedSaveDatabase(true);
Event e(RSensors, REventAdded, i->id());
enqueueEvent(e);
}
}

checkSensorBindingsForAttributeReporting(&*i);
}
}
Expand Down
1 change: 1 addition & 0 deletions de_web_plugin_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ public Q_SLOTS:

FindSensorsState findSensorsState;
deCONZ::Address fastProbeAddr;
QVariantMap findSensorResult;
QTimer *fastProbeTimer;
int findSensorsTimeout;
QString lastSensorsScan;
Expand Down
24 changes: 24 additions & 0 deletions event_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void DeRestPluginPrivate::eventQueueTimerFired()
{
QVariantMap map;
map["t"] = QLatin1String("event");
map["e"] = QLatin1String("changed");
map["r"] = QLatin1String("sensors");
map["id"] = e.id();
QVariantMap state;
Expand All @@ -39,6 +40,29 @@ void DeRestPluginPrivate::eventQueueTimerFired()
webSocketServer->broadcastTextMessage(Json::serialize(map));
}
}
else if (e.resource() == RSensors && e.what() == REventAdded)
{
Sensor *sensor = getSensorNodeForId(e.id());

if (sensor)
{
QVariantMap res;
res["name"] = sensor->name();
findSensorResult[sensor->id()] = res;

QVariantMap map;
map["t"] = QLatin1String("event");
map["e"] = QLatin1String("added");
map["r"] = QLatin1String("sensors");

QVariantMap smap;
sensorToMap(sensor, smap);
smap["id"] = sensor->id();
map["sensor"] = smap;

webSocketServer->broadcastTextMessage(Json::serialize(map));
}
}

eventQueue.pop_front();

Expand Down
2 changes: 2 additions & 0 deletions resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const char *RLights = "/lights";
const char *RGroups = "/groups";
const char *RConfig = "/config";

const char *REventAdded = "event/added";

const char *RInvalidSuffix = "invalid/suffix";
const char *RStateButtonEvent = "state/buttonevent";
const char *RStatePresence = "state/presence";
Expand Down
3 changes: 3 additions & 0 deletions resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ extern const char *RLights;
extern const char *RGroups;
extern const char *RConfig;

// resource events
extern const char *REventAdded;

// resouce suffixes: state/buttonevent, config/on, ...
extern const char *RInvalidSuffix;
extern const char *RStateButtonEvent;
Expand Down
65 changes: 24 additions & 41 deletions rest_sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,43 +615,12 @@ int DeRestPluginPrivate::updateSensor(const ApiRequest &req, ApiResponse &rsp)
updateSensorEtag(sensor);
}

if (mode == Sensor::ModeTwoGroups)
{
std::vector<Sensor>::iterator s = sensors.begin();
std::vector<Sensor>::iterator send = sensors.end();

for (; s != send; ++s)
{
if (s->uniqueId() == sensor->uniqueId() && s->id() != sensor->id() && s->deletedState() == Sensor::StateDeleted)
{
s->setDeletedState(Sensor::StateNormal);
s->setNeedSaveDatabase(true);
updateEtag(s->etag);

std::vector<Group>::iterator g = groups.begin();
std::vector<Group>::iterator gend = groups.end();

for (; g != gend; ++g)
{
std::vector<QString> &v = g->m_deviceMemberships;

if ((std::find(v.begin(), v.end(), s->id()) != v.end()) && (g->state() == Group::StateDeleted))
{
g->setState(Group::StateNormal);
updateEtag(g->etag);
break;
}
}

}
}
}
rspItemState[QString("/sensors/%1/mode:").arg(id)] = (double)mode;
rspItem["success"] = rspItemState;
rsp.list.append(rspItem);
updateEtag(sensor->etag);
updateEtag(gwConfigEtag);
queSaveDb(DB_SENSORS | DB_GROUPS, DB_SHORT_SAVE_DELAY);
rspItemState[QString("/sensors/%1/mode:").arg(id)] = (double)mode;
rspItem["success"] = rspItemState;
rsp.list.append(rspItem);
updateEtag(sensor->etag);
updateEtag(gwConfigEtag);
queSaveDb(DB_SENSORS | DB_GROUPS, DB_SHORT_SAVE_DELAY);
}
else
{
Expand Down Expand Up @@ -954,6 +923,13 @@ int DeRestPluginPrivate::getNewSensors(const ApiRequest &req, ApiResponse &rsp)
{
Q_UNUSED(req);

if (!findSensorResult.isEmpty() &&
(findSensorsState == FindSensorsActive || findSensorsState == FindSensorsDone))
{

rsp.map = findSensorResult;
}

if (findSensorsState == FindSensorsActive)
{
rsp.map["lastscan"] = QLatin1String("active");
Expand Down Expand Up @@ -1074,6 +1050,7 @@ void DeRestPluginPrivate::startFindSensors()
if (findSensorsState == FindSensorsIdle || findSensorsState == FindSensorsDone)
{
findSensorCandidates.clear();
findSensorResult.clear();
lastSensorsScan = QDateTime::currentDateTimeUtc().toString(QLatin1String("yyyy-MM-ddTHH:mm:ss"));
QTimer::singleShot(1000, this, SLOT(findSensorsTimerFired()));
findSensorsState = FindSensorsActive;
Expand Down Expand Up @@ -1475,7 +1452,7 @@ void DeRestPluginPrivate::handleIndicationFindSensors(const deCONZ::ApsDataIndic

bool update = false;

if (!s1 && isSceneSwitch)
if (!s1 && isSceneSwitch && findSensorsState == FindSensorsActive)
{
openDb();
sensorNode.setId(QString::number(getFreeSensorId()));
Expand All @@ -1487,24 +1464,28 @@ void DeRestPluginPrivate::handleIndicationFindSensors(const deCONZ::ApsDataIndic
sensors.push_back(sensorNode);
s1 = &sensors.back();
update = true;
Event e(RSensors, REventAdded, sensorNode.id());
enqueueEvent(e);
}
else if (isLightingSwitch)
{
if (!s1)
if (!s1 && findSensorsState == FindSensorsActive)
{
openDb();
sensorNode.setId(QString::number(getFreeSensorId()));
closeDb();
sensorNode.setMode(Sensor::ModeTwoGroups);
sensorNode.setModelId("Lighting Switch");
sensorNode.setModelId(QLatin1String("Lighting Switch"));
sensorNode.setName(QString("Lighting Switch %1").arg(sensorNode.id()));
sensorNode.setNeedSaveDatabase(true);
sensors.push_back(sensorNode);
s1 = &sensors.back();
update = true;
Event e(RSensors, REventAdded, sensorNode.id());
enqueueEvent(e);
}

if (!s2)
if (!s2 && findSensorsState == FindSensorsActive)
{
openDb();
sensorNode.setId(QString::number(getFreeSensorId()));
Expand All @@ -1517,6 +1498,8 @@ void DeRestPluginPrivate::handleIndicationFindSensors(const deCONZ::ApsDataIndic
sensors.push_back(sensorNode);
s2 = &sensors.back();
update = true;
Event e(RSensors, REventAdded, sensorNode.id());
enqueueEvent(e);
}
}

Expand Down

0 comments on commit 21a2102

Please sign in to comment.