From a9cacd64ae6f14986c7c0c246977991949573132 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 24 May 2016 22:47:46 +0200 Subject: [PATCH] New Device models is functional. --- src/domod/src/devices/Device.cpp | 2 +- src/domod/src/devices/Device.h | 2 +- src/domod/src/devices/Devices.cpp | 60 +++++++------------ src/domod/src/devices/Devices.h | 2 +- src/domod/src/devices/Light.cpp | 49 +++++++++++++++ src/domod/src/devices/Light.h | 7 +++ src/domod/src/devices/LightDevice.cpp | 38 ++++++++++++ src/domod/src/devices/LightDevice.h | 5 +- src/domod/src/devices/Shutter.cpp | 40 +++++++++++++ src/domod/src/devices/Shutter.h | 7 +++ src/domod/src/devices/ShutterDevice.cpp | 16 ++++- src/domod/src/devices/ShutterDevice.h | 6 +- src/domod/src/devices/Sprinkler.cpp | 48 +++++++++++++++ src/domod/src/devices/Sprinkler.h | 7 +++ src/domod/src/devices/SprinklerDevice.cpp | 44 +++++++++++++- src/domod/src/devices/SprinklerDevice.h | 5 +- src/domod/src/ubus/capabilities_lights.cpp | 39 ++---------- src/domod/src/ubus/capabilities_lights.h | 1 - .../src/ubus/capabilities_sprinklers.cpp | 34 ++--------- src/domod/src/ubus/capabilities_sprinklers.h | 1 - 20 files changed, 299 insertions(+), 114 deletions(-) diff --git a/src/domod/src/devices/Device.cpp b/src/domod/src/devices/Device.cpp index 3c07773b..ea095905 100644 --- a/src/domod/src/devices/Device.cpp +++ b/src/domod/src/devices/Device.cpp @@ -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"); diff --git a/src/domod/src/devices/Device.h b/src/domod/src/devices/Device.h index c3946722..f8fbf276 100644 --- a/src/domod/src/devices/Device.h +++ b/src/domod/src/devices/Device.h @@ -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; diff --git a/src/domod/src/devices/Devices.cpp b/src/domod/src/devices/Devices.cpp index 26d25616..bcba3981 100644 --- a/src/domod/src/devices/Devices.cpp +++ b/src/domod/src/devices/Devices.cpp @@ -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 \ No newline at end of file + return theResult; +} diff --git a/src/domod/src/devices/Devices.h b/src/domod/src/devices/Devices.h index 2765bf6f..da5ce85a 100644 --- a/src/domod/src/devices/Devices.h +++ b/src/domod/src/devices/Devices.h @@ -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; diff --git a/src/domod/src/devices/Light.cpp b/src/domod/src/devices/Light.cpp index 174844f8..81bc4e8b 100644 --- a/src/domod/src/devices/Light.cpp +++ b/src/domod/src/devices/Light.cpp @@ -27,6 +27,8 @@ #include +#include + #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); +} diff --git a/src/domod/src/devices/Light.h b/src/domod/src/devices/Light.h index 08e63424..010b2030 100644 --- a/src/domod/src/devices/Light.h +++ b/src/domod/src/devices/Light.h @@ -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; diff --git a/src/domod/src/devices/LightDevice.cpp b/src/domod/src/devices/LightDevice.cpp index 0a6ceefb..edaf2584 100644 --- a/src/domod/src/devices/LightDevice.cpp +++ b/src/domod/src/devices/LightDevice.cpp @@ -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::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; +} diff --git a/src/domod/src/devices/LightDevice.h b/src/domod/src/devices/LightDevice.h index 1a2f4943..d0584c55 100644 --- a/src/domod/src/devices/LightDevice.h +++ b/src/domod/src/devices/LightDevice.h @@ -47,7 +47,10 @@ 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 mLights; }; diff --git a/src/domod/src/devices/Shutter.cpp b/src/domod/src/devices/Shutter.cpp index f92693a5..6ed7c874 100644 --- a/src/domod/src/devices/Shutter.cpp +++ b/src/domod/src/devices/Shutter.cpp @@ -27,6 +27,8 @@ #include +#include + #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; +} diff --git a/src/domod/src/devices/Shutter.h b/src/domod/src/devices/Shutter.h index b01cbe98..dc32313a 100644 --- a/src/domod/src/devices/Shutter.h +++ b/src/domod/src/devices/Shutter.h @@ -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; diff --git a/src/domod/src/devices/ShutterDevice.cpp b/src/domod/src/devices/ShutterDevice.cpp index 47db4be0..10c81597 100644 --- a/src/domod/src/devices/ShutterDevice.cpp +++ b/src/domod/src/devices/ShutterDevice.cpp @@ -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; +} diff --git a/src/domod/src/devices/ShutterDevice.h b/src/domod/src/devices/ShutterDevice.h index badbaeeb..3c01b8d4 100644 --- a/src/domod/src/devices/ShutterDevice.h +++ b/src/domod/src/devices/ShutterDevice.h @@ -45,8 +45,10 @@ 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 mShutter; }; diff --git a/src/domod/src/devices/Sprinkler.cpp b/src/domod/src/devices/Sprinkler.cpp index fc1242fb..a981a2d0 100644 --- a/src/domod/src/devices/Sprinkler.cpp +++ b/src/domod/src/devices/Sprinkler.cpp @@ -27,6 +27,8 @@ #include +#include + #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); +} diff --git a/src/domod/src/devices/Sprinkler.h b/src/domod/src/devices/Sprinkler.h index 1f97c738..1de2ce44 100644 --- a/src/domod/src/devices/Sprinkler.h +++ b/src/domod/src/devices/Sprinkler.h @@ -47,6 +47,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; diff --git a/src/domod/src/devices/SprinklerDevice.cpp b/src/domod/src/devices/SprinklerDevice.cpp index e6cf6c2b..a8723e1d 100644 --- a/src/domod/src/devices/SprinklerDevice.cpp +++ b/src/domod/src/devices/SprinklerDevice.cpp @@ -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); @@ -95,6 +95,46 @@ 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::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; +} diff --git a/src/domod/src/devices/SprinklerDevice.h b/src/domod/src/devices/SprinklerDevice.h index 1ebc0450..53166a34 100644 --- a/src/domod/src/devices/SprinklerDevice.h +++ b/src/domod/src/devices/SprinklerDevice.h @@ -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 mSprinkler; diff --git a/src/domod/src/ubus/capabilities_lights.cpp b/src/domod/src/ubus/capabilities_lights.cpp index 3566d016..edc803ad 100644 --- a/src/domod/src/ubus/capabilities_lights.cpp +++ b/src/domod/src/ubus/capabilities_lights.cpp @@ -29,7 +29,7 @@ extern "C" { #include } -#include + #include @@ -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); -} diff --git a/src/domod/src/ubus/capabilities_lights.h b/src/domod/src/ubus/capabilities_lights.h index 11c08d17..a794f22a 100644 --- a/src/domod/src/ubus/capabilities_lights.h +++ b/src/domod/src/ubus/capabilities_lights.h @@ -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; }; diff --git a/src/domod/src/ubus/capabilities_sprinklers.cpp b/src/domod/src/ubus/capabilities_sprinklers.cpp index 54cd9aa4..f244e4fc 100644 --- a/src/domod/src/ubus/capabilities_sprinklers.cpp +++ b/src/domod/src/ubus/capabilities_sprinklers.cpp @@ -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); -} diff --git a/src/domod/src/ubus/capabilities_sprinklers.h b/src/domod/src/ubus/capabilities_sprinklers.h index 6d4a9e0d..e399cb35 100644 --- a/src/domod/src/ubus/capabilities_sprinklers.h +++ b/src/domod/src/ubus/capabilities_sprinklers.h @@ -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; };