wip on shutters

This commit is contained in:
jbnadal
2018-04-04 10:42:19 +02:00
parent f2f5096589
commit 5220d2b0d8
8 changed files with 188 additions and 115 deletions

View File

@@ -31,45 +31,49 @@
#include "devices/device.h"
/*! ----------------------------------------------------------------------------
* @fn Device
*
* @brief Constructor of the Device Object.
*/
Device::Device (void)
Device::Device(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Device
*
* @brief Destructor of the Device Object.
*/
Device::~Device (void)
Device::~Device(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn from_json
*
* @brief Load Device Object From a Json Object stringified.
*/
int Device::from_json (struct json_object *a_node)
int Device::from_json(struct json_object *a_node)
{
struct json_object *the_value_node;
fprintf(stderr, "Device::load_from_json...\n");
// speach name
if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node))
{
m_speach_name = json_object_get_string(the_value_node);
}
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn to_json
*
* @brief Export Device Object as JSON Object.
*/
struct json_object *Device::to_json (void) const
struct json_object *Device::to_json(void) const
{
return NULL;
}

View File

@@ -34,6 +34,11 @@
struct json_object;
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_speach_name "speach_name"
#define k_entry_data "data"
/*--------------------------------- CLASS ----------------------------------*/
class Device

View File

@@ -77,16 +77,19 @@ int DevicesManager::load(void)
if (json_object_object_get_ex(the_root_node, kLightEntry, &the_value_node))
{
printf(" On a des Lights.\n");
m_lights.from_json(the_value_node);
}
if (json_object_object_get_ex(the_root_node, kShutterEntry, &the_value_node))
{
printf(" On a des shutters.\n");
m_shutters.from_json(the_value_node);
}
if (json_object_object_get_ex(the_root_node, kSprinklerEntry, &the_value_node))
{
printf(" On a des sprintkler.\n");
m_sprinklers.from_json(the_value_node);
}
/* Clean the json object. */

View File

@@ -33,9 +33,6 @@
#include "devices/light-device.h"
#define k_entry_speach_name "speach_name"
#define k_entry_data "data"
/*! ----------------------------------------------------------------------------
* @fn LightDevice
*
@@ -61,17 +58,15 @@ LightDevice::~LightDevice(void)
*/
int LightDevice::from_json(struct json_object *a_node)
{
struct json_object *the_value_node, *the_data_node;
struct json_object *the_data_node;
fprintf(stderr, "LightDevice::load_from_json\n");
// speach name
if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node)) {
Device::from_json(a_node);
m_speach_name = json_object_get_string(the_value_node);
}
// Get Light
else if (json_object_object_get_ex(a_node, k_entry_data, &the_data_node) && (json_object_get_type(the_data_node) == json_type_array)) {
if (json_object_object_get_ex(a_node, k_entry_data, &the_data_node) && (json_object_get_type(the_data_node) == json_type_array))
{
int the_len;
struct json_object *the_light_node;
@@ -82,7 +77,7 @@ int LightDevice::from_json(struct json_object *a_node)
the_light_node = json_object_array_get_idx(the_data_node, i);
the_light.from_json(the_light_node);
m_lights.push_back (the_light);
m_lights.push_back(the_light);
}
}
@@ -107,8 +102,8 @@ struct json_object *LightDevice::to_json(void)
// data
json_object_object_add(the_root_node, k_entry_data, the_data_node);
for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++) {
for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++)
{
json_object_array_add(the_data_node, (*the_light_it).to_json());
}

View File

