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

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


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