Skip to content

Commit

Permalink
Support enable/disable pairing of gateways via REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
manup committed Sep 12, 2016
1 parent d7695e3 commit 91c6c09
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions de_web_plugin_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ class DeRestPluginPrivate : public QObject
int handleGatewaysApi(const ApiRequest &req, ApiResponse &rsp);
int getAllGateways(const ApiRequest &req, ApiResponse &rsp);
int getGatewayState(const ApiRequest &req, ApiResponse &rsp);
int setGatewayState(const ApiRequest &req, ApiResponse &rsp);
void gatewayToMap(const ApiRequest &req, const Gateway *gw, QVariantMap &map);

// REST API configuration
Expand Down
2 changes: 1 addition & 1 deletion gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Gateway::Gateway(QObject *parent) :
{
Q_D(Gateway);
d->state = Gateway::StateOffline;
d->pairingEnabled = true;
d->pairingEnabled = false;
d->reply = 0;
d->manager = new QNetworkAccessManager(this);
connect(d->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
Expand Down
78 changes: 76 additions & 2 deletions rest_gateways.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ int DeRestPluginPrivate::handleGatewaysApi(const ApiRequest &req, ApiResponse &r
{
return getGatewayState(req, rsp);
}
// PUT /api/<apikey>/gateways/<id>/state
else if ((req.path.size() == 5) && (req.hdr.method() == QLatin1String("PUT")) && (req.path[4] == QLatin1String("state")))
{
return setGatewayState(req, rsp);
}

return REQ_NOT_HANDLED;
}
Expand Down Expand Up @@ -77,8 +82,8 @@ int DeRestPluginPrivate::getAllGateways(const ApiRequest &req, ApiResponse &rsp)
}

/*! GET /api/<apikey>/gateways/<id>
\return 0 - on success
-1 - on error
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::getGatewayState(const ApiRequest &req, ApiResponse &rsp)
{
Expand Down Expand Up @@ -113,6 +118,75 @@ int DeRestPluginPrivate::getGatewayState(const ApiRequest &req, ApiResponse &rsp
return REQ_READY_SEND;
}

/*! PUT /api/<apikey>/gateways/<id>
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::setGatewayState(const ApiRequest &req, ApiResponse &rsp)
{
DBG_Assert(req.path.size() == 5);

if (req.path.size() != 5)
{
return REQ_NOT_HANDLED;
}

rsp.httpStatus = HttpStatusOk;

bool ok;
const QString &id = req.path[3];
size_t idx = id.toUInt(&ok);

if (!ok || idx == 0 || (idx - 1) >= gateways.size())
{
rsp.list.append(errorToMap(ERR_RESOURCE_NOT_AVAILABLE, QString("/gateways/%1").arg(id), QString("resource, /gateways/%1, not available").arg(id)));
rsp.httpStatus = HttpStatusNotFound;
return REQ_READY_SEND;
}

Gateway *gw = gateways[idx - 1];

QVariant var = Json::parse(req.content, ok);
QVariantMap map = var.toMap();

if (!ok || map.isEmpty())
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errorToMap(ERR_INVALID_JSON, QString("/gateways/%1/state").arg(id), QLatin1String("body contains invalid JSON")));
return REQ_READY_SEND;
}

if (map.contains(QLatin1String("pairing")))
{
if (map[QLatin1String("pairing")].type() == QVariant::Bool)
{
bool pairing = map[QLatin1String("pairing")].toBool();
if (gw->pairingEnabled() != pairing)
{
gw->setPairingEnabled(pairing);
}
QVariantMap rspItem;
QVariantMap rspItemState;
rspItemState[QString("/gateways/%1/state/pairing").arg(id)] = map[QLatin1String("pairing")];
rspItem[QLatin1String("success")] = rspItemState;
rsp.list.append(rspItem);
}
else
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/gateways/%1/state/pairing").arg(id), QString("invalid value, %1, for parameter, pairing").arg(map[QLatin1String("pairing")].toString())));
rsp.httpStatus = HttpStatusBadRequest;
return REQ_READY_SEND;
}
}

if (!rsp.list.empty())
{
return REQ_READY_SEND;
}

return REQ_NOT_HANDLED;
}

/*! Puts all parameters in a map for later JSON serialization.
*/
void DeRestPluginPrivate::gatewayToMap(const ApiRequest &req, const Gateway *gw, QVariantMap &map)
Expand Down

0 comments on commit 91c6c09

Please sign in to comment.