New Device models is functional.

This commit is contained in:
2016-05-24 22:47:46 +02:00
parent ef6abd7f02
commit a9cacd64ae
20 changed files with 299 additions and 114 deletions

View File

@@ -69,7 +69,7 @@ int Device::load_from_json (Json::Value anElem)
*
* @brief Export LightDevice Object as JSON Object.
*/
Json::Value Device::to_json (void) const
Json::Value Device::to_json (bool bDataOnly) const
{
Json::Value theResult;
fprintf (stderr, "Device::to_json...\n");

View File

@@ -44,7 +44,7 @@ public:
virtual ~Device (void);
virtual int load_from_json (Json::Value anElem);
virtual Json::Value to_json (void) const;
virtual Json::Value to_json (bool bDataOnly = false) const;
protected:
std::string mspeach_name;

View File

@@ -65,6 +65,7 @@ int Devices::load (void)
Json::Value theRoot;
std::ifstream theDevicesFile (mFilePath.c_str());
fprintf (stderr, "Devices::load...\n");
/* Load and Parse the Json File. */
if (!theReader.parse (theDevicesFile, theRoot)) {
@@ -103,8 +104,7 @@ int Devices::save (void)
theRoot["Sprinklers"] = mSprinklers.to_json();
/* Save to the File. */
//std::ofstream theOutput (mTimersPath.c_str());
std::ofstream theOutput ("/tmp/toto.json");
std::ofstream theOutput (mFilePath.c_str());
theWriter.write (theOutput, theRoot);
return 0;
@@ -124,13 +124,15 @@ std::string Devices::get (const std::string &aCapability)
if (aCapability == "Lights") {
theOutput[aCapability] = mLights.to_json(true);
theOutput[aCapability] = mLights.to_json (true);
}
else if (aCapability == "Shutters") {
theOutput[aCapability] = mShutters.to_json (true);
}
else if (aCapability == "Sprinklers") {
theOutput[aCapability] = mSprinklers.to_json (true);
}
return theWriter.write (theOutput).c_str();
@@ -138,51 +140,29 @@ std::string Devices::get (const std::string &aCapability)
/*! ----------------------------------------------------------------------------
* @fn get
* @fn set
*
* @brief get the device of a specific capabilities, with the ID.
*/
Json::Value Devices::get (const std::string &aCapability, int anId)
int Devices::set (const std::string &aCapability, Json::Value anElement)
{
int theResult = -1;
}
if (aCapability == "Lights") {
#if 0
theResult = mLights.set (anElement);
}
else if (aCapability == "Shutters") {
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the list of devices of a specific capabilities.
*/
std::string Devices::get (const std::string &aCapability)
{
Json::StyledWriter theWriter;
Json::Value theOutput;
theResult = mShutters.set (anElement);
}
else if (aCapability == "Sprinklers") {
printf ("Get aCapability: %s\n", aCapability.c_str());
theOutput[aCapability] = mRoot[aCapability]["data"];
return theWriter.write (theOutput).c_str();
}
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the device of a specific capabilities, with the ID.
*/
Json::Value Devices::get (const std::string &aCapability, int anId)
{
Json::Value theResult;
for (const Json::Value& theElement : mRoot[aCapability]["data"]) {
if (theElement["id"].asInt() == anId) {
theResult = theElement;
}
theResult = mSprinklers.set (anElement);
}
return theResult;
}
if (theResult == 0)
save ();
#endif
return theResult;
}

View File

@@ -51,7 +51,7 @@ public:
int save (void);
std::string get (const std::string &aCapability);
Json::Value get (const std::string &aCapability, int anId);
int set (const std::string &aCapability, Json::Value anElement);
private:
std::string mFilePath;

View File

@@ -27,6 +27,8 @@
#include <cstdio>
#include <ubuscpp/UBusCall.h>
#include "devices/Light.h"
@@ -88,3 +90,50 @@ Json::Value Light::to_json (void) const
return aResult;
}
/*! ----------------------------------------------------------------------------
* @fn update
*
* @brief Update the Light State.
*/
int Light::update (bool aState)
{
if (mState == aState)
return 1;
mState = aState;
return sendState ();
}
/*! ----------------------------------------------------------------------------
* @fn getID
*
* @brief Return the ID of the Light.
*/
uint16_t Light::getID (void)
{
return mID;
}
/*! ----------------------------------------------------------------------------
* @fn sendState
*
* @brief Send the Curent State of the Light.
*/
int Light::sendState (void)
{
UBusCall theCmd;
Json::Value theParam;
Json::StyledWriter theWriter;
std::string theResult;
theParam["sender"] = mSender;
theParam["interruptor"] = mInterruptor;
theParam["state"] = mState;
return theCmd.Exec ("chacon", "set", theWriter.write(theParam).c_str(),theResult);
}

View File

@@ -48,6 +48,13 @@ public:
int load_from_json (Json::Value anElem);
Json::Value to_json (void) const;
int update (bool aState);
uint16_t getID (void);
private:
int sendState (void);
private:
uint16_t mID;
std::string mName;

View File

@@ -103,3 +103,41 @@ Json::Value LightDevice::to_json (bool bDataOnly)
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new light state.
*/
int LightDevice::set (Json::Value anElement)
{
int theID;
bool theState;
theID = anElement["id"].asInt();
theState = anElement["state"].asBool();
return set (theID, theState);
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new light state.
*/
int LightDevice::set (int anID, bool aState)
{
std::vector<Light>::iterator theLight_it;
for (theLight_it = mLights.begin(); theLight_it != mLights.end(); theLight_it++) {
if ((*theLight_it).getID() == anID) {
return (*theLight_it).update (aState);
}
}
return -1;
}

View File

@@ -48,6 +48,9 @@ public:
int load_from_json (Json::Value anElem);
Json::Value to_json (bool bDataOnly = false);
int set (Json::Value anElement);
int set (int anID, bool aState);
private:
std::vector <Light> mLights;
};

View File

@@ -27,6 +27,8 @@
#include <cstdio>
#include <ubuscpp/UBusCall.h>
#include "devices/Shutter.h"
@@ -92,3 +94,41 @@ Json::Value Shutter::to_json (void) const
return aResult;
}
/*! ----------------------------------------------------------------------------
* @fn update
*
* @brief Update the Shutter State.
*/
int Shutter::update (bool aState)
{
if (mState == aState)
return 1;
mState = aState;
return sendState ();
}
/*! ----------------------------------------------------------------------------
* @fn getID
*
* @brief Return the ID of the Shutter.
*/
uint16_t Shutter::getID (void)
{
return mID;
}
/*! ----------------------------------------------------------------------------
* @fn sendState
*
* @brief Send the Curent State of the Light.
*/
int Shutter::sendState (void)
{
return 0;
}

