////////////////////////////////////////////////////////////////////////////////
// Controller Command Library
////////////////////////////////////////////////////////////////////////////////

// <script language="javascript">
// Copyright (c) Home Automation, Inc. All rights reserved.          
// Copyright (c) HomeRun Software Systems LLC. All rights reserved.

////////////////////////////////////////////////////////////////////////////////
// Globals
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// CMD_Control: Issue command for Units, Relays and Flags
////////////////////////////////////////////////////////////////////////////////
function CMD_Control
(
	iCommand,
	iID,
	iCommandAmount,
	iDuration
)	
{
	// If in demo mode then bail
	if (DEMO_MODE)
	{
		ErrorHandler(null, ERR_DEMO_COMMANDS);
		return;
	}

	try
	{
		switch (iCommand)	
		{
			case control_off:												// Off
				g_oHAI.Units(0,iID).Off(iDuration);
				break;
			case control_on:												// On
				g_oHAI.Units(0,iID).On(iDuration);
				break;
			case control_level:												// Level
				g_oHAI.Units(0,iID).Level(iCommandAmount);
				break;
			case control_bright:											// Brighten
				g_oHAI.Units(0,iID).Brighten(iCommandAmount, iDuration);
				break;
			case control_dim:												// Dim
				g_oHAI.Units(0,iID).Dim(iCommandAmount, iDuration);
				break;
			case control_all_on:											// All On
				g_oHAI.Areas.AllOn();
				break;
			case control_all_off:											// All Off
				g_oHAI.Areas.AllOff();
				break;
			case control_ramp:												// Ramp
				g_oHAI.Units(0,iID).Ramp(iCommandAmount, iDuration);
				break;
			case control_set:												// Set
				g_oHAI.Units(0,iID).Set(iCommandAmount);
				break;
			case control_inc:												// Increment
				g_oHAI.Units(0,iID).Inc();
				break;
			case control_dec:												// Decrement
				g_oHAI.Units(0,iID).Dec();
				break;
			case control_scene:												// Scene
				g_oHAI.Units(0,iID).Compose(iCommandAmount);
				break;
			default:
		}
	}
	catch (oError)
	{
		if (oError.number == -2147467231)
			ErrorHandler(null,ERR_INVALID_COMMAND);
		else
			ErrorHandler(oError);
	}
	
	// Reset control selection to default
	g_iSetActiveControl = null;
}

////////////////////////////////////////////////////////////////////////////////
// CMD_Button: Execute a Button
////////////////////////////////////////////////////////////////////////////////
function CMD_Button
(
	iID
)	
{
	// If in demo mode then bail
	if (DEMO_MODE)
	{
		ErrorHandler(null, ERR_DEMO_COMMANDS);
		return;
	}
	
	try
	{
		g_oHAI.Buttons(0,iID).Execute();					//Execute Command
	}
	catch (oError)
	{
		if (oError.number == -2147467231)
			ErrorHandler(null,ERR_INVALID_COMMAND);
		else
			ErrorHandler(oError);
	}
}

////////////////////////////////////////////////////////////////////////////////
// CMD_Message: Control a message
////////////////////////////////////////////////////////////////////////////////
function CMD_Message
(
	iCommand,
	iID,
	szMessage
)	
{
	// If in demo mode then bail
	if (DEMO_MODE)
	{
		ErrorHandler(null, ERR_DEMO_COMMANDS);
		return;
	}
	
	try
	{
		switch(iCommand)	
		{
			case message_show:
				g_oHAI.Messages(0,iID).Show();				// Show
				break;
			case message_say:
				g_oHAI.Messages(0,iID).Say();				// Say
				break;
			case message_log:
				g_oHAI.Messages(0,iID).Log();				// Log
				break;
			case message_custom:							// Show Custom Message
				var oMsg = g_oHAI.Messages.New(szMessage, Get_Message_Capabilities()-1);
				oMsg.Show();
				break;
			case message_clear:								// Clear
				g_oHAI.Messages(0,iID).Clear();
				break;
			case message_clear_all:							// Clear All
				g_oHAI.Messages.Clear(0);
		}
	}
	catch (oError)
	{
		if (oError.number == -2147467231)
			ErrorHandler(null,ERR_INVALID_COMMAND);
		else
			ErrorHandler(oError);
	}
}

