Exemple #1
0
        public SettingsPage(PageStorage <SettingsData> settingsData) : base("div")
        {
            bool useSafeLoading = false;

            if (settingsData.TryLoad() == false)
            {
                settingsData.UseSafeLoading();
                useSafeLoading = true;
            }
            if (PageStorage <HumiditySensorData> .Instance.TryLoad() == false)
            {
                PageStorage <HumiditySensorData> .Instance.UseSafeLoading();

                useSafeLoading = true;
            }
            this.AddScriptDependency("/lib/bootstrap3-typeahead.min.js");
            const int labelSize = 180;

            _settingsData = settingsData;

            #region Initialize Grid

            Container wrappingContainer = new Container(this);
            Grid      grid = new Grid(wrappingContainer);

            grid.AddStyling(StylingOption.MarginRight, 2);
            grid.AddStyling(StylingOption.MarginLeft, 2);
            grid.AddStyling(StylingOption.MarginTop, 4);
            grid.AddStyling(StylingOption.MarginBottom, 2);

            #endregion Initialize Grid

            if (useSafeLoading)
            {
                grid.AddRow().AppendCollum(new Heading(3, "Wegen Verbindungsproblemem wurden keine Daten geladen!")
                {
                    Style = { Color = Color.Red }
                });
            }

            #region AutoEnabled

            MultiInputGroup autoEnabledMultiInputGroup = new MultiInputGroup();
            autoEnabledMultiInputGroup.AppendLabel("Automatik", labelSize);
            autoEnabledMultiInputGroup
            .AppendCustomElement(
                new TwoStateButtonGroup("Aktiv", "Inaktiv", settingsData.StorageData.Enabled,
                                        !settingsData.StorageData.Enabled), false).FirstButtonStateChange += (sender, args) =>
                                                                                                             settingsData.StorageData.Enabled = args.NewButtonState;
            autoEnabledMultiInputGroup.AddStyling(StylingOption.MarginBottom, 2);
            autoEnabledMultiInputGroup.AppendCustomElement(new Button(StylingColor.Danger, true, Button.ButtonSize.Normal, false, "Alle Kanäle ausschalten", fontAwesomeIcon: "stop"), false).Click += (sender, args) =>
            {
                ServerConnection.DeleteAsync("settings", "stopall");
            };
            grid.AddRow().AppendCollum(autoEnabledMultiInputGroup, autoSize: true);

            #endregion AutoEnabled

            #region WeatherEnabled

            MultiInputGroup weatherEnabledMultiInputGroup = new MultiInputGroup();
            weatherEnabledMultiInputGroup.AppendLabel("Wetterdaten verwenden", labelSize);
            weatherEnabledMultiInputGroup
            .AppendCustomElement(
                new TwoStateButtonGroup("Aktiv", "Inaktiv", settingsData.StorageData.WeatherEnabled,
                                        !settingsData.StorageData.WeatherEnabled), false).FirstButtonStateChange += (sender, args) =>
                                                                                                                    settingsData.StorageData.WeatherEnabled = args.NewButtonState;
            weatherEnabledMultiInputGroup.AddStyling(StylingOption.MarginBottom, 2);
            grid.AddRow().AppendCollum(weatherEnabledMultiInputGroup, autoSize: true);

            #endregion WeatherEnabled

            #region Location

            Row locationRow = grid.AddRow();
            locationRow.AddStyling(StylingOption.MarginBottom, 2);
            MultiInputGroup weatherLocationMultiInputGroup = new MultiInputGroup();
            weatherLocationMultiInputGroup.AppendLabel("Standort", labelSize);

            StylableTextInput weatherLocationTextInput = weatherLocationMultiInputGroup.AppendTextInput("Bitte Eintragen...", false);
            weatherLocationTextInput.Value = settingsData.StorageData.LocationFriendlyName;

            #region Hidden TextInputs

            TextInput weatherLocationChangeTextInput = new TextInput {
                IsHidden = true, Value = settingsData.StorageData.Location
            };

            locationRow.AppendChild(weatherLocationChangeTextInput);
            TextInput weatherLocationNameChangeTextInput = new TextInput {
                IsHidden = true, Value = settingsData.StorageData.LocationFriendlyName
            };

            locationRow.AppendChild(weatherLocationNameChangeTextInput);

            #endregion Hidden TextInputs

            #region Autocomplete

            weatherLocationTextInput.ActivateAutocomplete("/settings/WeatherLocations.json", new Dictionary <string, TextInput>()
            {
                { "location", weatherLocationChangeTextInput },
                { "name", weatherLocationNameChangeTextInput }
            });

            #endregion Autocomplete

            locationRow.AppendCollum(weatherLocationMultiInputGroup, autoSize: true);

            #region Save Button

            Button saveLocationButton = new Button(StylingColor.Success, true, text: "Übernehmen");
            saveLocationButton.Click += (sender, args) =>
            {
                if (weatherLocationChangeTextInput.Value == "")
                {
                    weatherLocationTextInput.SetValidation(false, true);
                }
                else
                {
                    weatherLocationTextInput.SetValidation(false, false);
                    settingsData.StorageData.Location             = weatherLocationChangeTextInput.Value;
                    settingsData.StorageData.LocationFriendlyName = weatherLocationNameChangeTextInput.Value;
                    weatherLocationTextInput.Value = settingsData.StorageData.LocationFriendlyName;
                }
            };
            locationRow.AppendCollum(saveLocationButton, autoSize: true);

            #endregion Save Button

            #endregion Location

            #region Override

            _overrideInputGroup = new OverrideInputGroup(_settingsData.StorageData.OverrideValue, labelSizeInPx: labelSize);
            grid.AddRow().AppendCollum(_overrideInputGroup, autoSize: true);

            #endregion Override

            #region Rename HumiditySensors

            Row humidityRow = grid.AddRow();
            humidityRow.AppendChild(new Heading(3, "Feuchigkeitssensoren Umbenennen"));
            humidityRow.AddNewLine();

            #region Sync Server HumidityList with Storage

            foreach (string humiditySensor in PageStorage <HumiditySensorData> .Instance.StorageData.HumiditySensors)
            {
                if (settingsData.StorageData.HumiditySensors.ContainsKey(humiditySensor) == false)
                {
                    settingsData.StorageData.HumiditySensors.Add(humiditySensor, humiditySensor);
                }
            }

            List <string> removeList = new List <string>();
            foreach ((string realSensorName, string _) in settingsData.StorageData.HumiditySensors)
            {
                if (PageStorage <HumiditySensorData> .Instance.StorageData.HumiditySensors.Contains(realSensorName) == false)
                {
                    removeList.Add(realSensorName);
                }
            }

            foreach (string s in removeList)
            {
                settingsData.StorageData.HumiditySensors.Remove(s);
            }

            #endregion Sync Server HumidityList with Storage

            _humidityDropdown = new Dropdown(new Button(StylingColor.Secondary, true, widthInPx: 150));
            humidityRow.AppendCollum(_humidityDropdown, autoSize: true);

            foreach (string humiditySensor in PageStorage <HumiditySensorData> .Instance.StorageData.HumiditySensors)
            {
                StylableAnchor stylableAnchor = _humidityDropdown.AddEntry(humiditySensor);
                stylableAnchor.Click += (sender, args) => SelectHumiditySensor(humiditySensor);
            }

            _humiditySensorTextInputGroup = new TextInputGroup("Freundlicher Name", "Bitte Eingeben!");
            humidityRow.AppendCollum(_humiditySensorTextInputGroup, autoSize: true);

            Button button = new Button(StylingColor.Success, true, text: "Übernehmen");
            button.Click += (sender, args) =>
            {
                if (_humidityDropdown.Button.Text != "")
                {
                    _settingsData.StorageData.HumiditySensors[_humidityDropdown.Button.Text] = _humiditySensorTextInputGroup.TextInput.Value;
                }
            };

            humidityRow.AppendCollum(button, autoSize: true);
            if (PageStorage <HumiditySensorData> .Instance.StorageData.HumiditySensors.Count > 0)
            {
                SelectHumiditySensor(PageStorage <HumiditySensorData> .Instance.StorageData.HumiditySensors.First());
            }
            else
            {
                humidityRow.IsHidden = true;
            }
            humidityRow.AddStyling(StylingOption.MarginBottom, 2);

            #endregion Rename HumiditySensors

            #region Backend Server Path

            grid.AddRow().AppendCollum(new Heading(3, "Backend Server Schnittstelle einstellen")
            {
                ClassName = "text-center mb-4"
            });
            Row backendServerRow = grid.AddRow();
            Row backendServerConfigurationSingeApiRow = grid.AddRow();
            Row backendServerConfigurationMultiApiRow = grid.AddRow();
            backendServerRow.AddNewLine();

            BackendData backendInstanceStorageData = PageStorage <BackendData> .Instance.StorageData;

            MultiInputGroup backendConfigurationSourceSwitchingMultiInputGroup = backendServerRow.AppendCollum(new MultiInputGroup());
            backendConfigurationSourceSwitchingMultiInputGroup.AppendLabel("Quelle Auswählen", labelSize);
            TwoStateButtonGroup backendConfigurationSourceSwitchingTwoStateButton = backendConfigurationSourceSwitchingMultiInputGroup.AppendCustomElement(new TwoStateButtonGroup("Sammelkonfiguration", "einzele Konfiguration", !backendInstanceStorageData.SingleApiConfiguration, backendInstanceStorageData.SingleApiConfiguration), false);

            void OnBackendConfigurationSourceSwitchingTwoStateButtonOnFirstButtonStateChange(object sender, ButtonChangeEventHandlerArgs args)
            {
                if (args.NewButtonState == true)
                {
                    backendServerConfigurationSingeApiRow.Style.Display = "none";
                    backendServerConfigurationMultiApiRow.Style.Display = null;
                }
                else
                {
                    backendServerConfigurationSingeApiRow.Style.Display = null;
                    backendServerConfigurationMultiApiRow.Style.Display = "none";
                }

                backendInstanceStorageData.SingleApiConfiguration = !args.NewButtonState;
            }

            backendConfigurationSourceSwitchingTwoStateButton.FirstButtonStateChange += OnBackendConfigurationSourceSwitchingTwoStateButtonOnFirstButtonStateChange;
            OnBackendConfigurationSourceSwitchingTwoStateButtonOnFirstButtonStateChange(null, new ButtonChangeEventHandlerArgs(false, !backendInstanceStorageData.SingleApiConfiguration));
            backendConfigurationSourceSwitchingMultiInputGroup.AddStyling(StylingOption.MarginBottom, 5);

            #region backendServerConfigurationSingeApiRow

            foreach ((string name, BackendProperty backedProperties) in backendInstanceStorageData.BackendProperties)
            {
                backendServerConfigurationSingeApiRow.AddNewLine();
                backendServerConfigurationSingeApiRow.AppendCollum(CreateSingleBackendCollum(name, backedProperties), autoSize: true);
            }

            backendServerConfigurationSingeApiRow.AddNewLine();
            backendServerConfigurationSingeApiRow.AddNewLine();
            backendServerConfigurationSingeApiRow.AppendCollum(new Button(StylingColor.Light, false, Button.ButtonSize.Normal, false, "Standardkonfiguration eintragen")).Click += (sender, args) =>
            {
                foreach ((string name, BackendProperty _) in backendInstanceStorageData.BackendProperties)
                {
                    if (_backendPathTextInputDictionary[name].Value == "")
                    {
                        _backendPathTextInputDictionary[name].Value = $"http://{Dns.GetHostAddresses("WebPiServer.PiWeb")[0].ToString()}:5000/api/{name}";
                    }
                }
            };

            #endregion backendServerConfigurationSingeApiRow

            #region backendServerConfigurationMultiApiRow

            backendServerConfigurationMultiApiRow.AppendCollum(CreateMultiBackendCollum(backendInstanceStorageData, out StylableTextInput backendServerConfigurationMultiApiTextInput), autoSize: true);
            backendServerConfigurationMultiApiRow.AddNewLine();
            backendServerConfigurationMultiApiRow.AppendCollum(new Button(StylingColor.Light, false, Button.ButtonSize.Normal, false, "Standardkonfiguration eintragen")).Click += (sender, args) =>
            {
                if (backendServerConfigurationMultiApiTextInput.Value == "")
                {
                    backendServerConfigurationMultiApiTextInput.Value = $"http://{Dns.GetHostAddresses("WebPiServer.PiWeb")[0].ToString()}:5000/api";
                }
            };

            #endregion backendServerConfigurationMultiApiRow

            #endregion Backend Server Path
        }