コード例 #1
0
ファイル: HSPI.cs プロジェクト: spudwebb/Sample-Plugin-CS
        protected override void Initialize()
        {
            //Load the state of Settings saved to INI if there are any.
            LoadSettingsFromIni();
            if (LogDebug)
            {
                Console.WriteLine("Registering feature pages");
            }
            //Initialize feature pages
            HomeSeerSystem.RegisterFeaturePage(Id, "sample-guided-process.html", "Sample Guided Process");
            HomeSeerSystem.RegisterFeaturePage(Id, "sample-blank.html", "Sample Blank Page");
            HomeSeerSystem.RegisterFeaturePage(Id, "sample-trigger-feature.html", "Trigger Feature Page");
            HomeSeerSystem.RegisterDeviceIncPage(Id, "add-sample-device.html", "Add Sample Device");

            // If a speaker client is needed that handles sending speech to an audio device, initialize that here.
            // If you are supporting multiple speak devices such as multiple speakers, you would make this call
            // in your reoutine that initializes each speaker device. Create a new instance of the speaker client
            // for each speaker. We simply initalize one here as a sample implementation
            _speakerClient = new SpeakerClient(Name);
            // if the HS system has the setting "No password required for local subnet" enabled, the user/pass passed to Connect are ignored
            // if the connection is from the local subnet, else the user/pass passed here are must exist as a user in the system
            // You will need to allow the user to supply a user/pass in your plugin settings
            // This functions connects your speaker client to the system. Your client will then appear as a speaker client in the system
            // and can be selected as a target for speech and audio in event actions.
            // When the system speaks to your client, your SpeakText function is called in SpeakerClient class
            _speakerClient.Connect("default", "default");

            Console.WriteLine("Initialized");
            Status = PluginStatus.Ok();
        }
コード例 #2
0
ファイル: PlugIn.cs プロジェクト: dk307/HSPI_Tasmota
        protected override void Initialize()
        {
            try
            {
                pluginConfig = new PluginConfig(HomeSeerSystem);
                UpdateDebugLevel();

                logger.Info("Starting Plugin");

                HomeSeerSystem.RegisterEventCB(Constants.HSEvent.CONFIG_CHANGE, PlugInData.PlugInId);

                // Device Add Page
                HomeSeerSystem.RegisterDeviceIncPage(PlugInData.PlugInId, "adddevice.html", "Add Tasmota Device");

                // Feature pages
                HomeSeerSystem.RegisterFeaturePage(PlugInData.PlugInId, "configuration.html", "Configuration");
                HomeSeerSystem.RegisterFeaturePage(PlugInData.PlugInId, "devicelist.html", "Devices");
                HomeSeerSystem.RegisterFeaturePage(PlugInData.PlugInId, "mqttconfiguration.html", "MQTT Server Configuration");

                RestartProcessing();

                logger.Info("Plugin Started");
            }
            catch (Exception ex)
            {
                string result = Invariant($"Failed to initialize PlugIn with {ex.GetFullMessage()}");
                logger.Error(result);
                throw;
            }
        }
コード例 #3
0
ファイル: HSPI.cs プロジェクト: HomeSeer/Sample-Plugin-CS
        private string GetExtraData(int deviceRef, string key)
        {
            PlugExtraData extraData = (PlugExtraData)HomeSeerSystem.GetPropertyByRef(deviceRef, EProperty.PlugExtraData);

            if (extraData != null && extraData.ContainsNamed(key))
            {
                return(extraData[key]);
            }
            return("");
        }
コード例 #4
0
ファイル: HSPI.cs プロジェクト: HomeSeer/Sample-Plugin-CS
        private void SetExtraData(int deviceRef, string key, string value)
        {
            PlugExtraData extraData = (PlugExtraData)HomeSeerSystem.GetPropertyByRef(deviceRef, EProperty.PlugExtraData);

            if (extraData == null)
            {
                extraData = new PlugExtraData();
            }
            extraData[key] = value;
            HomeSeerSystem.UpdatePropertyByRef(deviceRef, EProperty.PlugExtraData, extraData);
        }
コード例 #5
0
ファイル: HSPI.cs プロジェクト: spudwebb/Sample-Plugin-CS
 /// <inheritdoc />
 public void WriteLog(ELogType logType, string message)
 {
     HomeSeerSystem.WriteLog(logType, message, Name);
 }
コード例 #6
0
ファイル: HSPI.cs プロジェクト: spudwebb/Sample-Plugin-CS
        /// <inheritdoc />
        /// <remarks>
        /// Process any HTTP POST requests targeting pages registered to your plugin.
        /// <para>
        /// This is a very flexible process that does not have a predefined structure. The form <see cref="data"/> sends
        ///  from a page is entirely up to you and what works for you.  JSON and Base64 strings are encouraged because
        ///  of how readily available resources are to translate to/from these types. In Javascript, see JSON.stringify();
        ///  and window.btoa();
        /// </para>
        /// </remarks>
        public override string PostBackProc(string page, string data, string user, int userRights)
        {
            if (LogDebug)
            {
                Console.WriteLine("PostBack");
            }

            var response = "";

            switch (page)
            {
            case "sample-trigger-feature.html":

                //Handle the Trigger Feature page
                try {
                    var triggerOptions = JsonConvert.DeserializeObject <List <bool> >(data);

                    //Get all triggers configured on the HomeSeer system that are of the SampleTriggerType
                    var configuredTriggers = HomeSeerSystem.GetTriggersByType(Name, SampleTriggerType.TriggerNumber);
                    if (configuredTriggers.Length == 0)
                    {
                        return("No triggers configured to fire.");
                    }

                    //Handle each trigger that matches
                    foreach (var configuredTrigger in configuredTriggers)
                    {
                        var trig = new SampleTriggerType(configuredTrigger, this, LogDebug);
                        if (trig.ShouldTriggerFire(triggerOptions.ToArray()))
                        {
                            HomeSeerSystem.TriggerFire(Name, configuredTrigger);
                        }
                    }
                }
                catch (JsonSerializationException exception) {
                    if (LogDebug)
                    {
                        Console.WriteLine(exception);
                    }
                    response = $"Error while deserializing data: {exception.Message}";
                }

                break;

            case "sample-guided-process.html":

                //Handle the Guided Process page
                try {
                    var postData = JsonConvert.DeserializeObject <SampleGuidedProcessData>(data);

                    if (LogDebug)
                    {
                        Console.WriteLine("Post back from sample-guided-process page");
                    }
                    response = postData.GetResponse();
                }
                catch (JsonSerializationException exception) {
                    if (LogDebug)
                    {
                        Console.WriteLine(exception.Message);
                    }
                    response = "error";
                }

                break;

            case "add-sample-device.html":

                try {
                    var postData = JsonConvert.DeserializeObject <DeviceAddPostData>(data);
                    if (LogDebug)
                    {
                        Console.WriteLine("Post back from add-sample-device page");
                    }

                    if (postData.Action == "verify")
                    {
                        response = JsonConvert.SerializeObject(postData.Device);
                    }
                    else
                    {
                        var deviceData = postData.Device;
                        var device     = deviceData.BuildDevice(Id);
                        var devRef     = HomeSeerSystem.CreateDevice(device);
                        deviceData.Ref = devRef;
                        response       = JsonConvert.SerializeObject(deviceData);
                    }
                }
                catch (Exception exception) {
                    if (LogDebug)
                    {
                        Console.WriteLine(exception.Message);
                    }
                    response = "error";
                }
                break;

            default:
                response = "error";
                break;
            }
            return(response);
        }