wip plugin
This commit is contained in:
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@@ -79,5 +79,5 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"version": 3
|
"version": 4
|
||||||
}
|
}
|
||||||
@@ -1,43 +1,43 @@
|
|||||||
// Lights
|
// Outlets
|
||||||
{
|
{
|
||||||
"resources": [
|
"resources": [
|
||||||
{
|
{
|
||||||
"model": "post",
|
"model": "post",
|
||||||
"endpoint": "/v1/lights",
|
"endpoint": "/v1/outlets",
|
||||||
"ubus": {
|
"ubus": {
|
||||||
"path": "domo.lights",
|
"path": "domo.outlets",
|
||||||
"method": "create"
|
"method": "create"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "get",
|
"model": "get",
|
||||||
"endpoint": "/v1/lights",
|
"endpoint": "/v1/outlets",
|
||||||
"ubus": {
|
"ubus": {
|
||||||
"path": "domo.lights",
|
"path": "domo.outlets",
|
||||||
"method": "list"
|
"method": "list"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "get",
|
"model": "get",
|
||||||
"endpoint": "/v1/lights/:id",
|
"endpoint": "/v1/outlets/:id",
|
||||||
"ubus": {
|
"ubus": {
|
||||||
"path": "domo.lights",
|
"path": "domo.outlets",
|
||||||
"method": "read"
|
"method": "read"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "put",
|
"model": "put",
|
||||||
"endpoint": "/v1/lights/:id",
|
"endpoint": "/v1/outlets/:id",
|
||||||
"ubus": {
|
"ubus": {
|
||||||
"path": "domo.lights",
|
"path": "domo.outlets",
|
||||||
"method": "update"
|
"method": "update"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "delete",
|
"model": "delete",
|
||||||
"endpoint": "/v1/lights/:id",
|
"endpoint": "/v1/outlets/:id",
|
||||||
"ubus": {
|
"ubus": {
|
||||||
"path": "domo.lights",
|
"path": "domo.outlets",
|
||||||
"method": "delete"
|
"method": "delete"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@
|
|||||||
"path": "domo.shutters",
|
"path": "domo.shutters",
|
||||||
"method": "delete"
|
"method": "delete"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
{
|
{
|
||||||
"model": "put",
|
"model": "put",
|
||||||
"endpoint": "/v1/shutters/:id/up",
|
"endpoint": "/v1/shutters/:id/up",
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ WebConnection *WebController::new_connection(struct uhttpd_ops *an_ops, struct c
|
|||||||
*
|
*
|
||||||
* @brief return true if the controller match with the url.
|
* @brief return true if the controller match with the url.
|
||||||
*/
|
*/
|
||||||
bool WebController::find(const std::string &an_url)
|
bool WebController::find(uint8_t a_method, const std::string &an_url)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class WebController
|
|||||||
WebController(const std::string &a_path);
|
WebController(const std::string &a_path);
|
||||||
virtual ~WebController(void);
|
virtual ~WebController(void);
|
||||||
|
|
||||||
virtual bool find(const std::string &an_url);
|
virtual bool find(uint8_t a_method, const std::string &an_url);
|
||||||
|
|
||||||
void set_name(const std::string &a_name);
|
void set_name(const std::string &a_name);
|
||||||
const std::string &get_name(void);
|
const std::string &get_name(void);
|
||||||
|
|||||||
@@ -49,11 +49,11 @@ static struct dispatch_handler g_ubus_dispatch = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/*! ----------------------------------------------------------------------------
|
/*! ----------------------------------------------------------------------------
|
||||||
* @fn uh_awox_api_plugin_init
|
* @fn uh_rest_api_plugin_init
|
||||||
*
|
*
|
||||||
* @brief
|
* @brief
|
||||||
*/
|
*/
|
||||||
static int uh_awox_api_plugin_init(const struct uhttpd_ops *an_ops, struct config *a_conf)
|
static int uh_rest_api_plugin_init(const struct uhttpd_ops *an_ops, struct config *a_conf)
|
||||||
{
|
{
|
||||||
struct ubus_context *the_ctx;
|
struct ubus_context *the_ctx;
|
||||||
|
|
||||||
@@ -72,11 +72,11 @@ static int uh_awox_api_plugin_init(const struct uhttpd_ops *an_ops, struct confi
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*! ----------------------------------------------------------------------------
|
/*! ----------------------------------------------------------------------------
|
||||||
* @fn uh_awox_api_plugin_post_init
|
* @fn uh_rest_api_plugin_post_init
|
||||||
*
|
*
|
||||||
* @brief
|
* @brief
|
||||||
*/
|
*/
|
||||||
static void uh_awox_api_plugin_post_init(void)
|
static void uh_rest_api_plugin_post_init(void)
|
||||||
{
|
{
|
||||||
ubus_add_uloop(get_uhttp_server_ctx());
|
ubus_add_uloop(get_uhttp_server_ctx());
|
||||||
}
|
}
|
||||||
@@ -85,6 +85,6 @@ static void uh_awox_api_plugin_post_init(void)
|
|||||||
|
|
||||||
struct uhttpd_plugin uhttpd_plugin = {
|
struct uhttpd_plugin uhttpd_plugin = {
|
||||||
|
|
||||||
.init = uh_awox_api_plugin_init,
|
.init = uh_rest_api_plugin_init,
|
||||||
.post_init = uh_awox_api_plugin_post_init,
|
.post_init = uh_rest_api_plugin_post_init,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
/*------------------------------- INCLUDES ----------------------------------*/
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
|
|
||||||
#include "rest/rest-connection.h"
|
#include "rest/rest-connection.h"
|
||||||
@@ -108,17 +110,7 @@ bool RestController::from_json(struct json_object *a_node)
|
|||||||
// endpoint
|
// endpoint
|
||||||
if (json_object_object_get_ex(a_node, kControllerEndpointKey, &the_value_node))
|
if (json_object_object_get_ex(a_node, kControllerEndpointKey, &the_value_node))
|
||||||
{
|
{
|
||||||
int the_len, i;
|
m_endpoint = json_object_get_string(the_value_node);
|
||||||
struct json_object *the_ep_node;
|
|
||||||
the_len = json_object_array_length(the_value_node);
|
|
||||||
|
|
||||||
for (i = 0; i < the_len; i++)
|
|
||||||
{
|
|
||||||
std::string the_endpoint;
|
|
||||||
the_ep_node = json_object_array_get_idx(the_value_node, i);
|
|
||||||
the_endpoint = json_object_get_string(the_ep_node);
|
|
||||||
m_endpoint.push_back(the_endpoint);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ubus
|
// ubus
|
||||||
@@ -144,21 +136,28 @@ bool RestController::from_json(struct json_object *a_node)
|
|||||||
*
|
*
|
||||||
* @brief return true if the controller match with the url.
|
* @brief return true if the controller match with the url.
|
||||||
*/
|
*/
|
||||||
bool RestController::find(const std::string &an_url)
|
bool RestController::find(uint8_t a_method, const std::string &an_url)
|
||||||
{
|
{
|
||||||
std::vector<std::string>::iterator the_it;
|
std::vector<std::string>::iterator the_it;
|
||||||
|
std::size_t the_pos;
|
||||||
|
|
||||||
printf("find : %s\n", an_url.c_str());
|
printf("find : url: %s, endpoint:%s \n", an_url.c_str(), m_endpoint.c_str());
|
||||||
|
|
||||||
for (the_it = m_endpoint.begin(); the_it != m_endpoint.end(); ++the_it)
|
if ((m_endpoint == an_url) && (m_method == a_method))
|
||||||
{
|
{
|
||||||
int the_error;
|
|
||||||
the_error = (*the_it).compare(an_url);
|
|
||||||
printf("compare <%s> et <%s> %d\n", (*the_it).c_str(), an_url.c_str(), the_error);
|
|
||||||
if (the_error == 0)
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
the_pos = m_endpoint.find_first_of(":");
|
||||||
|
if (the_pos == std::string::npos)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("ya un param.\n");
|
||||||
|
std::string the_endpoint = m_endpoint.substr(the_pos);
|
||||||
|
printf("endpoint: %s\n", the_endpoint.c_str());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,6 +171,16 @@ const Ubus &RestController::get_ubus(void) const
|
|||||||
return m_ubus;
|
return m_ubus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*! ----------------------------------------------------------------------------
|
||||||
|
* @fn get_endpoint
|
||||||
|
*
|
||||||
|
* @brief return the endpoint for this controller.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const std::string &RestController::get_endpoint(void) const
|
||||||
|
{
|
||||||
|
return m_endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
/*! ----------------------------------------------------------------------------
|
/*! ----------------------------------------------------------------------------
|
||||||
* @fn is_raw_response
|
* @fn is_raw_response
|
||||||
|
|||||||
@@ -27,7 +27,6 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <core/web-controller.h>
|
#include <core/web-controller.h>
|
||||||
#include <rest/ubus.h>
|
#include <rest/ubus.h>
|
||||||
@@ -56,9 +55,10 @@ class RestController : public WebController
|
|||||||
|
|
||||||
bool from_json(struct json_object *a_node);
|
bool from_json(struct json_object *a_node);
|
||||||
|
|
||||||
bool find(const std::string &an_url);
|
bool find(uint8_t a_method, const std::string &an_url);
|
||||||
|
|
||||||
const Ubus &get_ubus(void) const;
|
const Ubus &get_ubus(void) const;
|
||||||
|
const std::string &get_endpoint(void) const;
|
||||||
|
|
||||||
bool is_raw_response(void);
|
bool is_raw_response(void);
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ class RestController : public WebController
|
|||||||
*/
|
*/
|
||||||
protected:
|
protected:
|
||||||
uint8_t m_method;
|
uint8_t m_method;
|
||||||
std::vector<std::string> m_endpoint;
|
std::string m_endpoint;
|
||||||
Ubus m_ubus;
|
Ubus m_ubus;
|
||||||
bool mf_raw_response;
|
bool mf_raw_response;
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ extern "C" {
|
|||||||
|
|
||||||
/*------------------------------- GLOBALS ----------------------------------*/
|
/*------------------------------- GLOBALS ----------------------------------*/
|
||||||
|
|
||||||
#define kControllerKey "controller"
|
#define kResourcesKey "resources"
|
||||||
#define kEtagControllerKey "etag_controller"
|
#define kEtagControllerKey "etag_controller"
|
||||||
|
|
||||||
#define kNotificationKey "notification"
|
#define kNotificationKey "notification"
|
||||||
@@ -242,12 +242,6 @@ UhttpServer::~UhttpServer(void)
|
|||||||
{
|
{
|
||||||
ConnectionIterator the_cnx_it;
|
ConnectionIterator the_cnx_it;
|
||||||
|
|
||||||
for (std::vector< WebController *>::iterator the_it = m_controllers.begin();
|
|
||||||
the_it != m_controllers.end(); ++the_it)
|
|
||||||
{
|
|
||||||
delete *the_it;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_controllers.clear();
|
m_controllers.clear();
|
||||||
|
|
||||||
for (the_cnx_it = m_connections.begin(); the_cnx_it != m_connections.end(); ++the_cnx_it)
|
for (the_cnx_it = m_connections.begin(); the_cnx_it != m_connections.end(); ++the_cnx_it)
|
||||||
@@ -288,12 +282,12 @@ struct ubus_context *UhttpServer::get_context(void)
|
|||||||
*/
|
*/
|
||||||
bool UhttpServer::check_url(const std::string &an_url)
|
bool UhttpServer::check_url(const std::string &an_url)
|
||||||
{
|
{
|
||||||
std::vector<WebController *>::iterator the_it;
|
ControllerIterator the_it;
|
||||||
printf("UhttpServer::check_url: %s\n", an_url.c_str());
|
printf("UhttpServer::check_url: %s\n", an_url.c_str());
|
||||||
|
|
||||||
for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
|
for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
|
||||||
{
|
{
|
||||||
if ( (*the_it)->find(an_url))
|
if ( the_it->second.get()->find(0, an_url))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -328,7 +322,7 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
|
|||||||
the_url = an_url;
|
the_url = an_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
the_controller = get_controller(the_url);
|
the_controller = get_controller(a_cl->request.method, the_url);
|
||||||
if (the_controller == NULL)
|
if (the_controller == NULL)
|
||||||
{
|
{
|
||||||
printf ("not found :( \n");
|
printf ("not found :( \n");
|
||||||
@@ -474,7 +468,7 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node)
|
|||||||
int the_len;
|
int the_len;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (json_object_object_get_ex(a_root_node, kControllerKey, &the_ctr_array_node))
|
if (json_object_object_get_ex(a_root_node, kResourcesKey, &the_ctr_array_node))
|
||||||
{
|
{
|
||||||
RestController *the_rest_controller;
|
RestController *the_rest_controller;
|
||||||
the_len = json_object_array_length(the_ctr_array_node);
|
the_len = json_object_array_length(the_ctr_array_node);
|
||||||
@@ -487,7 +481,7 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node)
|
|||||||
if (the_rest_controller->from_json(the_ctrl_node))
|
if (the_rest_controller->from_json(the_ctrl_node))
|
||||||
{
|
{
|
||||||
//new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response)
|
//new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response)
|
||||||
add_controller("", the_rest_controller);
|
add_controller(the_rest_controller->get_endpoint(), the_rest_controller);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -660,18 +654,12 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
|
|||||||
{
|
{
|
||||||
std::string the_path;
|
std::string the_path;
|
||||||
std::size_t the_pos;
|
std::size_t the_pos;
|
||||||
|
|
||||||
if (a_controller == NULL)
|
if (a_controller == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (an_uri.empty())
|
|
||||||
{
|
|
||||||
m_controllers.push_back(a_controller);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
a_controller->set_name(an_uri);
|
a_controller->set_name(an_uri);
|
||||||
// TODO
|
m_controllers[an_uri] = std::unique_ptr<WebController>(a_controller);
|
||||||
}
|
|
||||||
|
|
||||||
//m_controller_array[an_uri] = a_controller;
|
//m_controller_array[an_uri] = a_controller;
|
||||||
|
|
||||||
@@ -698,17 +686,17 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
|
|||||||
*
|
*
|
||||||
* @brief Send an error message to the client.
|
* @brief Send an error message to the client.
|
||||||
*/
|
*/
|
||||||
WebController *UhttpServer::get_controller(const std::string &an_url)
|
WebController *UhttpServer::get_controller(uint8_t a_method, const std::string &an_url)
|
||||||
{
|
{
|
||||||
std::vector<WebController *>::iterator the_it;
|
ControllerIterator the_it;
|
||||||
WebController *the_controller = NULL;
|
WebController *the_controller = NULL;
|
||||||
|
|
||||||
for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
|
for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
|
||||||
{
|
{
|
||||||
if ((*the_it)->find(an_url))
|
if (the_it->second.get()->find(a_method, an_url))
|
||||||
{
|
{
|
||||||
printf ("return the controller.\n");
|
printf ("return the controller.\n");
|
||||||
return (*the_it);
|
return the_it->second.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
/*------------------------------- INCLUDES ----------------------------------*/
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ class WebConnection;
|
|||||||
class UhttpServer
|
class UhttpServer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::unordered_map<std::string, WebController *> ControllerContainer;
|
typedef std::unordered_map<std::string, std::unique_ptr<WebController>> ControllerContainer;
|
||||||
typedef ControllerContainer::iterator ControllerIterator;
|
typedef ControllerContainer::iterator ControllerIterator;
|
||||||
typedef std::unordered_map<int, WebConnection *> ConnectionContainer;
|
typedef std::unordered_map<int, WebConnection *> ConnectionContainer;
|
||||||
typedef ConnectionContainer::iterator ConnectionIterator;
|
typedef ConnectionContainer::iterator ConnectionIterator;
|
||||||
@@ -75,13 +76,13 @@ class UhttpServer
|
|||||||
std::string &a_get_method, std::string &a_set_method, bool &a_raw_response);
|
std::string &a_get_method, std::string &a_set_method, bool &a_raw_response);
|
||||||
struct json_object *load(const std::string &a_file);
|
struct json_object *load(const std::string &a_file);
|
||||||
int add_controller(const std::string &an_uri, WebController *a_controller);
|
int add_controller(const std::string &an_uri, WebController *a_controller);
|
||||||
WebController *get_controller(const std::string &an_url);
|
WebController *get_controller(uint8_t a_method, const std::string &an_url);
|
||||||
void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg);
|
void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct ubus_context *m_ctx;
|
struct ubus_context *m_ctx;
|
||||||
struct uhttpd_ops *m_ops;
|
struct uhttpd_ops *m_ops;
|
||||||
std::vector< WebController *> m_controllers;
|
ControllerContainer m_controllers;
|
||||||
ConnectionContainer m_connections;
|
ConnectionContainer m_connections;
|
||||||
std::list<std::string> m_path_list;
|
std::list<std::string> m_path_list;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user