1212' Class dealing only with the management of the communication socket with the Nut server
1313Imports System.Net.Sockets
1414Imports System.IO
15- Imports System.Windows.Forms
1615
1716Public Class Nut_Socket
1817
@@ -28,24 +27,41 @@ Public Class Nut_Socket
2827 End Get
2928 End Property
3029
30+ Public ReadOnly Property IsConnected() As Boolean
31+ Get
32+ Return ConnectionStatus
33+ End Get
34+ End Property
35+
36+ Public ReadOnly Property Nut_Version() As String
37+ Get
38+ Return Nut_Ver
39+ End Get
40+ End Property
41+
42+ Public ReadOnly Property Net_Version() As String
43+ Get
44+ Return Net_Ver
45+ End Get
46+ End Property
47+
3148# End Region
3249
3350 Private LogFile As Logger
51+ Private NutConfig As Nut_Parameter
52+
3453 'Socket Variables
3554 Private NutSocket As Socket
3655 Private NutTCP As TcpClient
3756 Private NutStream As NetworkStream
3857 Private ReaderStream As StreamReader
3958 Private WriterStream As StreamWriter
4059
41- Private Nut_Parameter As Nut_Parameter
42- Private NutConfig As Nut_Parameter
43-
4460 Private Nut_Ver As String
4561 Private Net_Ver As String
4662
4763 Public Auth_Success As Boolean = False
48- Private ReadOnly WatchDog As New Timer
64+ ' Private ReadOnly WatchDog As New Timer
4965
5066 'Public Event OnNotice(Message As String, NoticeLvl As LogLvl, sender As Object, ReportToGui As Boolean)
5167 Public Event OnNotice(Message As String , NoticeLvl As LogLvl, sender As Object )
@@ -65,47 +81,12 @@ Public Class Nut_Socket
6581 LogFile = logger
6682 NutConfig = Nut_Config
6783
68- With Me .WatchDog
69- .Interval = 1000
70- .Enabled = False
71- AddHandler .Tick, AddressOf Event_WatchDog
72- End With
84+ ' With Me.WatchDog
85+ ' .Interval = 1000
86+ ' .Enabled = False
87+ ' AddHandler .Tick, AddressOf Event_WatchDog
88+ ' End With
7389 End Sub
74- Public ReadOnly Property IsConnected() As Boolean
75- Get
76- Return Me .ConnectionStatus
77- End Get
78- End Property
79- Public ReadOnly Property Nut_Version() As String
80- Get
81- Return Me .Nut_Ver
82- End Get
83- End Property
84- Public ReadOnly Property Net_Version() As String
85- Get
86- Return Me .Net_Ver
87- End Get
88- End Property
89-
90- Public ReadOnly Property IsKnownUPS(Test_UPSname As String ) As Boolean
91- Get
92- If Not Me .ConnectionStatus Then
93- Return False
94- Else
95- Dim IsKnow As Boolean = False
96- Dim ListOfUPSs = Query_List_Datas( "LIST UPS" )
97- For Each Known_UPS In ListOfUPSs
98- If Known_UPS.VarValue = Test_UPSname Then
99- IsKnow = True
100- End If
101- Next
102- Return IsKnow
103- End If
104- End Get
105- End Property
106- 'Public Sub Update_Config(ByVal New_Nut_Config As Nut_Parameter)
107- ' Me.NutConfig = New_Nut_Config
108- 'End Sub
10990
11091 Public Function Connect() As Boolean
11192 Try
@@ -120,6 +101,7 @@ Public Class Nut_Socket
120101
121102 If ConnectionStatus Then
122103 AuthLogin(Login, Password)
104+ ' Bad login shouldn't necessarily preclude a successful connection (basic ops can still occur.)
123105 'If Not AuthLogin(Login, Password) Then
124106 ' ' Throw New Nut_Exception(Nut_Exception_Value.INVALID_AUTH_DATA)
125107 ' RaiseEvent OnNUTException()
@@ -153,17 +135,25 @@ Public Class Nut_Socket
153135 End Function
154136
155137 ''' <summary>
156- ''' Gracefully disconnect the socket from the NUT server.
138+ ''' Perform various functions necessary to disconnect the socket from the NUT server.
157139 ''' </summary>
158140 ''' <param name="Silent">Skip raising the <see cref="SocketDisconnected"/> event if true.</param>
159- Public Sub Disconnect( Optional Silent = False )
160- WatchDog.Stop()
161- Query_Data( "LOGOUT" )
141+ ''' <param name="Forceful">Skip sending the LOGOUT command to the NUT server. Unknown effects.</param>
142+ Public Sub Disconnect( Optional silent = False , Optional forceful = False )
143+ ' WatchDog.Stop()
144+
145+ If Not forceful Then
146+ Query_Data( "LOGOUT" )
147+ End If
148+
162149 Close_Socket()
163150
164- If Not Silent Then
151+ If Not silent Then
152+ LogFile.LogTracing( "NutSocket raising Disconnected event." , LogLvl.LOG_DEBUG, Me )
165153 RaiseEvent SocketDisconnected()
166154 End If
155+
156+ LogFile.LogTracing( "NutSocket has been Disconnected." , LogLvl.LOG_DEBUG, Me )
167157 End Sub
168158
169159 Private Sub Create_Socket(Host As String , Port As Integer )
@@ -183,27 +173,53 @@ Public Class Nut_Socket
183173 End Try
184174 End Sub
185175
176+ Public ReadOnly Property IsKnownUPS(Test_UPSname As String ) As Boolean
177+ Get
178+ If Not ConnectionStatus Then
179+ Return False
180+ Else
181+ Dim IsKnow As Boolean = False
182+ Dim ListOfUPSs = Query_List_Datas( "LIST UPS" )
183+ For Each Known_UPS In ListOfUPSs
184+ If Known_UPS.VarValue = Test_UPSname Then
185+ IsKnow = True
186+ End If
187+ Next
188+ Return IsKnow
189+ End If
190+ End Get
191+ End Property
192+
186193 Public Function Query_Data(Query_Msg As String ) As (Data As String , Response As NUTResponse)
187194 Dim Response As NUTResponse = NUTResponse.NORESPONSE
188195 Dim DataResult As String = ""
189- Try
190- If Me .ConnectionStatus Then
191- Me .WriterStream.WriteLine(Query_Msg & vbCr)
192- Me .WriterStream.Flush()
193- DataResult = Trim( Me .ReaderStream.ReadLine())
194-
195- If DataResult = Nothing Then
196- Disconnect()
197- RaiseEvent Socket_Broken()
198- Throw New Nut_Exception(Nut_Exception_Value.SOCKET_BROKEN, Query_Msg )
199- End If
200- Response = EnumResponse(DataResult )
196+ ' Try
197+ If Me .ConnectionStatus Then
198+ Me .WriterStream.WriteLine(Query_Msg & vbCr)
199+ Me .WriterStream.Flush()
200+ DataResult = Trim( Me .ReaderStream.ReadLine())
201+
202+ If DataResult = Nothing Then
203+ ' TODO: Does null dataresult really mean an error condition?
204+ ' https://stackoverflow.com/a/6523010/530172
205+ Disconnect( )
206+ RaiseEvent Socket_Broken()
207+ Throw New Nut_Exception(Nut_Exception_Value.SOCKET_BROKEN, Query_Msg )
201208 End If
202- Return (DataResult, Response)
203- Catch Excep As Exception
204- RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me )
205- Return (DataResult, Response)
206- End Try
209+ Response = EnumResponse(DataResult)
210+ End If
211+ Return (DataResult, Response)
212+ 'Catch ioEx As IOException
213+ ' ' Something's gone wrong with the connection.
214+ ' ' Try to safely shut everything down and notify listeners.
215+ ' ' Disconnect()
216+ ' RaiseEvent Socket_Broken()
217+ ' RaiseEvent OnNUTException(New Nut_Exception(Nut_Exception_Value.SOCKET_BROKEN, Query_Msg, ioEx), LogLvl.LOG_ERROR, Me)
218+ ' ' Return (Nothing, NUTResponse.NORESPONSE)
219+ 'Catch Excep As Exception
220+ ' RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me)
221+ ' Return (DataResult, Response)
222+ 'End Try
207223 End Function
208224 Public Function Query_Desc( ByVal VarName As String ) As String
209225 Try
0 commit comments