Skip to content

Commit e52fa65

Browse files
committed
Better handling of DATA-STALE status
Ran into a situation where my UPS was reporting DATA-STALE errors for all variables. I was able to cleanup code a bit to prevent crashes, but this still doesn't communicate the error to the user, and worse still substitutes default values when it shouldn't be. - Cleanup GetUPSVar and fix the DATA-STALE handling code so it runs properly. - Requesting ups.status variable defaults to "None" now.
1 parent 80f2e9e commit e52fa65

File tree

1 file changed

+28
-48
lines changed

1 file changed

+28
-48
lines changed

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ Public Class UPS_Device
320320
.Input_Voltage = Double.Parse(GetUPSVar("input.voltage", 220), ciClone)
321321
.Output_Voltage = Double.Parse(GetUPSVar("output.voltage", .Input_Voltage), ciClone)
322322
.Load = Double.Parse(GetUPSVar("ups.load", 100), ciClone)
323-
UPS_rt_Status = GetUPSVar("ups.status")
323+
UPS_rt_Status = GetUPSVar("ups.status", UPS_States.None)
324324
.Output_Power = Double.Parse((GetUPSVar("ups.realpower.nominal", 0)), ciClone)
325325
If .Output_Power = 0 Then
326326
.Output_Power = Double.Parse((GetUPSVar("ups.power.nominal", 0)), ciClone)
@@ -387,31 +387,28 @@ Public Class UPS_Device
387387
End Sub
388388

389389
Private Const MAX_VAR_RETRIES = 3
390-
Public Function GetUPSVar(ByVal varName As String, Optional ByVal Fallback_value As Object = Nothing, Optional recursing As Boolean = False) As String
391-
' Try
392-
' LogFile.LogTracing("Enter GetUPSVar", LogLvl.LOG_DEBUG, Me)
393-
'If Not Me.ConnectionStatus Then
390+
Public Function GetUPSVar(varName As String, Optional Fallback_value As Object = Nothing, Optional recursing As Boolean = False) As String
394391
If Not IsConnected Then
395-
' Throw New NutException(Nut_Exception_Value.SOCKET_BROKEN, varName)
396392
Throw New InvalidOperationException("Tried to GetUPSVar while disconnected.")
397-
' Return Nothing
398393
Else
399394
Dim Nut_Query As Transaction
395+
400396
Try
401397
Nut_Query = Nut_Socket.Query_Data("GET VAR " & Name & " " & varName)
402398

403-
Select Case Nut_Query.ResponseType
404-
Case NUTResponse.OK
405-
' LogFile.LogTracing("Process Result With " & varName & " : " & Nut_Query.Data, LogLvl.LOG_DEBUG, Me)
406-
Return ExtractData(Nut_Query.RawResponse)
407-
' Case NUTResponse.UNKNOWNUPS
408-
'Me.Invalid_Data = False
409-
'Me.Unknown_UPS_Name = True
410-
' RaiseEvent Unknown_UPS()
411-
' Throw New Nut_Exception(Nut_Exception_Value.UNKNOWN_UPS, "The UPS does not exist on the server.")
412-
' upsd won't provide any value for the var, in case of false reading.
399+
If Nut_Query.ResponseType = NUTResponse.OK Then
400+
Return ExtractData(Nut_Query.RawResponse)
401+
Else
402+
Throw New NutException(Nut_Query)
403+
End If
404+
405+
Catch ex As NutException
406+
Select Case ex.LastTransaction.ResponseType
407+
Case NUTResponse.VARNOTSUPPORTED
408+
LogFile.LogTracing(varName & " is not supported by server.", LogLvl.LOG_WARNING, Me)
409+
413410
Case NUTResponse.DATASTALE
414-
LogFile.LogTracing("DATA-STALE Error Result On Retrieving " & varName & " : " & Nut_Query.RawResponse, LogLvl.LOG_ERROR, Me)
411+
LogFile.LogTracing("DATA-STALE Error Result On Retrieving " & varName & " : " & ex.LastTransaction.RawResponse, LogLvl.LOG_ERROR, Me)
415412

416413
If recursing Then
417414
Return Nothing
@@ -422,42 +419,25 @@ Public Class UPS_Device
422419
While returnString Is Nothing AndAlso retryNum <= MAX_VAR_RETRIES
423420
LogFile.LogTracing("Attempting retry " & retryNum & " to get variable.", LogLvl.LOG_NOTICE, Me)
424421
returnString = GetUPSVar(varName, Fallback_value, True)
425-
Return returnString
422+
retryNum += 1
426423
End While
427424

428-
RaiseEvent EncounteredNUTException(New NutException(Nut_Query), Me)
429-
Return Fallback_value
425+
If returnString IsNot Nothing Then
426+
Return returnString
427+
End If
430428
End If
431-
432-
' Throw New System.Exception(varName & " : " & Nut_Query.Data)
433-
Throw New NutException(Nut_Query)
434-
435-
' Return NUTResponse.DATASTALE
436-
Case Else
437-
Throw New NutException(Nut_Query)
438-
' Return Nothing
439429
End Select
440-
Catch ex As NutException
441-
If ex.LastTransaction.ResponseType = NUTResponse.VARNOTSUPPORTED Then
442-
'Me.Unknown_UPS_Name = False
443-
'Me.Invalid_Data = False
444-
445-
If Not String.IsNullOrEmpty(Fallback_value) Then
446-
LogFile.LogTracing("Apply Fallback Value when retrieving " & varName, LogLvl.LOG_WARNING, Me)
447-
'Dim FakeData = "VAR " & Name & " " & varName & " " & """" & Fallback_value & """"
448-
'Return ExtractData(FakeData)
449-
Return Fallback_value
450-
Else
451-
' Var is unknown and caller was not prepared to handle it, pass exception along.
452-
Throw
453-
End If
430+
431+
If Not String.IsNullOrEmpty(Fallback_value) Then
432+
LogFile.LogTracing("Apply Fallback Value when retrieving " & varName, LogLvl.LOG_WARNING, Me)
433+
Return Fallback_value
434+
Else
435+
LogFile.LogTracing("Unhandled error while getting " & varName, LogLvl.LOG_ERROR, Me)
436+
Throw
454437
End If
455438
End Try
456439
End If
457-
'Catch Excep As Exception
458-
' 'RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me)
459-
' Return Nothing
460-
'End Try
440+
461441
Return Nothing
462442
End Function
463443

@@ -483,7 +463,7 @@ Public Class UPS_Device
483463
Return Response
484464
End Function
485465

486-
Private Function ExtractData(ByVal Var_Data As String) As String
466+
Private Function ExtractData(Var_Data As String) As String
487467
Dim SanitisedVar As String
488468
Dim StringArray(Nothing) As String
489469
Try

0 commit comments

Comments
 (0)