View File

@@ -48,6 +48,13 @@ public:
int load_from_json (Json::Value anElem);
Json::Value to_json (void) const;
int update (bool aState);
uint16_t getID (void);
private:
int sendState (void);
private:
uint16_t mID;
std::string mName;

View File

@@ -80,7 +80,7 @@ int ShutterDevice::load_from_json (Json::Value anElem)
*
* @brief Export ShutterDevice Object as JSON Object.
*/
Json::Value ShutterDevice::to_json (void)
Json::Value ShutterDevice::to_json (bool bDataOnly)
{
Json::Value theResult;
Json::Value data_json(Json::arrayValue);
@@ -96,5 +96,19 @@ Json::Value ShutterDevice::to_json (void)
theResult["speach_name"] = mspeach_name;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Shutter state.
*/
int ShutterDevice::set (Json::Value anElement)
{
return 0;
}

View File

@@ -45,7 +45,9 @@ public:
~ShutterDevice (void);
int load_from_json (Json::Value anElem);
Json::Value to_json (void);
Json::Value to_json (bool bDataOnly = false);
int set (Json::Value anElement);
private:
std::vector <Shutter> mShutter;

View File

@@ -27,6 +27,8 @@
#include <cstdio>
#include <ubuscpp/UBusCall.h>
#include "devices/Sprinkler.h"
@@ -82,3 +84,49 @@ Json::Value Sprinkler::to_json (void) const
return aResult;
}
/*! ----------------------------------------------------------------------------
* @fn update
*
* @brief Update the Sprinkler State.
*/
int Sprinkler::update (bool aState)
{
if (mState == aState)
return 1;
mState = aState;
return sendState ();
}
/*! ----------------------------------------------------------------------------
* @fn getID
*
* @brief Return the ID of the Sprinkler.
*/
uint16_t Sprinkler::getID (void)
{
return mID;
}
/*! ----------------------------------------------------------------------------
* @fn sendState
*
* @brief Send the Curent State of the Light.
*/
int Sprinkler::sendState (void)
{
UBusCall theCmd;
Json::Value theParam;
Json::StyledWriter theWriter;
std::string theResult;
theParam["station"] = mID;
theParam["state"] = mState;
return theCmd.Exec ("sprinklers", "set", theWriter.write(theParam).c_str(),theResult);
}

View File

@@ -48,6 +48,13 @@ public:
int load_from_json (Json::Value anElem);
Json::Value to_json (void) const;
int update (bool aState);
uint16_t getID (void);
private:
int sendState (void);
private:
uint16_t mID;
std::string mName;

View File

