/*!
 * main.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: 04/03/2015
 *
 */

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

#include <cstdio>

extern "C" {
#include <stdlib.h>     /* getenv */
#include <libubus.h>
}

#include "ubus/capabilities.h"
#include "ubus/capabilities_lights.h"
#include "ubus/capabilities_shutters.h"
#include "ubus/capabilities_sprinklers.h"
#include "ubus/speach_command.h"
#include "ubus/timers.h"

#include "timers/Timers.h"
#include "devices/Devices.h"

/*--------------------------------- GLOBALS ---------------------------------*/



/*!  ----------------------------------------------------------------------------
 *  @fn setupUbus
 *
 *  @brief Setup the UBus Mechanism.
 */
extern "C" {
static struct ubus_context *setupUbus (void)
{
	ubus_context *theCtx;
	
	if (uloop_init () !=0) {
		
		fprintf (stderr, "Failed to init Uloop.\n");
		return NULL;
	}
	
	signal (SIGPIPE, SIG_IGN);

	theCtx = ubus_connect (NULL);

	if (!theCtx) {

		fprintf (stderr, "Failed to connect to ubus\n");
		return NULL;
	}
	
	ubus_add_uloop (theCtx);
	
	return theCtx;
}
} // SetupUbus.


/*!  ----------------------------------------------------------------------------
 *  @fn main
 *
 *  @brief Main function of Domo Daemon.
 */
int main (void)
{
	int theRet = 0;
	char *theWritePath;
	std::string theDevicePath, theTimerPath;
	struct ubus_context *theCtx = NULL;

	theWritePath = getenv ("DOMO_WRITE_PATH");
	if (theWritePath == NULL) {

		fprintf (stderr, "you should export DOMO_WRITE_PATH. with the Path of data.\n");
		return -1;
	}

	// Create Configs Paths...

	theDevicePath = theWritePath;
	theDevicePath += "/Devices.json";
	
	theTimerPath = theWritePath;
	theTimerPath += "/Timers.json";

	//printf ("DOMO PATH: %s\n", thePath.c_str());
	
	Devices theDevices (theDevicePath);
	
	if (theDevices.load() != 0) {
		
		return -1;
	}

	/* Setup the Timers. */
	Timers theTimers (theTimerPath, &theDevices);
	
	if (theTimers.load() != 0) {
			
		return -1;
	}
	
	/* Setup the UBus Models. */
	CapabilitiesModel theCapabilities;
	CapabilitiesLightsModel theCapLights (&theDevices);
	CapabilitiesShuttersModel theCapShutters (&theDevices);
	CapabilitiesSprinklersModel theCapSprinklers (&theDevices);
	SpeachCommandModel theSpeachCommand (&theDevices);
	TimersModel theTimersModel (&theTimers);

	/* Setup the Ubus context. */
	theCtx = setupUbus ();
	if (theCtx == NULL) {

		return -1;
	}



	/* Add the UBus to the model exposed. */
	ubus_add_object (theCtx, &theCapabilities);
	ubus_add_object (theCtx, &theCapLights);
	ubus_add_object (theCtx, &theCapShutters);
	ubus_add_object (theCtx, &theCapSprinklers);
	ubus_add_object (theCtx, &theSpeachCommand);
	ubus_add_object (theCtx, &theTimersModel);
	
	/* Main Event Loop. */
	uloop_run ();

	ubus_free (theCtx);
	uloop_done ();

	return theRet;
}
