private void BtnSave(object sender, RoutedEventArgs e) { NUTConfig.SetConfig("IP Address", txtIPAddress.Text); NUTConfig.SetConfig("Port", txtPort.Text); NUTConfig.SetConfig("Poll Interval", txtPollFrequency.Text); // We'll also update the public variable here NUTInitialization.NUTConnectionSettings = Tuple.Create(txtIPAddress.Text, Convert.ToUInt16(txtPort.Text), Convert.ToUInt32(txtPollFrequency.Text) * 1000); NUTInitialization.NeedConfig = false; var poller = new NUTPoller(); poller.ResumeUPSPolling(); return; }
private void TestConnection(object sender, RoutedEventArgs e) { if (ValidateSettings()) { TestingRing.IsActive = true; bool PausePoll = false; if (NUTInitialization.isPolling) { NUTInitialization.debugLog.Debug("[SETTINGS] Polling is active. Temporarily paused for the duration of this test."); PausePoll = true; NUTInitialization.isPolling = false; } //Temporarily changing the button and disabling it for the duration of this test btnConnect.Content = "Testing..."; btnConnect.IsEnabled = false; Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Wait, 10); NUTInitialization.debugLog.Info("[SETTINGS] Testing connection settings"); //bool ValidationTest = NUTPoller.ValidateNUTServer(txtIPAddress.Text, Convert.ToUInt16(txtPort.Text)); bool ValidationTest = false; try { string testIP = txtIPAddress.Text; ushort testPort = Convert.ToUInt16(txtPort.Text); IAsyncAction ValidationTestTask = Windows.System.Threading.ThreadPool.RunAsync(async(workItem) => { NUTInitialization.debugLog.Trace("[SETTINGS] Executing telnet client task"); try { ValidationTest = await NUTPoller.ValidateNUTServer(testIP, testPort).ConfigureAwait(true); } catch (System.AggregateException eAggregate) { ValidationTest = false; } }); ValidationTestTask.Completed = new AsyncActionCompletedHandler(async(IAsyncAction asyncInfo, AsyncStatus asyncStatus) => { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, new DispatchedHandler(() => { if (ValidationTest) { ContentDialog TestDialog = new ContentDialog { //Title = "Server validation successful", Content = "Connection to the NUT server was successful", CloseButtonText = "OK" }; TestDialog.ShowAsync(); } else { ContentDialog TestDialog = new ContentDialog { //Title = "Server validation failed", Content = "Connection to the NUT server has failed. Please confirm the settings and try again.", CloseButtonText = "OK" }; TestDialog.ShowAsync(); } if (PausePoll) { NUTInitialization.debugLog.Debug("[SETTINGS] Polling has resumed."); NUTInitialization.isPolling = true; } btnConnect.Content = "Test Connection"; btnConnect.IsEnabled = true; TestingRing.IsActive = false; Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 10); })); }); } catch (Exception eTest) { NUTInitialization.debugLog.Fatal("[SETTINGS] An error occurred while trying to test the connection:\n" + eTest + "\n" + e); ValidationTest = false; } } }