Exemple #1
0
        public override string InitIO(string port)
        {
            string result = string.Empty;

            try
            {
                pluginConfig = new PluginConfig(HS);
                configPage   = new ConfigPage(HS, pluginConfig, base.Log);
                LogInfo("Starting Plugin");
#if DEBUG
                pluginConfig.DebugLogging = true;
#endif
                LogConfiguration();

                pluginConfig.ConfigChanged += PluginConfig_ConfigChanged;

                RegisterConfigPage();

                twilioService = new TwilioServiceFacade(HS, pluginConfig.DebugLogging);

                ScheduleRefreshTrigger();

                LogDebug("Plugin Started");
            }
            catch (Exception ex)
            {
                result = $"Failed to initialize PlugIn With {ex.GetFullMessage()}";
                LogError(result);
            }

            return(result);
        }
        public string PostBackProc(string data, [AllowNull] string user, int userRights)
        {
            NameValueCollection parts = HttpUtility.ParseQueryString(data);

            string form = parts["id"];

            if (form == "id_sendTestButton")
            {
                PluginConfig testConfig;
                using (testConfig = new PluginConfig(HS, offline: true))
                {
                    PopulatePluginConfig(testConfig, parts);

                    string toNumber = parts["testNumber"];

                    TwilioServiceFacade twilioService = new TwilioServiceFacade(HS, testConfig.DebugLogging);

                    SendMessageActionConfig messageConfig = new SendMessageActionConfig();
                    messageConfig.ToNumber = toNumber;
                    messageConfig.Message  = @"$time: this is a test message from the HomeSeer Twilio Plugin.";

                    try
                    {
                        twilioService.SendMessageToTwilio(testConfig, messageConfig);
                        this.divToUpdate.Add(SuccessDivId, @"Test message was sent successfully!");
                        this.divToUpdate.Add(ErrorDivId, string.Empty);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = e.Message;
                        if (errorMessage.Equals("Authenticate"))
                        {
                            errorMessage = "Invalid Auth Token";
                        }
                        this.divToUpdate.Add(SuccessDivId, string.Empty);
                        this.divToUpdate.Add(ErrorDivId, errorMessage);
                    }
                }
            }
            else if (form == NameToIdWithPrefix(SaveButtonName))
            {
                StringBuilder results = new StringBuilder();

                // Validate
                if (string.IsNullOrWhiteSpace(parts[AuthTokenId]))
                {
                    results.AppendLine("Auth Token is not Valid.<br>");
                }

                if (string.IsNullOrWhiteSpace(parts[AccountSIDId]))
                {
                    results.AppendLine("Account SID is not Valid.<br>");
                }

                if (string.IsNullOrWhiteSpace(parts[FromNumberId]))
                {
                    results.AppendLine("From Number is not Valid.<br>");
                }

                if (results.Length > 0)
                {
                    this.divToUpdate.Add(SuccessDivId, string.Empty);
                    this.divToUpdate.Add(ErrorDivId, results.ToString());
                }
                else
                {
                    this.divToUpdate.Add(SuccessDivId, "Settings have been saved successfully!");
                    this.divToUpdate.Add(ErrorDivId, string.Empty);

                    PopulatePluginConfig(this.pluginConfig, parts);

                    this.pluginConfig.FireConfigChanged();
                }
            }

            return(base.postBackProc(Name, data, user, userRights));
        }