From f904ba4c7c2ae3301d00cb76e6f1aae69ec5ee6b Mon Sep 17 00:00:00 2001 From: gbakeman Date: Fri, 23 Aug 2024 12:32:25 -0400 Subject: [PATCH 1/3] Move changestatus method Relocating the Event_ChangeStatus method to be closer to other NotifyIcon-related code. --- WinNUT_V2/WinNUT-Client/WinNUT.vb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/WinNUT_V2/WinNUT-Client/WinNUT.vb b/WinNUT_V2/WinNUT-Client/WinNUT.vb index b222db2..6c1adb3 100644 --- a/WinNUT_V2/WinNUT-Client/WinNUT.vb +++ b/WinNUT_V2/WinNUT-Client/WinNUT.vb @@ -618,18 +618,6 @@ Public Class WinNUT HasFocus = False End Sub - Public Shared Sub Event_ChangeStatus() Handles Me.On_Battery, Me.On_Line, - UPS_Device.Lost_Connect, UPS_Device.Connected, UPS_Device.Disconnected - - WinNUT.NotifyIcon.BalloonTipText = WinNUT.NotifyIcon.Text - If WinNUT.AllowToast And WinNUT.NotifyIcon.BalloonTipText <> "" Then - Dim Toastparts As String() = WinNUT.NotifyIcon.BalloonTipText.Split(New String() {Environment.NewLine}, StringSplitOptions.None) - WinNUT.ToastPopup.SendToast(Toastparts) - ElseIf WinNUT.NotifyIcon.Visible = True And WinNUT.NotifyIcon.BalloonTipText <> "" Then - WinNUT.NotifyIcon.ShowBalloonTip(10000) - End If - End Sub - Private Sub Update_UPS_Data() Handles UPS_Device.DataUpdated LogFile.LogTracing("Updating UPS data for Form.", LogLvl.LOG_DEBUG, Me) @@ -875,6 +863,18 @@ Public Class WinNUT End If End Sub + Public Shared Sub Event_ChangeStatus() Handles Me.On_Battery, Me.On_Line, + UPS_Device.Lost_Connect, UPS_Device.Connected, UPS_Device.Disconnected + + WinNUT.NotifyIcon.BalloonTipText = WinNUT.NotifyIcon.Text + If WinNUT.AllowToast And WinNUT.NotifyIcon.BalloonTipText <> "" Then + Dim Toastparts As String() = WinNUT.NotifyIcon.BalloonTipText.Split(New String() {Environment.NewLine}, StringSplitOptions.None) + WinNUT.ToastPopup.SendToast(Toastparts) + ElseIf WinNUT.NotifyIcon.Visible = True And WinNUT.NotifyIcon.BalloonTipText <> "" Then + WinNUT.NotifyIcon.ShowBalloonTip(10000) + End If + End Sub + Private Function GetIcon(IconIdx As Integer) As Icon Select Case IconIdx Case 1025 From ca65848a1d13ebeb6166dc508582da1557d29602 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Fri, 23 Aug 2024 15:37:06 -0400 Subject: [PATCH 2/3] Restructured toast/balloontip code - Sub made private, unshared and removed static references to instance members - Added logging for troubleshooting --- WinNUT_V2/WinNUT-Client/WinNUT.vb | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/WinNUT_V2/WinNUT-Client/WinNUT.vb b/WinNUT_V2/WinNUT-Client/WinNUT.vb index 6c1adb3..d999dac 100644 --- a/WinNUT_V2/WinNUT-Client/WinNUT.vb +++ b/WinNUT_V2/WinNUT-Client/WinNUT.vb @@ -863,15 +863,21 @@ Public Class WinNUT End If End Sub - Public Shared Sub Event_ChangeStatus() Handles Me.On_Battery, Me.On_Line, - UPS_Device.Lost_Connect, UPS_Device.Connected, UPS_Device.Disconnected - - WinNUT.NotifyIcon.BalloonTipText = WinNUT.NotifyIcon.Text - If WinNUT.AllowToast And WinNUT.NotifyIcon.BalloonTipText <> "" Then - Dim Toastparts As String() = WinNUT.NotifyIcon.BalloonTipText.Split(New String() {Environment.NewLine}, StringSplitOptions.None) - WinNUT.ToastPopup.SendToast(Toastparts) - ElseIf WinNUT.NotifyIcon.Visible = True And WinNUT.NotifyIcon.BalloonTipText <> "" Then - WinNUT.NotifyIcon.ShowBalloonTip(10000) + ''' + ''' Handle Toast (Windows 10+) and/or NotifyIcon pop-ups. + ''' + Private Sub ToastNotifyIcon() Handles Me.On_Battery, Me.On_Line, UPS_Device.Lost_Connect, + UPS_Device.Connected, UPS_Device.Disconnected + + LogFile.LogTracing("ToastNotifyIcon running.", LogLvl.LOG_DEBUG, Me) + NotifyIcon.BalloonTipText = NotifyIcon.Text + If AllowToast And NotifyIcon.BalloonTipText <> "" Then + Dim Toastparts As String() = NotifyIcon.BalloonTipText.Split(New String() {Environment.NewLine}, StringSplitOptions.None) + LogFile.LogTracing("Sending Toast popup with text: " & NotifyIcon.BalloonTipText, LogLvl.LOG_DEBUG, Me) + ToastPopup.SendToast(Toastparts) + ElseIf NotifyIcon.Visible = True And NotifyIcon.BalloonTipText <> "" Then + LogFile.LogTracing("Sending NotifyIcon ballowtip: " & NotifyIcon.BalloonTipText, LogLvl.LOG_DEBUG, Me) + NotifyIcon.ShowBalloonTip(10000) End If End Sub From eb6bfc6a27bb10789101a27b388fd8d59f696425 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Thu, 26 Sep 2024 16:13:43 -0400 Subject: [PATCH 3/3] Fix logged in state, logging in logic - Relocated the Login() call back into the UPS class to keep logic and exceptions out of the WinNUT form (fixes connected notifications not appearing on reconnect) - Properly unset logged in flag when disconnecting. From my limited testing, the NUT server does not care how you disconnect from it - you'll always be able to log back in. --- WinNUT_V2/WinNUT-Client/WinNUT.vb | 4 ---- WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb | 2 ++ WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb | 4 ++++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/WinNUT_V2/WinNUT-Client/WinNUT.vb b/WinNUT_V2/WinNUT-Client/WinNUT.vb index d999dac..11f3de6 100644 --- a/WinNUT_V2/WinNUT-Client/WinNUT.vb +++ b/WinNUT_V2/WinNUT-Client/WinNUT.vb @@ -402,10 +402,6 @@ Public Class WinNUT LogFile.LogTracing("Connection to Nut Host Established", LogLvl.LOG_NOTICE, Me, String.Format(StrLog.Item(AppResxStr.STR_LOG_CONNECTED), upsConf.Host, upsConf.Port)) - - If Not String.IsNullOrEmpty(upsConf.Login) Then - UPS_Device.Login() - End If End Sub Private Sub ConnectionError(sender As UPS_Device, ex As Exception) Handles UPS_Device.ConnectionError diff --git a/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb b/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb index 2a578fe..a8f616f 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb @@ -128,6 +128,8 @@ Public Class Nut_Socket Query_Data("LOGOUT") End If + _isLoggedIn = False + If WriterStream IsNot Nothing Then WriterStream.Dispose() End If diff --git a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb index 88595e7..4bbf0f9 100644 --- a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb +++ b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb @@ -144,6 +144,10 @@ Public Class UPS_Device Update_Data.Start() RaiseEvent Connected(Me) + If Not String.IsNullOrEmpty(Nut_Config.Login) Then + Login() + End If + Catch ex As NutException ' This is how we determine if we have a valid UPS name entered, among other errors. RaiseEvent EncounteredNUTException(Me, ex)