/* * RestAPI.h * * Created on: 22.05.2019 * Author: Micha Mueller */ #ifndef DCDBPUSHER_RESTAPI_H_ #define DCDBPUSHER_RESTAPI_H_ #include "RESTHttpsServer.h" #include #include "includes/PluginDefinitions.h" #include "../analytics/AnalyticsManager.h" #include "mqttchecker.h" #include "MQTTPusher.h" #define endpointArgs http::response& 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& 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_ */