/*!
 * 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 <ubuscpp/UBusCall.h>

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


/*!  ----------------------------------------------------------------------------
 *  @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);
}
