RestAPI.h 6.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * 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"
Micha Mueller's avatar
Micha Mueller committed
16
#include "globalconfiguration.h"
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "../analytics/AnalyticsManager.h"
#include "mqttchecker.h"
#include "MQTTPusher.h"

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

    virtual ~RestAPI() {}

    // String used as a response for the REST GET /help command
    const string restCheatSheet = "dcdbpusher RESTful API cheatsheet:\n"
Micha Mueller's avatar
Micha Mueller committed
33
34
35
36
37
38
39
40
41
42
43
44
45
                                  " -GET: /help               This help message.\n"
                                  "       /analytics/help     An help message for data analytics commands.\n"
                                  "       /plugins?[json]   D List of currently loaded plugins.\n"
                                  "       /sensors?plugin;[json]\n"
                                  "                         D List of currently running sensors which belong to\n"
                                  "                           the specified plugin.\n"
                                  "       /average?plugin;sensor;[interval]\n"
                                  "                           Average of last sensor readings from the last\n"
                                  "                           [interval] seconds or of all cached readings if no\n"
                                  "                           interval is given.\n"
                                  " -PUT: /start?plugin       Start the sensors of the plugin.\n"
                                  "       /stop?plugin        Stop the sensors of the plugin.\n"
                                  "       /reload?plugin      Reload the plugin configuration.\n"
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
                                  "\n";

private:

    /**
     * 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"
     *
89
90
     * @brief Get the average of the last readings of a sensor. Also allows
     *        access to analytics sensors.
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
     *
     * 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 "/start"
     *
Micha Mueller's avatar
Micha Mueller committed
109
     * @brief Start a plugin.
110
111
112
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
Micha Mueller's avatar
Micha Mueller committed
113
     * Required | plugin  | all plugin names     | specify the plugin
114
115
116
117
118
119
120
     * Optional |  -      |        -             |      -
     */
    void PUT_start(endpointArgs);

    /**
     * PUT "/stop"
     *
Micha Mueller's avatar
Micha Mueller committed
121
     * @brief Stop a plugin.
122
123
124
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
Micha Mueller's avatar
Micha Mueller committed
125
     * Required | plugin  | all plugin names     | specify the plugin
126
127
128
129
130
131
132
     * Optional |  -      |        -             |      -
     */
    void PUT_stop(endpointArgs);

    /**
     * PUT "/reload"
     *
Micha Mueller's avatar
Micha Mueller committed
133
     * @brief Reload a plugin's configuration (includes plugin restart).
134
135
136
137
138
139
140
141
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required | plugin  | all plugin names     | specify the plugin
     * Optional |  -      |        -             |      -
     */
    void PUT_reload(endpointArgs);

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    /**
     * PUT "/analytics/reload"
     *
     * @brief Reload configuration and initialization of all or only a specific
     *        analytics plugin.
     *
     * @detail Overwrites the method from the AnalyticsManager to ensure that
     *         MQTTPusher is stopped before reloading plugins.
     *
     * Queries  | key     | possible values      | explanation
     * -------------------------------------------------------------------------
     * Required |  -      |        -             |      -
     * Optional | plugin  | all analyzer plugin  | reload only the specified
     *          |         | names                | plugin
     */
    void PUT_analytics_reload(endpointArgs);
158

159
/******************************************************************************/
160
161
162
163
164
165
166
167
168
169
170
171
172
173

    // 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_ */