Beispiel #1
0
        public string UpdateConfig(IHttpContext context)
        {
            var        errorMsg = "";
            var        json     = "";
            JsonWizard settings;

            cumulus.LogMessage("Updating settings from the First Time Wizard");

            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 <JsonWizard>();
            }
            catch (Exception ex)
            {
                var msg = "Error de-serializing Set-up Wizard Settings JSON: " + ex.Message;
                cumulus.LogMessage(msg);
                cumulus.LogDebugMessage("Wizard Data: " + json);
                context.Response.StatusCode = 500;
                return(msg);
            }

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

                // website settings
                try
                {
                    cumulus.FtpOptions.Enabled = settings.internet.ftp.enabled;
                    if (cumulus.FtpOptions.Enabled)
                    {
                        cumulus.FtpOptions.Directory  = settings.internet.ftp.directory ?? string.Empty;
                        cumulus.FtpOptions.Port       = settings.internet.ftp.ftpport;
                        cumulus.FtpOptions.Hostname   = settings.internet.ftp.hostname ?? string.Empty;
                        cumulus.FtpOptions.FtpMode    = (Cumulus.FtpProtocols)settings.internet.ftp.sslftp;
                        cumulus.FtpOptions.Password   = settings.internet.ftp.password ?? string.Empty;
                        cumulus.FtpOptions.Username   = settings.internet.ftp.username ?? string.Empty;
                        cumulus.FtpOptions.SshAuthen  = settings.internet.ftp.sshAuth ?? string.Empty;
                        cumulus.FtpOptions.SshPskFile = settings.internet.ftp.pskFile ?? string.Empty;
                    }

                    cumulus.FtpOptions.LocalCopyEnabled = settings.internet.copy.localcopy;
                    if (cumulus.FtpOptions.LocalCopyEnabled)
                    {
                        cumulus.FtpOptions.LocalCopyFolder = settings.internet.copy.localcopyfolder;
                    }

                    // Now flag all the standard files to FTP/Copy or not
                    // do not process last entry = wxnow.txt, it is not used by the standard site
                    for (var i = 0; i < cumulus.StdWebFiles.Length - 1; i++)
                    {
                        cumulus.StdWebFiles[i].Create = cumulus.FtpOptions.Enabled || cumulus.FtpOptions.LocalCopyEnabled;
                        cumulus.StdWebFiles[i].FTP    = cumulus.FtpOptions.Enabled;
                        cumulus.StdWebFiles[i].Copy   = cumulus.FtpOptions.LocalCopyEnabled;
                    }
                    // and graph data files
                    for (var i = 0; i < cumulus.GraphDataFiles.Length; i++)
                    {
                        cumulus.GraphDataFiles[i].Create = cumulus.FtpOptions.Enabled || cumulus.FtpOptions.LocalCopyEnabled;
                        cumulus.GraphDataFiles[i].FTP    = cumulus.FtpOptions.Enabled;
                        cumulus.GraphDataFiles[i].Copy   = cumulus.FtpOptions.LocalCopyEnabled;
                    }
                    // and EOD data files
                    for (var i = 0; i < cumulus.GraphDataEodFiles.Length; i++)
                    {
                        cumulus.GraphDataEodFiles[i].Create = cumulus.FtpOptions.Enabled || cumulus.FtpOptions.LocalCopyEnabled;
                        cumulus.GraphDataEodFiles[i].FTP    = cumulus.FtpOptions.Enabled;
                        cumulus.GraphDataEodFiles[i].Copy   = cumulus.FtpOptions.LocalCopyEnabled;
                    }
                    // and Realtime files

                    // realtime.txt is not used by the standard site
                    //cumulus.RealtimeFiles[0].Create = cumulus.FtpOptions.Enabled || cumulus.FtpOptions.LocalCopyEnabled;
                    //cumulus.RealtimeFiles[0].FTP = cumulus.FtpOptions.Enabled;
                    //cumulus.RealtimeFiles[0].Copy = cumulus.FtpOptions.LocalCopyEnabled;

                    // realtimegauges.txt IS used by the standard site
                    cumulus.RealtimeFiles[1].Create = cumulus.FtpOptions.Enabled || cumulus.FtpOptions.LocalCopyEnabled;
                    cumulus.RealtimeFiles[1].FTP    = cumulus.FtpOptions.Enabled;
                    cumulus.RealtimeFiles[1].Copy   = cumulus.FtpOptions.LocalCopyEnabled;

                    // and Moon image
                    cumulus.MoonImage.Enabled = cumulus.FtpOptions.Enabled || cumulus.FtpOptions.LocalCopyEnabled;
                    cumulus.MoonImage.Ftp     = cumulus.FtpOptions.Enabled;
                    cumulus.MoonImage.Copy    = cumulus.FtpOptions.LocalCopyEnabled;
                    if (cumulus.MoonImage.Enabled)
                    {
                        cumulus.MoonImage.CopyDest = cumulus.FtpOptions.LocalCopyFolder + "images" + cumulus.DirectorySeparator + "moon.png";
                    }

                    // and NOAA reports
                    cumulus.NOAAconf.Create   = cumulus.FtpOptions.Enabled || cumulus.FtpOptions.LocalCopyEnabled;
                    cumulus.NOAAconf.AutoFtp  = cumulus.FtpOptions.Enabled;
                    cumulus.NOAAconf.AutoCopy = cumulus.FtpOptions.LocalCopyEnabled;
                    if (cumulus.NOAAconf.AutoCopy)
                    {
                        cumulus.NOAAconf.CopyFolder = cumulus.FtpOptions.LocalCopyFolder + "Reports";
                    }
                    if (cumulus.NOAAconf.AutoFtp)
                    {
                        cumulus.NOAAconf.FtpFolder = cumulus.FtpOptions.Directory + (cumulus.FtpOptions.Directory.EndsWith("/") ? "" : "/") + "Reports";
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing internet settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // web settings
                try
                {
                    cumulus.RealtimeIntervalEnabled = settings.website.realtime.enabled;
                    if (cumulus.RealtimeIntervalEnabled)
                    {
                        cumulus.FtpOptions.RealtimeEnabled = settings.website.realtime.enablerealtimeftp;
                        cumulus.RealtimeInterval           = settings.website.realtime.realtimeinterval * 1000;
                        if (cumulus.RealtimeTimer.Interval != cumulus.RealtimeInterval)
                        {
                            cumulus.RealtimeTimer.Interval = cumulus.RealtimeInterval;
                        }
                    }
                    cumulus.RealtimeTimer.Enabled = cumulus.RealtimeIntervalEnabled;
                    if (!cumulus.RealtimeTimer.Enabled || !cumulus.FtpOptions.RealtimeEnabled)
                    {
                        cumulus.RealtimeTimer.Stop();
                        cumulus.RealtimeFTPDisconnect();
                    }

                    cumulus.WebIntervalEnabled = settings.website.interval.enabled;
                    if (cumulus.WebIntervalEnabled)
                    {
                        cumulus.FtpOptions.IntervalEnabled = settings.website.interval.enableintervalftp;
                        cumulus.UpdateInterval             = settings.website.interval.ftpinterval;
                        if (cumulus.WebTimer.Interval != cumulus.UpdateInterval * 60 * 1000)
                        {
                            cumulus.WebTimer.Interval = cumulus.UpdateInterval * 60 * 1000;
                        }
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing web settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Location
                try
                {
                    cumulus.Altitude       = settings.location.altitude;
                    cumulus.AltitudeInFeet = (settings.location.altitudeunit == "feet");
                    cumulus.LocationName   = settings.location.sitename ?? string.Empty;
                    cumulus.LocationDesc   = settings.location.description ?? string.Empty;

                    cumulus.Latitude = settings.location.latitude;

                    cumulus.LatTxt = degToString(cumulus.Latitude, true);

                    cumulus.Longitude = settings.location.longitude;

                    cumulus.LonTxt = degToString(cumulus.Longitude, false);
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Location settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Units
                try
                {
                    if (cumulus.Units.Wind != settings.units.wind)
                    {
                        cumulus.Units.Wind = settings.units.wind;
                        cumulus.ChangeWindUnits();
                        cumulus.WindDPlaces    = cumulus.StationOptions.RoundWindSpeed ? 0 : cumulus.WindDPlaceDefaults[cumulus.Units.Wind];
                        cumulus.WindAvgDPlaces = cumulus.WindDPlaces;
                    }
                    if (cumulus.Units.Press != settings.units.pressure)
                    {
                        cumulus.Units.Press = settings.units.pressure;
                        cumulus.ChangePressureUnits();
                        cumulus.PressDPlaces = cumulus.PressDPlaceDefaults[cumulus.Units.Press];
                    }
                    if (cumulus.Units.Temp != settings.units.temp)
                    {
                        cumulus.Units.Temp = settings.units.temp;
                        cumulus.ChangeTempUnits();
                    }
                    if (cumulus.Units.Rain != settings.units.rain)
                    {
                        cumulus.Units.Rain = settings.units.rain;
                        cumulus.ChangeRainUnits();
                        cumulus.RainDPlaces = cumulus.RainDPlaceDefaults[cumulus.Units.Rain];
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Units settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // data logging
                try
                {
                    cumulus.DataLogInterval = settings.logs.loginterval;
                    cumulus.RolloverHour    = settings.logs.logrollover.time == "9am" ? 9 : 0;
                    if (cumulus.RolloverHour == 9)
                    {
                        cumulus.Use10amInSummer = settings.logs.logrollover.summer10am;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Logging setting: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Station type
                try
                {
                    if (cumulus.StationType != settings.station.stationtype)
                    {
                        cumulus.LogMessage("Station type changed, restart required");
                        cumulus.LogConsoleMessage("*** Station type changed, restart required ***", ConsoleColor.Yellow);
                    }
                    cumulus.StationType  = settings.station.stationtype;
                    cumulus.StationModel = settings.station.stationmodel;
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Station Type setting: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Davis VP/VP2/Vue
                try
                {
                    if (settings.station.davisvp2 != null)
                    {
                        cumulus.DavisOptions.ConnectionType = settings.station.davisvp2.conntype;
                        if (settings.station.davisvp2.tcpsettings != null)
                        {
                            cumulus.DavisOptions.IPAddr = settings.station.davisvp2.tcpsettings.ipaddress ?? string.Empty;
                            cumulus.DavisOptions.PeriodicDisconnectInterval = settings.station.davisvp2.tcpsettings.disconperiod;
                        }
                        if (cumulus.DavisOptions.ConnectionType == 0)
                        {
                            cumulus.ComportName = settings.station.davisvp2.comportname;
                        }

                        // set defaults for Davis
                        cumulus.UVdecimals = 1;

                        if (settings.units.rain == 1)
                        {
                            cumulus.RainDPlaces = 2;
                        }
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Davis VP/VP2/Vue settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // WLL
                try
                {
                    if (settings.station.daviswll != null)
                    {
                        cumulus.DavisOptions.ConnectionType = 2;                         // Always TCP/IP for WLL
                        cumulus.WLLAutoUpdateIpAddress      = settings.station.daviswll.network.autoDiscover;
                        cumulus.DavisOptions.IPAddr         = settings.station.daviswll.network.ipaddress ?? string.Empty;

                        cumulus.WllApiKey    = settings.station.daviswll.api.apiKey;
                        cumulus.WllApiSecret = settings.station.daviswll.api.apiSecret;
                        cumulus.WllStationId = settings.station.daviswll.api.apiStationId;

                        cumulus.WllPrimaryRain    = settings.station.daviswll.primary.rain;
                        cumulus.WllPrimarySolar   = settings.station.daviswll.primary.solar;
                        cumulus.WllPrimaryTempHum = settings.station.daviswll.primary.temphum;
                        cumulus.WllPrimaryUV      = settings.station.daviswll.primary.uv;
                        cumulus.WllPrimaryWind    = settings.station.daviswll.primary.wind;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing WLL settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // GW1000 connection details
                try
                {
                    if (settings.station.gw1000 != null)
                    {
                        cumulus.Gw1000IpAddress           = settings.station.gw1000.ipaddress;
                        cumulus.Gw1000AutoUpdateIpAddress = settings.station.gw1000.autoDiscover;
                        cumulus.Gw1000MacAddress          = settings.station.gw1000.macaddress;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing GW1000 settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // weatherflow connection details
                try
                {
                    if (settings.station.weatherflow != null)
                    {
                        cumulus.WeatherFlowOptions.WFDeviceId = settings.station.weatherflow.deviceid;
                        cumulus.WeatherFlowOptions.WFTcpPort  = settings.station.weatherflow.tcpport;
                        cumulus.WeatherFlowOptions.WFToken    = settings.station.weatherflow.token;
                        cumulus.WeatherFlowOptions.WFDaysHist = settings.station.weatherflow.dayshistory;
                    }
                }
                catch (Exception ex)
                {
                    var msg = $"Error processing WeatherFlow settings: {ex.Message}";
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // FineOffset
                try
                {
                    if (settings.station.fineoffset != null)
                    {
                        cumulus.FineOffsetOptions.SyncReads       = settings.station.fineoffset.syncreads;
                        cumulus.FineOffsetOptions.ReadAvoidPeriod = settings.station.fineoffset.readavoid;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Fine Offset settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // EasyWeather
                try
                {
                    if (settings.station.easyw != null)
                    {
                        cumulus.EwOptions.Interval = settings.station.easyw.interval;
                        cumulus.EwOptions.Filename = settings.station.easyw.filename;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing EasyWeather settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Instromet
                try
                {
                    if (settings.station.imet != null)
                    {
                        cumulus.ComportName          = settings.station.imet.comportname ?? cumulus.ComportName;
                        cumulus.ImetOptions.BaudRate = settings.station.imet.baudrate;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing Instromet settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // WMR928
                try
                {
                    if (settings.station.wmr928 != null)
                    {
                        cumulus.ComportName = settings.station.wmr928.comportname ?? cumulus.ComportName;
                    }
                }
                catch (Exception ex)
                {
                    var msg = "Error processing WMR928 settings: " + ex.Message;
                    cumulus.LogMessage(msg);
                    errorMsg += msg + "\n\n";
                    context.Response.StatusCode = 500;
                }

                // Save the settings
                cumulus.WriteIniFile();
            }
            catch (Exception ex)
            {
                var msg = "Error processing Wizard settings: " + ex.Message;
                cumulus.LogMessage(msg);
                cumulus.LogDebugMessage("Station Data: " + json);
                errorMsg += msg;
                context.Response.StatusCode = 500;
            }

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