@@ -15,6 +15,19 @@ Imports System.IO
1515Imports System.Windows.Forms
1616
1717Public Class Nut_Socket
18+ # Region "Properties"
19+ Public ReadOnly Property ConnectionStatus
20+ Get
21+ If NutSocket IsNot Nothing Then
22+ Return NutSocket.Connected
23+ Else
24+ Return False
25+ End If
26+ End Get
27+ End Property
28+ # End Region
29+
30+ Private LogFile As Logger
1831 'Socket Variables
1932 Private NutSocket As Socket
2033 Private NutTCP As TcpClient
@@ -27,8 +40,6 @@ Public Class Nut_Socket
2740 Private Nut_Ver As String
2841 Private Net_Ver As String
2942
30- Private ConnectionStatus As Boolean = False
31-
3243 Private Nut_Config As Nut_Parameter
3344
3445 Public Auth_Success As Boolean = False
@@ -44,8 +55,8 @@ Public Class Nut_Socket
4455 Public Event Socket_Broken()
4556 Public Event Socket_Deconnected()
4657
47- Public Sub New ( ByVal Nut_Config As Nut_Parameter)
48-
58+ Public Sub New ( ByVal Nut_Config As Nut_Parameter, ByRef logger As Logger )
59+ LogFile = logger
4960 With Me .WatchDog
5061 .Interval = 1000
5162 .Enabled = False
@@ -88,6 +99,7 @@ Public Class Nut_Socket
8899 Public Sub Update_Config( ByVal New_Nut_Config As Nut_Parameter)
89100 Me .Nut_Config = New_Nut_Config
90101 End Sub
102+
91103 Public Function Connect() As Boolean
92104 Try
93105 'TODO: Use LIST UPS protocol command to get valid UPSs.
@@ -97,24 +109,26 @@ Public Class Nut_Socket
97109 Dim Password = Nut_Config.Password
98110
99111 If Not String .IsNullOrEmpty(Host) And Not IsNothing(Port) Then
100- If Not Create_Socket(Host, Port) Then
101- ' Don't duplicate/override Create_Socket's error throwing functionality.
102- ' Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR)
103- Disconnect( )
104- Else
105- If Not AuthLogin(Login, Password) Then
106- Throw New Nut_Exception(Nut_Exception_Value.INVALID_AUTH_DATA )
107- End If
112+ Create_Socket(Host, Port)
113+
114+ If ConnectionStatus Then
115+ AuthLogin(Login, Password )
116+ 'If Not AuthLogin(Login, Password) Then
117+ ' ' Throw New Nut_Exception(Nut_Exception_Value.INVALID_AUTH_DATA)
118+ ' RaiseEvent OnNUTException( )
119+ ' End If
108120 Dim Nut_Query = Query_Data( "VER" )
109121
110122 If Nut_Query.Response = NUTResponse.OK Then
111- Me . Nut_Ver = (Nut_Query.Data.Split( " "c ))( 4 )
123+ Nut_Ver = (Nut_Query.Data.Split( " "c ))( 4 )
112124 End If
113125 Nut_Query = Query_Data( "NETVER" )
114126
115127 If Nut_Query.Response = NUTResponse.OK Then
116- Me . Net_Ver = Nut_Query.Data
128+ Net_Ver = Nut_Query.Data
117129 End If
130+
131+ LogFile.LogTracing( String .Format( "NUT server reports VER: {0} NETVER: {1}" , Nut_Ver, Net_Ver), LogLvl.LOG_NOTICE, Me )
118132 Return True
119133 End If
120134 End If
@@ -131,21 +145,23 @@ Public Class Nut_Socket
131145 Return False
132146 End Function
133147
134- Private Function Create_Socket( ByVal Host As String , ByVal Port As Integer ) As Boolean
148+ Private Sub Create_Socket( ByVal Host As String , ByVal Port As Integer )
135149 Try
136150 ' NutSocket = New Socket(AddressFamily.InterNetwork, ProtocolType.IP)
137151 NutSocket = New Socket(SocketType.Stream, ProtocolType.IP)
152+ LogFile.LogTracing( String .Format( "Attempting TCP socket connection to {0}:{1}..." , Host, Port), LogLvl.LOG_NOTICE, Me )
138153 NutTCP = New TcpClient(Host, Port)
139154 NutStream = NutTCP.GetStream
140155 ReaderStream = New StreamReader(NutStream)
141156 WriterStream = New StreamWriter(NutStream)
142- ConnectionStatus = True
157+ ' ConnectionStatus = True
158+ LogFile.LogTracing( String .Format( "Connection established and streams ready for {0}:{1}" , Host, Port), LogLvl.LOG_NOTICE, Me )
143159 Catch Excep As Exception
160+ Disconnect( True )
144161 RaiseEvent OnNUTException( New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me )
145- Me .ConnectionStatus = False
146162 End Try
147- Return Me .ConnectionStatus
148- End Function
163+ End Sub
164+
149165 Public Function Query_Data(Query_Msg As String ) As (Data As String , Response As NUTResponse)
150166 Dim Response As NUTResponse = NUTResponse.NORESPONSE
151167 Dim DataResult As String = ""
@@ -338,9 +354,11 @@ Public Class Nut_Socket
338354 End Select
339355 Return Response
340356 End Function
341- Private Function AuthLogin( ByVal Login As String , ByVal Password As String ) As Boolean
357+
358+ Private Sub AuthLogin(Login As String , Password As String )
342359 Try
343- Me .Auth_Success = False
360+ LogFile.LogTracing( "Attempting authentication..." , LogLvl.LOG_NOTICE, Me )
361+ Auth_Success = False
344362 If Not String .IsNullOrEmpty(Login) AndAlso String .IsNullOrEmpty(Password) Then
345363 Dim Nut_Query = Query_Data( "USERNAME " & Login)
346364
@@ -366,23 +384,27 @@ Public Class Nut_Socket
366384 End If
367385 End If
368386 End If
369- Me .Auth_Success = True
370- Return Me .Auth_Success
387+
388+ LogFile.LogTracing( "Authenticated successfully." , LogLvl.LOG_NOTICE, Me )
389+ Auth_Success = True
390+ ' Return Me.Auth_Success
391+ Catch nutEx As Nut_Exception
392+ ' Auth issues aren't necessarily fatal - some interaction can still occur unauthenticated.
393+ RaiseEvent OnNUTException(nutEx, LogLvl.LOG_ERROR, Me )
394+ ' Auth_Success = False
395+ ' Disconnect(True)
371396 Catch Excep As Exception
372397 RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me )
373- Me . Disconnect()
374- Return False
398+ Disconnect( True )
399+ ' Auth_Success = False
375400 End Try
376- End Function
401+ End Sub
377402
378403 Private Sub Event_WatchDog(sender As Object , e As EventArgs)
379404 Dim Nut_Query = Query_Data( "" )
380405 If Nut_Query.Response = NUTResponse.NORESPONSE Then
381- Me .ConnectionStatus = False
382406 Disconnect( True )
383407 RaiseEvent Socket_Broken()
384- ElseIf Nut_Query.Response = NUTResponse.UNKNOWNCOMMAND Then
385- Me .ConnectionStatus = True
386408 End If
387409 End Sub
388410
@@ -408,10 +430,15 @@ Public Class Nut_Socket
408430 NutSocket.Close()
409431 End If
410432 Catch Excep As Exception
433+ LogFile.LogTracing( "Error encountered while shutting down socket: " & vbNewLine & Excep.ToString(),
434+ LogLvl.LOG_ERROR, Me )
411435 End Try
412- Me .ConnectionStatus = False
413436 End Sub
414437
438+ ''' <summary>
439+ ''' Gracefully disconnect the socket from the NUT server.
440+ ''' </summary>
441+ ''' <param name="ForceDisconnect">Skip raising the <see cref="Socket_Deconnected"/> event if true.</param>
415442 Public Sub Disconnect( Optional ByVal ForceDisconnect = False )
416443 Query_Data( "LOGOUT" )
417444 Close_Socket()
0 commit comments