コード例 #1
0
        public string UpdateConfig(IHttpContext context)
        {
            var errorMsg = "";
            var json     = "";
            JsonThirdPartySettings settings;

            context.Response.StatusCode = 200;

            try
            {
                var data = new StreamReader(context.Request.InputStream).ReadToEnd();

                // Start at char 5 to skip the "json:" prefix
                json = WebUtility.UrlDecode(data.Substring(5));

                // de-serialize it to the settings structure
                settings = json.FromJson <JsonThirdPartySettings>();
            }
            catch (Exception ex)
            {
                var msg = "Error de-serializing 3rdParty Settings JSON: " + ex.Message;
                cumulus.LogMessage(msg);
                cumulus.LogDebugMessage("3rdParty Data: " + json);
                context.Response.StatusCode = 500;
                return(msg);
            }


            // process the settings
            try
            {
                cumulus.LogMessage("Updating third party settings");

                // wunderground
                try
                {
                    cumulus.Wund.Enabled = settings.wunderground.enabled;
                    if (cumulus.Wund.Enabled)
                    {
                        cumulus.Wund.SendIndoor         = settings.wunderground.includeindoor;
                        cumulus.Wund.SendSolar          = settings.wunderground.includesolar;
                        cumulus.Wund.SendUV             = settings.wunderground.includeuv;
                        cumulus.Wund.SendAirQuality     = settings.wunderground.includeaq;
                        cumulus.Wund.Interval           = settings.wunderground.interval;
                        cumulus.Wund.PW                 = settings.wunderground.password ?? string.Empty;
                        cumulus.Wund.RapidFireEnabled   = settings.wunderground.rapidfire;
                        cumulus.Wund.SendAverage        = settings.wunderground.sendavgwind;
                        cumulus.Wund.ID                 = settings.wunderground.stationid ?? string.Empty;
                        cumulus.Wund.CatchUp            = settings.wunderground.catchup;
                        cumulus.Wund.SynchronisedUpdate = (!cumulus.Wund.RapidFireEnabled) && (60 % cumulus.Wund.Interval == 0);

                        cumulus.WundTimer.Interval = cumulus.Wund.RapidFireEnabled ? 5000 : cumulus.Wund.Interval * 60 * 1000;
                        cumulus.WundTimer.Enabled  = cumulus.Wund.Enabled && !cumulus.Wund.SynchronisedUpdate && !string.IsNullOrWhiteSpace(cumulus.Wund.ID) && !string.IsNullOrWhiteSpace(cumulus.Wund.PW);
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing wunderground settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Windy
                try
                {
                    cumulus.Windy.Enabled = settings.windy.enabled;
                    if (cumulus.Windy.Enabled)
                    {
                        //cumulus.WindySendSolar = settings.windy.includesolar;
                        cumulus.Windy.SendUV     = settings.windy.includeuv;
                        cumulus.Windy.Interval   = settings.windy.interval;
                        cumulus.Windy.ApiKey     = settings.windy.apikey;
                        cumulus.Windy.StationIdx = settings.windy.stationidx;
                        cumulus.Windy.CatchUp    = settings.windy.catchup;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Windy settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Awekas
                try
                {
                    cumulus.AWEKAS.Enabled = settings.awekas.enabled;
                    if (cumulus.AWEKAS.Enabled)
                    {
                        cumulus.AWEKAS.Interval           = settings.awekas.interval;
                        cumulus.AWEKAS.Lang               = settings.awekas.lang;
                        cumulus.AWEKAS.PW                 = settings.awekas.password ?? string.Empty;
                        cumulus.AWEKAS.ID                 = settings.awekas.user ?? string.Empty;
                        cumulus.AWEKAS.SendSolar          = settings.awekas.includesolar;
                        cumulus.AWEKAS.SendUV             = settings.awekas.includeuv;
                        cumulus.AWEKAS.SendSoilTemp       = settings.awekas.includesoiltemp;
                        cumulus.AWEKAS.SendSoilMoisture   = settings.awekas.includesoilmoisture;
                        cumulus.AWEKAS.SendLeafWetness    = settings.awekas.includeleafwetness;
                        cumulus.AWEKAS.SendIndoor         = settings.awekas.includeindoor;
                        cumulus.AWEKAS.SendAirQuality     = settings.awekas.includeaq;
                        cumulus.AWEKAS.SynchronisedUpdate = (cumulus.AWEKAS.Interval % 60 == 0);

                        cumulus.AwekasTimer.Interval = cumulus.AWEKAS.Interval * 1000;
                        cumulus.AwekasTimer.Enabled  = cumulus.AWEKAS.Enabled && !cumulus.AWEKAS.SynchronisedUpdate && !string.IsNullOrWhiteSpace(cumulus.AWEKAS.ID) && !string.IsNullOrWhiteSpace(cumulus.AWEKAS.PW);
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing AWEKAS settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // WeatherCloud
                try
                {
                    cumulus.WCloud.Enabled = settings.weathercloud.enabled;
                    if (cumulus.WCloud.Enabled)
                    {
                        cumulus.WCloud.ID                 = settings.weathercloud.wid ?? string.Empty;
                        cumulus.WCloud.PW                 = settings.weathercloud.key ?? string.Empty;
                        cumulus.WCloud.Interval           = settings.weathercloud.interval;
                        cumulus.WCloud.SendSolar          = settings.weathercloud.includesolar;
                        cumulus.WCloud.SendUV             = settings.weathercloud.includeuv;
                        cumulus.WCloud.SendAirQuality     = settings.weathercloud.includeaqi;
                        cumulus.WCloud.SendSoilMoisture   = settings.weathercloud.includesoilmoist;
                        cumulus.WCloud.SoilMoistureSensor = settings.weathercloud.moistsensor;
                        cumulus.WCloud.SendLeafWetness    = settings.weathercloud.includeleafwet;
                        cumulus.WCloud.LeafWetnessSensor  = settings.weathercloud.leafwetsensor;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing WeatherCloud settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // PWS weather
                try
                {
                    cumulus.PWS.Enabled = settings.pwsweather.enabled;
                    if (cumulus.PWS.Enabled)
                    {
                        cumulus.PWS.Interval  = settings.pwsweather.interval;
                        cumulus.PWS.SendSolar = settings.pwsweather.includesolar;
                        cumulus.PWS.SendUV    = settings.pwsweather.includeuv;
                        cumulus.PWS.PW        = settings.pwsweather.password ?? string.Empty;
                        cumulus.PWS.ID        = settings.pwsweather.stationid ?? string.Empty;
                        cumulus.PWS.CatchUp   = settings.pwsweather.catchup;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing PWS weather settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // WOW
                try
                {
                    cumulus.WOW.Enabled = settings.wow.enabled;
                    if (cumulus.WOW.Enabled)
                    {
                        cumulus.WOW.SendSolar      = settings.wow.includesolar;
                        cumulus.WOW.SendUV         = settings.wow.includeuv;
                        cumulus.WOW.SendSoilTemp   = settings.wow.includesoiltemp;
                        cumulus.WOW.SoilTempSensor = settings.wow.soiltempsensor;
                        cumulus.WOW.Interval       = settings.wow.interval;
                        cumulus.WOW.PW             = settings.wow.password ?? string.Empty;
                        cumulus.WOW.ID             = settings.wow.stationid ?? string.Empty;
                        cumulus.WOW.CatchUp        = settings.wow.catchup;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing WOW settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // CWOP
                try
                {
                    cumulus.APRS.Enabled = settings.cwop.enabled;
                    if (cumulus.APRS.Enabled)
                    {
                        cumulus.APRS.ID        = settings.cwop.id ?? string.Empty;;
                        cumulus.APRS.Interval  = settings.cwop.interval;
                        cumulus.APRS.SendSolar = settings.cwop.includesolar;
                        cumulus.APRS.PW        = settings.cwop.password ?? string.Empty;;
                        cumulus.APRS.Port      = settings.cwop.port;
                        cumulus.APRS.Server    = settings.cwop.server ?? string.Empty;;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing CWOP settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // OpenWeatherMap
                try
                {
                    cumulus.OpenWeatherMap.Enabled = settings.openweathermap.enabled;
                    if (cumulus.OpenWeatherMap.Enabled)
                    {
                        cumulus.OpenWeatherMap.CatchUp  = settings.openweathermap.catchup;
                        cumulus.OpenWeatherMap.PW       = settings.openweathermap.apikey;
                        cumulus.OpenWeatherMap.ID       = settings.openweathermap.stationid;
                        cumulus.OpenWeatherMap.Interval = settings.openweathermap.interval;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing OpenWeatherMap settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Wind Guru
                try
                {
                    cumulus.WindGuru.Enabled = settings.windguru.enabled;
                    if (cumulus.WindGuru.Enabled)
                    {
                        cumulus.WindGuru.ID       = settings.windguru.uid;
                        cumulus.WindGuru.PW       = settings.windguru.password;
                        cumulus.WindGuru.SendRain = settings.windguru.includerain;
                        cumulus.WindGuru.Interval = settings.windguru.interval;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing WindGuru settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Custom HTTP
                try
                {
                    // custom seconds
                    cumulus.CustomHttpSecondsEnabled       = settings.customhttp.customseconds.enabled;
                    cumulus.CustomHttpSecondsTimer.Enabled = cumulus.CustomHttpSecondsEnabled;
                    if (cumulus.CustomHttpSecondsEnabled)
                    {
                        cumulus.CustomHttpSecondsStrings[0] = settings.customhttp.customseconds.url[0] ?? string.Empty;
                        for (var i = 1; i < 10; i++)
                        {
                            if (i < settings.customhttp.customseconds.url.Length)
                            {
                                cumulus.CustomHttpSecondsStrings[i] = settings.customhttp.customseconds.url[i] ?? null;
                            }
                            else
                            {
                                cumulus.CustomHttpSecondsStrings[i] = null;
                            }
                        }

                        cumulus.CustomHttpSecondsInterval       = settings.customhttp.customseconds.interval;
                        cumulus.CustomHttpSecondsTimer.Interval = cumulus.CustomHttpSecondsInterval * 1000;
                    }
                    // custom minutes
                    cumulus.CustomHttpMinutesEnabled = settings.customhttp.customminutes.enabled;
                    if (cumulus.CustomHttpMinutesEnabled)
                    {
                        cumulus.CustomHttpMinutesStrings[0] = settings.customhttp.customminutes.url[0] ?? string.Empty;
                        for (var i = 1; i < 10; i++)
                        {
                            if (i < settings.customhttp.customseconds.url.Length)
                            {
                                cumulus.CustomHttpMinutesStrings[i] = settings.customhttp.customminutes.url[i] ?? null;
                            }
                            else
                            {
                                cumulus.CustomHttpMinutesStrings[i] = null;
                            }
                        }

                        cumulus.CustomHttpMinutesIntervalIndex = settings.customhttp.customminutes.intervalindex;
                        if (cumulus.CustomHttpMinutesIntervalIndex >= 0 && cumulus.CustomHttpMinutesIntervalIndex < cumulus.FactorsOf60.Length)
                        {
                            cumulus.CustomHttpMinutesInterval = cumulus.FactorsOf60[cumulus.CustomHttpMinutesIntervalIndex];
                        }
                        else
                        {
                            cumulus.CustomHttpMinutesInterval = 10;
                        }
                    }
                    // custom roll-over
                    cumulus.CustomHttpRolloverEnabled = settings.customhttp.customrollover.enabled;
                    if (cumulus.CustomHttpRolloverEnabled)
                    {
                        cumulus.CustomHttpRolloverStrings[0] = settings.customhttp.customrollover.url[0] ?? string.Empty;
                        for (var i = 1; i < 10; i++)
                        {
                            if (i < settings.customhttp.customrollover.url.Length)
                            {
                                cumulus.CustomHttpMinutesStrings[i] = settings.customhttp.customrollover.url[i] ?? null;
                            }
                            else
                            {
                                cumulus.CustomHttpMinutesStrings[i] = null;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Custom settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Save the settings
                cumulus.WriteIniFile();

                // Do OpenWeatherMap set-up
                cumulus.EnableOpenWeatherMap();
            }
            catch (Exception ex)
            {
                var msg = "Error processing Third Party settings: " + ex.Message;
                cumulus.LogMessage(msg);
                cumulus.LogDebugMessage("Third Party data: " + json);
                errorMsg += msg;
                context.Response.StatusCode = 500;
            }

            return(context.Response.StatusCode == 200 ? "success" : errorMsg);
        }