@@ -80,7 +80,7 @@ int SprinklerDevice::load_from_json (Json::Value anElem)
*
* @brief Export SprinklerDevice Object as JSON Object.
*/
Json::Value SprinklerDevice::to_json (void)
Json::Value SprinklerDevice::to_json (bool bDataOnly)
{
Json::Value theResult;
Json::Value data_json(Json::arrayValue);
@@ -96,5 +96,45 @@ Json::Value SprinklerDevice::to_json (void)
theResult["speach_name"] = mspeach_name;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Sprinkler state.
*/
int SprinklerDevice::set (Json::Value anElement)
{
int theID;
bool theState;
theID = anElement["id"].asInt();
theState = anElement["state"].asBool();
return set (theID, theState);
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Sprinkler state.
*/
int SprinklerDevice::set (int anID, bool aState)
{
std::vector<Sprinkler>::iterator theSprinkler_it;
for (theSprinkler_it = mSprinkler.begin(); theSprinkler_it != mSprinkler.end(); theSprinkler_it++) {
if ((*theSprinkler_it).getID() == anID) {
return (*theSprinkler_it).update (aState);
}
}
return -1;
}

View File

@@ -45,7 +45,10 @@ public:
~SprinklerDevice (void);
int load_from_json (Json::Value anElem);
Json::Value to_json (void);
Json::Value to_json (bool bDataOnly = false);
int set (Json::Value anElement);
int set (int anID, bool aState);
private:
std::vector <Sprinkler> mSprinkler;

View File

@@ -29,7 +29,7 @@ extern "C" {
#include <libubox/blobmsg_json.h>
}
#include <ubuscpp/UBusCall.h>
#include <jsoncpp/json.h>
@@ -131,7 +131,7 @@ int CapabilitiesLights::Put (struct ubus_context*, struct ubus_request_data*, st
int CapabilitiesLights::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
int theResult = 0;
struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true);
Json::Reader theReader;
@@ -139,8 +139,6 @@ int CapabilitiesLights::Post (struct ubus_context *aCtx, struct ubus_request_dat
Json::Value theRoot;
Json::Value theOutput;
Json::Value theElement;
int theID;
bool theState;
fprintf (stderr,"CapabilitiesLights::Post \n");
if (!theReader.parse (theString, theRoot)) {
@@ -149,17 +147,12 @@ int CapabilitiesLights::Post (struct ubus_context *aCtx, struct ubus_request_dat
return UBUS_STATUS_INVALID_ARGUMENT;
}
theID = theRoot["id"].asInt();
theState = theRoot["state"].asBool();
/* TODO
theElement = mDevices->get("Lights", theID);
theElement["state"] = theState;
if (mDevices->set ("Lights", theRoot) < 0) {
if (ChangeState(theElement) == 0) {
fprintf (stderr, "Failed to set the new state.\n");
return -1;
}
mDevices->set("Lights", theID, theElement);
}
*/
theOutput["Lights"] = theElement;
blob_buf_init (&theBuf, 0);
@@ -172,23 +165,3 @@ int CapabilitiesLights::Post (struct ubus_context *aCtx, struct ubus_request_dat
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn ChangeState
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesLights::ChangeState (const Json::Value &aLight)
{
UBusCall theCmd;
Json::Value theParam;
Json::StyledWriter theWriter;
std::string theResult;
theParam["sender"] = aLight["sender"];
theParam["interruptor"] = aLight["interruptor"];
theParam["state"] = aLight["state"];
return theCmd.Exec ("chacon", "set", theWriter.write(theParam).c_str(),theResult);
}

View File

@@ -53,7 +53,6 @@ public:
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
int ChangeState (const Json::Value &aLight);
Devices *mDevices;
};

View File

@@ -148,18 +148,13 @@ int CapabilitiesSprinklers::Post (struct ubus_context *aCtx, struct ubus_request
return UBUS_STATUS_INVALID_ARGUMENT;
}
theID = theRoot["id"].asInt();
theState = theRoot["state"].asBool();
/*
theElement = mDevices->get("Sprinklers", theID);
theElement["state"] = theState;
if (mDevices->set ("Sprinklers", theRoot) < 0) {
if (ChangeState(theElement) == 0) {
fprintf (stderr, "Failed to set the new state.\n");
return -1;
}
mDevices->set ("Sprinklers", theID, theElement);
}
*/
theOutput["Sprinklers"] = theElement;
theOutput["Sprinklers"] = theElement;
blob_buf_init (&theBuf, 0);
@@ -171,22 +166,3 @@ int CapabilitiesSprinklers::Post (struct ubus_context *aCtx, struct ubus_request
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn ChangeState
*
* @brief Change the State of an station.
*/
int CapabilitiesSprinklers::ChangeState (const Json::Value &aStation)
{
UBusCall theCmd;
Json::Value theParam;
Json::StyledWriter theWriter;
std::string theResult;
theParam["station"] = aStation["id"];
theParam["state"] = aStation["state"];
return theCmd.Exec ("sprinklers", "set", theWriter.write(theParam).c_str(),theResult);
}

View File

@@ -50,7 +50,6 @@ public:
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
int ChangeState (const Json::Value &aStation);
Devices *mDevices;
};