Device could load and save as JSON Object.

This commit is contained in:
2016-05-23 23:01:12 +02:00
parent e05c267cec
commit a53d90e29e
16 changed files with 356 additions and 97 deletions

View File

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

View File

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

View File

@@ -28,6 +28,8 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <string>
#include <jsoncpp/json.h>
/*---------------------------------- 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;
};

View File

@@ -30,10 +30,6 @@
#include <jsoncpp/json.h>
#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,6 +96,11 @@ 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());

View File

@@ -29,7 +29,10 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <string>
#include <map>
#include "devices/LightDevice.h"
#include "devices/ShutterDevice.h"
#include "devices/SprinklerDevice.h"
#include "devices/Device.h"
@@ -49,7 +52,10 @@ public:
private:
std::string mFilePath;
std::map<std::string, Device> mDevices;
LightDevice mLights;
ShutterDevice mShutters;
SprinklerDevice mSprinklers;
};

View File

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

View File

@@ -29,6 +29,8 @@
#include <cstdio>
#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<Light>::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());
}
/*
int Timers::load (void)
{
Json::Reader theReader;
Json::Value theRoot;
std::ifstream theTimerFile (mTimersPath.c_str());
theResult["speach_name"] = mspeach_name;
theResult["data"] = data_json;
if (!theReader.parse (theTimerFile, theRoot)) {
fprintf (stderr, "Failed to parse the Timers File (%s).\n",
mTimersPath.c_str());
return -1;
return theResult;
}
for (Json::Value& theElement : theRoot["timers"]) {
Event theEvent;
if (theEvent.load_from_json (theElement) == 0) {
mTimers.push_back (theEvent);
}
}
Start (k60s, true);
return 0;
}*/

View File

@@ -28,12 +28,12 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <string>
#include <vector>
#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 <Light> mLights;
};

View File

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

View File

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

View File

@@ -28,6 +28,9 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <vector>
#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 <Shutter> mShutter;
};

View File

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

View File

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

View File

@@ -28,6 +28,9 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <vector>
#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 <Sprinkler> mSprinkler;
};

View File

@@ -30,8 +30,6 @@
#include <iostream>
#include <fstream>
#include <vector>
#include "devices/Devices.h"

View File

@@ -28,10 +28,11 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <jsoncpp/json.h>
#include <string>
#include <vector>
#include <jsoncpp/json.h>
#include "ubuscpp/UBusTimer.h"
#include "timers/Event.h"