diff --git a/bsp/board/domo/ovl/usr/local/share/domo/sequence-ref.json b/bsp/board/domo/ovl/usr/local/share/domo/sequence-ref.json new file mode 100644 index 00000000..24a735e9 --- /dev/null +++ b/bsp/board/domo/ovl/usr/local/share/domo/sequence-ref.json @@ -0,0 +1,84 @@ +{ + "sequences": [ + { + "id": 1, + "name": "arrosage", + "timer": { + "active": true, + "start_time": "17:30", + "recurrence": 1 + }, + "sequence": [ + { + "device_id": "sprinkler/7", + "state": true, + "duration": 2 + }, + { + "device_id": "sprinkler/8", + "state": true, + "duration": 1 + } + ] + }, + { + "id": 2, + "name": "sapin", + "timer": { + "active": false, + "start_time": "21:48", + "recurrence": 0 + }, + "sequence": [ + { + "device_id": "light/7", + "state": true, + "duration": 2 + } + ] + }, + { + "id": 2, + "name": "volet", + "timer": { + "active": false, + "start_time": "09:00", + "recurrence": 0 + }, + "sequence": [ + [ + { + "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 + }, + { + "device_id": "shutters/1", + "state": false + } + ] + } + ] +} diff --git a/src/prog/domod/include/domo.h b/src/prog/domod/include/domo.h index 2a8ad40e..2fa11293 100644 --- a/src/prog/domod/include/domo.h +++ b/src/prog/domod/include/domo.h @@ -37,7 +37,7 @@ #define k_entry_devices "devices" -#define k_entry_timers "timers" +#define k_entry_sequences "sequences" /* common */ #define k_entry_id "id" diff --git a/src/prog/domod/src/main.cpp b/src/prog/domod/src/main.cpp index df77f3ca..8a008ee9 100644 --- a/src/prog/domod/src/main.cpp +++ b/src/prog/domod/src/main.cpp @@ -147,6 +147,8 @@ int main(int argc, char *argv[]) return -2; } + the_sequences_manager.save(); + /* Setup the UBus Models. */ CapabilitiesModel the_capabilities(the_config_path + "./capabilities.json"); CapabilitiesLightsModel the_cap_lights(&the_devices_manager); diff --git a/src/prog/domod/src/sequences/sequence.cpp b/src/prog/domod/src/sequences/sequence.cpp index 2e5cd65a..7c99ac0a 100644 --- a/src/prog/domod/src/sequences/sequence.cpp +++ b/src/prog/domod/src/sequences/sequence.cpp @@ -27,6 +27,8 @@ #include +#include "domo.h" + #include "sequence.h" /*! ---------------------------------------------------------------------------- @@ -47,22 +49,65 @@ Sequence::~Sequence(void) { } +/*! ---------------------------------------------------------------------------- + * @fn from_json + * + * @brief Load Sequence Object. + */ +int Sequence::from_json(struct json_object *a_node) +{ + struct json_object *the_value_node; + + // id + if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node)) + { + m_id = json_object_get_int(the_value_node); + } + + // name + if (json_object_object_get_ex(a_node, k_entry_name, &the_value_node)) + { + m_name = json_object_get_string(the_value_node); + } + + return 0; +} + /*! ---------------------------------------------------------------------------- * @fn to_json * - * @brief Export Event Object as JSON Object. + * @brief Export Sequence Object as JSON Object. */ struct json_object *Sequence::to_json(void) const { - return NULL; + struct json_object *the_root_node; + + the_root_node = json_object_new_object(); + + // id + json_object_object_add(the_root_node, k_entry_id, json_object_new_int(m_id)); + // name + json_object_object_add(the_root_node, k_entry_name, json_object_new_string(m_name.c_str())); + + return the_root_node; } /*! ---------------------------------------------------------------------------- * @fn get_id * - * @brief return the ID of the Sequence. + * @brief return the id of the Sequence. */ uint16_t Sequence::get_id(void) const { - return m_id; + return m_id; +} + +/*! ---------------------------------------------------------------------------- + * @fn get_name + * + * @brief return the name of the Sequence. + */ +const std::string &Sequence::get_name(void) const +{ + return m_name; } diff --git a/src/prog/domod/src/sequences/sequence.h b/src/prog/domod/src/sequences/sequence.h index 42ca10cd..b40a2fde 100644 --- a/src/prog/domod/src/sequences/sequence.h +++ b/src/prog/domod/src/sequences/sequence.h @@ -28,6 +28,8 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include + /*---------------------------------- Deps -----------------------------------*/ struct json_object; @@ -40,12 +42,15 @@ public: Sequence(void); ~Sequence(void); + int from_json(struct json_object *a_node); struct json_object *to_json(void) const; uint16_t get_id(void) const; + const std::string &get_name(void) const; private: uint16_t m_id; + std::string m_name; }; #endif /* _SEQUENCE_H */ diff --git a/src/prog/domod/src/sequences/sequences-manager.cpp b/src/prog/domod/src/sequences/sequences-manager.cpp index 5c9f4621..735e0ee7 100644 --- a/src/prog/domod/src/sequences/sequences-manager.cpp +++ b/src/prog/domod/src/sequences/sequences-manager.cpp @@ -68,7 +68,7 @@ SequencesManager::~SequencesManager(void) */ int SequencesManager::load(void) { - struct json_object *the_root_node, *the_timers_node; + struct json_object *the_root_node, *the_sequence_array_node; the_root_node = json_object_from_file(m_sequences_file_path.c_str()); if (the_root_node == NULL) @@ -77,21 +77,19 @@ int SequencesManager::load(void) return -1; } - if (json_object_object_get_ex(the_root_node, k_entry_timers, &the_timers_node) && (json_object_get_type(the_timers_node) == json_type_array)) + if (json_object_object_get_ex(the_root_node, k_entry_sequences, &the_sequence_array_node) && (json_object_get_type(the_sequence_array_node) == json_type_array)) { int the_len; - struct json_object *the_event_node; + struct json_object *the_sequence_node; - the_len = json_object_array_length(the_timers_node); + the_len = json_object_array_length(the_sequence_array_node); for (int i = 0; i < the_len; i++) { -#if 0 - Event the_event; - the_event_node = json_object_array_get_idx(the_timers_node, i); - the_event.from_json(the_event_node); + Sequence the_sequence; + the_sequence_node = json_object_array_get_idx(the_sequence_array_node, i); + the_sequence.from_json(the_sequence_node); - m_timers.push_back(the_event); -#endif + m_sequences.push_back(the_sequence); } } @@ -174,16 +172,16 @@ int SequencesManager::remove(uint16_t an_id) struct json_object *SequencesManager::to_json(void) { std::vector::iterator the_seq_it; - struct json_object *the_root_node, *the_timer_list_node; + struct json_object *the_root_node, *the_sequence_array_node; the_root_node = json_object_new_object(); - the_timer_list_node = json_object_new_array(); - json_object_object_add(the_root_node, k_entry_timers, the_timer_list_node); + the_sequence_array_node = json_object_new_array(); + json_object_object_add(the_root_node, k_entry_sequences, the_sequence_array_node); for (the_seq_it = m_sequences.begin(); the_seq_it != m_sequences.end(); the_seq_it++) { - json_object_array_add(the_timer_list_node, (*the_seq_it).to_json()); + json_object_array_add(the_sequence_array_node, (*the_seq_it).to_json()); } return the_root_node;