From d2613350fc9d066e055efba7a4def8c7f97c1598 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 2 May 2018 22:52:38 +0200 Subject: [PATCH] update light controller --- docs/api/tests-api.md | 216 ++++++++++++++++++ src/prog/domod/builders/cmake/CMakeLists.txt | 1 + .../domod/src/devices/devices-manager.cpp | 12 + src/prog/domod/src/devices/devices-manager.h | 1 + src/prog/domod/src/helpers/controller.cpp | 71 ++++++ src/prog/domod/src/helpers/controller.h | 37 +++ src/prog/domod/src/ubus/lights-controller.cpp | 24 +- 7 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 docs/api/tests-api.md create mode 100644 src/prog/domod/src/helpers/controller.cpp create mode 100644 src/prog/domod/src/helpers/controller.h diff --git a/docs/api/tests-api.md b/docs/api/tests-api.md new file mode 100644 index 00000000..2725efcf --- /dev/null +++ b/docs/api/tests-api.md @@ -0,0 +1,216 @@ +# Test all the ubus API of domo. + + +## domo.capabilities + +### list + +``` +./usr/bin/ubus call domo.capabilities list +``` + +```json +{ + "capabilities": [ + { + "name": "lights", + "speach_name": "lumière" + }, + { + "name": "shutters", + "speach_name": "volet" + }, + { + "name": "sprinklers", + "speach_name": "station" + } + ] +} + +``` + +## domo.lights + +### create + +``` +./usr/bin/ubus call domo.lights create +``` + +### list + +``` +./usr/bin/ubus call domo.lights list +``` + +```json +{ + "lights": [ + { + "id": 1, + "name": "Bureau JB", + "speach_name": "bureau", + "zone": "", + "state": true, + "sender": 12797322, + "interruptor": 0 + }, + { + "id": 2, + "name": "Salon", + "speach_name": "salon", + "zone": "", + "state": false, + "sender": 12797322, + "interruptor": 1 + }, + { + "id": 3, + "name": "Sapin", + "speach_name": "sapin", + "zone": "", + "state": false, + "sender": 12797322, + "interruptor": 2 + } + ] +} +``` + +### read + +``` +./usr/bin/ubus call domo.lights read "{\"id\":2}" +``` + +### update + +``` +./usr/bin/ubus call domo.lights update +``` + +### delete + +``` +./usr/bin/ubus call domo.lights delete +``` + +## domo.sequences + +### create + +``` +./usr/bin/ubus call domo.sequences create +``` + +### list + +``` +./usr/bin/ubus call domo.sequences list +``` + +### read + +``` +./usr/bin/ubus call domo.sequences read +``` + +### update + +``` +./usr/bin/ubus call domo.sequences update +``` + +### delete + +``` +./usr/bin/ubus call domo.sequences delete +``` + +## domo.shutters + +### create + +``` +./usr/bin/ubus call domo.shutters create +``` + +### list + +``` +./usr/bin/ubus call domo.shutters list +``` + +### read + +``` +./usr/bin/ubus call domo.shutters read +``` + +### update + +``` +./usr/bin/ubus call domo.shutters update +``` + +### delete + +``` +./usr/bin/ubus call domo.shutters delete +``` + +### up + +``` +./usr/bin/ubus call domo.shutters up +``` + +### down + +``` +./usr/bin/ubus call domo.shutters down +``` + +## domo.sprinklers + +### create + +``` +./usr/bin/ubus call domo.sprinklers create +``` + +### list + +``` +./usr/bin/ubus call domo.sprinklers list +``` + +### read + +``` +./usr/bin/ubus call domo.sprinklers read +``` + +### update + +``` +./usr/bin/ubus call domo.sprinklers update +``` + +### delete + +``` +./usr/bin/ubus call domo.sprinklers delete +``` + +### open + +``` +./usr/bin/ubus call domo.sprinklers open +``` + +### close + +``` +./usr/bin/ubus call domo.sprinklers close +``` diff --git a/src/prog/domod/builders/cmake/CMakeLists.txt b/src/prog/domod/builders/cmake/CMakeLists.txt index baca56bd..c01a2956 100644 --- a/src/prog/domod/builders/cmake/CMakeLists.txt +++ b/src/prog/domod/builders/cmake/CMakeLists.txt @@ -39,6 +39,7 @@ file( ${workspaceRoot}/src/prog/domod/src/sequences/clock.cpp # helper ${workspaceRoot}/src/prog/domod/src/helpers/strings.cpp + ${workspaceRoot}/src/prog/domod/src/helpers/controller.cpp ) # $ENV{SRC_DIR}/src/prog/domod/src/ubus/speach_command.cpp diff --git a/src/prog/domod/src/devices/devices-manager.cpp b/src/prog/domod/src/devices/devices-manager.cpp index 15d0a586..320a6eb7 100644 --- a/src/prog/domod/src/devices/devices-manager.cpp +++ b/src/prog/domod/src/devices/devices-manager.cpp @@ -155,6 +155,18 @@ std::string DevicesManager::get(const std::string &a_capability) return the_output; } +/*! ---------------------------------------------------------------------------- + * @fn get + * + * @brief get the list of devices of a specific capabilities. + */ +std::string DevicesManager::get(const std::string &a_capability, uint32_t an_id) +{ + std::string the_result; +#warning TODO + return the_result; +} + /*! ---------------------------------------------------------------------------- * @fn set * diff --git a/src/prog/domod/src/devices/devices-manager.h b/src/prog/domod/src/devices/devices-manager.h index 66a8fd5c..24e33bd8 100644 --- a/src/prog/domod/src/devices/devices-manager.h +++ b/src/prog/domod/src/devices/devices-manager.h @@ -50,6 +50,7 @@ class DevicesManager int save(void); std::string get(const std::string &a_capability); + std::string get(const std::string &a_capability, uint32_t an_id); int set(const std::string &a_capability, struct json_object *a_node); int set_state(const std::string &a_capability, int an_id, bool a_state); diff --git a/src/prog/domod/src/helpers/controller.cpp b/src/prog/domod/src/helpers/controller.cpp new file mode 100644 index 00000000..b3f71243 --- /dev/null +++ b/src/prog/domod/src/helpers/controller.cpp @@ -0,0 +1,71 @@ +/*! + * controller.cpp + * + * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 02/05/2018 + * + */ + +/*-------------------------------- INCLUDES ---------------------------------*/ + +#include + +#include + +extern "C" { +#include +} + +#define k_entry_id "id" + +/*! ---------------------------------------------------------------------------- + * @fn get_id_from_blob + * + * @brief get an id from a blob. + */ +int get_id_from_blob(struct blob_attr *a_msg, uint32_t &an_id) +{ + int the_result; + char *the_string; + struct json_object *the_root_node, *the_value_node; + the_string = blobmsg_format_json(a_msg, true); + + the_root_node = json_tokener_parse(the_string); + free(the_string); + + if (the_root_node == NULL) + { + fprintf(stderr, "Failed parse the parameters.\n"); + return -1; + } + + the_result = -1; + + // id + if (json_object_object_get_ex(the_root_node, k_entry_id, &the_value_node)) + { + an_id = json_object_get_int(the_value_node); + the_result = 0; + } + + json_object_put(the_root_node); + + return the_result; +} diff --git a/src/prog/domod/src/helpers/controller.h b/src/prog/domod/src/helpers/controller.h new file mode 100644 index 00000000..4be5c37f --- /dev/null +++ b/src/prog/domod/src/helpers/controller.h @@ -0,0 +1,37 @@ +/*! + * controller.cpp + * + * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 02/05/2018 + * + */ + +#ifndef _HELPERS_CONTROLLER_H +#define _HELPERS_CONTROLLER_H + +#include + +struct blob_attr; + +/*------------------------------- INCLUDES ----------------------------------*/ + +int get_id_from_blob(struct blob_attr *a_msg, uint32_t &an_id); + + #endif /* _HELPERS_CONTROLLER_H */ diff --git a/src/prog/domod/src/ubus/lights-controller.cpp b/src/prog/domod/src/ubus/lights-controller.cpp index 37c1c83e..e5a2a9ff 100644 --- a/src/prog/domod/src/ubus/lights-controller.cpp +++ b/src/prog/domod/src/ubus/lights-controller.cpp @@ -31,6 +31,8 @@ extern "C" { #include } +#include "helpers/controller.h" + #include "devices/devices-manager.h" #include "domo.h" @@ -110,10 +112,28 @@ int LightsController::list(struct ubus_context *a_ctx, struct ubus_request_data * * @brief Read a specific sequence object. */ -int LightsController::read(struct ubus_context *, struct ubus_request_data *, struct blob_attr *) +int LightsController::read(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg) { + uint32_t the_id, the_result; + struct blob_buf the_buf = {0}; printf("%s\n", __PRETTY_FUNCTION__); - return 0; + + if (get_id_from_blob(a_msg, the_id) != 0) + { + return UBUS_STATUS_INVALID_ARGUMENT; + } + + printf ("update id: %d\n", the_id); + + blob_buf_init(&the_buf, 0); + + blobmsg_add_json_from_string(&the_buf, m_devices_manager->get(kLightEntry, the_id).c_str()); + + the_result = ubus_send_reply(a_ctx, a_req, the_buf.head); + + blob_buf_free(&the_buf); + + return the_result; } /*! ----------------------------------------------------------------------------