Skip to content

Commit 645e06b

Browse files
committed
Connection, watchdog and data logic changes
- Disabled building installer in Debug mode - Disabled WatchDog functionality throughout program, since regular data updating acts as a watchdog in and of its self. - Modified Nut_Exception to carry the Nut_Exception_Value as a property - Slightly reduced some Logging spam, and added a whole lot more. Socket: - Reorganized some functions/subroutines - Removed some fatal exceptions when a bad authentication occurs, since basic operations can still occur. - Removed exception handling from Query_Data so that exceptions are handled further up the stack. UPS_Device: - Connection events provide the UPS_Device as sender - Data polling is now handled inside the class rather than in the Form, and the Form must access the data from the UPS_Device object rather than getting its own copy. - Added Disconnect subroutine to handle shutting down a connection, from the UPS_Device. - Added subroutine to handle NUT exceptions thrown from the socket. Not sure what to do with these yet. WinNUT: - Relocated data updating responsibility to UPS_Device - Any attempts to disconnect now go through the UPS_Device - Created a new subroutine UPSReady that fires when the UPS indicated it's ready to start receiving data.
1 parent a5be907 commit 645e06b

File tree

5 files changed

+477
-363
lines changed

5 files changed

+477
-363
lines changed

WinNUT_V2/WinNUT-Client_Common/Common_Classes.vb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ End Class
3535
Public Class Nut_Exception
3636
Inherits System.ApplicationException
3737

38+
Public Property ExceptionValue As Nut_Exception_Value
39+
3840
Public Sub New(ByVal Nut_Error_Lvl As Nut_Exception_Value)
3941
MyBase.New(StringEnum.GetStringValue(Nut_Error_Lvl))
4042
End Sub
4143

42-
Public Sub New(ByVal Nut_Error_Lvl As Nut_Exception_Value, ByVal Message As String)
43-
MyBase.New(StringEnum.GetStringValue(Nut_Error_Lvl) & Message)
44+
Public Sub New(ByVal Nut_Error_Lvl As Nut_Exception_Value, ByVal Message As String, Optional innerEx As Exception = Nothing)
45+
MyBase.New(StringEnum.GetStringValue(Nut_Error_Lvl) & Message, innerEx)
46+
ExceptionValue = Nut_Error_Lvl
4447
End Sub
4548
End Class
4649

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
' Class dealing only with the management of the communication socket with the Nut server
1313
Imports System.Net.Sockets
1414
Imports System.IO
15-
Imports System.Windows.Forms
1615

1716
Public 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

Comments
 (0)