diff --git a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb index c6dd0b3..d603f7a 100644 --- a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb +++ b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb @@ -11,10 +11,34 @@ Imports System.Globalization Imports System.Windows.Forms Public Class UPS_Device +#Region "Properties" + + Public ReadOnly Property IsConnected() As Boolean + Get + Return (Nut_Socket.IsConnected) ' And Me.Socket_Status + End Get + End Property + + Public ReadOnly Property IsAuthenticated() As Boolean + Get + Return Nut_Socket.Auth_Success + End Get + End Property + + Public Property PollingInterval As Integer + Get + Return Update_Data.Interval + End Get + Set(value As Integer) + Update_Data.Interval = value + End Set + End Property + +#End Region Private Const CosPhi As Double = 0.6 ' How many milliseconds to wait before the Reconnect routine tries again. #If DEBUG Then - Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 3000 + Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 5000 #Else Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 30000 #End If @@ -96,29 +120,18 @@ Public Class UPS_Device Public Event Shutdown_Condition() Public Event Stop_Shutdown() - Private Polling_Interval As Integer Private WithEvents Update_Data As New Timer - Public ReadOnly Property IsConnected() As Boolean - Get - Return (Me.Nut_Socket.IsConnected) ' And Me.Socket_Status - End Get - End Property - Public ReadOnly Property IsAuthenticated() As Boolean - Get - Return Me.Nut_Socket.Auth_Success - End Get - End Property Public Sub New(ByRef Nut_Config As Nut_Parameter, ByRef LogFile As Logger, pollInterval As Integer) Me.LogFile = LogFile Me.Nut_Config = Nut_Config - ' Polling_Interval = pollInterval - Update_Data.Interval = pollInterval + PollingInterval = pollInterval ciClone = CType(CultureInfo.InvariantCulture.Clone(), CultureInfo) ciClone.NumberFormat.NumberDecimalSeparator = "." Nut_Socket = New Nut_Socket(Me.Nut_Config, LogFile) + With Reconnect_Nut .Interval = DEFAULT_RECONNECT_WAIT_MS .Enabled = False @@ -135,24 +148,19 @@ Public Class UPS_Device ' Dim UPSName = Me.Nut_Config.UPSName LogFile.LogTracing("Beginning connection: " & Nut_Config.ToString(), LogLvl.LOG_DEBUG, Me) - If Me.Nut_Socket.Connect() And Me.Nut_Socket.IsConnected Then + If Nut_Socket.Connect() And Nut_Socket.IsConnected Then LogFile.LogTracing("TCP Socket Created", LogLvl.LOG_NOTICE, Me) - ' Me.Socket_Status = True + If Nut_Socket.IsKnownUPS(Nut_Config.UPSName) Then - Me.UPS_Datas = GetUPSProductInfo() + UPS_Datas = GetUPSProductInfo() Init_Constant(Nut_Socket) Update_Data.Start() RaiseEvent Connected(Me) Else - LogFile.LogTracing("Given UPS Name is unknown", LogLvl.LOG_NOTICE, Me) + LogFile.LogTracing("Given UPS Name is unknown", LogLvl.LOG_ERROR, Me) RaiseEvent Unknown_UPS() + Disconnect(True, False, True) End If - ' WatchDog.Start() - 'Else - ' If Not Reconnect_Nut.Enabled Then - ' RaiseEvent Lost_Connect() - ' Me.Socket_Status = False - ' End If End If End Sub @@ -188,17 +196,8 @@ Public Class UPS_Device #Region "Socket Interaction" Private Sub SocketDisconnected() Handles Nut_Socket.SocketDisconnected - ' WatchDog.Stop() LogFile.LogTracing("NutSocket raised Disconnected event.", LogLvl.LOG_DEBUG, Me) - 'If Not Me.Socket_Status Then - ' RaiseEvent Lost_Connect() - 'End If - ' Me.Socket_Status = False - 'If Me.Nut_Config.AutoReconnect Then - ' LogFile.LogTracing("Reconnection Process Started", LogLvl.LOG_NOTICE, Me) - ' Reconnect_Nut.Enabled = True - ' Reconnect_Nut.Start() - 'End If + RaiseEvent Disconnected() End Sub @@ -210,7 +209,7 @@ Public Class UPS_Device ''' ''' Private Sub Event_WatchDog(sender As Object, e As EventArgs) - If Me.IsConnected Then + If IsConnected Then Dim Nut_Query = Nut_Socket.Query_Data("") If Nut_Query.Response = NUTResponse.NORESPONSE Then LogFile.LogTracing("WatchDog Socket report a Broken State", LogLvl.LOG_WARNING, Me) @@ -234,15 +233,15 @@ Public Class UPS_Device End Sub Private Sub Reconnect_Socket(sender As Object, e As EventArgs) Handles Reconnect_Nut.Tick - Me.Retry += 1 - If Me.Retry <= Me.MaxRetry Then + Retry += 1 + If Retry <= MaxRetry Then RaiseEvent New_Retry() - LogFile.LogTracing(String.Format("Try Reconnect {0} / {1}", Me.Retry, Me.MaxRetry), LogLvl.LOG_NOTICE, Me, String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_NEW_RETRY), Me.Retry, Me.MaxRetry)) - Me.Connect_UPS() - If Me.IsConnected Then + LogFile.LogTracing(String.Format("Try Reconnect {0} / {1}", Retry, MaxRetry), LogLvl.LOG_NOTICE, Me, String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_NEW_RETRY), Retry, MaxRetry)) + Connect_UPS() + If IsConnected Then LogFile.LogTracing("Nut Host Reconnected", LogLvl.LOG_DEBUG, Me) Reconnect_Nut.Stop() - Me.Retry = 0 + Retry = 0 RaiseEvent ReConnected(Me) End If Else @@ -271,44 +270,44 @@ Public Class UPS_Device Private Function GetUPSProductInfo() As UPS_Datas Dim UDatas As New UPS_Datas - Dim UPSName = Me.Nut_Config.UPSName - UDatas.Mfr = Trim(Me.GetUPSVar("ups.mfr", UPSName, "Unknown")) - UDatas.Model = Trim(Me.GetUPSVar("ups.model", UPSName, "Unknown")) - UDatas.Serial = Trim(Me.GetUPSVar("ups.serial", UPSName, "Unknown")) - UDatas.Firmware = Trim(Me.GetUPSVar("ups.firmware", UPSName, "Unknown")) + Dim UPSName = Nut_Config.UPSName + UDatas.Mfr = Trim(GetUPSVar("ups.mfr", UPSName, "Unknown")) + UDatas.Model = Trim(GetUPSVar("ups.model", UPSName, "Unknown")) + UDatas.Serial = Trim(GetUPSVar("ups.serial", UPSName, "Unknown")) + UDatas.Firmware = Trim(GetUPSVar("ups.firmware", UPSName, "Unknown")) Return UDatas End Function Private Sub Init_Constant(ByRef Nut_Socket As Nut_Socket) - Dim UPSName = Me.Nut_Config.UPSName - Me.UPS_Datas.UPS_Value.Batt_Capacity = Double.Parse(Me.GetUPSVar("battery.capacity", UPSName, 7), ciClone) - Me.Freq_Fallback = Double.Parse(Me.GetUPSVar("output.frequency.nominal", UPSName, (50 + CInt(WinNUT_Params.Arr_Reg_Key.Item("FrequencySupply")) * 10)), Me.ciClone) + Dim UPSName = Nut_Config.UPSName + UPS_Datas.UPS_Value.Batt_Capacity = Double.Parse(GetUPSVar("battery.capacity", UPSName, 7), ciClone) + Freq_Fallback = Double.Parse(GetUPSVar("output.frequency.nominal", UPSName, (50 + CInt(WinNUT_Params.Arr_Reg_Key.Item("FrequencySupply")) * 10)), ciClone) End Sub Public Sub Retrieve_UPS_Datas() Handles Update_Data.Tick ' As UPS_Datas - Dim UPSName = Me.Nut_Config.UPSName + Dim UPSName = Nut_Config.UPSName LogFile.LogTracing("Enter Retrieve_UPS_Datas", LogLvl.LOG_DEBUG, Me) Try Dim UPS_rt_Status As String Dim InputA As Double ' LogFile.LogTracing("Enter Retrieve_UPS_Data", LogLvl.LOG_DEBUG, Me) - If Me.IsConnected Then - With Me.UPS_Datas + If IsConnected Then + With UPS_Datas Select Case "Unknown" Case .Mfr, .Model, .Serial, .Firmware - Me.UPS_Datas = GetUPSProductInfo() + UPS_Datas = GetUPSProductInfo() End Select End With - With Me.UPS_Datas.UPS_Value + With UPS_Datas.UPS_Value .Batt_Charge = Double.Parse(GetUPSVar("battery.charge", UPSName, 255), ciClone) .Batt_Voltage = Double.Parse(GetUPSVar("battery.voltage", UPSName, 12), ciClone) .Batt_Runtime = Double.Parse(GetUPSVar("battery.runtime", UPSName, 86400), ciClone) - .Power_Frequency = Double.Parse(GetUPSVar("input.frequency", UPSName, Double.Parse(Me.GetUPSVar("output.frequency", UPSName, Freq_Fallback), ciClone)), ciClone) + .Power_Frequency = Double.Parse(GetUPSVar("input.frequency", UPSName, Double.Parse(GetUPSVar("output.frequency", UPSName, Freq_Fallback), ciClone)), ciClone) .Input_Voltage = Double.Parse(GetUPSVar("input.voltage", UPSName, 220), ciClone) .Output_Voltage = Double.Parse(GetUPSVar("output.voltage", UPSName, .Input_Voltage), ciClone) .Load = Double.Parse(GetUPSVar("ups.load", UPSName, 100), ciClone) - UPS_rt_Status = Me.GetUPSVar("ups.status", UPSName, "OL") - .Output_Power = Double.Parse((Me.GetUPSVar("ups.realpower.nominal", UPSName, 0)), ciClone) + UPS_rt_Status = GetUPSVar("ups.status", UPSName, "OL") + .Output_Power = Double.Parse((GetUPSVar("ups.realpower.nominal", UPSName, 0)), ciClone) If .Output_Power = 0 Then .Output_Power = Double.Parse((GetUPSVar("ups.power.nominal", UPSName, 0)), ciClone) If .Output_Power = 0 Then @@ -421,11 +420,11 @@ Public Class UPS_Device ' Try ' LogFile.LogTracing("Enter GetUPSVar", LogLvl.LOG_DEBUG, Me) 'If Not Me.ConnectionStatus Then - If Not Me.IsConnected Then + If Not IsConnected Then Throw New Nut_Exception(Nut_Exception_Value.SOCKET_BROKEN, varName) Return Nothing Else - Dim Nut_Query = Me.Nut_Socket.Query_Data("GET VAR " & UPSName & " " & varName) + Dim Nut_Query = Nut_Socket.Query_Data("GET VAR " & UPSName & " " & varName) Select Case Nut_Query.Response Case NUTResponse.OK @@ -464,15 +463,15 @@ Public Class UPS_Device Public Function GetUPS_ListVar() As List(Of UPS_List_Datas) Dim Response = New List(Of UPS_List_Datas) - Dim Query = "LIST VAR " & Me.Nut_Config.UPSName + Dim Query = "LIST VAR " & Nut_Config.UPSName Try LogFile.LogTracing("Enter GetUPS_ListVar", LogLvl.LOG_DEBUG, Me) 'If Not Me.ConnectionStatus Then - If Not Me.IsConnected Then + If Not IsConnected Then Throw New Nut_Exception(Nut_Exception_Value.SOCKET_BROKEN, Query) Return Nothing Else - Dim List_Var = Me.Nut_Socket.Query_List_Datas(Query) + Dim List_Var = Nut_Socket.Query_List_Datas(Query) If Not IsNothing(List_Var) Then Response = List_Var End If diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Params.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Params.vb index 51d60c2..33dc28a 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Params.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Params.vb @@ -19,7 +19,7 @@ Public Module WinNUT_Params .Add("ServerAddress", "") .Add("Port", 0) .Add("UPSName", "") - .Add("Delay", 0) + .Add("Delay", 1) .Add("NutLogin", Cryptor.EncryptData("")) .Add("NutPassword", Cryptor.EncryptData("")) .Add("AutoReconnect", vbFalse) @@ -161,6 +161,7 @@ Public Module WinNUT_Params Public Function Save_Params() As Boolean Dim Cryptor As New CryptData() Dim Result As Boolean = False + Try For Each RegKeys As KeyValuePair(Of String, Dictionary(Of String, Object)) In Arr_Reg_Key_Base For Each RegValue As KeyValuePair(Of String, Object) In RegKeys.Value diff --git a/WinNUT_V2/WinNUT_GUI/About_Gui.vb b/WinNUT_V2/WinNUT_GUI/About_Gui.vb index c9d53f0..b0163f9 100644 --- a/WinNUT_V2/WinNUT_GUI/About_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/About_Gui.vb @@ -7,28 +7,24 @@ ' ' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY -Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params -Imports Logger = WinNUT_Client_Common.Logger -Imports LogLvl = WinNUT_Client_Common.LogLvl -Imports WinNUT_Globals = WinNUT_Client_Common.WinNUT_Globals +Imports WinNUT_Client_Common Public Class About_Gui - Private LogFile As Logger Private Sub About_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load - Lbl_ProgNameVersion.Text = WinNUT_Globals.LongProgramName & vbNewLine & "Version " & WinNUT_Globals.ProgramVersion - Lbl_Copyright_2019.Text = Strings.Replace(WinNUT_Globals.Copyright, "©", vbNewLine & "©") - LkLbl_Github.Text = WinNUT_Globals.GitHubURL - Me.Icon = WinNUT.Icon + Lbl_ProgNameVersion.Text = LongProgramName & vbNewLine & "Version " & ProgramVersion + Lbl_Copyright_2019.Text = Replace(Copyright, "©", vbNewLine & "©") + LkLbl_Github.Text = GitHubURL + Icon = WinNUT.Icon ' Me.LogFile = WinNUT.LogFile LogFile.LogTracing("Load About Gui", LogLvl.LOG_DEBUG, Me) End Sub Private Sub Btn_OK_Click(sender As Object, e As EventArgs) Handles Btn_OK.Click LogFile.LogTracing("Close About Gui", LogLvl.LOG_DEBUG, Me) - Me.Close() + Close() End Sub Private Sub LkLbl_Github_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LkLbl_Github.LinkClicked - System.Diagnostics.Process.Start(sender.Text) + Process.Start(sender.Text) End Sub End Class diff --git a/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb b/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb index bceb7fa..daccf5b 100644 --- a/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb @@ -11,15 +11,14 @@ Imports WinNUT_Client_Common Public Class List_Var_Gui Private List_Var_Datas As List(Of UPS_List_Datas) - Private LogFile As Logger Private UPS_Name = WinNUT.UPS_Device.Nut_Config.UPSName + Private Sub List_Var_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load - ' Me.LogFile = WinNUT.LogFile LogFile.LogTracing("Load List Var Gui", LogLvl.LOG_DEBUG, Me) - Me.Icon = WinNUT.Icon - Me.Visible = False + Icon = WinNUT.Icon + Visible = False PopulateTreeView() - Me.Visible = True + Visible = True End Sub Private Sub PopulateTreeView() @@ -34,14 +33,14 @@ Public Class List_Var_Gui action = Sub() TView_UPSVar.Nodes.Clear() TView_UPSVar.Invoke(action) - action = Sub() TView_UPSVar.Nodes.Add(WinNUT_Params.Arr_Reg_Key.Item("UPSName"), WinNUT_Params.Arr_Reg_Key.Item("UPSName")) + action = Sub() TView_UPSVar.Nodes.Add(Arr_Reg_Key.Item("UPSName"), Arr_Reg_Key.Item("UPSName")) TView_UPSVar.Invoke(action) Dim TreeChild As New TreeNode Dim LastNode As New TreeNode For Each UPS_Var In List_Var_Datas LastNode = TView_UPSVar.Nodes(0) Dim FullPathNode = String.Empty - For Each SubPath In (Strings.Split(UPS_Var.VarKey, ".")) + For Each SubPath In (Split(UPS_Var.VarKey, ".")) FullPathNode += SubPath & "." Dim Nodes = TView_UPSVar.Nodes.Find(FullPathNode, True) If Nodes.Length = 0 Then @@ -83,10 +82,10 @@ Public Class List_Var_Gui Dim SelectedNode As TreeNode = TView_UPSVar.SelectedNode If SelectedNode IsNot Nothing Then If SelectedNode.Parent IsNot Nothing Then - If SelectedNode.Parent.Text <> Me.UPS_Name And SelectedNode.Nodes.Count = 0 Then - Dim VarName = Strings.Replace(TView_UPSVar.SelectedNode.FullPath, Me.UPS_Name & ".", "") + If SelectedNode.Parent.Text <> UPS_Name And SelectedNode.Nodes.Count = 0 Then + Dim VarName = Replace(TView_UPSVar.SelectedNode.FullPath, UPS_Name & ".", "") LogFile.LogTracing("Update {VarName}", LogLvl.LOG_DEBUG, Me) - Lbl_V_Value.Text = WinNUT.UPS_Device.GetUPSVar(VarName, Me.UPS_Name) + Lbl_V_Value.Text = WinNUT.UPS_Device.GetUPSVar(VarName, UPS_Name) End If End If End If @@ -94,7 +93,7 @@ Public Class List_Var_Gui Private Sub Btn_Close_Click(sender As Object, e As EventArgs) Handles Btn_Close.Click LogFile.LogTracing("Close List Var Gui", LogLvl.LOG_DEBUG, Me) - Me.Close() + Close() End Sub Private Sub Btn_Reload_Click(sender As Object, e As EventArgs) Handles Btn_Reload.Click @@ -103,13 +102,13 @@ Public Class List_Var_Gui Lbl_V_Value.Text = "" Lbl_D_Value.Text = "" TView_UPSVar.Nodes.Clear() - Me.PopulateTreeView() + PopulateTreeView() End Sub Private Sub TView_UPSVar_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TView_UPSVar.NodeMouseClick Dim index As Integer = 0 - Dim UPSName = WinNUT_Params.Arr_Reg_Key.Item("UPSName") - Dim SelectedChild = Strings.Replace(e.Node.FullPath, UPSName & ".", "") + Dim UPSName = Arr_Reg_Key.Item("UPSName") + Dim SelectedChild = Replace(e.Node.FullPath, UPSName & ".", "") Dim FindChild As Predicate(Of UPS_List_Datas) = Function(ByVal x As UPS_List_Datas) If x.VarKey = SelectedChild Then Return True @@ -134,7 +133,7 @@ Public Class List_Var_Gui LogFile.LogTracing("Export TreeView To Clipboard", LogLvl.LOG_DEBUG, Me) Dim ToClipBoard As String = Nothing With WinNUT.UPS_Device.UPS_Datas - ToClipBoard = WinNUT_Params.Arr_Reg_Key.Item("UPSName") & " (" & .Mfr & "/" & .Model & "/" & .Firmware & ")" & vbNewLine + ToClipBoard = Arr_Reg_Key.Item("UPSName") & " (" & .Mfr & "/" & .Model & "/" & .Firmware & ")" & vbNewLine End With For Each LDatas In List_Var_Datas ToClipBoard &= LDatas.VarKey & " (" & LDatas.VarDesc & ") : " & LDatas.VarValue & vbNewLine diff --git a/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb b/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb index e924dcf..3dbdbe9 100644 --- a/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb @@ -14,7 +14,10 @@ Imports WinNUT_Client_Common Public Class Pref_Gui Private IsShowed As Boolean = False - Private IsSaved As Boolean = False + Private PrefsModified As Boolean = False + + ' Indicate that parameters have been saved (and if one or more were changed) + Public Event SavedPreferences(isModified As Boolean) Private Sub Btn_Cancel_Click(sender As Object, e As EventArgs) Handles Btn_Cancel.Click LogFile.LogTracing("Close Pref Gui from Button Cancel", LogLvl.LOG_DEBUG, Me) @@ -23,7 +26,7 @@ Public Class Pref_Gui Private Sub Save_Params() Try - IsSaved = False + ' PrefsModified = False LogFile.LogTracing("Save Parameters.", LogLvl.LOG_DEBUG, Me) Arr_Reg_Key.Item("ServerAddress") = Tb_Server_IP.Text Arr_Reg_Key.Item("Port") = CInt(Tb_Port.Text) @@ -61,6 +64,7 @@ Public Class Pref_Gui Arr_Reg_Key.Item("VerifyUpdateAtStart") = Cb_Update_At_Start.Checked Arr_Reg_Key.Item("DelayBetweenEachVerification") = Cbx_Delay_Verif.SelectedIndex Arr_Reg_Key.Item("StableOrDevBranch") = Cbx_Branch_Update.SelectedIndex + WinNUT_Params.Save_Params() If CB_Start_W_Win.Checked Then If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", Application.ProductName, Nothing) Is Nothing Then @@ -80,22 +84,24 @@ Public Class Pref_Gui LogFile.LogTracing("Pref_Gui Params Saved", 1, Me) SetLogControlsStatus() - WinNUT.WinNUT_PrefsChanged() - IsSaved = True + ' WinNUT.WinNUT_PrefsChanged() + RaiseEvent SavedPreferences(PrefsModified) + + ' PrefsModified = True Catch e As Exception - IsSaved = False + ' PrefsModified = False End Try End Sub Private Sub Btn_Apply_Click(sender As Object, e As EventArgs) Handles Btn_Apply.Click Save_Params() - If IsSaved Then + If PrefsModified Then Btn_Apply.Enabled = False End If End Sub Private Sub Btn_Ok_Click(sender As Object, e As EventArgs) Handles Btn_Ok.Click - If Not IsSaved Then + If PrefsModified Then Save_Params() End If Close() @@ -351,9 +357,14 @@ Public Class Pref_Gui LogFile.LogTracing("Load Pref Gui", LogLvl.LOG_DEBUG, Me) End Sub + ''' + ''' Handle any value in the form changing. + ''' + ''' + ''' Private Sub Event_Ctrl_Value_Changed(sender As Object, e As EventArgs) If IsShowed Then - IsSaved = False + PrefsModified = True Btn_Apply.Enabled = True End If End Sub diff --git a/WinNUT_V2/WinNUT_GUI/WinNUT.vb b/WinNUT_V2/WinNUT_GUI/WinNUT.vb index 98d17a2..a3ae3ed 100644 --- a/WinNUT_V2/WinNUT_GUI/WinNUT.vb +++ b/WinNUT_V2/WinNUT_GUI/WinNUT.vb @@ -12,6 +12,26 @@ Imports WinNUT_Client_Common Public Class WinNUT #Region "Properties" + Public Property UpdateMethod() As String + Get + If mUpdate Then + mUpdate = False + Return True + Else + Return False + End If + End Get + Set(Value As String) + mUpdate = Value + End Set + End Property + + Public WriteOnly Property HasCrashed() As Boolean + Set(Value As Boolean) + WinNUT_Crashed = Value + End Set + End Property + #End Region Private WithEvents LogFile As Logger = WinNUT_Globals.LogFile @@ -29,7 +49,7 @@ Public Class WinNUT 'Variable used with Toast Functionnality Public WithEvents FrmBuild As Update_Gui - Public ToastPopup As New WinNUT_Client_Common.ToastPopup + Public ToastPopup As New ToastPopup Private WindowsVersion As Version = Version.Parse(My.Computer.Info.OSVersion) Private MinOsVersionToast As Version = Version.Parse("10.0.18362.0") Private AllowToast As Boolean = False @@ -65,32 +85,14 @@ Public Class WinNUT Private Event On_Battery() Private Event On_Line() ' Private Event Data_Updated() - Private Event UpdateNotifyIconStr(ByVal Reason As String, ByVal Message As String) - Private Event UpdateBatteryState(ByVal Reason As String) + Private Event UpdateNotifyIconStr(Reason As String, Message As String) + Private Event UpdateBatteryState(Reason As String) + ' UPS object operation Private Event RequestConnect() + ' Private Event RequestDisconnect() 'Handle sleep/hibernate mode from windows API - Declare Function SetSuspendState Lib "PowrProf" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer - - Public Property UpdateMethod() As String - Get - If mUpdate Then - mUpdate = False - Return True - Else - Return False - End If - End Get - Set(ByVal Value As String) - mUpdate = Value - End Set - End Property - - Public WriteOnly Property HasCrashed() As Boolean - Set(ByVal Value As Boolean) - WinNUT_Crashed = Value - End Set - End Property + Declare Function SetSuspendState Lib "PowrProf" (Hibernate As Integer, ForceCritical As Integer, DisableWakeEvent As Integer) As Integer Private Sub WinNUT_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Make sure we have an app directory to write to. @@ -149,7 +151,7 @@ Public Class WinNUT 'Load WinNUT Parameters Load_Params() - WinNUT_PrefsChanged() + WinNUT_PrefsChanged(True) LogFile.LogTracing("Loaded Params Complete", LogLvl.LOG_DEBUG, Me) ' Setup logging preferences @@ -283,18 +285,16 @@ Public Class WinNUT ' RaiseEvent RequestConnect() End Sub - Private Sub SystemEvents_PowerModeChanged(ByVal sender As Object, ByVal e As Microsoft.Win32.PowerModeChangedEventArgs) + Private Sub SystemEvents_PowerModeChanged(sender As Object, e As Microsoft.Win32.PowerModeChangedEventArgs) Select Case e.Mode Case Microsoft.Win32.PowerModes.Resume LogFile.LogTracing("Restarting WinNUT after waking up from Windows", LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_MAIN_EXITSLEEP)) If Arr_Reg_Key.Item("AutoReconnect") = True Then - 'UPS_Device.Connect() UPS_Connect() End If Case Microsoft.Win32.PowerModes.Suspend LogFile.LogTracing("Windows standby, WinNUT will disconnect", LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_MAIN_GOTOSLEEP)) - ' UPSDisconnect() - UPS_Device.Disconnect() + UPSDisconnect() End Select End Sub @@ -331,45 +331,35 @@ Public Class WinNUT LogFile.LogTracing(String.Format("Something went wrong connecting to UPS {0}. IsConnected: {1}, IsAuthenticated: {2}", upsConf.UPSName, UPS_Device.IsConnected, UPS_Device.IsAuthenticated), LogLvl.LOG_ERROR, Me, String.Format(StrLog.Item(AppResxStr.STR_LOG_CON_FAILED), upsConf.Host, upsConf.Port, "Connection Error")) - ' UPSDisconnect() - UPS_Device.Disconnect() + UPSDisconnect() Else - LogFile.LogTracing("Connection to Nut Host Established", LogLvl.LOG_NOTICE, Me, - String.Format(StrLog.Item(AppResxStr.STR_LOG_CONNECTED), - upsConf.Host, upsConf.Port)) - - ' AddHandler Update_Data.Tick, AddressOf Retrieve_UPS_Datas - ' AddHandler UPS_Device.Lost_Connect, AddressOf UPS_Lostconnect - ' Me.Device_Data = UPS_Device.Retrieve_UPS_Datas() - ' RaiseEvent Data_Updated() - ' Update_Data.Start() Menu_UPS_Var.Enabled = True - UpdateIcon_NotifyIcon() LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) RaiseEvent UpdateNotifyIconStr("Connected", Nothing) + LogFile.LogTracing("Connection to Nut Host Established", LogLvl.LOG_NOTICE, Me, + String.Format(StrLog.Item(AppResxStr.STR_LOG_CONNECTED), + upsConf.Host, upsConf.Port)) End If End Sub ''' ''' Prepare application for and handle disconnecting from the UPS. ''' - Private Sub UPSDisconnect() Handles UPS_Device.Disconnected + Private Sub UPSDisconnect() ' Handles UPS_Device.Disconnected ' LogFile.LogTracing("Running Client disconnect subroutine.", LogLvl.LOG_DEBUG, Me) - ' Update_Data.Stop() - ' Update_Data. - ' RemoveHandler Update_Data.Tick, AddressOf Retrieve_UPS_Datas - - 'If UPS_Device IsNot Nothing Then - ' ' RemoveHandler UPS_Device.Connected, AddressOf UPSReady - ' RemoveHandler UPS_Device.Lost_Connect, AddressOf UPS_Lostconnect - 'End If - - 'If UPS_Device.Nut_Socket IsNot Nothing Then - ' UPS_Device.Nut_Socket.Disconnect(True) - 'End If + If UPS_Device IsNot Nothing Then + UPS_Device.Disconnect(True) + Else + LogFile.LogTracing("Attempted to disconnect when UPS_Device is Nothing.", LogLvl.LOG_DEBUG, Me) + End If + End Sub + ''' + ''' Perform final actions to wrap up a disconnected UPS. + ''' + Private Sub UPSDisconnectedEvent() Handles UPS_Device.Disconnected ReInitDisplayValues() ActualAppIconIdx = AppIconIdx.IDX_ICO_OFFLINE LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) @@ -378,19 +368,8 @@ Public Class WinNUT RaiseEvent UpdateBatteryState("Deconnected") LogFile.LogTracing("Disconnected from Nut Host", LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_LOG_LOGOFF)) - ' Nut_Socket = Nothing - ' UPS_Device.Dispose() Dispose in the future... End Sub - 'Private Sub Retrieve_UPS_Datas(sender As Object, e As EventArgs) Handles Update_Data.Tick - ' If Not Update_Data.Enabled Then - ' LogFile.LogTracing("Update_Data timer Ticked while disabled. Ignoring.", LogLvl.LOG_DEBUG, Me) - ' Else - ' Me.Device_Data = UPS_Device.Retrieve_UPS_Datas() - ' RaiseEvent Data_Updated() - ' End If - 'End Sub - Private Sub WinNUT_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) UpdateIcon_NotifyIcon() @@ -431,13 +410,10 @@ Public Class WinNUT NotifyIcon.Visible = True e.Cancel = True Else + ' TODO: Common shutdown subroutine? --v LogFile.LogTracing("Init Disconnecting Before Close WinNut", LogLvl.LOG_DEBUG, Me) RemoveHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf SystemEvents_PowerModeChanged - ' UPSDisconnect() - If UPS_Device IsNot Nothing Then - UPS_Device.Disconnect() - End If - + UPSDisconnect() LogFile.LogTracing("WinNut Is now Closed", LogLvl.LOG_DEBUG, Me) End End If @@ -450,6 +426,7 @@ Public Class WinNUT Private Sub Menu_Settings_Click(sender As Object, e As EventArgs) Handles Menu_Settings.Click LogFile.LogTracing("Open Pref Gui From Menu", LogLvl.LOG_DEBUG, Me) + AddHandler Pref_Gui.SavedPreferences, AddressOf WinNUT_PrefsChanged Pref_Gui.Activate() Pref_Gui.Visible = True HasFocus = False @@ -462,6 +439,7 @@ Public Class WinNUT Private Sub Menu_Sys_Settings_Click(sender As Object, e As EventArgs) Handles Menu_Sys_Settings.Click LogFile.LogTracing("Open Pref Gui From Systray", LogLvl.LOG_DEBUG, Me) + AddHandler Pref_Gui.SavedPreferences, AddressOf WinNUT_PrefsChanged Pref_Gui.Activate() Pref_Gui.Visible = True HasFocus = False @@ -503,13 +481,7 @@ Public Class WinNUT LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) End Sub - 'Private Sub Reconnect_NotifyIcon() Handles UPS_Device.Connected - - 'End Sub - - - - Private Sub Event_UpdateNotifyIconStr(ByVal Optional Reason As String = Nothing, ByVal Optional Message As String = Nothing) Handles Me.UpdateNotifyIconStr + Private Sub Event_UpdateNotifyIconStr(Optional Reason As String = Nothing, Optional Message As String = Nothing) Handles Me.UpdateNotifyIconStr Dim ShowVersion As String = ShortProgramVersion Dim NotifyStr As String = ProgramName & " - " & ShowVersion & vbNewLine Dim FormText As String = ProgramName @@ -568,7 +540,7 @@ Public Class WinNUT LogFile.LogTracing("NotifyIcon Text => " & vbNewLine & NotifyStr, LogLvl.LOG_DEBUG, Me) End Sub - Private Sub Event_UpdateBatteryState(ByVal Optional Reason As String = Nothing) Handles Me.UpdateBatteryState + Private Sub Event_UpdateBatteryState(Optional Reason As String = Nothing) Handles Me.UpdateBatteryState Static Dim Old_Battery_Value As Integer = UPS_BattCh Dim Status As String = "Unknown" Select Case Reason @@ -621,12 +593,7 @@ Public Class WinNUT Private Sub UPS_Lostconnect() Handles UPS_Device.Lost_Connect LogFile.LogTracing("Notify user of lost connection", LogLvl.LOG_ERROR, Me, String.Format(StrLog.Item(AppResxStr.STR_MAIN_LOSTCONNECT), UPS_Device.Nut_Config.Host, UPS_Device.Nut_Config.Port)) - UPSDisconnect() - 'Dim Host = UPS_Device.Nut_Config.Host - 'Dim Port = UPS_Device.Nut_Config.Port - 'Update_Data.Stop() - 'LogFile.LogTracing("Fix All data to null/empty String", LogLvl.LOG_DEBUG, Me) - 'LogFile.LogTracing("Fix All Dial Data to Min Value/0", LogLvl.LOG_DEBUG, Me) + ' UPSDisconnect() 'ReInitDisplayValues() If UPS_Device.Nut_Config.AutoReconnect And UPS_Retry <= UPS_MaxRetry Then @@ -779,9 +746,8 @@ Public Class WinNUT End Sub Private Sub Menu_Disconnect_Click(sender As Object, e As EventArgs) Handles Menu_Disconnect.Click - LogFile.LogTracing("Force Disconnect from menu", LogLvl.LOG_DEBUG, Me) - ' UPSDisconnect() - UPS_Device.Disconnect() + LogFile.LogTracing("Disconnect from menu", LogLvl.LOG_DEBUG, Me) + UPSDisconnect() End Sub Private Sub ReInitDisplayValues() @@ -810,11 +776,7 @@ Public Class WinNUT Private Sub Menu_Reconnect_Click(sender As Object, e As EventArgs) Handles Menu_Reconnect.Click LogFile.LogTracing("Force Reconnect from menu", LogLvl.LOG_DEBUG, Me) - - ' UPSDisconnect() - If UPS_Device.IsConnected Then - UPS_Device.Disconnect() - End If + UPSDisconnect() UPS_Connect() End Sub @@ -857,76 +819,49 @@ Public Class WinNUT End If End Sub - Public Sub WinNUT_PrefsChanged() + Public Sub WinNUT_PrefsChanged(isChanged As Boolean) + LogFile.LogTracing("Beginning WinNUT_PrefsChanged subroutine.", LogLvl.LOG_DEBUG, Me) + ' Setup logging preferences If Arr_Reg_Key.Item("UseLogFile") Then LogFile.LogLevelValue = Arr_Reg_Key.Item("Log Level") LogFile.InitializeLogFile() ElseIf LogFile.IsWritingToFile Then LogFile.DeleteLogFile() - End If 'Dim NeedReconnect As Boolean = False 'With UPS_Device.Nut_Config - ' If .AutoReconnect <> WinNUT_Params.Arr_Reg_Key.Item("autoreconnect") Then - ' .AutoReconnect = WinNUT_Params.Arr_Reg_Key.Item("autoreconnect") - ' End If - ' If .Host <> WinNUT_Params.Arr_Reg_Key.Item("ServerAddress") Then - ' NeedReconnect = True - ' .Host = WinNUT_Params.Arr_Reg_Key.Item("ServerAddress") - ' End If - ' If .Port <> WinNUT_Params.Arr_Reg_Key.Item("Port") Then + ' If .AutoReconnect <> Arr_Reg_Key.Item("autoreconnect") Or + ' .Host <> Arr_Reg_Key.Item("ServerAddress") Or + ' .Port <> Arr_Reg_Key.Item("Port") Or + ' .UPSName <> Arr_Reg_Key.Item("UPSName") Or + ' UPS_Device.PollingInterval <> Arr_Reg_Key.Item("Delay") Or + ' .Login <> Arr_Reg_Key.Item("NutLogin") Or + ' UPS_Device.NutPassword <> Arr_Reg_Key.Item("NutPassword") Then ' NeedReconnect = True - ' .Port = WinNUT_Params.Arr_Reg_Key.Item("Port") + ' UPS_Device.NutPassword = Arr_Reg_Key.Item("NutPassword") ' End If - ' If .UPSName <> WinNUT_Params.Arr_Reg_Key.Item("UPSName") Then - ' NeedReconnect = True - ' .UPSName = WinNUT_Params.Arr_Reg_Key.Item("UPSName") + ' If UPS_Device.UPS_Follow_FSD <> Arr_Reg_Key.Item("Follow_FSD") Then + ' UPS_Device.UPS_Follow_FSD = Arr_Reg_Key.Item("Follow_FSD") ' End If - ' If Polling_Interval <> WinNUT_Params.Arr_Reg_Key.Item("Delay") Then - ' NeedReconnect = True - ' Polling_Interval = WinNUT_Params.Arr_Reg_Key.Item("Delay") - ' End If - ' If .Login <> WinNUT_Params.Arr_Reg_Key.Item("NutLogin") Then - ' NeedReconnect = True - ' .Login = WinNUT_Params.Arr_Reg_Key.Item("NutLogin") - ' End If - ' If UPS_Device.NutPassword <> WinNUT_Params.Arr_Reg_Key.Item("NutPassword") Then - ' NeedReconnect = True - ' UPS_Device.NutPassword = WinNUT_Params.Arr_Reg_Key.Item("NutPassword") - ' End If - ' If UPS_Device.UPS_Follow_FSD <> WinNUT_Params.Arr_Reg_Key.Item("Follow_FSD") Then - ' UPS_Device.UPS_Follow_FSD = WinNUT_Params.Arr_Reg_Key.Item("Follow_FSD") - ' End If - ' UPS_Device.Battery_Limit = WinNUT_Params.Arr_Reg_Key.Item("ShutdownLimitBatteryCharge") - ' UPS_Device.Backup_Limit = WinNUT_Params.Arr_Reg_Key.Item("ShutdownLimitUPSRemainTime") + ' UPS_Device.Battery_Limit = Arr_Reg_Key.Item("ShutdownLimitBatteryCharge") + ' UPS_Device.Backup_Limit = Arr_Reg_Key.Item("ShutdownLimitUPSRemainTime") 'End With - ' Automatically reconnect regardless - ' UPSDisconnect() - If UPS_Device IsNot Nothing Then - UPS_Device.Disconnect() + ' Automatically reconnect if already connected and prefs are changed. + If (UPS_Device IsNot Nothing) AndAlso UPS_Device.IsConnected And isChanged Then + LogFile.LogTracing("Connection parameters Changed. Force Reconnect", LogLvl.LOG_NOTICE, Me) + UPSDisconnect() + 'ReInitDisplayValues() + 'ActualAppIconIdx = AppIconIdx.IDX_ICO_OFFLINE + 'LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) + 'UpdateIcon_NotifyIcon() + 'LogFile.LogTracing("New Parameter Applyed. Force Reconnect", LogLvl.LOG_DEBUG, Me) + UPS_Connect() End If - ' UPS_Connect() - 'If UPS_Device.IsConnected Then ' NeedReconnect And - ' LogFile.LogTracing("Connection parameters Changed. Force Disconnect", LogLvl.LOG_DEBUG, Me) - ' 'UPS_Device.Disconnect(True, True) - ' ReInitDisplayValues() - ' ActualAppIconIdx = AppIconIdx.IDX_ICO_OFFLINE - ' LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) - ' UpdateIcon_NotifyIcon() - ' LogFile.LogTracing("New Parameter Applyed. Force Reconnect", LogLvl.LOG_DEBUG, Me) - ' UPS_Connect() - ' 'UPS_Device.Connect() - 'ElseIf Not UPS_Device.IsConnected Then - ' LogFile.LogTracing("New Parameter Applyed. Force Reconnect", LogLvl.LOG_DEBUG, Me) - ' 'UPS_Device.Connect() - ' UPS_Connect() - 'End If - 'NeedReconnect = Nothing With AG_InV If (.MaxValue <> Arr_Reg_Key.Item("MaxInputVoltage")) Or (.MinValue <> Arr_Reg_Key.Item("MinInputVoltage")) Then LogFile.LogTracing("Parameter Dial Input Voltage Need to be Updated", LogLvl.LOG_DEBUG, Me) @@ -1013,7 +948,7 @@ Public Class WinNUT End If End Sub - Private Function GetIcon(ByVal IconIdx As Integer) As Icon + Private Function GetIcon(IconIdx As Integer) As Icon Select Case IconIdx Case 1025 Return My.Resources._1025 @@ -1169,7 +1104,7 @@ Public Class WinNUT LogFile.LogTracing("Initialisation Params Complete", LogLvl.LOG_DEBUG, Me) LogFile.LogTracing("Loaded Params Complete", LogLvl.LOG_DEBUG, Me) End If - WinNUT_PrefsChanged() + WinNUT_PrefsChanged(True) UPS_Connect() End Sub End Class