From dd19bc593318d249423c9052c7b9dabb9bf4d6e9 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Sun, 6 May 2018 22:47:31 +0200 Subject: [PATCH] wip refactor --- docs/api/tests-api.md | 12 ++ src/prog/domod/builders/cmake/CMakeLists.txt | 13 +- src/prog/domod/src/devices/device.cpp | 60 ++++++++- src/prog/domod/src/devices/device.h | 11 +- .../domod/src/devices/devices-collection.cpp | 115 +++++++++++++++++ .../domod/src/devices/devices-collection.h | 58 +++++++++ .../domod/src/devices/devices-manager.cpp | 121 ++++++++++++++++-- src/prog/domod/src/devices/devices-manager.h | 23 ++-- .../devices/{light.cpp => generic-dio.cpp} | 77 ++++++----- .../src/devices/{light.h => generic-dio.h} | 27 ++-- src/prog/domod/src/devices/shutter.cpp | 83 ++++-------- src/prog/domod/src/devices/shutter.h | 23 ++-- src/prog/domod/src/devices/sprinkler.cpp | 4 +- src/prog/domod/src/devices/sprinkler.h | 23 ++-- 14 files changed, 475 insertions(+), 175 deletions(-) create mode 100644 src/prog/domod/src/devices/devices-collection.cpp create mode 100644 src/prog/domod/src/devices/devices-collection.h rename src/prog/domod/src/devices/{light.cpp => generic-dio.cpp} (71%) rename src/prog/domod/src/devices/{light.h => generic-dio.h} (79%) diff --git a/docs/api/tests-api.md b/docs/api/tests-api.md index 2725efcf..c597f065 100644 --- a/docs/api/tests-api.md +++ b/docs/api/tests-api.md @@ -83,6 +83,18 @@ ./usr/bin/ubus call domo.lights read "{\"id\":2}" ``` +```json +{ + "id": 2, + "name": "Salon", + "speach_name": "salon", + "state": false, + "zone": "", + "sender": 12797322, + "interruptor": 1 +} +``` + ### update ``` diff --git a/src/prog/domod/builders/cmake/CMakeLists.txt b/src/prog/domod/builders/cmake/CMakeLists.txt index c01a2956..87adbb24 100644 --- a/src/prog/domod/builders/cmake/CMakeLists.txt +++ b/src/prog/domod/builders/cmake/CMakeLists.txt @@ -18,13 +18,16 @@ file( ${workspaceRoot}/src/prog/domod/src/main.cpp # devices ${workspaceRoot}/src/prog/domod/src/devices/devices-manager.cpp + ${workspaceRoot}/src/prog/domod/src/devices/devices-collection.cpp ${workspaceRoot}/src/prog/domod/src/devices/device.cpp - ${workspaceRoot}/src/prog/domod/src/devices/light-device.cpp - ${workspaceRoot}/src/prog/domod/src/devices/shutter-device.cpp - ${workspaceRoot}/src/prog/domod/src/devices/sprinkler-device.cpp - ${workspaceRoot}/src/prog/domod/src/devices/light.cpp - ${workspaceRoot}/src/prog/domod/src/devices/shutter.cpp + ${workspaceRoot}/src/prog/domod/src/devices/generic-dio.cpp ${workspaceRoot}/src/prog/domod/src/devices/sprinkler.cpp + ${workspaceRoot}/src/prog/domod/src/devices/shutter.cpp + +# ${workspaceRoot}/src/prog/domod/src/devices/light-device.cpp +# ${workspaceRoot}/src/prog/domod/src/devices/shutter-device.cpp +# ${workspaceRoot}/src/prog/domod/src/devices/sprinkler-device.cpp + # ubus models ${workspaceRoot}/src/prog/domod/src/ubus/capabilities-controller.cpp ${workspaceRoot}/src/prog/domod/src/ubus/lights-controller.cpp diff --git a/src/prog/domod/src/devices/device.cpp b/src/prog/domod/src/devices/device.cpp index 63ba3586..df8949f9 100644 --- a/src/prog/domod/src/devices/device.cpp +++ b/src/prog/domod/src/devices/device.cpp @@ -25,8 +25,6 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include - #include #include "domo.h" @@ -51,6 +49,17 @@ Device::~Device(void) { } +/*! ---------------------------------------------------------------------------- + * @fn get_id + * + * @brief Return the ID of the device. + */ +uint16_t Device::get_id(void) +{ + return m_id; +} + + /*! ---------------------------------------------------------------------------- * @fn from_json * @@ -59,13 +68,27 @@ Device::~Device(void) int Device::from_json(struct json_object *a_node) { struct json_object *the_value_node; - fprintf(stderr, "Device::load_from_json...\n"); - - // speach name + // 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); + } + // speech_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); } + // state + if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node)) + { + m_state = json_object_get_boolean(the_value_node); + } return 0; } @@ -77,5 +100,30 @@ int Device::from_json(struct json_object *a_node) */ struct json_object *Device::to_json(void) const { - return NULL; + struct json_object *the_root_node; + + the_root_node = json_object_new_object(); + + to_json(the_root_node); + + return the_root_node; +} + +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Export Device Object as JSON Object without creating a node. + */ +int Device::to_json(struct json_object *a_node) const +{ + // id + json_object_object_add(a_node, k_entry_id, json_object_new_int(m_id)); + // name + json_object_object_add(a_node, k_entry_name, json_object_new_string(m_name.c_str())); + // speach_name + json_object_object_add(a_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str())); + // state + json_object_object_add(a_node, k_entry_state, json_object_new_boolean(m_state)); + + return 0; } diff --git a/src/prog/domod/src/devices/device.h b/src/prog/domod/src/devices/device.h index 05b798f7..ba932507 100644 --- a/src/prog/domod/src/devices/device.h +++ b/src/prog/domod/src/devices/device.h @@ -1,5 +1,5 @@ /*! - * Device.h + * device.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -34,7 +34,6 @@ struct json_object; - /*--------------------------------- CLASS ----------------------------------*/ class Device @@ -43,11 +42,19 @@ class Device Device(void); virtual ~Device(void); + uint16_t get_id(void); + virtual int from_json(struct json_object *a_node); virtual struct json_object *to_json(void) const; + protected: + virtual int to_json(struct json_object *a_node) const; + protected: + uint16_t m_id; + std::string m_name; std::string m_speach_name; + bool m_state; }; #endif /* _DEVICE_H */ diff --git a/src/prog/domod/src/devices/devices-collection.cpp b/src/prog/domod/src/devices/devices-collection.cpp new file mode 100644 index 00000000..9a5b82ca --- /dev/null +++ b/src/prog/domod/src/devices/devices-collection.cpp @@ -0,0 +1,115 @@ +/*! + * device-collection.cpp + * + * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 04/05/2018 + * + */ + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include + +#include "device.h" + +#include "devices-collection.h" + +/*! ---------------------------------------------------------------------------- + * @fn DeviceCollection + * + * @brief Constructor of the Device Collection. + */ +DeviceCollection::DeviceCollection(void) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn DeviceCollection + * + * @brief Destructor of the Device Collection. + */ +DeviceCollection::~DeviceCollection(void) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn operator[] + * + * @brief to access to a specific light object. + */ +Device *DeviceCollection::operator[](int a_pos) +{ + return m_devices[a_pos].get(); +} + +/*! ---------------------------------------------------------------------------- + * @fn add + * + * @brief add a device to the collection. + */ +int DeviceCollection::add(Device *a_device) +{ + m_devices.push_back(std::unique_ptr(a_device)); + return 0; +} + +/*! ---------------------------------------------------------------------------- + * @fn find + * + * @brief find a device in the collection. + */ +int DeviceCollection::find(int a_pos) +{ + std::vector >::iterator the_it; + int the_status = -1; + int the_pos = 0; + + for (the_it = m_devices.begin(); the_it != m_devices.end(); the_it++) + { + if ((*the_it).get()->get_id() == a_pos) + { + return the_pos; + } + + the_pos++; + } + + return the_status; +} + +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Export the device collection as JSON Object. + */ +struct json_object *DeviceCollection::to_json(void) +{ + struct json_object *the_root_node; + std::vector >::iterator the_it; + + the_root_node = json_object_new_array(); + + for (the_it = m_devices.begin(); the_it != m_devices.end(); the_it++) + { + json_object_array_add(the_root_node, (*the_it).get()->to_json()); + } + + return the_root_node; +} diff --git a/src/prog/domod/src/devices/devices-collection.h b/src/prog/domod/src/devices/devices-collection.h new file mode 100644 index 00000000..303fbd16 --- /dev/null +++ b/src/prog/domod/src/devices/devices-collection.h @@ -0,0 +1,58 @@ +/*! + * device-collection.h + * + * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 04/05/2018 + * + */ + +#ifndef _DEVICE_COLLECTION_H +#define _DEVICE_COLLECTION_H + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include +#include + +/*------------------------------ DEPENDENCIES -------------------------------*/ + +class Device; + +/*--------------------------------- CLASS ----------------------------------*/ + +class DeviceCollection +{ + public: + DeviceCollection(void); + ~DeviceCollection(void); + + Device *operator[](int a_pos); + + int add(Device *a_device); + + int find(int a_pos); + + struct json_object *to_json(void); + + private: + std::vector > m_devices; +}; + +#endif /* _DEVICE_COLLECTION_H */ diff --git a/src/prog/domod/src/devices/devices-manager.cpp b/src/prog/domod/src/devices/devices-manager.cpp index c61e61d8..6a629e89 100644 --- a/src/prog/domod/src/devices/devices-manager.cpp +++ b/src/prog/domod/src/devices/devices-manager.cpp @@ -1,5 +1,5 @@ /*! - * Devices.cpp + * devices-manager.cpp * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -32,12 +32,17 @@ #include "domo.h" +#include "generic-dio.h" +#include "shutter.h" +#include "sprinkler.h" + + #include "devices/devices-manager.h" /*! ---------------------------------------------------------------------------- * @fn DevicesManager * - * @brief Constructor of the Devices Managagers. + * @brief Constructor of the Devices Managers. */ DevicesManager::DevicesManager(const std::string &a_path) : m_file_path(a_path) { @@ -46,7 +51,7 @@ DevicesManager::DevicesManager(const std::string &a_path) : m_file_path(a_path) /*! ---------------------------------------------------------------------------- * @fn ~DevicesManager * - * @brief Destructor of the Devices Managagers. + * @brief Destructor of the Devices Managers. */ DevicesManager::~DevicesManager(void) { @@ -72,19 +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); + load_lights(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); + load_shutters(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); + load_sprinklers(the_value_node); } /* Clean the json object. */ @@ -106,11 +111,11 @@ int DevicesManager::save(void) the_root_node = json_object_new_object(); // Lights - json_object_object_add(the_root_node, kLightEntry, m_lights.to_json()); + //json_object_object_add(the_root_node, kLightEntry, m_lights.to_json()); // Shutters - json_object_object_add(the_root_node, kShutterEntry, m_shutters.to_json()); + //json_object_object_add(the_root_node, kShutterEntry, m_shutters.to_json()); // Sprinklers - json_object_object_add(the_root_node, kSprinklerEntry, m_sprinklers.to_json()); + //json_object_object_add(the_root_node, kSprinklerEntry, m_sprinklers.to_json()); the_result = json_object_to_file(m_file_path.c_str(), the_root_node); @@ -167,7 +172,7 @@ std::string DevicesManager::get(const std::string &a_capability, uint32_t an_id) the_pos = m_lights.find(an_id); if (the_pos != -1) { - the_output = json_object_to_json_string(m_lights[the_pos].to_json()); + the_output = json_object_to_json_string(m_lights[the_pos]->to_json()); } } @@ -182,7 +187,7 @@ std::string DevicesManager::get(const std::string &a_capability, uint32_t an_id) int DevicesManager::set(const std::string &a_capability, struct json_object *a_node) { int the_result = -1; - +#if 0 if (a_capability == kLightEntry) { the_result = m_lights.set(a_node); @@ -198,7 +203,7 @@ int DevicesManager::set(const std::string &a_capability, struct json_object *a_n if (the_result == 0) save(); - +#endif return the_result; } @@ -210,7 +215,7 @@ int DevicesManager::set(const std::string &a_capability, struct json_object *a_n int DevicesManager::set_state(const std::string &a_capability, int an_id, bool a_state) { int the_result = -1; - +#if 0 if (a_capability == kLightEntry) { the_result = m_lights.set(an_id, a_state); @@ -223,7 +228,7 @@ int DevicesManager::set_state(const std::string &a_capability, int an_id, bool a { the_result = m_sprinklers.set(an_id, a_state); } - +#endif if (the_result == 0) { save(); @@ -231,3 +236,91 @@ int DevicesManager::set_state(const std::string &a_capability, int an_id, bool a return the_result; } + +/*! ---------------------------------------------------------------------------- + * @fn load_lights + * + * @brief Load Lights Object From a Json Node. + */ +int DevicesManager::load_lights(struct json_object *a_node) +{ + fprintf(stderr, "DevicesManager::load_lights\n"); + + // Get Lights + if (json_object_get_type(a_node) == json_type_array) + { + int the_len; + struct json_object *the_light_node; + + the_len = json_object_array_length(a_node); + for (int i = 0; i < the_len; i++) + { + GenericDio* the_light = new GenericDio; + the_light_node = json_object_array_get_idx(a_node, i); + the_light->from_json(the_light_node); + + m_lights.add(the_light); + } + } + + return 0; +} + +/*! ---------------------------------------------------------------------------- + * @fn load_lights + * + * @brief Load Lights Object From a Json Node. + */ +int DevicesManager::load_shutters(struct json_object *a_node) +{ + fprintf(stderr, "DevicesManager::load_shutters\n"); + + // Get Shutter + if (json_object_get_type(a_node) == json_type_array) + { + int the_len; + struct json_object *the_shutter_node; + + the_len = json_object_array_length(a_node); + for (int i = 0; i < the_len; i++) + { + Shutter* the_shutter = new Shutter; + the_shutter_node = json_object_array_get_idx(a_node, i); + the_shutter->from_json(the_shutter_node); + + m_shutters.add(the_shutter); + } + } + + return 0; + +} + +/*! ---------------------------------------------------------------------------- + * @fn load_lights + * + * @brief Load Lights Object From a Json Node. + */ +int DevicesManager::load_sprinklers(struct json_object *a_node) +{ + fprintf(stderr, "DevicesManager::load_sprinklers\n"); + + // Get Sprinklers + if (json_object_get_type(a_node) == json_type_array) + { + int the_len; + struct json_object *the_sprinkler_node; + + the_len = json_object_array_length(a_node); + for (int i = 0; i < the_len; i++) + { + Sprinkler* the_sprinkler = new Sprinkler; + the_sprinkler_node = json_object_array_get_idx(a_node, i); + the_sprinkler->from_json(the_sprinkler_node); + + m_sprinklers.add(the_sprinkler); + } + } + + return 0; +} diff --git a/src/prog/domod/src/devices/devices-manager.h b/src/prog/domod/src/devices/devices-manager.h index 24e33bd8..477875a1 100644 --- a/src/prog/domod/src/devices/devices-manager.h +++ b/src/prog/domod/src/devices/devices-manager.h @@ -1,5 +1,5 @@ /*! - * Devices.h + * devices-manager.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -30,14 +30,16 @@ #include -#include "devices/light-device.h" -#include "devices/shutter-device.h" -#include "devices/sprinkler-device.h" +//#include "devices/light-device.h" +//#include "devices/shutter-device.h" +//#include "devices/sprinkler-device.h" -#include "devices/device.h" +#include "devices/devices-collection.h" /*---------------------------------- Deps -----------------------------------*/ +struct json_object; + /*--------------------------------- CLASS ----------------------------------*/ class DevicesManager @@ -54,12 +56,17 @@ class DevicesManager int set(const std::string &a_capability, struct json_object *a_node); int set_state(const std::string &a_capability, int an_id, bool a_state); +private: + int load_lights(struct json_object *a_node); + int load_shutters(struct json_object *a_node); + int load_sprinklers(struct json_object *a_node); + private: std::string m_file_path; - LightDevice m_lights; - ShutterDevice m_shutters; - SprinklerDevice m_sprinklers; + DeviceCollection m_lights; + DeviceCollection m_shutters; + DeviceCollection m_sprinklers; }; #endif /* _DEVICES_MANAGER_H */ diff --git a/src/prog/domod/src/devices/light.cpp b/src/prog/domod/src/devices/generic-dio.cpp similarity index 71% rename from src/prog/domod/src/devices/light.cpp rename to src/prog/domod/src/devices/generic-dio.cpp index d46fc342..60713c11 100644 --- a/src/prog/domod/src/devices/light.cpp +++ b/src/prog/domod/src/devices/generic-dio.cpp @@ -1,5 +1,5 @@ /*! - * Light.cpp + * generic-dio.cpp * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -33,15 +33,15 @@ #include "domo.h" -#include "devices/light.h" +#include "generic-dio.h" /*! ---------------------------------------------------------------------------- - * @fn Light + * @fn GenericDio * * @brief Constructor of the Light Object. */ -Light::Light(void) +GenericDio::GenericDio(void) { } @@ -50,7 +50,7 @@ Light::Light(void) * * @brief Destructor of the Light Object. */ -Light::~Light(void) +GenericDio::~GenericDio(void) { } @@ -59,35 +59,20 @@ Light::~Light(void) * * @brief Load a Light Element from a Json Entry. */ -int Light::from_json(struct json_object *a_node) +int GenericDio::from_json(struct json_object *a_node) { + int the_result; 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); - } - // 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); - } + the_result = Device::from_json(a_node); + if (the_result < 0) + return the_result; + // 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)) { @@ -107,30 +92,39 @@ int Light::from_json(struct json_object *a_node) * * @brief Return Light Object as a Json Object. */ -struct json_object *Light::to_json(void) const +struct json_object *GenericDio::to_json(void) const { 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())); - // 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)); + to_json(the_root_node); return the_root_node; } +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Export Device Object as JSON Object without creating a node. + */ +int GenericDio::to_json(struct json_object *a_node) const +{ + // Get DEvices fields + Device::to_json(a_node); + + // zone + json_object_object_add(a_node, k_entry_zone, json_object_new_string(m_zone.c_str())); + // sender + json_object_object_add(a_node, k_entry_sender, json_object_new_int(m_sender)); + // interruptor + json_object_object_add(a_node, k_entry_interruptor, json_object_new_int(m_interruptor)); + + return 0; +} + + +#if 0 /*! ---------------------------------------------------------------------------- * @fn update * @@ -179,3 +173,4 @@ int Light::send_state(void) return the_result; } +#endif diff --git a/src/prog/domod/src/devices/light.h b/src/prog/domod/src/devices/generic-dio.h similarity index 79% rename from src/prog/domod/src/devices/light.h rename to src/prog/domod/src/devices/generic-dio.h index dd1e5afa..93252fe6 100644 --- a/src/prog/domod/src/devices/light.h +++ b/src/prog/domod/src/devices/generic-dio.h @@ -1,5 +1,5 @@ /*! - * Light.h + * generic-dio.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -28,9 +28,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include - -#include +#include "device.h" /*---------------------------------- Deps -----------------------------------*/ @@ -38,28 +36,25 @@ struct json_object; /*--------------------------------- CLASS ----------------------------------*/ -class Light +class GenericDio : public Device { public: - Light(void); - ~Light(void); + GenericDio(void); + ~GenericDio(void); - int from_json(struct json_object *a_node); - struct json_object *to_json(void) const; + virtual int from_json(struct json_object *a_node); + virtual struct json_object *to_json(void) const; - int update(bool a_state); +protected: + virtual int to_json(struct json_object *a_node) const; - uint16_t get_id(void); +// int update(bool a_state); private: - int send_state(void); +// int send_state(void); private: - uint16_t m_id; - std::string m_name; - std::string m_speach_name; std::string m_zone; - bool m_state; uint32_t m_sender; uint8_t m_interruptor; }; diff --git a/src/prog/domod/src/devices/shutter.cpp b/src/prog/domod/src/devices/shutter.cpp index 3fda2986..e0999b95 100644 --- a/src/prog/domod/src/devices/shutter.cpp +++ b/src/prog/domod/src/devices/shutter.cpp @@ -1,5 +1,5 @@ /*! - * Shutter.cpp + * shutter.cpp * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -61,43 +61,13 @@ Shutter::~Shutter(void) */ int Shutter::from_json(struct json_object *a_node) { + int the_result; 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); - } + + the_result = GenericDio::from_json(a_node); + if (the_result < 0) + return the_result; + // speed up if (json_object_object_get_ex(a_node, k_entry_speed_up, &the_value_node)) { @@ -123,28 +93,30 @@ struct json_object *Shutter::to_json(void) const 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)); + to_json(the_root_node); return the_root_node; } +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Export Device Object as JSON Object without creating a node. + */ +int Shutter::to_json(struct json_object *a_node) const +{ + // Get Generic Dio fields + GenericDio::to_json(a_node); + + // speed up + json_object_object_add(a_node, k_entry_speed_up, json_object_new_int(m_speed_up)); + // speed down + json_object_object_add(a_node, k_entry_speed_down, json_object_new_int(m_speed_down)); + + return 0; +} + +#if 0 /*! ---------------------------------------------------------------------------- * @fn update * @@ -179,3 +151,4 @@ int Shutter::send_state(void) { return 0; } +#endif \ No newline at end of file diff --git a/src/prog/domod/src/devices/shutter.h b/src/prog/domod/src/devices/shutter.h index 8e24f3bc..b739dd81 100644 --- a/src/prog/domod/src/devices/shutter.h +++ b/src/prog/domod/src/devices/shutter.h @@ -1,5 +1,5 @@ /*! - * Shutter.h + * shutter.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -28,7 +28,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include +#include "generic-dio.h" #include @@ -38,7 +38,7 @@ struct json_object; /*--------------------------------- CLASS ----------------------------------*/ -class Shutter +class Shutter : public GenericDio { public: Shutter(void); @@ -47,21 +47,18 @@ class Shutter int from_json(struct json_object *a_node); struct json_object *to_json(void) const; - int update(bool a_state); + protected: + int to_json(struct json_object *a_node) const; - uint16_t get_id(void); + +// int update(bool a_state); + +// uint16_t get_id(void); private: - int send_state(void); +// int send_state(void); private: - uint16_t m_id; - std::string m_name; - std::string m_speach_name; - std::string m_zone; - bool m_state; - uint32_t m_sender; - uint8_t m_interruptor; uint8_t m_speed_up; uint8_t m_speed_down; }; diff --git a/src/prog/domod/src/devices/sprinkler.cpp b/src/prog/domod/src/devices/sprinkler.cpp index 6bac354e..6c766628 100644 --- a/src/prog/domod/src/devices/sprinkler.cpp +++ b/src/prog/domod/src/devices/sprinkler.cpp @@ -1,5 +1,5 @@ /*! - * Sprinkler.cpp + * sprinkler.cpp * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -57,6 +57,7 @@ Sprinkler::~Sprinkler(void) { } +#if 0 /*! ---------------------------------------------------------------------------- * @fn from_json * @@ -160,3 +161,4 @@ int Sprinkler::send_state(void) return the_result; } +#endif \ No newline at end of file diff --git a/src/prog/domod/src/devices/sprinkler.h b/src/prog/domod/src/devices/sprinkler.h index dc3d32ce..50b94fb1 100644 --- a/src/prog/domod/src/devices/sprinkler.h +++ b/src/prog/domod/src/devices/sprinkler.h @@ -1,5 +1,5 @@ /*! - * Sprinkler.h + * sprinkler.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -28,9 +28,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include - -#include +#include "device.h" /*---------------------------------- Deps -----------------------------------*/ @@ -38,27 +36,24 @@ struct json_object; /*--------------------------------- CLASS ----------------------------------*/ -class Sprinkler +class Sprinkler : public Device { public: Sprinkler(void); ~Sprinkler(void); - int from_json(struct json_object *a_node); - struct json_object *to_json(void) const; +// int from_json(struct json_object *a_node); +// struct json_object *to_json(void) const; - int update(bool aState); +// int update(bool aState); - uint16_t get_id(void); +// uint16_t get_id(void); private: - int send_state(void); +// int send_state(void); private: - uint16_t m_id; - std::string m_name; - std::string m_speach_name; - bool m_state; + }; #endif /* _SPRINKLER_H */