using System;
using HomeSeerAPI;    // @DONOTEXPORT




public class csToggleDeviceSample
{
    readonly HomeSeerAPI.IHSApplication hs = CSscripts.CSinit.hsobject;       // @DONOTEXPORT
    readonly HomeSeer.PluginSdk.IHsController hs4 = CSscripts.CSinit.hs4object;    // @DONOTEXPORT
    public void Main(object parm)
    {
        //
        // Toggle the Specified Device ON/OFF
        //   Anything but OFF is considered ON
        //
        // Specify the Device Name, including Locations, as a Parameter
        //
        string theDevice;
        CAPI.CAPIControl ccOFF;
        CAPI.CAPIControl ccON;
        string theOffLabel;
        string devStatus;
        CAPI.CAPIControlResponse ccResponse;
        // Get the Device from the parameter
        theDevice = Convert.ToString(parm);
        // Get the CAPI ControlUse value for OFF
        ccOFF = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._Off);
        if (ccOFF == null)
        {
            hs.WriteLog("ToggleDevice", "ControlUse not define for device: " + theDevice);
            return;
        }
        theOffLabel = ccOFF.Label;
        // Get the Device CAPIStatus
        devStatus = hs.CAPIGetStatus(hs.GetDeviceRefByName(theDevice)).Status;
        // Determine Current Status, and toggle it
        if (theOffLabel.ToUpper().Trim() == devStatus.ToUpper().Trim())
        {
            // Currently OFF, turn it ON
            ccON = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._On);
            ccResponse = hs.CAPIControlHandler(ccON);

            if (ccResponse != CAPI.CAPIControlResponse.All_Success)
            {
                hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " + Convert.ToString(ccResponse));
            }
            else
            {
                // NOT OFF, turn it OFF
                ccResponse = hs.CAPIControlHandler(ccOFF);
                if (ccResponse != CAPI.CAPIControlResponse.All_Success)
                {
                    hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " + ccResponse.ToString());
                }
            }
        }
    }
}




public class csScriptWithEmbeddedClassDefinition
{
    readonly HomeSeerAPI.IHSApplication hs = CSscripts.CSinit.hsobject;       // @DONOTEXPORT
    readonly HomeSeer.PluginSdk.IHsController hs4 = CSscripts.CSinit.hs4object;    // @DONOTEXPORT
    public void Main(object parm)
    {
        TestClass o;
        o = new TestClass("Hello World", hs);
        o.Logit();
    }
    class TestClass
    {
        private string _LogMessage;
        private HomeSeerAPI.IHSApplication _hs;
        public TestClass(string LogMessage, HomeSeerAPI.IHSApplication hs)
        {
            _LogMessage = LogMessage;
            _hs = hs;
        }
        public void Logit()
        {
            _hs.WriteLog("Logit", _LogMessage);
        }
    }
}



public class csNonHsParmsSample
//
// If running under tenScripting, don't forget to uncheck: Process as HS Parameters
//
{
    readonly HomeSeerAPI.IHSApplication hs = CSscripts.CSinit.hsobject;       // @DONOTEXPORT
    readonly HomeSeer.PluginSdk.IHsController hs4 = CSscripts.CSinit.hs4object;    // @DONOTEXPORT
    public void Main(object parm)
    {
        string[] parms;
        parms = parm as string[];
        if (parms.Length > 0)
        {
            for (int i = 0; i < parms.Length; i++)
            {
                hs.WriteLog("SampleScript", "Parameter" + i.ToString() + "=" + parms[i].ToString());
            }
        }
        else
        {
            hs.WriteLog("SampleScript", "No Parameters Specified");
        }
    }
}



public class csTwoMethods
{
    readonly HomeSeerAPI.IHSApplication hs = CSscripts.CSinit.hsobject;       // @DONOTEXPORT
    readonly HomeSeer.PluginSdk.IHsController hs4 = CSscripts.CSinit.hs4object;    // @DONOTEXPORT
    public void ToggleFrontPorch(object parm)
    {
        ToggleDevice("Outside Front Porch");
    }
    public void ToggleBackPorch(object parm)
    {
        ToggleDevice("Outside Side Porch");
    }
    private void ToggleDevice(string Parms)
    //
    // Toggle the Specified Device ON/OFF
    //   Anything but OFF is considered ON
    //
    // Specify the Device Name, including Locations, as a Parm
    //
    {
        string theDevice;
        CAPI.CAPIControl ccOFF;
        CAPI.CAPIControl ccON;
        string theOffLabel;
        string devStatus;
        CAPI.CAPIControlResponse ccResponse;
        // Get the Device from the script parms
        theDevice = Parms.ToString();
        if (theDevice.Length < 1)
        {
            hs.WriteLog("ToggleDevice", "Specify Full Device Name as Script Parameter");
            return;
        }
        // Get the CAPI ControlUse value for OFF
        ccOFF = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._Off);
        if (ccOFF == null)
        {
            hs.WriteLog("ToggleDevice", "ControlUse not defined for device: " + theDevice);
            return;
        }
        theOffLabel = ccOFF.Label;
        // Get the Device CAPIStatus
        devStatus = hs.CAPIGetStatus(hs.GetDeviceRefByName(theDevice)).Status;
        // Determine Current Status, and toggle it
        if (theOffLabel.ToUpper().Trim() == devStatus.ToUpper().Trim())
        {
            // Currently OFF, turn it ON
            ccON = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._On);
            ccResponse = hs.CAPIControlHandler(ccON);
            if (ccResponse != CAPI.CAPIControlResponse.All_Success)
            {
                hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " + ccResponse.ToString());
            }
        }
        else
        {
            // NOT OFF, turn it OFF
            ccResponse = hs.CAPIControlHandler(ccOFF);
            if (ccResponse != CAPI.CAPIControlResponse.All_Success)
            {
                hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " + ccResponse.ToString());
            }
        }
    }
}