@@ -33,85 +33,88 @@
#include "devices/light.h"
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_zone "zone"
#define k_entry_state "state"
#define k_entry_sender "sender"
#define k_entry_interruptor "interruptor"
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_zone "zone"
#define k_entry_state "state"
#define k_entry_sender "sender"
#define k_entry_interruptor "interruptor"
/*! ----------------------------------------------------------------------------
* @fn Light
*
* @brief Constructor of the Light Object.
*/
Light::Light (void)
Light::Light(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~Light
*
* @brief Destructor of the Light Object.
*/
Light::~Light (void)
Light::~Light(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn from_json
*
* @brief Load a Light Element from a Json Entry.
*/
int Light::from_json (struct json_object *a_node)
int Light::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)) {
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)) {
if (json_object_object_get_ex(a_node, k_entry_name, &the_value_node))
{
m_name = json_object_get_string(the_value_node);
}
// speach_name
if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node)) {
if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node))
{
m_speach_name = json_object_get_string(the_value_node);
}
// zone
if (json_object_object_get_ex(a_node, k_entry_zone, &the_value_node)) {
if (json_object_object_get_ex(a_node, k_entry_zone, &the_value_node))
{
m_zone = json_object_get_string(the_value_node);
}
// state
if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node)) {
if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
{
m_state = json_object_get_boolean(the_value_node);
}
// sender
if (json_object_object_get_ex(a_node, k_entry_sender, &the_value_node)) {
if (json_object_object_get_ex(a_node, k_entry_sender, &the_value_node))
{
m_sender = json_object_get_int(the_value_node);
}
// interruptor
if (json_object_object_get_ex(a_node, k_entry_interruptor, &the_value_node)) {
if (json_object_object_get_ex(a_node, k_entry_interruptor, &the_value_node))
{
m_interruptor = json_object_get_int(the_value_node);
}
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn to_json
*
* @brief Return Light Object as a Json Object.
*/
struct json_object *Light::to_json (void) const
struct json_object *Light::to_json(void) const
{
struct json_object *the_root_node;
@@ -135,40 +138,37 @@ struct json_object *Light::to_json (void) const
return the_root_node;
}
/*! ----------------------------------------------------------------------------
* @fn update
*
* @brief Update the Light State.
*/
int Light::update (bool a_state)
int Light::update(bool a_state)
{
if (m_state == a_state)
return 1;
m_state = a_state;
return send_state ();
return send_state();
}
/*! ----------------------------------------------------------------------------
* @fn get_id
*
* @brief Return the ID of the Light.
*/
uint16_t Light::get_id (void)
uint16_t Light::get_id(void)
{
return m_id;
}
/*! ----------------------------------------------------------------------------
* @fn sendState
*
* @brief Send the Curent State of the Light.
*/
int Light::send_state (void)
int Light::send_state(void)
{
#if 0
UBusCall theCmd;

View File

@@ -23,64 +23,72 @@
*
*/
/*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio>
#include <json-c/json.h>
#include "devices/shutter-device.h"
/*! ----------------------------------------------------------------------------
* @fn ShutterDevice
*
* @brief Constructor of the Shutter Device Object.
*/
ShutterDevice::ShutterDevice(void) : Device()
{
}
/*! ----------------------------------------------------------------------------
* @fn ShutterDevice
*
* @brief Constructor of the ShutterDevice Object.
* @brief Destructor of the Shutter Device Object.
*/
ShutterDevice::ShutterDevice (void) : Device()
ShutterDevice::~ShutterDevice(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn ShutterDevice
*
* @brief Destructor of the Device Object.
*/
ShutterDevice::~ShutterDevice (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn from_json
*
* @brief Load ShutterDevice Object From a Json Object.
*/
int ShutterDevice::from_json (struct json_object *a_node)
int ShutterDevice::from_json(struct json_object *a_node)
{
fprintf (stderr, "ShutterDevice::load_from_json\n");
#if 0
mspeach_name = anElem["speach_name"].asString();
struct json_object *the_data_node;
fprintf(stderr, "ShutterDevice::load_from_json\n");
for (Json::Value& theElement : anElem["data"]) {
Shutter theShutter;
if (theShutter.load_from_json (theElement) == 0) {
// speach name
Device::from_json(a_node);
mShutter.push_back (theShutter);
// Get Shutter
if (json_object_object_get_ex(a_node, k_entry_data, &the_data_node) && (json_object_get_type(the_data_node) == json_type_array))
{
int the_len;
struct json_object *the_shutter_node;
the_len = json_object_array_length(the_data_node);
for (int i = 0; i < the_len; i++)
{
Shutter the_shutter;
the_shutter_node = json_object_array_get_idx(the_data_node, i);
the_shutter.from_json(the_shutter_node);
m_shutters.push_back(the_shutter);
}
}
#endif
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn to_json
*
* @brief Export ShutterDevice Object as JSON Object.
*/
struct json_object *ShutterDevice::to_json (void)
struct json_object *ShutterDevice::to_json(void)
{
#if 0
Json::Value theResult;
@@ -104,24 +112,22 @@ struct json_object *ShutterDevice::to_json (void)
#endif
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Shutter state.
*/
int ShutterDevice::set(struct json_object *a_node)
{
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Shutter state.
*/
int ShutterDevice::set (struct json_object *a_node)
{
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Shutter state.
*/
int ShutterDevice::set (int an_id, bool a_state)
int ShutterDevice::set(int an_id, bool a_state)
{
return 0;
}

View File

@@ -50,7 +50,7 @@ class ShutterDevice : public Device
int set(int an_id, bool a_state);
private:
std::vector<Shutter> m_shutter;
std::vector<Shutter> m_shutters;
};
#endif /* _SHUTTER_DEVICE_H */

View File

@@ -27,10 +27,24 @@
#include <cstdio>
#include <json-c/json.h>
#include <ubus-cpp/ubus-call.h>
#include "devices/shutter.h"
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_zone "zone"
#define k_entry_state "state"
#define k_entry_sender "sender"
#define k_entry_interruptor "interruptor"
#define k_entry_speed_up "speed_up"
#define k_entry_speed_down "speed_down"
/*! ----------------------------------------------------------------------------
* @fn Shutter
*
@@ -56,17 +70,54 @@ Shutter::~Shutter(void)
*/
int Shutter::from_json(struct json_object *a_node)
{
#if 0
mID = anElem["id"].asInt();
mName = anElem["name"].asString();
mSpeach_name = anElem["speach_name"].asString();
mZone = anElem["zone"].asString();
mState = anElem["state"].asBool();
mSender = anElem["sender"].asInt();
mInterruptor = anElem["interruptor"].asInt();
mSpeed_up = anElem["speed_up"].asInt();
mSpeed_down = anElem["speed_down"].asInt();
#endif
struct json_object *the_value_node;
// TODO: Should shared with Light.
// 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);
}
// speach_name
if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node))
{
m_speach_name = json_object_get_string(the_value_node);
}
// zone
if (json_object_object_get_ex(a_node, k_entry_zone, &the_value_node))
{
m_zone = json_object_get_string(the_value_node);
}
// state
if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
{
m_state = json_object_get_boolean(the_value_node);
}
// sender
if (json_object_object_get_ex(a_node, k_entry_sender, &the_value_node))
{
m_sender = json_object_get_int(the_value_node);
}
// interruptor
if (json_object_object_get_ex(a_node, k_entry_interruptor, &the_value_node))
{
m_interruptor = json_object_get_int(the_value_node);
}
// speed up
if (json_object_object_get_ex(a_node, k_entry_speed_up, &the_value_node))
{
m_speed_up = json_object_get_int(the_value_node);
}
// speed down
if (json_object_object_get_ex(a_node, k_entry_speed_down, &the_value_node))
{
m_speed_down = json_object_get_int(the_value_node);
}
return 0;
}
@@ -77,21 +128,30 @@ int Shutter::from_json(struct json_object *a_node)
*/
struct json_object *Shutter::to_json(void) const
{
#if 0
Json::Value aResult(Json::objectValue);
fprintf (stderr, "Shutter::to_json\n");
aResult["id"] = mID;
aResult["name"] = mName;
aResult["zone"] = mZone;
aResult["state"] = mState;
aResult["sender"] = mSender;
aResult["interruptor"] = mInterruptor;
aResult["speed_up"] = mSpeed_up;
aResult["speed_down"] = mSpeed_down;
aResult["speach_name"] = mSpeach_name;
struct json_object *the_root_node;
return aResult;
#endif
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()));
// speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// zone
json_object_object_add(the_root_node, k_entry_zone, json_object_new_string(m_zone.c_str()));
// state
json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state));
// sender
json_object_object_add(the_root_node, k_entry_sender, json_object_new_int(m_sender));
// interruptor
json_object_object_add(the_root_node, k_entry_interruptor, json_object_new_int(m_interruptor));
// speed up
json_object_object_add(the_root_node, k_entry_speed_up, json_object_new_int(m_speed_up));
// speed down
json_object_object_add(the_root_node, k_entry_speed_down, json_object_new_int(m_speed_down));
return the_root_node;
}
/*! ----------------------------------------------------------------------------