/*!
 * UBusSprinkerModel.cpp
 *
 * Copyright (c) 2015, 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: 30/04/2015
 *
 */


/*-------------------------------- INCLUDES ---------------------------------*/

#include <fstream>
#include <string>
#include <sstream> 

#include <jsoncpp/json.h>

extern "C" {
#include <libubox/blobmsg_json.h>
}

#include "sprinklers.h"

#include "UbusSprinklerModel.h"

namespace { 

using namespace UBus;

static ObjectType gSprinklerModelUbus_types(
	"sprinklers",
	Method("get", UBUS_CPP(SprinkerModel, Get)),
	Method("set", UBUS_CPP(SprinkerModel, Set))
);

}

/*!  ----------------------------------------------------------------------------
 *  @fn SprinkerModel
 *
 *  @brief Constructor of the UBus Mixer Volume.
 */
SprinkerModel::SprinkerModel (void) : 
	UBusObject (gSprinklerModelUbus_types, "sprinklers")
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn ~SprinkerModel
 *
 *  @brief Destructor of the UBus Mixer Volume.
 */
SprinkerModel::~SprinkerModel (void)
{
	
}


/*!  ----------------------------------------------------------------------------
 *  @fn Get
 *
 *  @brief Get the State of a Sprinkler Station.
 */
int SprinkerModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
							struct blob_attr *aMsg)
{
	int theResult = 0;
	struct blob_buf theBuf = {0};
	char *theString = blobmsg_format_json (aMsg, true);
	Json::Reader theReader;
	Json::StyledWriter theWriter;
	Json::Value theRoot;
	Json::Value theOutput;
	uint32_t theStation = 0;
	bool theState = false;

	if (!theReader.parse (theString, theRoot)) {

		fprintf (stderr, "Failed parse the parameters.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
  	}

	theStation = theRoot["station"].asInt();

	if (theStation == 0) {

		fprintf (stderr, "Station ID is missing.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	theResult = sprinklers_get_station (theStation, theState);

	if (theResult != 0) {

		fprintf (stderr, "Wrong Station ID (%d).\n", theStation);
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	printf ("aState: %d\n", theState);
	theOutput ["station"] = theStation;
	theOutput ["state"] = theState;

	blob_buf_init (&theBuf, 0);
	
	blobmsg_add_json_from_string (&theBuf, theWriter.write (theOutput).c_str());
	
	theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
	
	blob_buf_free (&theBuf);

	return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn Set
 *
 *  @brief Set a new State for a Sprinkler Station.
 */
int SprinkerModel::Set (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
							struct blob_attr *aMsg)
{
	int theResult;
	char *theString = blobmsg_format_json (aMsg, true);
	Json::Reader theReader;
	Json::Value theRoot;
	uint32_t theStation = 0;
	bool theState = false;

	if (!theReader.parse (theString, theRoot)) {

		fprintf (stderr, "Failed parse the parameters.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
  	}

	theStation = theRoot["station"].asInt();
	theState = theRoot["state"].asBool();

	if (theStation == 0) {

		fprintf (stderr, "Station ID is missing.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	theResult = sprinklers_set_station (theStation, theState);

	if (theResult != 0) {

		fprintf (stderr, "Wrong Station ID (%d).\n", theStation);
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	return theResult;
}
