From a53d90e29e1a3f557abb9e0daf48a902376032eb Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 23 May 2016 23:01:12 +0200 Subject: [PATCH] Device could load and save as JSON Object. --- src/data/Timers.json | 4 +- src/domod/src/devices/Device.cpp | 3 +- src/domod/src/devices/Device.h | 5 ++ src/domod/src/devices/Devices.cpp | 61 +++------------ src/domod/src/devices/Devices.h | 14 +++- src/domod/src/devices/Light.cpp | 6 +- src/domod/src/devices/LightDevice.cpp | 56 +++++++------- src/domod/src/devices/LightDevice.h | 5 +- src/domod/src/devices/Shutter.cpp | 94 +++++++++++++++++++++++ src/domod/src/devices/ShutterDevice.cpp | 48 ++++++++++++ src/domod/src/devices/ShutterDevice.h | 9 +++ src/domod/src/devices/Sprinkler.cpp | 84 ++++++++++++++++++++ src/domod/src/devices/SprinklerDevice.cpp | 48 ++++++++++++ src/domod/src/devices/SprinklerDevice.h | 9 +++ src/domod/src/timers/Timers.cpp | 4 +- src/domod/src/timers/Timers.h | 3 +- 16 files changed, 356 insertions(+), 97 deletions(-) diff --git a/src/data/Timers.json b/src/data/Timers.json index e564a769..477cfe6a 100644 --- a/src/data/Timers.json +++ b/src/data/Timers.json @@ -11,9 +11,9 @@ { "active": true, "id": "Sprinklers/7", - "start_time": "17:30", + "start_time": "22:38", "recurrence": 0, - "duration": 30, + "duration": 1, "action": "stop" } ] diff --git a/src/domod/src/devices/Device.cpp b/src/domod/src/devices/Device.cpp index 9ed19e6f..3c07773b 100644 --- a/src/domod/src/devices/Device.cpp +++ b/src/domod/src/devices/Device.cpp @@ -59,6 +59,7 @@ Device::~Device (void) */ int Device::load_from_json (Json::Value anElem) { + fprintf (stderr, "Device::load_from_json...\n"); return 0; } @@ -71,6 +72,6 @@ int Device::load_from_json (Json::Value anElem) Json::Value Device::to_json (void) const { Json::Value theResult; - + fprintf (stderr, "Device::to_json...\n"); return theResult; } diff --git a/src/domod/src/devices/Device.h b/src/domod/src/devices/Device.h index 0179f750..c3946722 100644 --- a/src/domod/src/devices/Device.h +++ b/src/domod/src/devices/Device.h @@ -28,6 +28,8 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include + #include /*---------------------------------- Deps -----------------------------------*/ @@ -43,6 +45,9 @@ public: virtual int load_from_json (Json::Value anElem); virtual Json::Value to_json (void) const; + +protected: + std::string mspeach_name; }; diff --git a/src/domod/src/devices/Devices.cpp b/src/domod/src/devices/Devices.cpp index 9612cd9d..98250b8c 100644 --- a/src/domod/src/devices/Devices.cpp +++ b/src/domod/src/devices/Devices.cpp @@ -30,10 +30,6 @@ #include -#include "devices/LightDevice.h" -#include "devices/ShutterDevice.h" -#include "devices/SprinklerDevice.h" - #include "devices/Devices.h" @@ -78,62 +74,18 @@ int Devices::load (void) } /* Open Lights. */ - LightDevice theLights; - theLights.load_from_json (theRoot["Lights"]); - mDevices["Lights"] = theLights; + mLights.load_from_json (theRoot["Lights"]); /* Open Shutters. */ - ShutterDevice theShutters; - theShutters.load_from_json (theRoot["Shutters"]); - mDevices["Shutters"] = theShutters; + mShutters.load_from_json (theRoot["Shutters"]); /* Open Sprinklers. */ - SprinklerDevice theSprinklers; - theSprinklers.load_from_json (theRoot["Sprinklers"]); - mDevices["Sprinklers"] = theSprinklers; + mSprinklers.load_from_json (theRoot["Sprinklers"]); return 0; } -/* - - "Lights" : { - "speach_name": "lumière", - "data": [ - { - "id": 1, - "name": "Bureau JB", - "speach_name": "bureau", - "zone": "", - "state": true, - "sender": 12797322, - "interruptor": 0 - }, - { - "id": 2, - "name": "Salon", - "speach_name": "salon", - "zone": "", - "state": false, - "sender": 12797322, - "interruptor": 1 - }, - { - "id": 3, - "name": "Sapin", - "speach_name": "sapin", - "zone": "", - "state": false, - "sender": 12797322, - "interruptor": 2 - } - ] - }, - -*/ - - /*! ---------------------------------------------------------------------------- * @fn save * @@ -144,7 +96,12 @@ int Devices::save (void) Json::StyledStreamWriter theWriter; Json::Value theRoot(Json::objectValue); - + fprintf (stderr, "Devices::Save...\n"); + + theRoot["Lights"] = mLights.to_json(); + theRoot["Shutters"] = mShutters.to_json(); + theRoot["Sprinklers"] = mSprinklers.to_json(); + /* Save to the File. */ //std::ofstream theOutput (mTimersPath.c_str()); std::ofstream theOutput ("/tmp/toto.json"); diff --git a/src/domod/src/devices/Devices.h b/src/domod/src/devices/Devices.h index ddaaf0c6..6c070981 100644 --- a/src/domod/src/devices/Devices.h +++ b/src/domod/src/devices/Devices.h @@ -29,7 +29,10 @@ /*------------------------------- INCLUDES ----------------------------------*/ #include -#include + +#include "devices/LightDevice.h" +#include "devices/ShutterDevice.h" +#include "devices/SprinklerDevice.h" #include "devices/Device.h" @@ -43,13 +46,16 @@ class Devices { public: Devices (std::string aPath); ~Devices (void); - + int load (void); int save (void); - + private: std::string mFilePath; - std::map mDevices; + + LightDevice mLights; + ShutterDevice mShutters; + SprinklerDevice mSprinklers; }; diff --git a/src/domod/src/devices/Light.cpp b/src/domod/src/devices/Light.cpp index 68f166e6..174844f8 100644 --- a/src/domod/src/devices/Light.cpp +++ b/src/domod/src/devices/Light.cpp @@ -37,7 +37,6 @@ */ Light::Light (void) { - } @@ -48,7 +47,6 @@ Light::Light (void) */ Light::~Light (void) { - } @@ -66,6 +64,8 @@ int Light::load_from_json (Json::Value anElem) mState = anElem["state"].asBool(); mSender = anElem["sender"].asInt(); mInterruptor = anElem["interruptor"].asInt(); + + return 0; } @@ -77,7 +77,7 @@ int Light::load_from_json (Json::Value anElem) Json::Value Light::to_json (void) const { Json::Value aResult(Json::objectValue); - + fprintf (stderr, "Light::to_json\n"); aResult["id"] = mID; aResult["name"] = mName; aResult["speach_name"] = mSpeach_name; diff --git a/src/domod/src/devices/LightDevice.cpp b/src/domod/src/devices/LightDevice.cpp index 1958214b..56ddea40 100644 --- a/src/domod/src/devices/LightDevice.cpp +++ b/src/domod/src/devices/LightDevice.cpp @@ -29,6 +29,8 @@ #include +#include "devices/Light.h" + #include "devices/LightDevice.h" @@ -60,6 +62,17 @@ LightDevice::~LightDevice (void) int LightDevice::load_from_json (Json::Value anElem) { fprintf (stderr, "LightDevice::load_from_json\n"); + + mspeach_name = anElem["speach_name"].asString(); + + for (Json::Value& theElement : anElem["data"]) { + Light theLight; + if (theLight.load_from_json (theElement) == 0) { + + mLights.push_back (theLight); + } + } + return 0; } @@ -69,34 +82,21 @@ int LightDevice::load_from_json (Json::Value anElem) * * @brief Export LightDevice Object as JSON Object. */ -Json::Value LightDevice::to_json (void) const +Json::Value LightDevice::to_json (void) { + Json::Value theResult; + Json::Value data_json(Json::arrayValue); + std::vector::iterator theLight_it; + fprintf (stderr, "LightDevice::to_json\n"); + + for (theLight_it = mLights.begin(); theLight_it != mLights.end(); theLight_it++) { + + data_json.append ((*theLight_it).to_json()); + } + + theResult["speach_name"] = mspeach_name; + theResult["data"] = data_json; + + return theResult; } - -/* -int Timers::load (void) -{ - Json::Reader theReader; - Json::Value theRoot; - std::ifstream theTimerFile (mTimersPath.c_str()); - - if (!theReader.parse (theTimerFile, theRoot)) { - - fprintf (stderr, "Failed to parse the Timers File (%s).\n", - mTimersPath.c_str()); - return -1; - } - - for (Json::Value& theElement : theRoot["timers"]) { - Event theEvent; - if (theEvent.load_from_json (theElement) == 0) { - - mTimers.push_back (theEvent); - } - } - - Start (k60s, true); - - return 0; -}*/ diff --git a/src/domod/src/devices/LightDevice.h b/src/domod/src/devices/LightDevice.h index 4580cd6e..00728f8a 100644 --- a/src/domod/src/devices/LightDevice.h +++ b/src/domod/src/devices/LightDevice.h @@ -28,12 +28,12 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include #include #include "devices/Light.h" #include "devices/Device.h" + /*---------------------------------- Deps -----------------------------------*/ @@ -46,10 +46,9 @@ public: ~LightDevice (void); int load_from_json (Json::Value anElem); - Json::Value to_json (void) const; + Json::Value to_json (void); private: - std::string mspeach_name; std::vector mLights; }; diff --git a/src/domod/src/devices/Shutter.cpp b/src/domod/src/devices/Shutter.cpp index e69de29b..f92693a5 100644 --- a/src/domod/src/devices/Shutter.cpp +++ b/src/domod/src/devices/Shutter.cpp @@ -0,0 +1,94 @@ +/*! + * Shutter.cpp + * + * Copyright (c) 2016, 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: 21/05/2016 + * + */ + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include + +#include "devices/Shutter.h" + + +/*! ---------------------------------------------------------------------------- + * @fn Shutter + * + * @brief Constructor of the Shutter Object. + */ +Shutter::Shutter (void) +{ +} + + +/*! ---------------------------------------------------------------------------- + * @fn ~Shutter + * + * @brief Destructor of the Shutter Object. + */ +Shutter::~Shutter (void) +{ +} + + +/*! ---------------------------------------------------------------------------- + * @fn load_from_json + * + * @brief Load a Shutter Element from a Json Entry. + */ +int Shutter::load_from_json (Json::Value anElem) +{ + 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(); + + return 0; +} + + +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Return Shutter Object as a Json Object. + */ +Json::Value Shutter::to_json (void) const +{ + 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; + + return aResult; +} diff --git a/src/domod/src/devices/ShutterDevice.cpp b/src/domod/src/devices/ShutterDevice.cpp index 24baf490..47db4be0 100644 --- a/src/domod/src/devices/ShutterDevice.cpp +++ b/src/domod/src/devices/ShutterDevice.cpp @@ -50,3 +50,51 @@ ShutterDevice::ShutterDevice (void) : Device() ShutterDevice::~ShutterDevice (void) { } + + +/*! ---------------------------------------------------------------------------- + * @fn load_from_json + * + * @brief Load ShutterDevice Object From a Json Object. + */ +int ShutterDevice::load_from_json (Json::Value anElem) +{ + fprintf (stderr, "ShutterDevice::load_from_json\n"); + + mspeach_name = anElem["speach_name"].asString(); + + for (Json::Value& theElement : anElem["data"]) { + Shutter theShutter; + if (theShutter.load_from_json (theElement) == 0) { + + mShutter.push_back (theShutter); + } + } + + return 0; +} + + +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Export ShutterDevice Object as JSON Object. + */ +Json::Value ShutterDevice::to_json (void) +{ + Json::Value theResult; + Json::Value data_json(Json::arrayValue); + std::vector::iterator theShutter_it; + + fprintf (stderr, "ShutterDevice::to_json\n"); + + for (theShutter_it = mShutter.begin(); theShutter_it != mShutter.end(); theShutter_it++) { + + data_json.append ((*theShutter_it).to_json()); + } + + theResult["speach_name"] = mspeach_name; + theResult["data"] = data_json; + + return theResult; +} diff --git a/src/domod/src/devices/ShutterDevice.h b/src/domod/src/devices/ShutterDevice.h index 090ac3b7..badbaeeb 100644 --- a/src/domod/src/devices/ShutterDevice.h +++ b/src/domod/src/devices/ShutterDevice.h @@ -28,6 +28,9 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include + +#include "devices/Shutter.h" #include "devices/Device.h" /*---------------------------------- Deps -----------------------------------*/ @@ -40,6 +43,12 @@ class ShutterDevice : public Device { public: ShutterDevice (void); ~ShutterDevice (void); + + int load_from_json (Json::Value anElem); + Json::Value to_json (void); + +private: + std::vector mShutter; }; diff --git a/src/domod/src/devices/Sprinkler.cpp b/src/domod/src/devices/Sprinkler.cpp index e69de29b..fc1242fb 100644 --- a/src/domod/src/devices/Sprinkler.cpp +++ b/src/domod/src/devices/Sprinkler.cpp @@ -0,0 +1,84 @@ +/*! + * Sprinkler.cpp + * + * Copyright (c) 2016, 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: 21/05/2016 + * + */ + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include + +#include "devices/Sprinkler.h" + + +/*! ---------------------------------------------------------------------------- + * @fn Sprinkler + * + * @brief Constructor of the Sprinkler Object. + */ +Sprinkler::Sprinkler (void) +{ +} + + +/*! ---------------------------------------------------------------------------- + * @fn ~Sprinkler + * + * @brief Destructor of the Sprinkler Object. + */ +Sprinkler::~Sprinkler (void) +{ +} + + +/*! ---------------------------------------------------------------------------- + * @fn load_from_json + * + * @brief Load a Sprinkler Element from a Json Entry. + */ +int Sprinkler::load_from_json (Json::Value anElem) +{ + mID = anElem["id"].asInt(); + mName = anElem["name"].asString(); + mSpeach_name = anElem["speach_name"].asString(); + mState = anElem["state"].asBool(); + + return 0; +} + + +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Return Sprinkler Object as a Json Object. + */ +Json::Value Sprinkler::to_json (void) const +{ + Json::Value aResult(Json::objectValue); + fprintf (stderr, "Sprinkler::to_json\n"); + aResult["id"] = mID; + aResult["name"] = mName; + aResult["speach_name"] = mSpeach_name; + aResult["state"] = mState; + + return aResult; +} diff --git a/src/domod/src/devices/SprinklerDevice.cpp b/src/domod/src/devices/SprinklerDevice.cpp index 190888a9..e6cf6c2b 100644 --- a/src/domod/src/devices/SprinklerDevice.cpp +++ b/src/domod/src/devices/SprinklerDevice.cpp @@ -50,3 +50,51 @@ SprinklerDevice::SprinklerDevice (void) : Device() SprinklerDevice::~SprinklerDevice (void) { } + + +/*! ---------------------------------------------------------------------------- + * @fn load_from_json + * + * @brief Load SprinklerDevice Object From a Json Object. + */ +int SprinklerDevice::load_from_json (Json::Value anElem) +{ + fprintf (stderr, "SprinklerDevice::load_from_json\n"); + + mspeach_name = anElem["speach_name"].asString(); + + for (Json::Value& theElement : anElem["data"]) { + Sprinkler theSprinkler; + if (theSprinkler.load_from_json (theElement) == 0) { + + mSprinkler.push_back (theSprinkler); + } + } + + return 0; +} + + +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Export SprinklerDevice Object as JSON Object. + */ +Json::Value SprinklerDevice::to_json (void) +{ + Json::Value theResult; + Json::Value data_json(Json::arrayValue); + std::vector::iterator theSprinkler_it; + + fprintf (stderr, "SprinklerDevice::to_json\n"); + + for (theSprinkler_it = mSprinkler.begin(); theSprinkler_it != mSprinkler.end(); theSprinkler_it++) { + + data_json.append ((*theSprinkler_it).to_json()); + } + + theResult["speach_name"] = mspeach_name; + theResult["data"] = data_json; + + return theResult; +} diff --git a/src/domod/src/devices/SprinklerDevice.h b/src/domod/src/devices/SprinklerDevice.h index c48dde63..1ebc0450 100644 --- a/src/domod/src/devices/SprinklerDevice.h +++ b/src/domod/src/devices/SprinklerDevice.h @@ -28,6 +28,9 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include + +#include "devices/Sprinkler.h" #include "devices/Device.h" /*---------------------------------- Deps -----------------------------------*/ @@ -40,6 +43,12 @@ class SprinklerDevice : public Device { public: SprinklerDevice (void); ~SprinklerDevice (void); + + int load_from_json (Json::Value anElem); + Json::Value to_json (void); + +private: + std::vector mSprinkler; }; diff --git a/src/domod/src/timers/Timers.cpp b/src/domod/src/timers/Timers.cpp index e8d0fc28..90ceb150 100644 --- a/src/domod/src/timers/Timers.cpp +++ b/src/domod/src/timers/Timers.cpp @@ -30,8 +30,6 @@ #include #include -#include - #include "devices/Devices.h" @@ -109,7 +107,7 @@ int Timers::save (void) std::vector::iterator timer_evt; Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue); - for(timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { + for (timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { timers_json.append ((*timer_evt).to_json()); } diff --git a/src/domod/src/timers/Timers.h b/src/domod/src/timers/Timers.h index df84d76e..1386511f 100644 --- a/src/domod/src/timers/Timers.h +++ b/src/domod/src/timers/Timers.h @@ -28,10 +28,11 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include #include #include +#include + #include "ubuscpp/UBusTimer.h" #include "timers/Event.h"