////////////////////////////////////////////////////////////////////////////////
// CMD_Security: Control Areas and Zones
////////////////////////////////////////////////////////////////////////////////
function CMD_Security
(
	iCommand,
	iID,
	szUsername,
	guid_Login,
	ItemType
)	
{
	var bResult = true;
	
	// If in demo mode then bail
	if (DEMO_MODE)
	{
		ErrorHandler(null, ERR_DEMO_COMMANDS);
		return false;
	}

	// If password was not specified then prompt for it
	if ( (guid_Login == null) || (guid_Login == "") )
	{
		ShowPage(PAGE_LOGIN, new Array(iCommand, iID, ItemType));
		return false;
	}
		  
	try
	{
		// Issue the command
		iCommand = parseInt(iCommand);
		iID = parseInt(iID);
		ItemType = parseInt(ItemType);
		
		switch (iCommand)	
		{
			case security_disarm:
				g_oHAI.Areas.Item(0,iID).Disarm(guid_Login);
				break;
			case security_armday:
				g_oHAI.Areas.Item(0,iID).Arm(guid_Login,1);
				break;
			case security_armnight:
				g_oHAI.Areas.Item(0,iID).Arm(guid_Login,2);
				break;
			case security_armaway:
				g_oHAI.Areas.Item(0,iID).Arm(guid_Login,3);
				break;
			case security_armvacation:
				g_oHAI.Areas.Item(0,iID).Arm(guid_Login,4);
				break;
			case security_armdayinstant:
				g_oHAI.Areas.Item(0,iID).Arm(guid_Login,5);
				break;
			case security_armnightdelayed:
				g_oHAI.Areas.Item(0,iID).Arm(guid_Login,6);
				break;
			case security_bypass:
				g_oHAI.Zones.Item(0,iID).ByPass(guid_Login);
				break;
			case security_restore:
				g_oHAI.Zones.Item(0,iID).Restore(guid_Login);
				break;
			case security_restoreallzones:
				// If a zone was selected then need to get it's Area
				if ( ItemType == autoTypeZone)
				{
					iID = g_oHAI.Zones.Item(0,iID).Area.ID;
				}
				g_oHAI.Areas(0,iID).Restore(guid_Login);
				break;
			case security_restoreallareas:
				g_oHAI.Areas.Restore(guid_Login);
				break;
			case security_armallareas:
				g_oHAI.Areas.Arm(guid_Login);
				break;
			case security_disarmallareas:
				g_oHAI.Areas.Disarm(guid_Login);
				break;
		}		
	}
	catch (oError)
	{
		if (oError.number == -2147467231)
			ErrorHandler(null,ERR_INVALID_COMMAND);
		else if (oError.number == -1073610747)
			ErrorHandler(null,ERR_ACCESS_DENIED);
		else
			ErrorHandler(oError);
		bResult = false;
	}
	
	return bResult;
}

