RestAPI.h 11.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * RestAPI.h
 *
 *  Created on: 22.05.2019
 *      Author: Micha Mueller
 */

#ifndef DCDBPUSHER_RESTAPI_H_
#define DCDBPUSHER_RESTAPI_H_

#include "RESTHttpsServer.h"

#include <boost/asio.hpp>

#include "includes/PluginDefinitions.h"
#include "../analytics/AnalyticsManager.h"
#include "mqttchecker.h"
#include "MQTTPusher.h"

#define endpointArgs http::response<http::string_body>& res, queries_t& queries
#define stdBind(fun) std::bind(&RestAPI::fun, \
          this, \
          std::placeholders::_1, \
          std::placeholders::_2)

class RestAPI : public RESTHttpsServer {
public:
    RestAPI(serverSettings_t settings,
            pluginVector_t& plugins,
            MQTTPusher* mqttPusher,
            AnalyticsManager* manager,
            boost::asio::io_service& io);

    virtual ~RestAPI() {}

    //TODO rewrite help-section + README + endpoint docs once finished
    // String used as a response for the REST GET /help command
    const string restCheatSheet = "dcdbpusher RESTful API cheatsheet:\n"
                                  " -GET:  /help     This help message\n"
                                  "        /analytics/help\n"
                                  "                  An help message for data analytics commands\n"
                                  "        /plugins  List of currently loaded plugins (Discovery)\n"
                                  "        /[plugin]/sensors\n"
                                  "                  List of currently running sensors which belong\n"
                                  "                  to the specified plugin (Discovery)\n"
                                  "        /[plugin]/[sensor]/avg?interval=[timeInSec]\n"
                                  "                  Average of last sensor readings from the last\n"
                                  "                  [interval] seconds or of all cached readings\n"
                                  "                  if no interval is given\n"
                                  " -PUT:  /[plugin]/[start|stop|reload]\n"
                                  "                  Start/stop the sensors of the plugin or\n"
                                  "                  reload the plugin configuration\n"
                                  "\n";

private:

    /**
     * GET "/analytics/help"
     *
     * @brief Return a cheatsheet of available REST API endpoints specific for
     *        the analytics manager.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void GET_analytics_help(endpointArgs);

    /**
     * GET "/analytics/plugins"
     *
     * @brief List all data analytic plugins.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional | json    | true                 | format response as json
     */
    void GET_analytics_plugins(endpointArgs);

    /**
     * GET "/analytics/sensors"
     *
     * @brief List all running sensors in one or all analyzers of a plugin.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required | plugin  | all analyzer plugin  | specify the plugin
     *          |         | names                |
     * Optional | analyzer| all analyzers of a   | restrict sensors list to an
     *          |         | plugin               | analyzer
     *          | json    | true                 | format response as json
     */
    void GET_analytics_sensors(endpointArgs);

    /**
     * GET "/analytics/units"
     *
     * @brief List all units of a plugin sensors are associated with
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required | plugin  | all analyzer plugin  | specify the plugin
     *          |         | names                |
     * Optional | analyzer| all analyzers of a   | restrict unit list to an
     *          |         | plugin               | analyzer
     *          | json    | true                 | format response as json
     */
    void GET_analytics_units(endpointArgs);

    /**
     * GET "/analytics/analyzers"
     *
     * @brief List all running analyzers of a plugin.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required | plugin  | all analyzer plugin  | specify the plugin
     *          |         | names                |
     * Optional | json    | true                 | format response as json
     */
    void GET_analytics_analyzers(endpointArgs);

    /**
     * GET "/help"
     *
     * @brief Return a cheatsheet of possible REST API endpoints.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void GET_help(endpointArgs);

    /**
     * GET "/plugins"
     *
     * @brief List all loaded dcdbpusher plugins.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional | json    | true                 | format response as json
     */
    void GET_plugins(endpointArgs);

    /**
     * GET "/sensors"
     *
     * @brief List all sensors of a specific plugin.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required | plugin  | all plugin names     | specify the plugin
     * Optional | json    | true                 | format response as json
     */
    void GET_sensors(endpointArgs);

    /**
     * GET "/average"
     *
     * @brief Get the average of the last readings of a sensor.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required | plugin  | all plugin names     | specify the plugin
     *          | sensor  | all sensor names of  | specify the sensor within the
     *          |         | the plugin or the    | plugin
     *          |         | analytics manager    |
     * Optional | interval| number of seconds    | use only readings more recent
     *          |         |                      | than (now - interval) for
     *          |         |                      | average calculation
     */
    void GET_average(endpointArgs);

/******************************************************************************/

    /**
     * PUT "/analytics/start"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void PUT_analytics_start(endpointArgs);

    /**
     * PUT "/analytics/stop"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void PUT_analytics_stop(endpointArgs);

    /**
     * PUT "/analytics/reload"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void PUT_analytics_reload(endpointArgs);

    /**
     * PUT "/analytics/compute"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void PUT_analytics_compute(endpointArgs);

    /**
     * PUT "/analytics/analyzer"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void PUT_analytics_analyzer(endpointArgs);

    /**
     * PUT "/start"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void PUT_start(endpointArgs);

    /**
     * PUT "/stop"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional |  -      |        -             |      -
     */
    void PUT_stop(endpointArgs);

    /**
     * PUT "/reload"
     *
     * @brief
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required | plugin  | all plugin names     | specify the plugin
     * Optional |  -      |        -             |      -
     */
    void PUT_reload(endpointArgs);

/******************************************************************************/

    // Utility method to check the status of the analytics manager.
    // Return true if loaded, false otherwise.
    inline bool managerLoaded(http::response<http::string_body>& res) {
        if (_manager->getStatus() != AnalyticsManager::LOADED) {
            const std::string err = "AnalyticsManager is not loaded!";
            RESTAPILOG(error) << err;
            res.body() = err;
            res.result(http::status::internal_server_error);
            return false;
        }
        return true;
    }

    // Utility method to retrieve the value for a key-query from queries.
    // Return the associated value if found or "" otherwise.
    inline std::string getQuery(const std::string& key, queries_t& queries) {
        try {
            return queries.at(key);
        } catch (const std::out_of_range&) {
            //fall through
        }
        return "";
    }

    // Utility method to remove all MQTT topics associated to a plugin from the used set
    void removeTopics(dl_t p);

    // Utility method to check for the validity of all MQTT topics in a plugin
    bool checkTopics(dl_t p);

    pluginVector_t& _plugins;
    MQTTPusher* _mqttPusher;
    AnalyticsManager* _manager;
    boost::asio::io_service& _io;
};

#endif /* DCDBPUSHER_RESTAPI_H_ */