Skip to content

Commit

Permalink
Endpoint for writing custom attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI committed Oct 23, 2019
1 parent b879bb8 commit 8749dc7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions de_web.pro
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ SOURCES = authorisation.cpp \
rest_scenes.cpp \
rest_info.cpp \
rest_capabilities.cpp \
rest_attributes.cpp \
rule.cpp \
upnp.cpp \
permitJoin.cpp \
Expand Down
4 changes: 4 additions & 0 deletions de_web_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15144,6 +15144,10 @@ int DeRestPlugin::handleHttpRequest(const QHttpRequestHeader &hdr, QTcpSocket *s
{
ret = d->handleGatewaysApi(req, rsp);
}
else if (path[2] == QLatin1String("attributes"))
{
ret = d->handleAttributesApi(req, rsp);
}
else
{
resourceExist = false;
Expand Down
4 changes: 4 additions & 0 deletions de_web_plugin_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ class DeRestPluginPrivate : public QObject
int handleCapabilitiesApi(const ApiRequest &req, ApiResponse &rsp);
int getCapabilities(const ApiRequest &req, ApiResponse &rsp);

// REST API attributes
int handleAttributesApi(const ApiRequest &req, ApiResponse &rsp);
int writeAttribute(const ApiRequest &req, ApiResponse &rsp);

// REST API common
QVariantMap errorToMap(int id, const QString &ressource, const QString &description);

Expand Down
80 changes: 80 additions & 0 deletions rest_attributes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2013-2019 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/

#include <QString>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "json.h"

int DeRestPluginPrivate::handleAttributesApi(const ApiRequest &req, ApiResponse &rsp)
{
if (req.path[2] != QLatin1String("attributes"))
{
return REQ_NOT_HANDLED;
}

// GET /api/<apikey>/attributes
if ((req.path.size() == 4) && (req.hdr.method() == "POST"))
{
return writeAttribute(req, rsp);
}
return REQ_NOT_HANDLED;
}

/*! GET /api/<apikey>/attributes
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::writeAttribute(const ApiRequest &req, ApiResponse &rsp)
{
DBG_Assert(req.path.size() == 4);
const QString &id = req.path[3];
bool ok;
QVariant var = Json::parse(req.content, ok);
QVariantMap map = var.toMap();
if (!ok || map.isEmpty()) {
rsp.list.append(errorToMap(ERR_INVALID_JSON, QString("/attributes/%1").arg(id), QString("body contains invalid JSON")));
rsp.httpStatus = HttpStatusBadRequest;
return REQ_READY_SEND;
}
if (!map.contains("key")) {
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/attributes/%1").arg(id), QString("key is required")));
rsp.httpStatus = HttpStatusNotFound;
return REQ_READY_SEND;
}
if (!map.contains("value")) {
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/attributes/%1").arg(id), QString("value is required")));
rsp.httpStatus = HttpStatusNotFound;
return REQ_READY_SEND;
}

const int endpoint = 1;
const int mfcode = 0x115f;
const uint16_t key = map["key"].toInt(&ok);
const uint8_t value = map["value"].toInt(&ok);
DBG_Printf(DBG_INFO, "id: %s\n", qPrintable(id));
DBG_Printf(DBG_INFO, "key: %d\n", key);
DBG_Printf(DBG_INFO, "value: %d\n", value);

RestNodeBase *node = getSensorNodeForId(id);
DBG_Printf(DBG_INFO, "node unique id: %s\n", qPrintable(node->uniqueId()));

deCONZ::ZclAttribute attr(key, deCONZ::Zcl8BitUint, "dec", deCONZ::ZclReadWrite, true);
attr.setValue((quint64)value);
if (writeAttribute(node, endpoint, BASIC_CLUSTER_ID, attr, mfcode)) {
DBG_Printf(DBG_INFO, "Write successful\n");
rsp.list.append(QLatin1String("success"));
} else {
DBG_Printf(DBG_INFO, "Write unsuccessful\n");
rsp.list.append(QLatin1String("failure"));
}
rsp.httpStatus = HttpStatusOk;
return REQ_READY_SEND;
}

0 comments on commit 8749dc7

Please sign in to comment.