From 1cd7e4db36911c3f7a88cbe803b3af29a8c19875 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 11 Apr 2018 22:40:16 +0200 Subject: [PATCH] new sequencer model parsing --- .../ovl/usr/local/share/domo/sequences.json | 30 +------- sequences.json | 77 +++++++++++++++++++ src/prog/domod/builders/cmake/CMakeLists.txt | 5 +- .../src/helpers/{Strings.cpp => strings.cpp} | 44 ++++++++--- .../src/helpers/{Strings.h => strings.h} | 2 + src/prog/domod/src/sequences/action.cpp | 16 ++-- src/prog/domod/src/sequences/action.h | 4 +- src/prog/domod/src/sequences/sequence.cpp | 8 +- src/prog/domod/src/sequences/sequence.h | 2 +- .../domod/src/sequences/sequences-manager.cpp | 2 +- src/prog/domod/src/sequences/timer.cpp | 4 +- 11 files changed, 140 insertions(+), 54 deletions(-) create mode 100644 sequences.json rename src/prog/domod/src/helpers/{Strings.cpp => strings.cpp} (56%) rename src/prog/domod/src/helpers/{Strings.h => strings.h} (95%) diff --git a/bsp/board/domo/ovl/usr/local/share/domo/sequences.json b/bsp/board/domo/ovl/usr/local/share/domo/sequences.json index 2ef0b0a7..ca38da89 100644 --- a/bsp/board/domo/ovl/usr/local/share/domo/sequences.json +++ b/bsp/board/domo/ovl/usr/local/share/domo/sequences.json @@ -46,37 +46,13 @@ "recurrence": 0 }, "actions": [ - [ - { - "device_id": "shutters/1", - "state": true - }, - { - "device_id": "shutters/2", - "state": true - } - ], - { - "duration": 60 - }, - [ - { - "device_id": "shutters/1", - "state": false - }, - { - "device_id": "shutters/2", - "state": false - } - ], { "device_id": "shutters/1", - "state": true, - "duration": 60 + "state": true }, { - "device_id": "shutters/1", - "state": false + "device_id": "shutters/2", + "state": true } ] } diff --git a/sequences.json b/sequences.json new file mode 100644 index 00000000..15a029ef --- /dev/null +++ b/sequences.json @@ -0,0 +1,77 @@ +{ + "sequences": [ + { + "id": 1, + "name": "arrosage", + "timer": { + "active": true, + "start_time": "17:30", + "recurrence": 1 + }, + "actions": [ + { + "device_id": "sprinkler\/7", + "active": false, + "duration": 2 + }, + { + "device_id": "sprinkler\/8", + "active": false, + "duration": 1 + } + ] + }, + { + "id": 2, + "name": "sapin", + "timer": { + "active": false, + "start_time": "21:48", + "recurrence": 0 + }, + "actions": [ + { + "device_id": "light\/7", + "active": false, + "duration": 2 + } + ] + }, + { + "id": 2, + "name": "volet", + "timer": { + "active": false, + "start_time": "9:0", + "recurrence": 0 + }, + "actions": [ + { + "device_id": "\/0", + "active": false, + "duration": 0 + }, + { + "device_id": "\/0", + "active": false, + "duration": 60 + }, + { + "device_id": "\/0", + "active": false, + "duration": 0 + }, + { + "device_id": "shutters\/1", + "active": false, + "duration": 60 + }, + { + "device_id": "shutters\/1", + "active": false, + "duration": 0 + } + ] + } + ] +} \ No newline at end of file diff --git a/src/prog/domod/builders/cmake/CMakeLists.txt b/src/prog/domod/builders/cmake/CMakeLists.txt index c44c4893..61ab96c5 100644 --- a/src/prog/domod/builders/cmake/CMakeLists.txt +++ b/src/prog/domod/builders/cmake/CMakeLists.txt @@ -31,17 +31,18 @@ file( ${workspaceRoot}/src/prog/domod/src/ubus/capabilities_shutters_model.cpp ${workspaceRoot}/src/prog/domod/src/ubus/capabilities_sprinklers_model.cpp ${workspaceRoot}/src/prog/domod/src/ubus/sequences_model.cpp - # Timers + # timers ${workspaceRoot}/src/prog/domod/src/sequences/sequences-manager.cpp ${workspaceRoot}/src/prog/domod/src/sequences/sequence.cpp ${workspaceRoot}/src/prog/domod/src/sequences/timer.cpp ${workspaceRoot}/src/prog/domod/src/sequences/action.cpp ${workspaceRoot}/src/prog/domod/src/sequences/clock.cpp + # helper + ${workspaceRoot}/src/prog/domod/src/helpers/strings.cpp ) # $ENV{SRC_DIR}/src/prog/domod/src/ubus/speach_command.cpp # $ENV{SRC_DIR}/src/prog/domod/src/helpers/Tokenizer.cpp -# $ENV{SRC_DIR}/src/prog/domod/src/helpers/Strings.cpp #) add_executable (domod ${source_files}) diff --git a/src/prog/domod/src/helpers/Strings.cpp b/src/prog/domod/src/helpers/strings.cpp similarity index 56% rename from src/prog/domod/src/helpers/Strings.cpp rename to src/prog/domod/src/helpers/strings.cpp index 44ab8fda..e5b73f66 100644 --- a/src/prog/domod/src/helpers/Strings.cpp +++ b/src/prog/domod/src/helpers/strings.cpp @@ -25,10 +25,11 @@ /*-------------------------------- INCLUDES ---------------------------------*/ +#include #include #include -#include "Strings.h" +#include "strings.h" typedef std::string::value_type char_t; @@ -37,21 +38,46 @@ typedef std::string::value_type char_t; * * @brief Constructor of the UBus Speach Command execution. */ -char_t up_char (char_t ch) +char_t up_char(char_t ch) { - return std::use_facet< std::ctype< char_t > >( std::locale() ).toupper (ch); + return std::use_facet>(std::locale()).toupper(ch); } - /*! ---------------------------------------------------------------------------- * @fn toupper * * @brief method to make a toupper missing commands on the std::string. */ -std::string toupper (const std::string &src) +std::string toupper(const std::string &src) { - //std::string result; - //std::transform (src.begin(), src.end(), std::back_inserter (result), up_char); - //return result; - return src; + //std::string result; + //std::transform (src.begin(), src.end(), std::back_inserter (result), up_char); + //return result; + return src; } + +std::string string_vsprintf(const char *format, std::va_list args) +{ + va_list tmp_args; //unfortunately you cannot consume a va_list twice + va_copy(tmp_args, args); //so we have to copy it + const int required_len = vsnprintf(nullptr, 0, format, tmp_args) + 1; + va_end(tmp_args); + + std::string buf(required_len, '\0'); + if (std::vsnprintf(&buf[0], buf.size(), format, args) < 0) + { + throw std::runtime_error{"string_vsprintf encoding error"}; + } + return buf; +} + +std::string string_sprintf(const char *format, ...) __attribute__((format(printf, 1, 2))); + +std::string string_sprintf(const char *format, ...) +{ + std::va_list args; + va_start(args, format); + std::string str{string_vsprintf(format, args)}; + va_end(args); + return str; +} \ No newline at end of file diff --git a/src/prog/domod/src/helpers/Strings.h b/src/prog/domod/src/helpers/strings.h similarity index 95% rename from src/prog/domod/src/helpers/Strings.h rename to src/prog/domod/src/helpers/strings.h index 98ea4f57..7ac4dca2 100644 --- a/src/prog/domod/src/helpers/Strings.h +++ b/src/prog/domod/src/helpers/strings.h @@ -31,5 +31,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ std::string toupper (const std::string &src); +std::string string_sprintf(const char *format, ...); + #endif /* _HELPERS_STRINGS_H */ diff --git a/src/prog/domod/src/sequences/action.cpp b/src/prog/domod/src/sequences/action.cpp index 3fa1b715..3d22cfbb 100644 --- a/src/prog/domod/src/sequences/action.cpp +++ b/src/prog/domod/src/sequences/action.cpp @@ -33,15 +33,14 @@ #include "action.h" - /*! ---------------------------------------------------------------------------- * @fn Action * * @brief Constructor of Action Object. */ Action::Action(void) : m_device_id(0), - m_state(false), - m_duration(0) + m_state(false), + m_duration(-1) { } @@ -63,7 +62,7 @@ Action::~Action(void) int Action::from_json(struct json_object *a_node) { struct json_object *the_value_node; - + // device_id if (json_object_object_get_ex(a_node, k_entry_device_id, &the_value_node)) { @@ -107,10 +106,13 @@ struct json_object *Action::to_json(void) const json_object_object_add(the_root_node, k_entry_device_id, json_object_new_string(the_device_id.c_str())); // state - json_object_object_add(the_root_node, k_entry_active, json_object_new_boolean(m_state)); + json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state)); // duration - json_object_object_add(the_root_node, k_entry_duration, json_object_new_int(m_duration)); + if (m_duration != -1) + { + json_object_object_add(the_root_node, k_entry_duration, json_object_new_int(m_duration)); + } return the_root_node; } @@ -198,7 +200,7 @@ bool Action::get_state(void) const * * @brief Return the Duration of the Event. */ -uint16_t Action::get_duration(void) const +int16_t Action::get_duration(void) const { return m_duration; } diff --git a/src/prog/domod/src/sequences/action.h b/src/prog/domod/src/sequences/action.h index 15a756a6..532b59c0 100644 --- a/src/prog/domod/src/sequences/action.h +++ b/src/prog/domod/src/sequences/action.h @@ -55,7 +55,7 @@ class Action uint16_t get_device_id(void) const; const std::string &get_capability(void) const; bool get_state(void) const; - uint16_t get_duration(void) const; + int16_t get_duration(void) const; //bool is_in_progress(void) const; @@ -66,7 +66,7 @@ class Action uint16_t m_device_id; std::string m_capability; bool m_state; - uint16_t m_duration; + int16_t m_duration; //bool m_in_progress; }; diff --git a/src/prog/domod/src/sequences/sequence.cpp b/src/prog/domod/src/sequences/sequence.cpp index 15f7ac75..92ead3f5 100644 --- a/src/prog/domod/src/sequences/sequence.cpp +++ b/src/prog/domod/src/sequences/sequence.cpp @@ -101,7 +101,7 @@ int Sequence::from_json(struct json_object *a_node) * * @brief Export Sequence Object as JSON Object. */ -struct json_object *Sequence::to_json(void) const +struct json_object *Sequence::to_json(void) { std::vector::iterator the_action_it; struct json_object *the_root_node, *the_action_array_node; @@ -120,12 +120,12 @@ struct json_object *Sequence::to_json(void) const // actions the_action_array_node = json_object_new_array(); json_object_object_add(the_root_node, k_entry_actions, the_action_array_node); -#if 0 + for (the_action_it = m_actions.begin(); the_action_it != m_actions.end(); the_action_it++) { - //json_object_array_add(the_action_array_node, (*the_action_it).to_json()); + json_object_array_add(the_action_array_node, (*the_action_it).to_json()); } -#endif + return the_root_node; } diff --git a/src/prog/domod/src/sequences/sequence.h b/src/prog/domod/src/sequences/sequence.h index c01853e3..413b99a5 100644 --- a/src/prog/domod/src/sequences/sequence.h +++ b/src/prog/domod/src/sequences/sequence.h @@ -48,7 +48,7 @@ public: ~Sequence(void); int from_json(struct json_object *a_node); - struct json_object *to_json(void) const; + struct json_object *to_json(void); uint16_t get_id(void) const; const std::string &get_name(void) const; diff --git a/src/prog/domod/src/sequences/sequences-manager.cpp b/src/prog/domod/src/sequences/sequences-manager.cpp index 0b28c445..841a2225 100644 --- a/src/prog/domod/src/sequences/sequences-manager.cpp +++ b/src/prog/domod/src/sequences/sequences-manager.cpp @@ -114,7 +114,7 @@ int SequencesManager::save(void) the_root_node = to_json(); the_result = json_object_to_file(m_sequences_file_path.c_str(), the_root_node); - + /* Clean the json object. */ json_object_put(the_root_node); diff --git a/src/prog/domod/src/sequences/timer.cpp b/src/prog/domod/src/sequences/timer.cpp index 7623ca88..1a57d4e6 100644 --- a/src/prog/domod/src/sequences/timer.cpp +++ b/src/prog/domod/src/sequences/timer.cpp @@ -29,6 +29,8 @@ #include "domo.h" +#include "helpers/strings.h" + #include "timer.h" /*------------------------------- DEFINES ----------------------------------*/ @@ -105,7 +107,7 @@ struct json_object *Timer::to_json(void) const json_object_object_add(the_root_node, k_entry_active, json_object_new_boolean(m_active)); //start_time - std::string the_start_time = std::to_string(m_hour) + ":" + std::to_string(m_minute); + std::string the_start_time = string_sprintf("%.2d:%.2d", m_hour, m_minute); json_object_object_add(the_root_node, k_entry_start_time, json_object_new_string(the_start_time.c_str())); // recurrence