////////////////////////////////////////////////////////////////////////////////
// CMD_Temperature
////////////////////////////////////////////////////////////////////////////////
function CMD_Temperature
(
	iCommand,
	iID,
	iTemperature,
	iTemperatureOption,
	iDuration
)
{
	// If in demo mode then bail
	if (DEMO_MODE)
	{
		ErrorHandler(null, ERR_DEMO_COMMANDS);
		return;
	}
	
	// If a temperature value was specified with a string then we need to parse it
	var szTemperature = new String(iTemperature);
	var iSpace = szTemperature.indexOf(" ");
	if (iSpace > 0)
	{
		iTemperature = parseInt(szTemperature.substring(0,iSpace));
	}
		
	var iTempOption = 0;
	
	// Determine the temperature option
	switch (iTemperatureOption)
	{
		case temperature_on:
		case temperature_heat:
			iTempOption = 1;
			break;
		case temperature_cool:
			iTempOption = 2;
			break;
		case temperature_auto:
			if (iCommand != temperature_mode_fan)
				iTempOption = 3;
			break;
		case temperature_hold:
			iTempOption = 255;
			break;
	}

	try
	{
		switch (iCommand)
		{
			case temperature_pesm_on:
				g_oHAI.TemperatureSensors.item(0,iID).On(iDuration);
				break;
			case temperature_pesm_off:
				g_oHAI.TemperatureSensors.item(0,iID).Off(iDuration);
				break;
			case temperature_setpoint_heat:
				if (g_oHAI.TemperatureFormat == tfFarenheit)
					g_oHAI.Thermostats.item(0,iID).HeatSetpointF = iTemperature;
				else
					g_oHAI.Thermostats.item(0,iID).HeatSetpointC = iTemperature;
				break;
			case temperature_setpoint_cool:
				if (g_oHAI.TemperatureFormat == tfFarenheit)
					g_oHAI.Thermostats.item(0,iID).CoolSetpointF = iTemperature;
				else
					g_oHAI.Thermostats.item(0,iID).CoolSetpointC = iTemperature;
				break;
			case temperature_mode_system:
				g_oHAI.Thermostats.item(0,iID).SystemMode = iTempOption;
				break;
			case temperature_mode_fan:
				g_oHAI.Thermostats.item(0,iID).FanMode = iTempOption;
				break;
			case temperature_mode_hold:
				g_oHAI.Thermostats.item(0,iID).HoldMode = iTempOption;
				break;
			case temperature_setpoint_high:
				if (g_oHAI.TemperatureFormat == tfFarenheit)
					g_oHAI.TemperatureSensors.item(0,iID).CoolSetpointF = iTemperature;
				else
					g_oHAI.TemperatureSensors.item(0,iID).CoolSetpointC = iTemperature;
				break;
			case temperature_setpoint_low:
				if (g_oHAI.TemperatureFormat == tfFarenheit)
					g_oHAI.TemperatureSensors.item(0,iID).HeatSetpointF = iTemperature;
				else
					g_oHAI.TemperatureSensors.item(0,iID).HeatSetpointC = iTemperature;
				break;
			case temperature_temp_setpoint_low_humid:
				g_oHAI.TemperatureSensors.item(0,iID).HeatSetpointF = iTemperature;
				break;
			case temperature_temp_setpoint_high_humid:
				g_oHAI.TemperatureSensors.item(0,iID).CoolSetpointF = iTemperature;
				break;
		}
	}
	catch (oError)
	{
		if (oError.number == -2147467231)
			ErrorHandler(null,ERR_INVALID_COMMAND);
		else
			ErrorHandler(oError);
		return false;
	}
	return true;
}

////////////////////////////////////////////////////////////////////////////////
// SetThermostatSystemMode: Set Thermostat System Mode
////////////////////////////////////////////////////////////////////////////////
function SetThermostatSystemMode
(
	iID,
	szMode
)
{
	var iMode = 0;
	switch(szMode.toUpperCase())
	{
		case "OFF":
			iMode = temperature_off;
			break;
		case "HEAT":
			iMode = temperature_heat;
			break;
		case "COOL":
			iMode = temperature_cool;
			break;
		case "AUTO":
			iMode = temperature_auto;
			break;
	}
	if (iMode > 0)
		return CMD_Temperature(temperature_mode_system,iID,0,iMode,0);
		
}
////////////////////////////////////////////////////////////////////////////////
// CMD_getSeconds: Convert any specified duration to seconds
////////////////////////////////////////////////////////////////////////////////
function CMD_getDuration
(
	iDurationAmount,
	DurationType
)	
{
	switch (DurationType)	
	{
		case duration_Indefinitely:
			return(0);
			break;
		case duration_Seconds:
			return(iDurationAmount);
			break;
		case duration_Minutes:
			return(iDurationAmount + 100);
			break;
		case duration_Hours:
			return(iDurationAmount + 200);
			break;
		default:
			return(0);
	}
}


