Skip to content

Commit 7f3c45c

Browse files
authored
Move REST API common code to rest_api.cpp/h (dresden-elektronik#7674)
1 parent 6bf2997 commit 7f3c45c

14 files changed

+199
-158
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ set(PLUGIN_INCLUDE_FILES
9191
resource.h
9292
resourcelinks.h
9393
rest_alarmsystems.h
94+
rest_api.h
9495
rest_devices.h
9596
rest_node_base.h
9697
rule.h
@@ -187,6 +188,7 @@ add_library(${PROJECT_NAME} SHARED
187188
resource.cpp
188189
resourcelinks.cpp
189190
rest_alarmsystems.cpp
191+
rest_api.cpp
190192
rest_capabilities.cpp
191193
rest_configuration.cpp
192194
rest_devices.cpp

de_web_plugin.cpp

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "rest_devices.h"
5252
#include "rest_alarmsystems.h"
5353
#include "read_files.h"
54+
#include "tuya.h"
5455
#include "utils/utils.h"
5556
#include "xiaomi.h"
5657
#include "zcl/zcl.h"
@@ -65,23 +66,6 @@
6566

6667
DeRestPluginPrivate *plugin = nullptr;
6768

68-
const char *HttpStatusOk = "200 OK"; // OK
69-
const char *HttpStatusAccepted = "202 Accepted"; // Accepted but not complete
70-
const char *HttpStatusNotModified = "304 Not Modified"; // For ETag / If-None-Match
71-
const char *HttpStatusBadRequest = "400 Bad Request"; // Malformed request
72-
const char *HttpStatusUnauthorized = "401 Unauthorized"; // Unauthorized
73-
const char *HttpStatusForbidden = "403 Forbidden"; // Understand request but no permission
74-
const char *HttpStatusNotFound = "404 Not Found"; // Requested uri not found
75-
const char *HttpStatusServiceUnavailable = "503 Service Unavailable";
76-
const char *HttpStatusNotImplemented = "501 Not Implemented";
77-
const char *HttpContentHtml = "text/html; charset=utf-8";
78-
const char *HttpContentCss = "text/css";
79-
const char *HttpContentJson = "application/json; charset=utf-8";
80-
const char *HttpContentJS = "text/javascript";
81-
const char *HttpContentPNG = "image/png";
82-
const char *HttpContentJPG = "image/jpg";
83-
const char *HttpContentSVG = "image/svg+xml";
84-
8569
static int checkZclAttributesDelay = 750;
8670
static uint MaxGroupTasks = 4;
8771

@@ -2243,26 +2227,6 @@ bool DeRestPluginPrivate::isInNetwork()
22432227
return false;
22442228
}
22452229

2246-
/*! Creates a error map used in JSON response.
2247-
\param id - error id
2248-
\param ressource example: "/lights/2"
2249-
\param description example: "resource, /lights/2, not available"
2250-
\return the map
2251-
*/
2252-
QVariantMap errorToMap(int id, const QString &ressource, const QString &description)
2253-
{
2254-
QVariantMap map;
2255-
QVariantMap error;
2256-
error["type"] = (double)id;
2257-
error["address"] = ressource.toHtmlEscaped();
2258-
error["description"] = description.toHtmlEscaped();
2259-
map["error"] = error;
2260-
2261-
DBG_Printf(DBG_INFO_L2, "API error %d, %s, %s\n", id, qPrintable(ressource), qPrintable(description));
2262-
2263-
return map;
2264-
}
2265-
22662230
/*! Creates a new unique ETag for a resource.
22672231
*/
22682232
void DeRestPluginPrivate::updateEtag(QString &etag)

de_web_plugin_private.h

Lines changed: 1 addition & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#include <stdint.h>
1919
#include <queue>
2020
#include <memory>
21-
#if QT_VERSION < 0x050000
22-
#include <QHttpRequestHeader>
23-
#endif
2421
#include <sqlite3.h>
2522
#include <deconz.h>
2623
#include "device.h"
@@ -31,19 +28,17 @@
3128
#include "event_emitter.h"
3229
#include "green_power.h"
3330
#include "resource.h"
31+
#include "rest_api.h"
3432
#include "rest_node_base.h"
3533
#include "light_node.h"
3634
#include "group.h"
3735
#include "group_info.h"
38-
#include "ias_zone.h"
3936
#include "scene.h"
4037
#include "sensor.h"
4138
#include "resourcelinks.h"
4239
#include "rule.h"
4340
#include "bindings.h"
44-
#include <math.h>
4541
#include "websocket_server.h"
46-
#include "tuya.h"
4742

4843
// enable domain specific string literals
4944
using namespace deCONZ::literals;
@@ -53,35 +48,6 @@ using namespace deCONZ::literals;
5348
#define ARCH_ARM
5449
#endif
5550

56-
/*! JSON generic error message codes */
57-
#define ERR_UNAUTHORIZED_USER 1
58-
#define ERR_INVALID_JSON 2
59-
#define ERR_RESOURCE_NOT_AVAILABLE 3
60-
#define ERR_METHOD_NOT_AVAILABLE 4
61-
#define ERR_MISSING_PARAMETER 5
62-
#define ERR_PARAMETER_NOT_AVAILABLE 6
63-
#define ERR_INVALID_VALUE 7
64-
#define ERR_PARAMETER_NOT_MODIFIABLE 8
65-
#define ERR_TOO_MANY_ITEMS 11
66-
#define ERR_DUPLICATE_EXIST 100 // de extension
67-
#define ERR_NOT_ALLOWED_SENSOR_TYPE 501
68-
#define ERR_SENSOR_LIST_FULL 502
69-
#define ERR_RULE_ENGINE_FULL 601
70-
#define ERR_CONDITION_ERROR 607
71-
#define ERR_ACTION_ERROR 608
72-
#define ERR_INTERNAL_ERROR 901
73-
74-
#define ERR_NOT_CONNECTED 950 // de extension
75-
#define ERR_BRIDGE_BUSY 951 // de extension
76-
77-
#define ERR_LINK_BUTTON_NOT_PRESSED 101
78-
#define ERR_DEVICE_OFF 201
79-
#define ERR_DEVICE_NOT_REACHABLE 202
80-
#define ERR_BRIDGE_GROUP_TABLE_FULL 301
81-
#define ERR_DEVICE_GROUP_TABLE_FULL 302
82-
83-
#define ERR_DEVICE_SCENES_TABLE_FULL 402 // de extension
84-
8551
#define IDLE_TIMER_INTERVAL 1000
8652
#define IDLE_LIMIT 30
8753
#define IDLE_READ_LIMIT 120
@@ -428,10 +394,6 @@ using namespace deCONZ::literals;
428394
#define MAX_RULE_NAME_LENGTH 64
429395
#define MAX_SENSOR_NAME_LENGTH 32
430396

431-
// REST API return codes
432-
#define REQ_READY_SEND 0
433-
#define REQ_NOT_HANDLED -1
434-
435397
// Special application return codes
436398
#define APP_RET_UPDATE 40
437399
#define APP_RET_RESTART_APP 41
@@ -489,9 +451,6 @@ void getTime(quint32 *time, qint32 *tz, quint32 *dstStart, quint32 *dstEnd, qint
489451
int getFreeSensorId(); // TODO needs to be part of a Database class
490452
int getFreeLightId(); // TODO needs to be part of a Database class
491453

492-
// REST API common
493-
QVariantMap errorToMap(int id, const QString &ressource, const QString &description);
494-
495454
extern const quint64 macPrefixMask;
496455

497456
extern const quint64 celMacPrefix;
@@ -704,24 +663,6 @@ inline bool checkMacAndVendor(const deCONZ::Node *node, quint16 vendor)
704663
quint8 zclNextSequenceNumber();
705664
const deCONZ::Node *getCoreNode(uint64_t extAddress);
706665

707-
// HTTP status codes
708-
extern const char *HttpStatusOk;
709-
extern const char *HttpStatusAccepted;
710-
extern const char *HttpStatusNotModified;
711-
extern const char *HttpStatusUnauthorized;
712-
extern const char *HttpStatusBadRequest;
713-
extern const char *HttpStatusForbidden;
714-
extern const char *HttpStatusNotFound;
715-
extern const char *HttpStatusNotImplemented;
716-
extern const char *HttpStatusServiceUnavailable;
717-
extern const char *HttpContentHtml;
718-
extern const char *HttpContentCss;
719-
extern const char *HttpContentJson;
720-
extern const char *HttpContentJS;
721-
extern const char *HttpContentPNG;
722-
extern const char *HttpContentJPG;
723-
extern const char *HttpContentSVG;
724-
725666
// Forward declarations
726667
class DeviceDescriptions;
727668
class DeviceWidget;
@@ -981,67 +922,6 @@ class ApiAuth
981922
QString useragent;
982923
};
983924

984-
enum ApiVersion
985-
{
986-
ApiVersion_1, //!< common version 1.0
987-
ApiVersion_1_DDEL, //!< version 1.0, "Accept: application/vnd.ddel.v1"
988-
ApiVersion_1_1_DDEL, //!< version 1.1, "Accept: application/vnd.ddel.v1.1"
989-
ApiVersion_2_DDEL, //!< version 2.0, "Accept: application/vnd.ddel.v2"
990-
ApiVersion_3_DDEL //!< version 3.0, "Accept: application/vnd.ddel.v3"
991-
};
992-
993-
enum ApiAuthorisation
994-
{
995-
ApiAuthNone,
996-
ApiAuthLocal,
997-
ApiAuthInternal,
998-
ApiAuthFull
999-
};
1000-
1001-
enum ApiMode
1002-
{
1003-
ApiModeNormal,
1004-
ApiModeStrict,
1005-
ApiModeEcho,
1006-
ApiModeHue
1007-
};
1008-
1009-
/*! \class ApiRequest
1010-
1011-
Helper to simplify HTTP REST request handling.
1012-
*/
1013-
class ApiRequest
1014-
{
1015-
public:
1016-
ApiRequest(const QHttpRequestHeader &h, const QStringList &p, QTcpSocket *s, const QString &c);
1017-
QString apikey() const;
1018-
ApiVersion apiVersion() const { return version; }
1019-
1020-
const QHttpRequestHeader &hdr;
1021-
const QStringList &path;
1022-
QTcpSocket *sock;
1023-
QString content;
1024-
ApiVersion version;
1025-
ApiAuthorisation auth;
1026-
ApiMode mode;
1027-
};
1028-
1029-
/*! \class ApiResponse
1030-
1031-
Helper to simplify HTTP REST request handling.
1032-
*/
1033-
class ApiResponse
1034-
{
1035-
public:
1036-
QString etag;
1037-
const char *httpStatus;
1038-
const char *contentType;
1039-
QList<QPair<QString, QString> > hdrFields; // extra header fields
1040-
QVariantMap map; // json content
1041-
QVariantList list; // json content
1042-
QString str; // json string
1043-
};
1044-
1045925
/*! \class ApiConfig
1046926
1047927
Provide config to the resource system.

electrical_measurement.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <math.h>
12
#include "de_web_plugin.h"
23
#include "de_web_plugin_private.h"
34
#include "device.h"

hue.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
#include <QString>
6+
#include <math.h>
67
#include "de_web_plugin.h"
78
#include "de_web_plugin_private.h"
89

rest_api.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2024 dresden elektronik ingenieurtechnik gmbh.
3+
* All rights reserved.
4+
*
5+
* The software in this package is published under the terms of the BSD
6+
* style license a copy of which has been included with this distribution in
7+
* the LICENSE.txt file.
8+
*
9+
*/
10+
11+
#include <deconz/dbg_trace.h>
12+
#include "rest_api.h"
13+
14+
const char *HttpStatusOk = "200 OK"; // OK
15+
const char *HttpStatusAccepted = "202 Accepted"; // Accepted but not complete
16+
const char *HttpStatusNotModified = "304 Not Modified"; // For ETag / If-None-Match
17+
const char *HttpStatusBadRequest = "400 Bad Request"; // Malformed request
18+
const char *HttpStatusUnauthorized = "401 Unauthorized"; // Unauthorized
19+
const char *HttpStatusForbidden = "403 Forbidden"; // Understand request but no permission
20+
const char *HttpStatusNotFound = "404 Not Found"; // Requested uri not found
21+
const char *HttpStatusServiceUnavailable = "503 Service Unavailable";
22+
const char *HttpStatusNotImplemented = "501 Not Implemented";
23+
const char *HttpContentHtml = "text/html; charset=utf-8";
24+
const char *HttpContentCss = "text/css";
25+
const char *HttpContentJson = "application/json; charset=utf-8";
26+
const char *HttpContentJS = "text/javascript";
27+
const char *HttpContentPNG = "image/png";
28+
const char *HttpContentJPG = "image/jpg";
29+
const char *HttpContentSVG = "image/svg+xml";
30+
31+
/*! Creates a error map used in JSON response.
32+
\param id - error id
33+
\param ressource example: "/lights/2"
34+
\param description example: "resource, /lights/2, not available"
35+
\return the map
36+
*/
37+
QVariantMap errorToMap(int id, const QString &ressource, const QString &description)
38+
{
39+
QVariantMap map;
40+
QVariantMap error;
41+
error["type"] = (double)id;
42+
error["address"] = ressource.toHtmlEscaped();
43+
error["description"] = description.toHtmlEscaped();
44+
map["error"] = error;
45+
46+
DBG_Printf(DBG_INFO_L2, "API error %d, %s, %s\n", id, qPrintable(ressource), qPrintable(description));
47+
48+
return map;
49+
}

0 commit comments

Comments
 (0)