wip sequence

This commit is contained in:
jbnadal
2018-04-11 16:56:38 +02:00
parent cfe5726e0e
commit 66f6fb11ca
6 changed files with 153 additions and 19 deletions

View File

@@ -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
}
]
}
]
}

View File

@@ -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"

View File

@@ -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);

View File

@@ -27,6 +27,8 @@
#include <json-c/json.h>
#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;
}

View File

@@ -28,6 +28,8 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <string>
/*---------------------------------- 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 */

View File

@@ -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<Sequence>::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;