From 911a463648c01f8e7e6018364cdf90a890e2ef29 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Sun, 7 Aug 2022 15:05:17 -0700 Subject: [PATCH 1/2] Enable documentation gen again Thought I was disabling it for Release only... --- WinNUT_V2/WinNUT_GUI/WinNUT-client.vbproj | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/WinNUT_V2/WinNUT_GUI/WinNUT-client.vbproj b/WinNUT_V2/WinNUT_GUI/WinNUT-client.vbproj index c12a460..19623c6 100644 --- a/WinNUT_V2/WinNUT_GUI/WinNUT-client.vbproj +++ b/WinNUT_V2/WinNUT_GUI/WinNUT-client.vbproj @@ -41,8 +41,7 @@ true true bin\Debug\ - - + WinNUT-Client.xml 42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 false @@ -53,8 +52,7 @@ true true bin\Release\ - - + WinNUT-Client.xml 42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 From 8371127804fd14ab2b64650cb735c796a5e024ab Mon Sep 17 00:00:00 2001 From: gbakeman Date: Mon, 8 Aug 2022 12:03:21 -0700 Subject: [PATCH 2/2] Logging init happens earlier in code, & other changes - Moved Logger object and initialization back into Globals module. - Disabled system.diagnostics listeners in app.config after trying to figure out how to change log location. It looks like setting a FileLog location in app.config isn't supported. If we're going to upgrade logging anyways, it's best to get a third party library. - ApplicationEvents.vb now has a Startup subroutine for initializaing the Globals module and logging. - Cleaned up Report generation subroutine. User data dir is synchronized with the Globals module, and an Explorer window is opened to the destination after the crash report is generated. - More Logger changes. Base file name follows the ProgramName, the event buffer size has been quadrupled (and is now a property) - Prefs_Gui no longer directly controls Logging - this is handled back in WinNUT.vb. - Fixed crash in Update_Gui due to missing Logger variable, and cleaned up code. WinNUT.vb: - After loading parameters, trigger PrefsChanged to make sure everything is applied. Logging init is also handled elsewhere now. - More code cleanup. --- WinNUT_V2/SharedAssemblyInfo.vb | 2 +- WinNUT_V2/WinNUT-Client_Common/Logger.vb | 144 +++++---- .../WinNUT-Client_Common/WinNUT_Globals.vb | 8 +- WinNUT_V2/WinNUT_GUI/App.config | 7 +- WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb | 43 ++- .../WinNUT_GUI/My Project/AssemblyInfo.vb | 4 +- WinNUT_V2/WinNUT_GUI/Pref_Gui.vb | 245 ++++++++-------- WinNUT_V2/WinNUT_GUI/Update_Gui.vb | 25 +- WinNUT_V2/WinNUT_GUI/WinNUT.vb | 274 +++++++++--------- 9 files changed, 409 insertions(+), 343 deletions(-) diff --git a/WinNUT_V2/SharedAssemblyInfo.vb b/WinNUT_V2/SharedAssemblyInfo.vb index fef9b63..44c7deb 100644 --- a/WinNUT_V2/SharedAssemblyInfo.vb +++ b/WinNUT_V2/SharedAssemblyInfo.vb @@ -17,7 +17,7 @@ Imports System.Reflection ' Vérifiez les valeurs des attributs de l'assembly - + diff --git a/WinNUT_V2/WinNUT-Client_Common/Logger.vb b/WinNUT_V2/WinNUT-Client_Common/Logger.vb index a1f2f28..fe8b663 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Logger.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Logger.vb @@ -10,32 +10,50 @@ Imports System.IO Public Class Logger - Private Const BaseFileName = "WinNUT-CLient" +#Region "Constants/Shared" + Private Shared ReadOnly BASE_FILE_NAME = ProgramName ' "WinNUT-CLient" + Private Const LOG_FILE_CREATION_SCHEDULE = Logging.LogFileCreationScheduleOption.Daily + ' The LogFileCreationScheduleOption doesn't present the string format of what it uses + Private Const LOG_FILE_DATESTRING = "yyyy-MM-dd" ' Logs will be stored in the program's appdata folder, in a Logs subdirectory. Public Shared ReadOnly LogFolder = Path.Combine(ApplicationData, "Logs") +#End Region Private LogFile As Logging.FileLogTraceListener Private ReadOnly TEventCache As New TraceEventCache() Public LogLevelValue As LogLvl Private L_CurrentLogData As String Private LastEventsList As New List(Of Object) - Public Event NewData(ByVal sender As Object) + Public Event NewData(sender As Object) #Region "Properties" + Private _MaxEvents As Integer = 200 + Public Property MaxEvents As Integer + Get + Return _MaxEvents + End Get + Set(value As Integer) + If value < 0 Then + Throw New ArgumentOutOfRangeException("MaxInteger", "Maximum number of events cannot be negative.") + End If + End Set + End Property + Public Property CurrentLogData() As String Get - Dim Tmp_Data = Me.L_CurrentLogData - Me.L_CurrentLogData = Nothing + Dim Tmp_Data = L_CurrentLogData + L_CurrentLogData = Nothing Return Tmp_Data End Get - Set(ByVal Value As String) - Me.L_CurrentLogData = Value + Set(Value As String) + L_CurrentLogData = Value End Set End Property + Public ReadOnly Property LastEvents() As List(Of Object) Get - Return Me.LastEventsList + Return LastEventsList End Get End Property @@ -43,56 +61,70 @@ Public Class Logger ''' Returns if data is being written to a file. Also allows for file logging to be setup or stopped. ''' ''' True when the object is instantiated, false if not. - Public Property IsWritingToFile() As Boolean + Public ReadOnly Property IsWritingToFile() As Boolean Get - Return Not (LogFile Is Nothing) + Return LogFile IsNot Nothing End Get - Set(Value As Boolean) - If Value = False And LogFile IsNot Nothing Then - LogFile.Close() - LogFile.Dispose() - LogFile = Nothing - LogTracing("Logging to file has been disabled.", LogLvl.LOG_NOTICE, Me) - ElseIf Value Then - SetupLogfile() - End If - End Set + 'Get + ' Return Not (LogFile Is Nothing) + 'End Get + + 'Set(Value As Boolean) + ' If Value = False And LogFile IsNot Nothing Then + ' LogFile.Close() + ' LogFile.Dispose() + ' LogFile = Nothing + ' LogTracing("Logging to file has been disabled.", LogLvl.LOG_NOTICE, Me) + ' ElseIf Value Then + ' SetupLogfile() + ' End If + 'End Set End Property + ''' + ''' Either retrieve the log file location from the object, or give an estimate of what it + ''' would be. + ''' + ''' The possible path to the log file. Note that this does not gaurantee it exists. Public ReadOnly Property LogFileLocation() As String Get If IsWritingToFile Then Return LogFile.FullLogFileName Else - Return String.Empty + Return Path.Combine(LogFolder, BASE_FILE_NAME & Date.Now.ToString(LOG_FILE_DATESTRING)) End If End Get End Property - Public Property LogLevel() As LogLvl - Get - Return Me.LogLevelValue - End Get - Set(ByVal Value As LogLvl) - Me.LogLevelValue = Value - End Set - End Property + ' Log all events - this object will keep all logs and allow accessors to decide which ones they want. + 'Public Property LogLevel() As LogLvl + ' Get + ' Return Me.LogLevelValue + ' End Get + ' Set(ByVal Value As LogLvl) + ' Me.LogLevelValue = Value + ' End Set + 'End Property #End Region - Public Sub New(WriteLog As Boolean, LogLevel As LogLvl) - IsWritingToFile = WriteLog + Public Sub New(writeLog As Boolean, LogLevel As LogLvl) LogLevelValue = LogLevel - LastEventsList.Capacity = 50 + ' LastEventsList.Capacity = 50 + + ' IsWritingToFile = writeLog + If writeLog = True Then + InitializeLogFile() + End If End Sub - Public Sub SetupLogfile() - LogFile = New Logging.FileLogTraceListener(BaseFileName) With { + Public Sub InitializeLogFile() + LogFile = New Logging.FileLogTraceListener(BASE_FILE_NAME) With { .TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId, .Append = True, .AutoFlush = True, - .LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily, + .LogFileCreationSchedule = LOG_FILE_CREATION_SCHEDULE, .CustomLocation = LogFolder, .Location = Logging.LogFileLocation.Custom } @@ -100,13 +132,28 @@ Public Class Logger LogTracing("Log file is initialized at " & LogFile.FullLogFileName, LogLvl.LOG_NOTICE, Me) End Sub + ''' + ''' Disable logging and delete the current file. + ''' + ''' True if file was successfully deleted. False if an exception was encountered. Public Function DeleteLogFile() As Boolean + Dim fileLocation = LogFile.FullLogFileName + + ' Disable logging first. + If LogFile IsNot Nothing Then + LogFile.Close() + LogFile.Dispose() + ' For some reason, the object needs to be dereferenced to actually get it to close the handle. + LogFile = Nothing + LogTracing("Logging to file has been disabled.", LogLvl.LOG_NOTICE, Me) + End If + Try - Dim fileLocation = LogFile.FullLogFileName - IsWritingToFile = False + ' IsWritingToFile = False File.Delete(fileLocation) Return True Catch ex As Exception + LogTracing("Error when deleteing log file: " & ex.ToString(), LogLvl.LOG_ERROR, Me) Return False End Try End Function @@ -117,39 +164,34 @@ Public Class Logger ''' is specified. ''' ''' The raw information that needs to be recorded. - ''' How important the information is. - ''' + ''' The severity of the message. + ''' What generated this message. ''' A user-friendly, translated string to be shown. - Public Sub LogTracing(ByVal message As String, ByVal LvlError As Int16, sender As Object, Optional ByVal LogToDisplay As String = Nothing) + Public Sub LogTracing(message As String, LvlError As LogLvl, sender As Object, Optional LogToDisplay As String = Nothing) Dim Pid = TEventCache.ProcessId Dim SenderName = sender.GetType.Name Dim EventTime = Now.ToLocalTime Dim FinalMsg = EventTime & " Pid: " & Pid & " " & SenderName & " : " & message - 'Update LogFilePath to make sure it's still the correct path - ' gbakeman 31/7/2022: Disabling since the LogFilePath should never change throughout the lifetime of this - ' object, unless proper initialization has occured. - - ' WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName - ' Always write log messages to the attached debug messages window. #If DEBUG Then Debug.WriteLine(FinalMsg) #End If 'Create Event in EventList in case of crash for generate Report - If Me.LastEventsList.Count = Me.LastEventsList.Capacity Then - Me.LastEventsList.RemoveAt(0) + If LastEventsList.Count >= MaxEvents Then + LastEventsList.RemoveAt(0) End If + LastEventsList.Add(FinalMsg) - Me.LastEventsList.Add(FinalMsg) - - If IsWritingToFile AndAlso Me.LogLevel >= LvlError Then + ' Send message to log file if enabled + If IsWritingToFile AndAlso LogLevelValue >= LvlError Then LogFile.WriteLine(FinalMsg) End If + 'If LvlError = LogLvl.LOG_NOTICE Then If LogToDisplay IsNot Nothing Then - Me.L_CurrentLogData = LogToDisplay + L_CurrentLogData = LogToDisplay RaiseEvent NewData(sender) End If End Sub diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index 2ab7659..d313ba6 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -33,14 +33,16 @@ Public Module WinNUT_Globals ' Public LogFile As String ' Handle application messages and debug events. ' Public WithEvents LogFile As Logger ' As New Logger(False, 0) - Public AppIcon As Dictionary(Of Integer, System.Drawing.Icon) + ' Logging + Public WithEvents LogFile As Logger + Public AppIcon As Dictionary(Of Integer, Drawing.Icon) Public StrLog As New List(Of String) ' Public LogFilePath As String Public Sub Init_Globals() LongProgramName = My.Application.Info.Description ProgramName = My.Application.Info.ProductName - ProgramVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString + ProgramVersion = Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString ShortProgramVersion = ProgramVersion.Substring(0, ProgramVersion.IndexOf(".", ProgramVersion.IndexOf(".") + 1)) GitHubURL = My.Application.Info.Trademark Copyright = My.Application.Info.Copyright @@ -89,6 +91,8 @@ Public Module WinNUT_Globals 'StrLog.Insert(AppResxStr.STR_LOG_NO_UPDATE, Resources.Log_Str_10) 'StrLog.Insert(AppResxStr.STR_LOG_UPDATE, Resources.Log_Str_11) 'StrLog.Insert(AppResxStr.STR_LOG_NUT_FSD, Resources.Log_Str_12) + + LogFile = New Logger(False, LogLvl.LOG_DEBUG) End Sub 'Sub SetupAppDirectory() diff --git a/WinNUT_V2/WinNUT_GUI/App.config b/WinNUT_V2/WinNUT_GUI/App.config index 5690060..c939246 100644 --- a/WinNUT_V2/WinNUT_GUI/App.config +++ b/WinNUT_V2/WinNUT_GUI/App.config @@ -8,11 +8,10 @@ - - + @@ -25,7 +24,7 @@ - + diff --git a/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb b/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb index 866c06d..f8ec428 100644 --- a/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb +++ b/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb @@ -7,8 +7,8 @@ ' ' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY +Imports Microsoft.VisualBasic.ApplicationServices Imports WinNUT_Client_Common -Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params Namespace My ' Les événements suivants sont disponibles pour MyApplication : @@ -24,7 +24,13 @@ Namespace My Private Msg_Crash As New Label Private Msg_Error As New TextBox - Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException + Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup + 'Init WinNUT Variables + Init_Globals() + LogFile.LogTracing("Init Globals Variables Complete", LogLvl.LOG_DEBUG, Me) + End Sub + + Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Handles Me.UnhandledException e.ExitApplication = False Dim Frms As New FormCollection @@ -81,9 +87,9 @@ Namespace My .Controls.Add(BtnGenerate) End With - AddHandler BtnClose.Click, AddressOf My.Application.Close_Button_Click + AddHandler BtnClose.Click, AddressOf Application.Close_Button_Click AddHandler BtnGenerate.Click, AddressOf Application.Generate_Button_Click - AddHandler CrashBug_Form.FormClosing, AddressOf My.Application.CrashBug_FormClosing + AddHandler CrashBug_Form.FormClosing, AddressOf Application.CrashBug_FormClosing CrashBug_Form.Show() CrashBug_Form.BringToFront() @@ -100,21 +106,21 @@ Namespace My Private Sub Generate_Button_Click(sender As Object, e As EventArgs) 'Generate a bug report with all essential datas Dim Crash_Report As String = "WinNUT Bug Report" & vbNewLine - Dim WinNUT_Config As New Dictionary(Of String, Object)(WinNUT_Params.Arr_Reg_Key) - Dim WinNUT_UserData_Dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client" + Dim WinNUT_Config As New Dictionary(Of String, Object)(Arr_Reg_Key) + Dim WinNUT_UserData_Dir = ApplicationData ' Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client" Dim CrashLog_Dir = WinNUT_UserData_Dir & "\CrashLog" Dim CrashLog_Filename As String = "Crash_Report_" & Format(Now, "dd-MM-yyyy") & "_" & String.Format("{0}-{1}-{2}.txt", Now.Hour.ToString("00"), Now.Minute.ToString("00"), Now.Second.ToString("00")) - For Each kvp As KeyValuePair(Of String, Object) In WinNUT_Params.Arr_Reg_Key + For Each kvp As KeyValuePair(Of String, Object) In Arr_Reg_Key Select Case kvp.Key Case "ServerAddress", "Port", "UPSName", "NutLogin", "NutPassword" WinNUT_Config.Remove(kvp.Key) End Select Next - Crash_Report &= "Os Version : " & My.Computer.Info.OSVersion & vbNewLine - Crash_Report &= "WinNUT Version : " & System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString & vbNewLine + Crash_Report &= "Os Version : " & Computer.Info.OSVersion & vbNewLine + Crash_Report &= "WinNUT Version : " & Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString & vbNewLine Crash_Report &= vbNewLine & "WinNUT Parameters : " & vbNewLine @@ -123,19 +129,26 @@ Namespace My Crash_Report &= Msg_Error.Text & vbNewLine & vbNewLine Crash_Report &= "Last Events :" & vbNewLine - For Each WinNUT_Event In WinNUT.LogFile.LastEvents + For Each WinNUT_Event In LogFile.LastEvents Crash_Report &= WinNUT_Event & vbNewLine Next - My.Computer.Clipboard.SetText(Crash_Report) - If Not My.Computer.FileSystem.DirectoryExists(CrashLog_Dir) Then - My.Computer.FileSystem.CreateDirectory(CrashLog_Dir) + Computer.Clipboard.SetText(Crash_Report) + + If Not Computer.FileSystem.DirectoryExists(CrashLog_Dir) Then + Computer.FileSystem.CreateDirectory(CrashLog_Dir) End If - Dim CrashLog_Report As System.IO.StreamWriter - CrashLog_Report = My.Computer.FileSystem.OpenTextFileWriter(CrashLog_Dir & "\" & CrashLog_Filename, True) + Dim CrashLog_Report As IO.StreamWriter + CrashLog_Report = Computer.FileSystem.OpenTextFileWriter(CrashLog_Dir & "\" & CrashLog_Filename, True) CrashLog_Report.WriteLine(Crash_Report) CrashLog_Report.Close() + + ' Open an Explorer window to the crash log. + ' Dim fullFilepath As String = CrashLog_Dir & "\" & CrashLog_Filename + ' If WinNUT IsNot Nothing Then + Process.Start(CrashLog_Dir) + ' End If End End Sub End Class diff --git a/WinNUT_V2/WinNUT_GUI/My Project/AssemblyInfo.vb b/WinNUT_V2/WinNUT_GUI/My Project/AssemblyInfo.vb index 286204f..9aa41dc 100644 --- a/WinNUT_V2/WinNUT_GUI/My Project/AssemblyInfo.vb +++ b/WinNUT_V2/WinNUT_GUI/My Project/AssemblyInfo.vb @@ -7,8 +7,6 @@ ' ' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY -Imports System.Resources -Imports System Imports System.Reflection Imports System.Runtime.InteropServices @@ -18,7 +16,7 @@ Imports System.Runtime.InteropServices ' Vérifiez les valeurs des attributs de l'assembly - + diff --git a/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb b/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb index 48bbfbf..e924dcf 100644 --- a/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb @@ -10,135 +10,136 @@ Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params Imports LogLvl = WinNUT_Client_Common.LogLvl Imports System.IO +Imports WinNUT_Client_Common Public Class Pref_Gui Private IsShowed As Boolean = False Private IsSaved As Boolean = False Private Sub Btn_Cancel_Click(sender As Object, e As EventArgs) Handles Btn_Cancel.Click - WinNUT.LogFile.LogTracing("Close Pref Gui from Button Cancel", LogLvl.LOG_DEBUG, Me) - Me.Close() + LogFile.LogTracing("Close Pref Gui from Button Cancel", LogLvl.LOG_DEBUG, Me) + Close() End Sub Private Sub Save_Params() Try - Me.IsSaved = False - WinNUT.LogFile.LogTracing("Save Parameters.", LogLvl.LOG_DEBUG, Me) - WinNUT_Params.Arr_Reg_Key.Item("ServerAddress") = Tb_Server_IP.Text - WinNUT_Params.Arr_Reg_Key.Item("Port") = CInt(Tb_Port.Text) - WinNUT_Params.Arr_Reg_Key.Item("UPSName") = Tb_UPS_Name.Text - WinNUT_Params.Arr_Reg_Key.Item("Delay") = (CInt(Tb_Delay_Com.Text) * 1000) - WinNUT_Params.Arr_Reg_Key.Item("NutLogin") = Tb_Login_Nut.Text - WinNUT_Params.Arr_Reg_Key.Item("NutPassword") = Tb_Pwd_Nut.Text - WinNUT_Params.Arr_Reg_Key.Item("AutoReconnect") = Cb_Reconnect.Checked - WinNUT_Params.Arr_Reg_Key.Item("MinInputVoltage") = CInt(Tb_InV_Min.Text) - WinNUT_Params.Arr_Reg_Key.Item("MaxInputVoltage") = CInt(Tb_InV_Max.Text) - WinNUT_Params.Arr_Reg_Key.Item("FrequencySupply") = Cbx_Freq_Input.SelectedIndex - WinNUT_Params.Arr_Reg_Key.Item("MinInputFrequency") = CInt(Tb_InF_Min.Text) - WinNUT_Params.Arr_Reg_Key.Item("MaxInputFrequency") = CInt(Tb_InF_Max.Text) - WinNUT_Params.Arr_Reg_Key.Item("MinOutputVoltage") = CInt(Tb_OutV_Min.Text) - WinNUT_Params.Arr_Reg_Key.Item("MaxOutputVoltage") = CInt(Tb_OutV_Max.Text) - WinNUT_Params.Arr_Reg_Key.Item("MinUPSLoad") = CInt(Tb_Load_Min.Text) - WinNUT_Params.Arr_Reg_Key.Item("MaxUPSLoad") = CInt(Tb_Load_Max.Text) - WinNUT_Params.Arr_Reg_Key.Item("MinBattVoltage") = CInt(Tb_BattV_Min.Text) - WinNUT_Params.Arr_Reg_Key.Item("MaxBattVoltage") = CInt(Tb_BattV_Max.Text) - WinNUT_Params.Arr_Reg_Key.Item("MinimizeToTray") = CB_Systray.Checked - WinNUT_Params.Arr_Reg_Key.Item("MinimizeOnStart") = CB_Start_Mini.Checked - WinNUT_Params.Arr_Reg_Key.Item("CloseToTray") = CB_Close_Tray.Checked - WinNUT_Params.Arr_Reg_Key.Item("StartWithWindows") = CB_Start_W_Win.Checked - WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") = CB_Use_Logfile.Checked - WinNUT_Params.Arr_Reg_Key.Item("Log Level") = Cbx_LogLevel.SelectedIndex - WinNUT_Params.Arr_Reg_Key.Item("ShutdownLimitBatteryCharge") = CInt(Tb_BattLimit_Load.Text) - WinNUT_Params.Arr_Reg_Key.Item("ShutdownLimitUPSRemainTime") = CInt(Tb_BattLimit_Time.Text) - WinNUT_Params.Arr_Reg_Key.Item("ImmediateStopAction") = Cb_ImmediateStop.Checked - WinNUT_Params.Arr_Reg_Key.Item("Follow_FSD") = CB_Follow_FSD.Checked - WinNUT_Params.Arr_Reg_Key.Item("TypeOfStop") = Cbx_TypeStop.SelectedIndex - WinNUT_Params.Arr_Reg_Key.Item("DelayToShutdown") = CInt(Tb_Delay_Stop.Text) - WinNUT_Params.Arr_Reg_Key.Item("AllowExtendedShutdownDelay") = Cb_ExtendTime.Checked - WinNUT_Params.Arr_Reg_Key.Item("ExtendedShutdownDelay") = CInt(Tb_GraceTime.Text) - WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdate") = Cb_Verify_Update.Checked - WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdateAtStart") = Cb_Update_At_Start.Checked - WinNUT_Params.Arr_Reg_Key.Item("DelayBetweenEachVerification") = Cbx_Delay_Verif.SelectedIndex - WinNUT_Params.Arr_Reg_Key.Item("StableOrDevBranch") = Cbx_Branch_Update.SelectedIndex + IsSaved = 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) + Arr_Reg_Key.Item("UPSName") = Tb_UPS_Name.Text + Arr_Reg_Key.Item("Delay") = (CInt(Tb_Delay_Com.Text) * 1000) + Arr_Reg_Key.Item("NutLogin") = Tb_Login_Nut.Text + Arr_Reg_Key.Item("NutPassword") = Tb_Pwd_Nut.Text + Arr_Reg_Key.Item("AutoReconnect") = Cb_Reconnect.Checked + Arr_Reg_Key.Item("MinInputVoltage") = CInt(Tb_InV_Min.Text) + Arr_Reg_Key.Item("MaxInputVoltage") = CInt(Tb_InV_Max.Text) + Arr_Reg_Key.Item("FrequencySupply") = Cbx_Freq_Input.SelectedIndex + Arr_Reg_Key.Item("MinInputFrequency") = CInt(Tb_InF_Min.Text) + Arr_Reg_Key.Item("MaxInputFrequency") = CInt(Tb_InF_Max.Text) + Arr_Reg_Key.Item("MinOutputVoltage") = CInt(Tb_OutV_Min.Text) + Arr_Reg_Key.Item("MaxOutputVoltage") = CInt(Tb_OutV_Max.Text) + Arr_Reg_Key.Item("MinUPSLoad") = CInt(Tb_Load_Min.Text) + Arr_Reg_Key.Item("MaxUPSLoad") = CInt(Tb_Load_Max.Text) + Arr_Reg_Key.Item("MinBattVoltage") = CInt(Tb_BattV_Min.Text) + Arr_Reg_Key.Item("MaxBattVoltage") = CInt(Tb_BattV_Max.Text) + Arr_Reg_Key.Item("MinimizeToTray") = CB_Systray.Checked + Arr_Reg_Key.Item("MinimizeOnStart") = CB_Start_Mini.Checked + Arr_Reg_Key.Item("CloseToTray") = CB_Close_Tray.Checked + Arr_Reg_Key.Item("StartWithWindows") = CB_Start_W_Win.Checked + Arr_Reg_Key.Item("UseLogFile") = CB_Use_Logfile.Checked + Arr_Reg_Key.Item("Log Level") = Cbx_LogLevel.SelectedIndex + Arr_Reg_Key.Item("ShutdownLimitBatteryCharge") = CInt(Tb_BattLimit_Load.Text) + Arr_Reg_Key.Item("ShutdownLimitUPSRemainTime") = CInt(Tb_BattLimit_Time.Text) + Arr_Reg_Key.Item("ImmediateStopAction") = Cb_ImmediateStop.Checked + Arr_Reg_Key.Item("Follow_FSD") = CB_Follow_FSD.Checked + Arr_Reg_Key.Item("TypeOfStop") = Cbx_TypeStop.SelectedIndex + Arr_Reg_Key.Item("DelayToShutdown") = CInt(Tb_Delay_Stop.Text) + Arr_Reg_Key.Item("AllowExtendedShutdownDelay") = Cb_ExtendTime.Checked + Arr_Reg_Key.Item("ExtendedShutdownDelay") = CInt(Tb_GraceTime.Text) + Arr_Reg_Key.Item("VerifyUpdate") = Cb_Verify_Update.Checked + 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 My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath) - WinNUT.LogFile.LogTracing("WinNUT Added to Startup.", LogLvl.LOG_DEBUG, Me) + LogFile.LogTracing("WinNUT Added to Startup.", LogLvl.LOG_DEBUG, Me) End If Else If Not My.Computer.Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", Application.ProductName, Nothing) Is Nothing Then My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName) - WinNUT.LogFile.LogTracing("WinNUT Removed From Startup.", LogLvl.LOG_DEBUG, Me) + LogFile.LogTracing("WinNUT Removed From Startup.", LogLvl.LOG_DEBUG, Me) End If End If - WinNUT.LogFile.LogLevel = Cbx_LogLevel.SelectedIndex - WinNUT.LogFile.IsWritingToFile = CB_Use_Logfile.Checked + 'LogFile.LogLevel = Cbx_LogLevel.SelectedIndex + 'LogFile.IsWritingToFile = CB_Use_Logfile.Checked - WinNUT.LogFile.LogTracing("Pref_Gui Params Saved", 1, Me) + LogFile.LogTracing("Pref_Gui Params Saved", 1, Me) SetLogControlsStatus() WinNUT.WinNUT_PrefsChanged() - Me.IsSaved = True + IsSaved = True Catch e As Exception - Me.IsSaved = False + IsSaved = False End Try End Sub Private Sub Btn_Apply_Click(sender As Object, e As EventArgs) Handles Btn_Apply.Click - Me.Save_Params() - If Me.IsSaved Then - Me.Btn_Apply.Enabled = False + Save_Params() + If IsSaved 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 Me.IsSaved Then - Me.Save_Params() + If Not IsSaved Then + Save_Params() End If - Me.Close() + Close() End Sub Private Sub Pref_Gui_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown Try - Me.IsShowed = False - Tb_Server_IP.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("ServerAddress")) - Tb_Port.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("Port")) - Tb_UPS_Name.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("UPSName")) - Tb_Delay_Com.Text = CStr(Math.Round(WinNUT_Params.Arr_Reg_Key.Item("Delay") / 1000)) - Tb_Login_Nut.Text = WinNUT_Params.Arr_Reg_Key.Item("NutLogin") - Tb_Pwd_Nut.Text = WinNUT_Params.Arr_Reg_Key.Item("NutPassword") - Cb_Reconnect.Checked = WinNUT_Params.Arr_Reg_Key.Item("AutoReconnect") - Tb_InV_Min.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MinInputVoltage")) - Tb_InV_Max.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MaxInputVoltage")) - Cbx_Freq_Input.SelectedIndex = WinNUT_Params.Arr_Reg_Key.Item("FrequencySupply") - Tb_InF_Min.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MinInputFrequency")) - Tb_InF_Max.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MaxInputFrequency")) - Tb_OutV_Min.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MinOutputVoltage")) - Tb_OutV_Max.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MaxOutputVoltage")) - Tb_Load_Min.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MinUPSLoad")) - Tb_Load_Max.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MaxUPSLoad")) - Tb_BattV_Min.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MinBattVoltage")) - Tb_BattV_Max.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("MaxBattVoltage")) - CB_Systray.Checked = WinNUT_Params.Arr_Reg_Key.Item("MinimizeToTray") - CB_Start_Mini.Checked = WinNUT_Params.Arr_Reg_Key.Item("MinimizeOnStart") - CB_Close_Tray.Checked = WinNUT_Params.Arr_Reg_Key.Item("CloseToTray") - CB_Start_W_Win.Checked = WinNUT_Params.Arr_Reg_Key.Item("StartWithWindows") - CB_Use_Logfile.Checked = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") - Cbx_LogLevel.SelectedIndex = WinNUT_Params.Arr_Reg_Key.Item("Log Level") - Tb_BattLimit_Load.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("ShutdownLimitBatteryCharge")) - Tb_BattLimit_Time.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("ShutdownLimitUPSRemainTime")) - Cb_ImmediateStop.Checked = WinNUT_Params.Arr_Reg_Key.Item("ImmediateStopAction") - CB_Follow_FSD.Checked = WinNUT_Params.Arr_Reg_Key.Item("Follow_FSD") - Cbx_TypeStop.SelectedIndex = WinNUT_Params.Arr_Reg_Key.Item("TypeOfStop") - Tb_Delay_Stop.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("DelayToShutdown")) - Cb_ExtendTime.Checked = WinNUT_Params.Arr_Reg_Key.Item("AllowExtendedShutdownDelay") - Tb_GraceTime.Text = CStr(WinNUT_Params.Arr_Reg_Key.Item("ExtendedShutdownDelay")) - Cb_Verify_Update.Checked = WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdate") - Cb_Update_At_Start.Checked = WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdateAtStart") - Cbx_Delay_Verif.SelectedIndex = WinNUT_Params.Arr_Reg_Key.Item("DelayBetweenEachVerification") - Cbx_Branch_Update.SelectedIndex = WinNUT_Params.Arr_Reg_Key.Item("StableOrDevBranch") + IsShowed = False + Tb_Server_IP.Text = CStr(Arr_Reg_Key.Item("ServerAddress")) + Tb_Port.Text = CStr(Arr_Reg_Key.Item("Port")) + Tb_UPS_Name.Text = CStr(Arr_Reg_Key.Item("UPSName")) + Tb_Delay_Com.Text = CStr(Math.Round(Arr_Reg_Key.Item("Delay") / 1000)) + Tb_Login_Nut.Text = Arr_Reg_Key.Item("NutLogin") + Tb_Pwd_Nut.Text = Arr_Reg_Key.Item("NutPassword") + Cb_Reconnect.Checked = Arr_Reg_Key.Item("AutoReconnect") + Tb_InV_Min.Text = CStr(Arr_Reg_Key.Item("MinInputVoltage")) + Tb_InV_Max.Text = CStr(Arr_Reg_Key.Item("MaxInputVoltage")) + Cbx_Freq_Input.SelectedIndex = Arr_Reg_Key.Item("FrequencySupply") + Tb_InF_Min.Text = CStr(Arr_Reg_Key.Item("MinInputFrequency")) + Tb_InF_Max.Text = CStr(Arr_Reg_Key.Item("MaxInputFrequency")) + Tb_OutV_Min.Text = CStr(Arr_Reg_Key.Item("MinOutputVoltage")) + Tb_OutV_Max.Text = CStr(Arr_Reg_Key.Item("MaxOutputVoltage")) + Tb_Load_Min.Text = CStr(Arr_Reg_Key.Item("MinUPSLoad")) + Tb_Load_Max.Text = CStr(Arr_Reg_Key.Item("MaxUPSLoad")) + Tb_BattV_Min.Text = CStr(Arr_Reg_Key.Item("MinBattVoltage")) + Tb_BattV_Max.Text = CStr(Arr_Reg_Key.Item("MaxBattVoltage")) + CB_Systray.Checked = Arr_Reg_Key.Item("MinimizeToTray") + CB_Start_Mini.Checked = Arr_Reg_Key.Item("MinimizeOnStart") + CB_Close_Tray.Checked = Arr_Reg_Key.Item("CloseToTray") + CB_Start_W_Win.Checked = Arr_Reg_Key.Item("StartWithWindows") + CB_Use_Logfile.Checked = Arr_Reg_Key.Item("UseLogFile") + Cbx_LogLevel.SelectedIndex = Arr_Reg_Key.Item("Log Level") + Tb_BattLimit_Load.Text = CStr(Arr_Reg_Key.Item("ShutdownLimitBatteryCharge")) + Tb_BattLimit_Time.Text = CStr(Arr_Reg_Key.Item("ShutdownLimitUPSRemainTime")) + Cb_ImmediateStop.Checked = Arr_Reg_Key.Item("ImmediateStopAction") + CB_Follow_FSD.Checked = Arr_Reg_Key.Item("Follow_FSD") + Cbx_TypeStop.SelectedIndex = Arr_Reg_Key.Item("TypeOfStop") + Tb_Delay_Stop.Text = CStr(Arr_Reg_Key.Item("DelayToShutdown")) + Cb_ExtendTime.Checked = Arr_Reg_Key.Item("AllowExtendedShutdownDelay") + Tb_GraceTime.Text = CStr(Arr_Reg_Key.Item("ExtendedShutdownDelay")) + Cb_Verify_Update.Checked = Arr_Reg_Key.Item("VerifyUpdate") + Cb_Update_At_Start.Checked = Arr_Reg_Key.Item("VerifyUpdateAtStart") + Cbx_Delay_Verif.SelectedIndex = Arr_Reg_Key.Item("DelayBetweenEachVerification") + Cbx_Branch_Update.SelectedIndex = Arr_Reg_Key.Item("StableOrDevBranch") If CB_Systray.Checked Then CB_Start_Mini.Enabled = True CB_Close_Tray.Enabled = True @@ -166,7 +167,7 @@ Public Class Pref_Gui Cbx_Branch_Update.Enabled = False End If - For Each TabCtrl In Me.TabControl_Options.Controls.OfType(Of TabPage)() + For Each TabCtrl In TabControl_Options.Controls.OfType(Of TabPage)() Dim TBoxes = TabCtrl.Controls.OfType(Of TextBox)() Dim ChkBoxes = TabCtrl.Controls.OfType(Of CheckBox)() Dim CmbBoxes = TabCtrl.Controls.OfType(Of ComboBox)() @@ -180,13 +181,13 @@ Public Class Pref_Gui AddHandler CmbBox.SelectedIndexChanged, AddressOf Event_Ctrl_Value_Changed Next Next - Me.Btn_Apply.Enabled = False - Me.IsShowed = True - WinNUT.LogFile.LogTracing("Pref Gui Opened.", LogLvl.LOG_DEBUG, Me) + Btn_Apply.Enabled = False + IsShowed = True + LogFile.LogTracing("Pref Gui Opened.", LogLvl.LOG_DEBUG, Me) Catch Except As Exception - Me.IsShowed = False - Me.Close() - WinNUT.LogFile.LogTracing("Error on Opening Pref_Gui.", LogLvl.LOG_ERROR, Me) + IsShowed = False + Close() + LogFile.LogTracing("Error on Opening Pref_Gui.", LogLvl.LOG_ERROR, Me) End Try End Sub @@ -231,12 +232,12 @@ Public Class Pref_Gui End Sub Private Sub Number_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Tb_Port.Validating, Tb_OutV_Min.Validating, Tb_OutV_Max.Validating, Tb_Load_Min.Validating, Tb_Load_Max.Validating, Tb_InV_Min.Validating, Tb_InV_Max.Validating, Tb_InF_Min.Validating, Tb_InF_Max.Validating, Tb_GraceTime.Validating, Tb_Delay_Stop.Validating, Tb_Delay_Com.Validating, Tb_BattV_Min.Validating, Tb_BattV_Max.Validating, Tb_BattLimit_Time.Validating, Tb_BattLimit_Load.Validating - If Me.IsShowed Then + If IsShowed Then Dim StrTest As String = sender.Text Dim Result As Object = 0 Dim MinValue, MaxValue As Integer - WinNUT.LogFile.LogTracing(String.Format("Check that the value of {0} for {1} is correct.", sender.Text, sender.Name), LogLvl.LOG_DEBUG, Me) + LogFile.LogTracing(String.Format("Check that the value of {0} for {1} is correct.", sender.Text, sender.Name), LogLvl.LOG_DEBUG, Me) Select Case sender.Name Case "Tb_Delay_Com" MinValue = 0 @@ -265,10 +266,10 @@ Public Class Pref_Gui If Integer.TryParse(sender.Text, Result) Then If (Result >= MinValue And Result <= MaxValue) Then - WinNUT.LogFile.LogTracing(String.Format("Value of {0} for {1} is valid.", Result, sender.Name), LogLvl.LOG_DEBUG, Me) + LogFile.LogTracing(String.Format("Value of {0} for {1} is valid.", Result, sender.Name), LogLvl.LOG_DEBUG, Me) sender.BackColor = Color.White Else - WinNUT.LogFile.LogTracing(String.Format("Value of {0} for {1} is invalid.", Result, sender.Name), LogLvl.LOG_ERROR, Me) + LogFile.LogTracing(String.Format("Value of {0} for {1} is invalid.", Result, sender.Name), LogLvl.LOG_ERROR, Me) e.Cancel = True sender.BackColor = Color.Red End If @@ -279,7 +280,7 @@ Public Class Pref_Gui End If End Sub Private Sub Correct_IP_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Tb_Server_IP.Validating - WinNUT.LogFile.LogTracing("Check that the Nut Host address is valid.", LogLvl.LOG_DEBUG, Me) + LogFile.LogTracing("Check that the Nut Host address is valid.", LogLvl.LOG_DEBUG, Me) Dim Pattern As String Dim StrTest As String = sender.Text Dim Is_Correct As Boolean = False @@ -287,26 +288,26 @@ Public Class Pref_Gui Pattern = "^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$" If System.Text.RegularExpressions.Regex.IsMatch(sender.Text, Pattern) Then Is_Correct = True - WinNUT.LogFile.LogTracing("The Nut Host address is a valid IPV4 address.", LogLvl.LOG_WARNING, Me) + LogFile.LogTracing("The Nut Host address is a valid IPV4 address.", LogLvl.LOG_WARNING, Me) End If 'Test IPV6 Pattern = "^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$" If (System.Text.RegularExpressions.Regex.IsMatch(sender.Text, Pattern) And Not Is_Correct) Then Is_Correct = True - WinNUT.LogFile.LogTracing("The Nut Host address is a valid IPV6 address.", LogLvl.LOG_WARNING, Me) + LogFile.LogTracing("The Nut Host address is a valid IPV6 address.", LogLvl.LOG_WARNING, Me) End If 'Test fqdn Pattern = "^(?:(?!\d+\.|-)[a-zA-Z0-9_\-]{1,63}(? Private Sub SetLogControlsStatus() - If WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") Then ' Directory.Exists(Logger.LogFolder) + If Arr_Reg_Key.Item("UseLogFile") Then ' Directory.Exists(Logger.LogFolder) Btn_ViewLog.Enabled = True Btn_DeleteLog.Enabled = True Else @@ -369,6 +370,6 @@ Public Class Pref_Gui Btn_DeleteLog.Enabled = False End If - WinNUT.LogFile.LogTracing("Setting LogControl statuses.", LogLvl.LOG_DEBUG, Me) + LogFile.LogTracing("Setting LogControl statuses.", LogLvl.LOG_DEBUG, Me) End Sub End Class diff --git a/WinNUT_V2/WinNUT_GUI/Update_Gui.vb b/WinNUT_V2/WinNUT_GUI/Update_Gui.vb index 32c417d..68ba9e8 100644 --- a/WinNUT_V2/WinNUT_GUI/Update_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/Update_Gui.vb @@ -8,17 +8,16 @@ ' 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 AppResxStr = WinNUT_Client_Common.AppResxStr Imports WinNUT_Globals = WinNUT_Client_Common.WinNUT_Globals Public Class Update_Gui + Private LogFile As WinNUT_Client_Common.Logger = WinNUT_Globals.LogFile Private ChangeLogByteSize As Long - Private LogFile As Logger = WinNUT.LogFile Private Const GitApiURL As String = "https://api.github.com/repos/nutdotnet/WinNUT-Client/releases" - Private WithEvents WebC As New System.Net.WebClient + Private WithEvents WebC As New Net.WebClient Private JSONReleaseFile As Object Private sChangeLog As String Private ReadOnly ManualUpdate As Boolean = False @@ -72,17 +71,17 @@ Public Class Update_Gui Case 2 DelayVerif = DateInterval.Month End Select - Dim Today As DateTime = Now + Dim Today As Date = Now Dim Diff As Integer = 1 If WinNUT_Params.Arr_Reg_Key.Item("LastDateVerification") <> "" Then - Dim LastVerif As DateTime = Convert.ToDateTime(WinNUT_Params.Arr_Reg_Key.Item("LastDateVerification")) - Diff = DateAndTime.DateDiff(DelayVerif, LastVerif, Today, FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1) + Dim LastVerif As Date = Convert.ToDateTime(WinNUT_Params.Arr_Reg_Key.Item("LastDateVerification")) + Diff = DateDiff(DelayVerif, LastVerif, Today, FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1) End If If Diff >= 1 Or ManualUpdate Then - System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 - WebC.Headers.Add(System.Net.HttpRequestHeader.Accept, "application/json") - WebC.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 OPR/73.0.3856.344") - WebC.Headers.Add(System.Net.HttpRequestHeader.AcceptLanguage, "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7") + Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls12 + WebC.Headers.Add(Net.HttpRequestHeader.Accept, "application/json") + WebC.Headers.Add(Net.HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 OPR/73.0.3856.344") + WebC.Headers.Add(Net.HttpRequestHeader.AcceptLanguage, "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7") AddHandler WebC.DownloadStringCompleted, AddressOf Changelog_Downloaded WebC.DownloadStringAsync(New Uri(GitApiURL)) Else @@ -209,10 +208,10 @@ Public Class Update_Gui .Size = New Point(280, (.Size.Height * 2)) End With Download_Form.Show() - Dim MSIFile = System.IO.Path.GetTempPath() + "WinNUT_" & Me.NewVersion & "_Setup.msi" + Dim MSIFile = IO.Path.GetTempPath() + "WinNUT_" & Me.NewVersion & "_Setup.msi" Using WebC = New Net.WebClient - System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 + Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls12 AddHandler WebC.DownloadFileCompleted, AddressOf New_Version_Downloaded AddHandler WebC.DownloadProgressChanged, AddressOf Update_DPBar WebC.QueryString.Add("FileName", MSIFile) @@ -236,7 +235,7 @@ Public Class Update_Gui Dim SaveFile As SaveFileDialog = New SaveFileDialog With SaveFile .Filter = "MSI Files (*.msi)|*.msi" - .FileName = System.IO.Path.GetFileName(Filename) + .FileName = IO.Path.GetFileName(Filename) End With If SaveFile.ShowDialog() = DialogResult.OK Then My.Computer.FileSystem.MoveFile(Filename, SaveFile.FileName, True) diff --git a/WinNUT_V2/WinNUT_GUI/WinNUT.vb b/WinNUT_V2/WinNUT_GUI/WinNUT.vb index a9c1904..98d17a2 100644 --- a/WinNUT_V2/WinNUT_GUI/WinNUT.vb +++ b/WinNUT_V2/WinNUT_GUI/WinNUT.vb @@ -13,8 +13,7 @@ Public Class WinNUT #Region "Properties" #End Region - ' Logging - Public Shared WithEvents LogFile As Logger + Private WithEvents LogFile As Logger = WinNUT_Globals.LogFile 'Object for UPS management Public WithEvents UPS_Device As UPS_Device @@ -97,75 +96,73 @@ Public Class WinNUT ' Make sure we have an app directory to write to. ' SetupAppDirectory() - LogFile = New Logger(False, LogLvl.LOG_DEBUG) - AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf SystemEvents_PowerModeChanged AddHandler RequestConnect, AddressOf UPS_Connect - 'Init WinNUT Variables - Init_Globals() - LogFile.LogTracing("Initialisation Globals Variables Complete", LogLvl.LOG_DEBUG, Me) - 'Add Main Gui's Strings - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_OLDINI_RENAMED, My.Resources.Frm_Main_Str_01) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_OLDINI, My.Resources.Frm_Main_Str_02) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_RECONNECT, My.Resources.Frm_Main_Str_03) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_RETRY, My.Resources.Frm_Main_Str_04) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_NOTCONN, My.Resources.Frm_Main_Str_05) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_CONN, My.Resources.Frm_Main_Str_06) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_OL, My.Resources.Frm_Main_Str_07) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_OB, My.Resources.Frm_Main_Str_08) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_LOWBAT, My.Resources.Frm_Main_Str_09) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_BATOK, My.Resources.Frm_Main_Str_10) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_UNKNOWN_UPS, My.Resources.Frm_Main_Str_11) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_LOSTCONNECT, My.Resources.Frm_Main_Str_12) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_INVALIDLOGIN, My.Resources.Frm_Main_Str_13) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_EXITSLEEP, My.Resources.Frm_Main_Str_14) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_GOTOSLEEP, My.Resources.Frm_Main_Str_15) + StrLog.Insert(AppResxStr.STR_MAIN_OLDINI_RENAMED, My.Resources.Frm_Main_Str_01) + StrLog.Insert(AppResxStr.STR_MAIN_OLDINI, My.Resources.Frm_Main_Str_02) + StrLog.Insert(AppResxStr.STR_MAIN_RECONNECT, My.Resources.Frm_Main_Str_03) + StrLog.Insert(AppResxStr.STR_MAIN_RETRY, My.Resources.Frm_Main_Str_04) + StrLog.Insert(AppResxStr.STR_MAIN_NOTCONN, My.Resources.Frm_Main_Str_05) + StrLog.Insert(AppResxStr.STR_MAIN_CONN, My.Resources.Frm_Main_Str_06) + StrLog.Insert(AppResxStr.STR_MAIN_OL, My.Resources.Frm_Main_Str_07) + StrLog.Insert(AppResxStr.STR_MAIN_OB, My.Resources.Frm_Main_Str_08) + StrLog.Insert(AppResxStr.STR_MAIN_LOWBAT, My.Resources.Frm_Main_Str_09) + StrLog.Insert(AppResxStr.STR_MAIN_BATOK, My.Resources.Frm_Main_Str_10) + StrLog.Insert(AppResxStr.STR_MAIN_UNKNOWN_UPS, My.Resources.Frm_Main_Str_11) + StrLog.Insert(AppResxStr.STR_MAIN_LOSTCONNECT, My.Resources.Frm_Main_Str_12) + StrLog.Insert(AppResxStr.STR_MAIN_INVALIDLOGIN, My.Resources.Frm_Main_Str_13) + StrLog.Insert(AppResxStr.STR_MAIN_EXITSLEEP, My.Resources.Frm_Main_Str_14) + StrLog.Insert(AppResxStr.STR_MAIN_GOTOSLEEP, My.Resources.Frm_Main_Str_15) 'Add Update Gui's Strings - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_UP_AVAIL, My.Resources.Frm_Update_Str_01) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_UP_SHOW, My.Resources.Frm_Update_Str_02) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_UP_HIDE, My.Resources.Frm_Update_Str_03) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_UP_UPMSG, My.Resources.Frm_Update_Str_04) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_UP_DOWNFROM, My.Resources.Frm_Update_Str_05) + StrLog.Insert(AppResxStr.STR_UP_AVAIL, My.Resources.Frm_Update_Str_01) + StrLog.Insert(AppResxStr.STR_UP_SHOW, My.Resources.Frm_Update_Str_02) + StrLog.Insert(AppResxStr.STR_UP_HIDE, My.Resources.Frm_Update_Str_03) + StrLog.Insert(AppResxStr.STR_UP_UPMSG, My.Resources.Frm_Update_Str_04) + StrLog.Insert(AppResxStr.STR_UP_DOWNFROM, My.Resources.Frm_Update_Str_05) 'Add Shutdown Gui's Strings - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_SHUT_STAT, My.Resources.Frm_Shutdown_Str_01) + StrLog.Insert(AppResxStr.STR_SHUT_STAT, My.Resources.Frm_Shutdown_Str_01) 'Add App Event's Strings - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_APP_SHUT, My.Resources.App_Event_Str_01) + StrLog.Insert(AppResxStr.STR_APP_SHUT, My.Resources.App_Event_Str_01) 'Add Log's Strings - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_PREFS, My.Resources.Log_Str_01) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_CONNECTED, My.Resources.Log_Str_02) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_CON_FAILED, My.Resources.Log_Str_03) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_CON_RETRY, My.Resources.Log_Str_04) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_LOGOFF, My.Resources.Log_Str_05) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_NEW_RETRY, My.Resources.Log_Str_06) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_STOP_RETRY, My.Resources.Log_Str_07) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_SHUT_START, My.Resources.Log_Str_08) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_SHUT_STOP, My.Resources.Log_Str_09) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_NO_UPDATE, My.Resources.Log_Str_10) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_UPDATE, My.Resources.Log_Str_11) - WinNUT_Globals.StrLog.Insert(AppResxStr.STR_LOG_NUT_FSD, My.Resources.Log_Str_12) + StrLog.Insert(AppResxStr.STR_LOG_PREFS, My.Resources.Log_Str_01) + StrLog.Insert(AppResxStr.STR_LOG_CONNECTED, My.Resources.Log_Str_02) + StrLog.Insert(AppResxStr.STR_LOG_CON_FAILED, My.Resources.Log_Str_03) + StrLog.Insert(AppResxStr.STR_LOG_CON_RETRY, My.Resources.Log_Str_04) + StrLog.Insert(AppResxStr.STR_LOG_LOGOFF, My.Resources.Log_Str_05) + StrLog.Insert(AppResxStr.STR_LOG_NEW_RETRY, My.Resources.Log_Str_06) + StrLog.Insert(AppResxStr.STR_LOG_STOP_RETRY, My.Resources.Log_Str_07) + StrLog.Insert(AppResxStr.STR_LOG_SHUT_START, My.Resources.Log_Str_08) + StrLog.Insert(AppResxStr.STR_LOG_SHUT_STOP, My.Resources.Log_Str_09) + StrLog.Insert(AppResxStr.STR_LOG_NO_UPDATE, My.Resources.Log_Str_10) + StrLog.Insert(AppResxStr.STR_LOG_UPDATE, My.Resources.Log_Str_11) + StrLog.Insert(AppResxStr.STR_LOG_NUT_FSD, My.Resources.Log_Str_12) 'Init WinNUT Parameters - WinNUT_Params.Init_Params() + Init_Params() LogFile.LogTracing("Initialisation Params Complete", LogLvl.LOG_DEBUG, Me) 'Load WinNUT Parameters - WinNUT_Params.Load_Params() + Load_Params() + WinNUT_PrefsChanged() LogFile.LogTracing("Loaded Params Complete", LogLvl.LOG_DEBUG, Me) ' Setup logging preferences - LogFile.LogLevel = Arr_Reg_Key.Item("Log Level") - LogFile.IsWritingToFile = Arr_Reg_Key.Item("UseLogFile") + ' LogFile.LogLevel = Arr_Reg_Key.Item("Log Level") + ' LogFile.IsWritingToFile = Arr_Reg_Key.Item("UseLogFile") + 'If Arr_Reg_Key.Item("UseLogFile") Then + ' LogFile.InitializeLogFile() + 'End If - LogFile.LogTracing("Logging is configured.", LogLvl.LOG_DEBUG, Me) + 'LogFile.LogTracing("Logging is configured.", LogLvl.LOG_DEBUG, Me) 'Init Systray - NotifyIcon.Text = WinNUT_Globals.LongProgramName & " - " & WinNUT_Globals.ShortProgramVersion + NotifyIcon.Text = LongProgramName & " - " & ShortProgramVersion NotifyIcon.Visible = False LogFile.LogTracing("NotifyIcons Initialised", LogLvl.LOG_DEBUG, Me) @@ -173,7 +170,7 @@ Public Class WinNUT If MinOsVersionToast.CompareTo(WindowsVersion) < 0 Then AllowToast = True - ToastPopup.ToastHeader = WinNUT_Globals.ProgramName & " - " & WinNUT_Globals.ShortProgramVersion + ToastPopup.ToastHeader = ProgramName & " - " & ShortProgramVersion LogFile.LogTracing("Windows 10 Toast Notification Available", LogLvl.LOG_DEBUG, Me) 'Dim ico As Icon = Me.Icon 'Dim file As System.IO.FileStream = New System.IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client\WinNut.ico", System.IO.FileMode.OpenOrCreate) @@ -192,22 +189,22 @@ Public Class WinNUT 'Add DialGraph With AG_InV .Location = New Point(6, 26) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxInputVoltage") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinInputVoltage") + .MaxValue = Arr_Reg_Key.Item("MaxInputVoltage") + .MinValue = Arr_Reg_Key.Item("MinInputVoltage") .Value1 = UPS_InputV .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) End With With AG_InF .Location = New Point(6, 26) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxInputFrequency") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinInputFrequency") + .MaxValue = Arr_Reg_Key.Item("MaxInputFrequency") + .MinValue = Arr_Reg_Key.Item("MinInputFrequency") .Value1 = UPS_InputF .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) End With With AG_OutV .Location = New Point(6, 26) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxOutputVoltage") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinOutputVoltage") + .MaxValue = Arr_Reg_Key.Item("MaxOutputVoltage") + .MinValue = Arr_Reg_Key.Item("MinOutputVoltage") .Value1 = UPS_OutputV .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) End With @@ -220,16 +217,16 @@ Public Class WinNUT End With With AG_Load .Location = New Point(6, 26) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxUPSLoad") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinUPSLoad") + .MaxValue = Arr_Reg_Key.Item("MaxUPSLoad") + .MinValue = Arr_Reg_Key.Item("MinUPSLoad") .Value1 = UPS_Load .Value2 = UPS_OutPower .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) End With With AG_BattV .Location = New Point(6, 26) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxBattVoltage") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinBattVoltage") + .MaxValue = Arr_Reg_Key.Item("MaxBattVoltage") + .MinValue = Arr_Reg_Key.Item("MinBattVoltage") .Value1 = UPS_BattV .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) End With @@ -275,7 +272,7 @@ Public Class WinNUT ' Start_Tray_Icon = Nothing 'Run Update - If WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdate") = True And WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdateAtStart") = True Then + If Arr_Reg_Key.Item("VerifyUpdate") = True And Arr_Reg_Key.Item("VerifyUpdateAtStart") = True Then LogFile.LogTracing("Run Automatic Update", LogLvl.LOG_DEBUG, Me) Dim Update_Frm = New Update_Gui() Update_Frm.Activate() @@ -289,13 +286,13 @@ Public Class WinNUT Private Sub SystemEvents_PowerModeChanged(ByVal sender As Object, ByVal 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, WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_EXITSLEEP)) - If WinNUT_Params.Arr_Reg_Key.Item("AutoReconnect") = True Then + 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, WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_GOTOSLEEP)) + LogFile.LogTracing("Windows standby, WinNUT will disconnect", LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_MAIN_GOTOSLEEP)) ' UPSDisconnect() UPS_Device.Disconnect() End Select @@ -333,12 +330,12 @@ Public Class WinNUT If Not (UPS_Device.IsConnected And UPS_Device.IsAuthenticated) Then 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(WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_CON_FAILED), upsConf.Host, upsConf.Port, "Connection Error")) + String.Format(StrLog.Item(AppResxStr.STR_LOG_CON_FAILED), upsConf.Host, upsConf.Port, "Connection Error")) ' UPSDisconnect() UPS_Device.Disconnect() Else LogFile.LogTracing("Connection to Nut Host Established", LogLvl.LOG_NOTICE, Me, - String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_CONNECTED), + String.Format(StrLog.Item(AppResxStr.STR_LOG_CONNECTED), upsConf.Host, upsConf.Port)) ' AddHandler Update_Data.Tick, AddressOf Retrieve_UPS_Datas @@ -397,7 +394,7 @@ Public Class WinNUT Private Sub WinNUT_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) UpdateIcon_NotifyIcon() - If WinNUT_Params.Arr_Reg_Key.Item("MinimizeToTray") = True And WinNUT_Params.Arr_Reg_Key.Item("MinimizeOnStart") = True Then + If Arr_Reg_Key.Item("MinimizeToTray") = True And Arr_Reg_Key.Item("MinimizeOnStart") = True Then LogFile.LogTracing("Minimize WinNut On Start", LogLvl.LOG_DEBUG, Me) WindowState = FormWindowState.Minimized NotifyIcon.Visible = True @@ -405,7 +402,7 @@ Public Class WinNUT LogFile.LogTracing("Show WinNut Main Gui", LogLvl.LOG_DEBUG, Me) NotifyIcon.Visible = False End If - If WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdate") = True Then + If Arr_Reg_Key.Item("VerifyUpdate") = True Then Menu_Help_Sep1.Visible = True Menu_Update.Visible = True Menu_Update.Visible = Enabled = True @@ -425,7 +422,7 @@ Public Class WinNUT End Sub Private Sub WinNUT_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing - If WinNUT_Params.Arr_Reg_Key.Item("CloseToTray") = True And WinNUT_Params.Arr_Reg_Key.Item("MinimizeToTray") = True Then + If Arr_Reg_Key.Item("CloseToTray") = True And Arr_Reg_Key.Item("MinimizeToTray") = True Then LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) UpdateIcon_NotifyIcon() LogFile.LogTracing("Minimize Main Gui To Notify Icon", LogLvl.LOG_DEBUG, Me) @@ -481,7 +478,7 @@ Public Class WinNUT Private Sub WinNUT_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize If sender.WindowState = FormWindowState.Minimized Then - If WinNUT_Params.Arr_Reg_Key.Item("MinimizeToTray") = True Then + If Arr_Reg_Key.Item("MinimizeToTray") = True Then LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) UpdateIcon_NotifyIcon() LogFile.LogTracing("Minimize Main Gui To Notify Icon", LogLvl.LOG_DEBUG, Me) @@ -500,7 +497,7 @@ Public Class WinNUT End Sub Private Sub NewRetry_NotifyIcon() Handles UPS_Device.New_Retry - Dim Message As String = String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_RETRY), UPS_Device.Retry, UPS_Device.MaxRetry) + Dim Message As String = String.Format(StrLog.Item(AppResxStr.STR_MAIN_RETRY), UPS_Device.Retry, UPS_Device.MaxRetry) RaiseEvent UpdateNotifyIconStr("Retry", Message) UpdateIcon_NotifyIcon() LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) @@ -513,48 +510,48 @@ Public Class WinNUT Private Sub Event_UpdateNotifyIconStr(ByVal Optional Reason As String = Nothing, ByVal Optional Message As String = Nothing) Handles Me.UpdateNotifyIconStr - Dim ShowVersion As String = WinNUT_Globals.ShortProgramVersion - Dim NotifyStr As String = WinNUT_Globals.ProgramName & " - " & ShowVersion & vbNewLine - Dim FormText As String = WinNUT_Globals.ProgramName + Dim ShowVersion As String = ShortProgramVersion + Dim NotifyStr As String = ProgramName & " - " & ShowVersion & vbNewLine + Dim FormText As String = ProgramName Select Case Reason Case Nothing If (UPS_Device Is Nothing) OrElse Not UPS_Device.IsConnected Then - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) - FormText &= " - " & WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) + FormText &= " - " & StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) End If Case "Retry" - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_RECONNECT) & vbNewLine + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_RECONNECT) & vbNewLine NotifyStr &= Message - FormText &= " - Bat: " & UPS_BattCh & "% - " & WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_RECONNECT) & " - " & Message + FormText &= " - Bat: " & UPS_BattCh & "% - " & StrLog.Item(AppResxStr.STR_MAIN_RECONNECT) & " - " & Message Case "Connected" - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_CONN) - FormText &= " - Bat: " & UPS_BattCh & "% - " & WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_CONN) + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_CONN) + FormText &= " - Bat: " & UPS_BattCh & "% - " & StrLog.Item(AppResxStr.STR_MAIN_CONN) Case "Deconnected" - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) - FormText &= " - " & WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) + FormText &= " - " & StrLog.Item(AppResxStr.STR_MAIN_NOTCONN) Case "Unknown UPS" - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_UNKNOWN_UPS) - FormText &= " - " & WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_UNKNOWN_UPS) + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_UNKNOWN_UPS) + FormText &= " - " & StrLog.Item(AppResxStr.STR_MAIN_UNKNOWN_UPS) Case "Lost Connect" NotifyStr &= String.Format(StrLog.Item(AppResxStr.STR_MAIN_LOSTCONNECT), UPS_Device.Nut_Config.Host, UPS_Device.Nut_Config.Port) FormText &= " - " & String.Format(StrLog.Item(AppResxStr.STR_MAIN_LOSTCONNECT), UPS_Device.Nut_Config.Host, UPS_Device.Nut_Config.Port) Case "Update Data" - FormText &= " - Bat: " & UPS_BattCh & "% - " & WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_CONN) & " - " - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_CONN) & vbNewLine + FormText &= " - Bat: " & UPS_BattCh & "% - " & StrLog.Item(AppResxStr.STR_MAIN_CONN) & " - " + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_CONN) & vbNewLine If UPS_Status.Trim().StartsWith("OL") Or StrReverse(UPS_Status.Trim()).StartsWith("LO") Then - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_OL) & vbNewLine - FormText &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_OL) & " - " + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_OL) & vbNewLine + FormText &= StrLog.Item(AppResxStr.STR_MAIN_OL) & " - " Else - NotifyStr &= String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_OB), UPS_Device.UPS_Datas.UPS_Value.Batt_Charge) & vbNewLine - FormText &= String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_OB), UPS_Device.UPS_Datas.UPS_Value.Batt_Charge) & " - " + NotifyStr &= String.Format(StrLog.Item(AppResxStr.STR_MAIN_OB), UPS_Device.UPS_Datas.UPS_Value.Batt_Charge) & vbNewLine + FormText &= String.Format(StrLog.Item(AppResxStr.STR_MAIN_OB), UPS_Device.UPS_Datas.UPS_Value.Batt_Charge) & " - " End If Select Case UPS_Device.UPS_Datas.UPS_Value.Batt_Charge Case 0 To 40 - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_LOWBAT) - FormText &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_LOWBAT) + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_LOWBAT) + FormText &= StrLog.Item(AppResxStr.STR_MAIN_LOWBAT) Case 41 To 100 - NotifyStr &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_BATOK) - FormText &= WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_BATOK) + NotifyStr &= StrLog.Item(AppResxStr.STR_MAIN_BATOK) + FormText &= StrLog.Item(AppResxStr.STR_MAIN_BATOK) End Select End Select If NotifyStr.Length > 63 Then @@ -564,7 +561,7 @@ Public Class WinNUT If Me.WindowState = System.Windows.Forms.FormWindowState.Minimized And NotifyIcon.Visible = False Then Text = FormText Else - Text = WinNUT_Globals.LongProgramName + Text = LongProgramName End If Me.FormText = FormText @@ -603,7 +600,7 @@ Public Class WinNUT LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me) UpdateIcon_NotifyIcon() RaiseEvent UpdateNotifyIconStr("Unknown UPS", Nothing) - LogFile.LogTracing("Cannot Connect : Unknow UPS Name", LogLvl.LOG_DEBUG, Me, WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_UNKNOWN_UPS)) + LogFile.LogTracing("Cannot Connect : Unknow UPS Name", LogLvl.LOG_DEBUG, Me, StrLog.Item(AppResxStr.STR_MAIN_UNKNOWN_UPS)) Menu_UPS_Var.Enabled = False End Sub @@ -623,7 +620,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(WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_LOSTCONNECT), UPS_Device.Nut_Config.Host, UPS_Device.Nut_Config.Port)) + 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 @@ -802,13 +799,13 @@ Public Class WinNUT Lbl_VName.Text = UPS_Model Lbl_VSerial.Text = UPS_Serial Lbl_VFirmware.Text = UPS_Firmware - AG_InV.Value1 = WinNUT_Params.Arr_Reg_Key.Item("MinInputVoltage") - AG_InF.Value1 = WinNUT_Params.Arr_Reg_Key.Item("MinInputFrequency") - AG_OutV.Value1 = WinNUT_Params.Arr_Reg_Key.Item("MinOutputVoltage") + AG_InV.Value1 = Arr_Reg_Key.Item("MinInputVoltage") + AG_InF.Value1 = Arr_Reg_Key.Item("MinInputFrequency") + AG_OutV.Value1 = Arr_Reg_Key.Item("MinOutputVoltage") AG_BattCh.Value1 = 0 - AG_Load.Value1 = WinNUT_Params.Arr_Reg_Key.Item("MinUPSLoad") + AG_Load.Value1 = Arr_Reg_Key.Item("MinUPSLoad") AG_Load.Value2 = 0 - AG_BattV.Value1 = WinNUT_Params.Arr_Reg_Key.Item("MinBattVoltage") + AG_BattV.Value1 = Arr_Reg_Key.Item("MinBattVoltage") End Sub Private Sub Menu_Reconnect_Click(sender As Object, e As EventArgs) Handles Menu_Reconnect.Click @@ -861,7 +858,15 @@ Public Class WinNUT End Sub Public Sub WinNUT_PrefsChanged() - LogFile.LogTracing("WinNut Preferences Changed", LogLvl.LOG_NOTICE, Me, WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_PREFS)) + ' 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 @@ -901,8 +906,11 @@ Public Class WinNUT ' Automatically reconnect regardless ' UPSDisconnect() - UPS_Device.Disconnect() - UPS_Connect() + If UPS_Device IsNot Nothing Then + UPS_Device.Disconnect() + 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) @@ -920,51 +928,51 @@ Public Class WinNUT 'End If 'NeedReconnect = Nothing With AG_InV - If (.MaxValue <> WinNUT_Params.Arr_Reg_Key.Item("MaxInputVoltage")) Or (.MinValue <> WinNUT_Params.Arr_Reg_Key.Item("MinInputVoltage")) Then + 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) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxInputVoltage") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinInputVoltage") + .MaxValue = Arr_Reg_Key.Item("MaxInputVoltage") + .MinValue = Arr_Reg_Key.Item("MinInputVoltage") .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) LogFile.LogTracing("Parameter Dial Input Voltage Updated", LogLvl.LOG_DEBUG, Me) End If End With With AG_InF - If (.MaxValue <> WinNUT_Params.Arr_Reg_Key.Item("MaxInputFrequency")) Or (.MinValue <> WinNUT_Params.Arr_Reg_Key.Item("MinInputFrequency")) Then + If (.MaxValue <> Arr_Reg_Key.Item("MaxInputFrequency")) Or (.MinValue <> Arr_Reg_Key.Item("MinInputFrequency")) Then LogFile.LogTracing("Parameter Dial Input Frequency Need to be Updated", LogLvl.LOG_DEBUG, Me) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxInputFrequency") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinInputFrequency") + .MaxValue = Arr_Reg_Key.Item("MaxInputFrequency") + .MinValue = Arr_Reg_Key.Item("MinInputFrequency") .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) LogFile.LogTracing("Parameter Dial Input Frequency Updated", LogLvl.LOG_DEBUG, Me) End If End With With AG_OutV - If (.MaxValue <> WinNUT_Params.Arr_Reg_Key.Item("MaxOutputVoltage")) Or (.MinValue <> WinNUT_Params.Arr_Reg_Key.Item("MinOutputVoltage")) Then + If (.MaxValue <> Arr_Reg_Key.Item("MaxOutputVoltage")) Or (.MinValue <> Arr_Reg_Key.Item("MinOutputVoltage")) Then LogFile.LogTracing("Parameter Dial Output Voltage Need to be Updated", LogLvl.LOG_DEBUG, Me) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxOutputVoltage") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinOutputVoltage") + .MaxValue = Arr_Reg_Key.Item("MaxOutputVoltage") + .MinValue = Arr_Reg_Key.Item("MinOutputVoltage") .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) LogFile.LogTracing("Parameter Dial Output Voltage Updated", LogLvl.LOG_DEBUG, Me) End If End With With AG_Load - If (.MaxValue <> WinNUT_Params.Arr_Reg_Key.Item("MaxUPSLoad")) Or (.MinValue <> WinNUT_Params.Arr_Reg_Key.Item("MinUPSLoad")) Then + If (.MaxValue <> Arr_Reg_Key.Item("MaxUPSLoad")) Or (.MinValue <> Arr_Reg_Key.Item("MinUPSLoad")) Then LogFile.LogTracing("Parameter Dial UPS Load Need to be Updated", LogLvl.LOG_DEBUG, Me) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxUPSLoad") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinUPSLoad") + .MaxValue = Arr_Reg_Key.Item("MaxUPSLoad") + .MinValue = Arr_Reg_Key.Item("MinUPSLoad") .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) LogFile.LogTracing("Parameter Dial UPS Load Updated", LogLvl.LOG_DEBUG, Me) End If End With With AG_BattV - If (.MaxValue <> WinNUT_Params.Arr_Reg_Key.Item("MinBattVoltage")) Or (.MinValue <> WinNUT_Params.Arr_Reg_Key.Item("MinBattVoltage")) Then + If (.MaxValue <> Arr_Reg_Key.Item("MinBattVoltage")) Or (.MinValue <> Arr_Reg_Key.Item("MinBattVoltage")) Then LogFile.LogTracing("Parameter Dial Voltage Battery Need to be Updated", LogLvl.LOG_DEBUG, Me) - .MaxValue = WinNUT_Params.Arr_Reg_Key.Item("MaxBattVoltage") - .MinValue = WinNUT_Params.Arr_Reg_Key.Item("MinBattVoltage") + .MaxValue = Arr_Reg_Key.Item("MaxBattVoltage") + .MinValue = Arr_Reg_Key.Item("MinBattVoltage") .ScaleLinesMajorStepValue = CInt((.MaxValue - .MinValue) / 5) LogFile.LogTracing("Parameter Dial Voltage Battery Updated", LogLvl.LOG_DEBUG, Me) End If End With - If WinNUT_Params.Arr_Reg_Key.Item("VerifyUpdate") = True Then + If Arr_Reg_Key.Item("VerifyUpdate") = True Then Menu_Help_Sep1.Visible = True Menu_Update.Visible = True Menu_Update.Visible = Enabled = True @@ -973,6 +981,8 @@ Public Class WinNUT Menu_Update.Visible = False Menu_Update.Visible = Enabled = False End If + + LogFile.LogTracing("WinNut Preferences Applied.", LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_LOG_PREFS)) End Sub Private Sub UpdateIcon_NotifyIcon() @@ -1065,23 +1075,23 @@ Public Class WinNUT HasFocus = False End Sub - Public Shared Sub Update_InstantLog(sender As Object) Handles LogFile.NewData + Public Sub Update_InstantLog(sender As Object) Handles LogFile.NewData Dim Message As String = LogFile.CurrentLogData Static Dim Event_Id = 1 LogFile.LogTracing("New Log to CB_Current Log : " & Message, LogLvl.LOG_DEBUG, sender.ToString) Message = "[Id " & Event_Id & ": " & Format(Now, "General Date") & "] " & Message Event_Id += 1 - WinNUT.CB_CurrentLog.Items.Insert(0, Message) - WinNUT.CB_CurrentLog.SelectedIndex = 0 - If WinNUT.CB_CurrentLog.Items.Count > 10 Then - For i = 10 To (WinNUT.CB_CurrentLog.Items.Count - 1) Step 1 - WinNUT.CB_CurrentLog.Items.Remove(i) + CB_CurrentLog.Items.Insert(0, Message) + CB_CurrentLog.SelectedIndex = 0 + If CB_CurrentLog.Items.Count > 10 Then + For i = 10 To (CB_CurrentLog.Items.Count - 1) Step 1 + CB_CurrentLog.Items.Remove(i) Next End If End Sub Private Sub Shutdown_Event() Handles UPS_Device.Shutdown_Condition - If WinNUT_Params.Arr_Reg_Key.Item("ImmediateStopAction") Then + If Arr_Reg_Key.Item("ImmediateStopAction") Then ' UPSDisconnect() UPS_Device.Disconnect() Shutdown_Action() @@ -1103,7 +1113,7 @@ Public Class WinNUT End Sub Public Sub Shutdown_Action() - Select Case WinNUT_Params.Arr_Reg_Key.Item("TypeOfStop") + Select Case Arr_Reg_Key.Item("TypeOfStop") Case 0 Process.Start("C:\WINDOWS\system32\Shutdown.exe", "-f -s -t 0") Case 1 @@ -1130,7 +1140,7 @@ Public Class WinNUT Dim IniFile As String = "" With SelectIni .Title = "Locate ups.ini" - If System.IO.Directory.Exists("C:\Winnut") Then + If IO.Directory.Exists("C:\Winnut") Then .InitialDirectory = "C:\Winnut\" Else .InitialDirectory = "C:\" @@ -1145,13 +1155,13 @@ Public Class WinNUT End If End With - If WinNUT_Params.ImportIni(IniFile) Then + If ImportIni(IniFile) Then LogFile.LogTracing("Import Old IniFile : Success", LogLvl.LOG_DEBUG, Me) If Not IniFile.EndsWith("old") Then My.Computer.FileSystem.MoveFile(IniFile, IniFile & ".old") - MsgBox(String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_OLDINI_RENAMED), IniFile)) + MsgBox(String.Format(StrLog.Item(AppResxStr.STR_MAIN_OLDINI_RENAMED), IniFile)) Else - MsgBox(String.Format(WinNUT_Globals.StrLog.Item(AppResxStr.STR_MAIN_OLDINI), IniFile)) + MsgBox(String.Format(StrLog.Item(AppResxStr.STR_MAIN_OLDINI), IniFile)) End If Else