diff --git a/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb b/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb index 177961b..009d828 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb @@ -16,7 +16,6 @@ Imports System.IO Public Class Nut_Socket #Region "Properties" - Public ReadOnly Property ConnectionStatus As Boolean Get If NutSocket IsNot Nothing Then @@ -33,6 +32,13 @@ Public Class Nut_Socket End Get End Property + Private _isLoggedIn As Boolean = False + Public ReadOnly Property IsLoggedIn() As Boolean + Get + Return _isLoggedIn + End Get + End Property + Private Nut_Ver As String Public ReadOnly Property Nut_Version() As String Get @@ -46,7 +52,6 @@ Public Class Nut_Socket Return Net_Ver End Get End Property - #End Region Private LogFile As Logger @@ -64,10 +69,6 @@ Public Class Nut_Socket ''' Private streamInUse As Boolean - - Public Auth_Success As Boolean = False - ' Private ReadOnly WatchDog As New Timer - Public Event Socket_Broken(ex As NutException) ''' @@ -115,7 +116,12 @@ Public Class Nut_Socket End Try If ConnectionStatus Then - AuthLogin(Login, Password) + Try + AuthLogin(Login, Password) + Catch ex As NutException + ' TODO: Make friendly message string for user. + LogFile.LogTracing("Error while attempting to log in: " & ex.Message, LogLvl.LOG_ERROR, Me) + End Try Dim Nut_Query = Query_Data("VER") @@ -132,6 +138,32 @@ Public Class Nut_Socket End If End Sub + ''' + ''' Register with the UPSd server as being dependant on it for power. + ''' + ''' + ''' + ''' A protocol error was encountered while trying to authenticate. + Private Sub AuthLogin(Login As String, Password As String) + If _isLoggedIn Then + Throw New InvalidOperationException("Attempted to login when already logged in.") + End If + + LogFile.LogTracing("Attempting authentication...", LogLvl.LOG_NOTICE, Me) + + If Not String.IsNullOrEmpty(Login) Then + Query_Data("USERNAME " & Login) + + If Not String.IsNullOrEmpty(Password) Then + Query_Data("PASSWORD " & Password) + End If + End If + + Query_Data("LOGIN") + _isLoggedIn = True + LogFile.LogTracing("Authenticated successfully.", LogLvl.LOG_NOTICE, Me) + End Sub + ''' ''' Perform various functions necessary to disconnect the socket from the NUT server. ''' @@ -356,27 +388,6 @@ Public Class Nut_Socket End If End Function - Private Sub AuthLogin(Login As String, Password As String) - LogFile.LogTracing("Attempting authentication...", LogLvl.LOG_NOTICE, Me) - Auth_Success = False - If Not String.IsNullOrEmpty(Login) AndAlso String.IsNullOrEmpty(Password) Then - Dim Nut_Query = Query_Data("USERNAME " & Login) - - If Nut_Query.ResponseType <> NUTResponse.OK Then - Throw New NutException(Nut_Query) - End If - - Nut_Query = Query_Data("PASSWORD " & Password) - - If Nut_Query.ResponseType <> NUTResponse.OK Then - Throw New NutException(Nut_Query) - End If - End If - - LogFile.LogTracing("Authenticated successfully.", LogLvl.LOG_NOTICE, Me) - Auth_Success = True - End Sub - Private Sub Event_WatchDog(sender As Object, e As EventArgs) Dim Nut_Query = Query_Data("") If Nut_Query.ResponseType = NUTResponse.NORESPONSE Then diff --git a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb index 9c8bc6f..47c6f50 100644 --- a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb +++ b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb @@ -27,7 +27,7 @@ Public Class UPS_Device Public ReadOnly Property IsAuthenticated As Boolean Get - Return Nut_Socket.Auth_Success + Return Nut_Socket.IsLoggedIn End Get End Property @@ -61,6 +61,26 @@ Public Class UPS_Device End Property #End Region + +#Region "Events" + + ' Public Event Unknown_UPS() + Public Event DataUpdated() + Public Event Connected(sender As UPS_Device) + Public Event ReConnected(sender As UPS_Device) + ' Notify that the connection was closed gracefully. + Public Event Disconnected() + ' Notify of an unexpectedly lost connection (??) + Public Event Lost_Connect() + ' Error encountered when trying to connect. + Public Event ConnectionError(sender As UPS_Device, innerException As Exception) + Public Event EncounteredNUTException(ex As NutException, sender As Object) + Public Event New_Retry() + ' Public Event Shutdown_Condition() + ' Public Event Stop_Shutdown() + +#End Region + Private Const CosPhi As Double = 0.6 ' How many milliseconds to wait before the Reconnect routine tries again. Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 5000 @@ -81,73 +101,11 @@ Public Class UPS_Device Public Retry As Integer = 0 Public MaxRetry As Integer = 30 Private WithEvents Reconnect_Nut As New System.Windows.Forms.Timer - ' Private ReadOnly WatchDog As New System.Windows.Forms.Timer - ' Private Socket_Status As Boolean = False Private LogFile As Logger - 'Private ConnectionStatus As Boolean = False - 'Private Server As String - 'Private Port As Integer - 'Private UPSName As String - 'Private Delay As Integer - 'Private Login As String - 'Private Password As String - 'Private Mfr As String - 'Private Model As String - 'Private Serial As String - 'Private Firmware As String - 'Private BattCh As Double - 'Private BattV As Double - 'Private BattRuntime As Double - 'Private BattCapacity As Double - 'Private PowerFreq As Double - 'Private InputV As Double - 'Private OutputV As Double - 'Private Load As Double - 'Private Status As String - 'Private OutPower As Double - 'Private InputA As Double - 'Private Low_Batt As Integer - 'Private Low_Backup As Integer - 'Private LConnect As Boolean = False - 'Private AReconnect As Boolean = False - 'Private MaxRetry As Integer = 30 - 'Private Retry As Integer = 0 - 'Private ErrorStatus As Boolean = False - 'Private ErrorMsg As String = "" - 'Private Update_Nut As New System.Windows.Forms.Timer - 'Private Reconnect_Nut As New System.Windows.Forms.Timer - 'Private NutSocket As System.Net.Sockets.Socket - 'Private NutTCP As System.Net.Sockets.TcpClient - 'Private NutStream As System.Net.Sockets.NetworkStream - 'Private ReaderStream As System.IO.StreamReader - 'Private WriterStream As System.IO.StreamWriter - 'Private Follow_FSD As Boolean = False - 'Private Unknown_UPS_Name As Boolean = False - 'Private Invalid_Data As Boolean = False - 'Private Invalid_Auth_Data As Boolean = False - -#Region "Properties" - -#End Region - - ' Public Event Unknown_UPS() - Public Event DataUpdated() - Public Event Connected(sender As UPS_Device) - Public Event ReConnected(sender As UPS_Device) - ' Notify that the connection was closed gracefully. - Public Event Disconnected() - ' Notify of an unexpectedly lost connection (??) - Public Event Lost_Connect() - ' Error encountered when trying to connect. - Public Event ConnectionError(sender As UPS_Device, innerException As Exception) - Public Event EncounteredNUTException(ex As NutException, sender As Object) - Public Event New_Retry() - ' Public Event Shutdown_Condition() - ' Public Event Stop_Shutdown() ''' ''' Raise an event when a status code is added to the UPS that wasn't there before.