@@ -15,13 +15,17 @@ Public Class UPS_Device
15
15
16
16
Public ReadOnly Property Name As String
17
17
Get
18
- Return Nut_Config.UPSName
18
+ If Nut_Config IsNot Nothing Then
19
+ Return Nut_Config.UPSName
20
+ Else
21
+ Return "null"
22
+ End If
19
23
End Get
20
24
End Property
21
25
22
26
Public ReadOnly Property IsConnected As Boolean
23
27
Get
24
- Return (Nut_Socket.IsConnected) ' And Me.Socket_Status
28
+ Return (Nut_Socket.IsConnected)
25
29
End Get
26
30
End Property
27
31
@@ -74,11 +78,18 @@ Public Class UPS_Device
74
78
Public Event Lost_Connect()
75
79
' Error encountered when trying to connect.
76
80
Public Event ConnectionError(sender As UPS_Device, innerException As Exception)
77
- Public Event EncounteredNUTException(ex As NutException, sender As Object )
78
81
Public Event New_Retry()
79
82
' Public Event Shutdown_Condition()
80
83
' Public Event Stop_Shutdown()
81
84
85
+ ''' <summary>
86
+ ''' Raised when the NUT server returns an error during normal communication and is deemed important for the client
87
+ ''' application to know.
88
+ ''' </summary>
89
+ ''' <param name="sender">The device object that has received the error.</param>
90
+ ''' <param name="nutEx">An exception detailing the error and cirucmstances surrounding it.</param>
91
+ Public Event EncounteredNUTException(sender As UPS_Device, nutEx As NutException)
92
+
82
93
# End Region
83
94
84
95
Private Const CosPhi As Double = 0.6
@@ -124,13 +135,7 @@ Public Class UPS_Device
124
135
With Reconnect_Nut
125
136
.Interval = DEFAULT_RECONNECT_WAIT_MS
126
137
.Enabled = False
127
- ' AddHandler .Tick, AddressOf Reconnect_Socket
128
138
End With
129
- 'With WatchDog
130
- ' .Interval = 1000
131
- ' .Enabled = False
132
- ' AddHandler .Tick, AddressOf Event_WatchDog
133
- 'End With
134
139
End Sub
135
140
136
141
Public Sub Connect_UPS( Optional retryOnConnFailure = False )
@@ -144,7 +149,7 @@ Public Class UPS_Device
144
149
RaiseEvent Connected( Me )
145
150
Catch ex As NutException
146
151
' This is how we determine if we have a valid UPS name entered, among other errors.
147
- RaiseEvent EncounteredNUTException(ex, Me )
152
+ RaiseEvent EncounteredNUTException( Me , ex )
148
153
149
154
Catch ex As Exception
150
155
RaiseEvent ConnectionError( Me , ex)
@@ -156,33 +161,23 @@ Public Class UPS_Device
156
161
End Try
157
162
End Sub
158
163
159
- 'Private Sub HandleDisconnectRequest(sender As Object, Optional cancelAutoReconnect As Boolean = True) Handles Me.RequestDisconnect
160
- ' If disconnectInProgress Then
161
- ' Throw New InvalidOperationException("Disconnection already in progress.")
162
- ' End If
163
-
164
- ' ' WatchDog.Stop()
165
-
166
- ' If cancelAutoReconnect And Reconnect_Nut.Enabled = True Then
167
- ' Debug.WriteLine("Cancelling ")
168
- ' End If
169
- 'End Sub
170
-
171
- Public Sub Disconnect( Optional cancelReconnect As Boolean = True , Optional silent As Boolean = False , Optional forceful As Boolean = False )
164
+ Public Sub Disconnect( Optional cancelReconnect As Boolean = True , Optional forceful As Boolean = False )
172
165
LogFile.LogTracing( "Processing request to disconnect..." , LogLvl.LOG_DEBUG, Me )
173
- ' WatchDog.Stop()
166
+
174
167
Update_Data.Stop()
175
168
If cancelReconnect And Reconnect_Nut.Enabled Then
176
169
LogFile.LogTracing( "Stopping Reconnect timer." , LogLvl.LOG_DEBUG, Me )
177
170
Reconnect_Nut.Stop()
178
171
End If
179
172
180
173
Retry = 0
181
- Nut_Socket.Disconnect(silent, forceful)
182
- ' Confirmation of disconnection will come from raised Disconnected event.
183
-
184
- 'LogFile.LogTracing("Completed disconnecting UPS, notifying listeners.", LogLvl.LOG_DEBUG, Me)
185
- 'RaiseEvent Disconnected()
174
+ Try
175
+ Nut_Socket.Disconnect(forceful)
176
+ Catch nutEx As NutException
177
+ RaiseEvent EncounteredNUTException( Me , nutEx)
178
+ Finally
179
+ RaiseEvent Disconnected()
180
+ End Try
186
181
End Sub
187
182
188
183
# Region "Socket Interaction"
@@ -193,29 +188,8 @@ Public Class UPS_Device
193
188
RaiseEvent Disconnected()
194
189
End Sub
195
190
196
- ''' <summary>
197
- ''' Check underlying connection for an error state by sending an empty query to the server.
198
- ''' A watchdog may not actually be necessary under normal circumstances, since queries are regularly being sent to
199
- ''' the NUT server and will catch a broken socket that way.
200
- ''' </summary>
201
- ''' <param name="sender"></param>
202
- ''' <param name="e"></param>
203
- Private Sub Event_WatchDog(sender As Object , e As EventArgs)
204
- If IsConnected Then
205
- Dim Nut_Query = Nut_Socket.Query_Data( "" )
206
- If Nut_Query.ResponseType = NUTResponse.NORESPONSE Then
207
- LogFile.LogTracing( "WatchDog Socket report a Broken State" , LogLvl.LOG_WARNING, Me )
208
- Nut_Socket.Disconnect( True )
209
- RaiseEvent Lost_Connect()
210
- ' Me.Socket_Status = False
211
- End If
212
- End If
213
- End Sub
214
-
215
191
Private Sub Socket_Broken() Handles Nut_Socket.Socket_Broken
216
- ' LogFile.LogTracing("TCP Socket seems Broken", LogLvl.LOG_WARNING, Me)
217
192
LogFile.LogTracing( "Socket has reported a Broken event." , LogLvl.LOG_WARNING, Me )
218
- ' SocketDisconnected()
219
193
RaiseEvent Lost_Connect()
220
194
221
195
If Nut_Config.AutoReconnect Then
@@ -228,7 +202,7 @@ Public Class UPS_Device
228
202
Retry += 1
229
203
If Retry <= MaxRetry Then
230
204
RaiseEvent New_Retry()
231
- LogFile.LogTracing( String .Format( "Try Reconnect {0} / {1}" , Retry, MaxRetry), LogLvl.LOG_NOTICE, Me , String .Format(WinNUT_Globals. StrLog.Item(AppResxStr.STR_LOG_NEW_RETRY), Retry, MaxRetry))
205
+ LogFile.LogTracing( String .Format( "Try Reconnect {0} / {1}" , Retry, MaxRetry), LogLvl.LOG_NOTICE, Me , String .Format(StrLog.Item(AppResxStr.STR_LOG_NEW_RETRY), Retry, MaxRetry))
232
206
Connect_UPS()
233
207
If IsConnected Then
234
208
LogFile.LogTracing( "Nut Host Reconnected" , LogLvl.LOG_DEBUG, Me )
@@ -237,9 +211,7 @@ Public Class UPS_Device
237
211
RaiseEvent ReConnected( Me )
238
212
End If
239
213
Else
240
- LogFile.LogTracing( "Max Retry reached. Stop Process Autoreconnect and wait for manual Reconnection" , LogLvl.LOG_ERROR, Me , WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_STOP_RETRY))
241
- 'Reconnect_Nut.Stop()
242
- 'RaiseEvent Disconnected()
214
+ LogFile.LogTracing( "Max Retry reached. Stop Process Autoreconnect and wait for manual Reconnection" , LogLvl.LOG_ERROR, Me , StrLog.Item(AppResxStr.STR_LOG_STOP_RETRY))
243
215
Disconnect( True )
244
216
End If
245
217
End Sub
@@ -259,7 +231,7 @@ Public Class UPS_Device
259
231
260
232
' Other constant values for UPS calibration.
261
233
freshData.UPS_Value.Batt_Capacity = Double .Parse(GetUPSVar( "battery.capacity" , 7 ), ciClone)
262
- Freq_Fallback = Double .Parse(GetUPSVar( "output.frequency.nominal" , ( 50 + CInt (WinNUT_Params. Arr_Reg_Key.Item( "FrequencySupply" )) * 10 )), ciClone)
234
+ Freq_Fallback = Double .Parse(GetUPSVar( "output.frequency.nominal" , ( 50 + CInt (Arr_Reg_Key.Item( "FrequencySupply" )) * 10 )), ciClone)
263
235
264
236
Return freshData
265
237
End Function
@@ -340,10 +312,8 @@ Public Class UPS_Device
340
312
Catch Excep As Exception
341
313
' Something went wrong while trying to read the data... Consider the socket broken and proceed from here.
342
314
LogFile.LogTracing( "Something went wrong in Retrieve_UPS_Datas: " & Excep.ToString(), LogLvl.LOG_ERROR, Me )
343
- Disconnect( False , True , True )
315
+ Disconnect( False , True )
344
316
Socket_Broken()
345
- 'Me.Disconnect(True)
346
- 'Enter_Reconnect_Process(Excep, "Error When Retrieve_UPS_Data : ")
347
317
End Try
348
318
End Sub
349
319
@@ -435,4 +405,8 @@ Public Class UPS_Device
435
405
End Try
436
406
Return StringArray(StringArray.Length - 1 )
437
407
End Function
408
+
409
+ Public Overrides Function ToString() As String
410
+ Return Name
411
+ End Function
438
412
End Class
0 commit comments