new sequencer model parsing

This commit is contained in:
2018-04-11 22:40:16 +02:00
parent a7f4200c8d
commit 1cd7e4db36
11 changed files with 140 additions and 54 deletions

View File

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

77
sequences.json Normal file
View File

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

View File

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

View File

@@ -25,10 +25,11 @@
/*-------------------------------- INCLUDES ---------------------------------*/
#include <cstdarg>
#include <locale>
#include <algorithm>
#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::ctype<char_t>>(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;
}

View File

@@ -31,5 +31,7 @@
/*------------------------------- INCLUDES ----------------------------------*/
std::string toupper (const std::string &src);
std::string string_sprintf(const char *format, ...);
#endif /* _HELPERS_STRINGS_H */

View File

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

View File

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

View File

@@ -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<Action>::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;
}

View File

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

View File

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

View File

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