Skip to content

Commit 52dd4fb

Browse files
authored
Merge pull request #215 from leongurman/feature/multi-params-for-GetUpsVar
Enhanced GetUPSVar to support variable name arrays
2 parents a9d474d + 127ae87 commit 52dd4fb

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ Public Class UPS_Device
230230
LogFile.LogTracing("Retrieving basic UPS product information...", LogLvl.LOG_NOTICE, Me)
231231

232232
Dim freshData = New UPSData(
233-
Trim(GetUPSVar("ups.mfr", "Unknown")),
234-
Trim(GetUPSVar("ups.model", "Unknown")),
235-
Trim(GetUPSVar("ups.serial", "Unknown")),
233+
Trim(GetUPSVar({"ups.mfr", "device.mfr"}, "Unknown")),
234+
Trim(GetUPSVar({"ups.model", "device.model"}, "Unknown")),
235+
Trim(GetUPSVar({"ups.serial", "device.serial"}, "Unknown")),
236236
Trim(GetUPSVar("ups.firmware", "Unknown")))
237237

238238
With freshData.UPS_Value
@@ -434,16 +434,21 @@ Public Class UPS_Device
434434
End Sub
435435

436436
Private Const MAX_VAR_RETRIES = 3
437-
Public Function GetUPSVar(varName As String, Optional Fallback_value As Object = Nothing, Optional recursing As Boolean = False) As String
437+
Public Function GetUPSVar(varNames As String(), Optional Fallback_value As Object = Nothing, Optional recursing As Boolean = False) As String
438438
If Not IsConnected Then
439439
Throw New InvalidOperationException("Tried to GetUPSVar while disconnected.")
440-
Else
441-
Dim Nut_Query As Transaction
440+
End If
442441

442+
' Try each variable in the array sequentially
443+
For Each varName As String In varNames
443444
Try
445+
LogFile.LogTracing("Trying variable: " & varName, LogLvl.LOG_DEBUG, Me)
446+
447+
Dim Nut_Query As Transaction
444448
Nut_Query = Nut_Socket.Query_Data("GET VAR " & Name & " " & varName)
445449

446450
If Nut_Query.ResponseType = NUTResponse.OK Then
451+
LogFile.LogTracing("Success with " & varName, LogLvl.LOG_DEBUG, Me)
447452
Return ExtractData(Nut_Query.RawResponse)
448453
Else
449454
Throw New NutException(Nut_Query)
@@ -452,39 +457,56 @@ Public Class UPS_Device
452457
Catch ex As NutException
453458
Select Case ex.LastTransaction.ResponseType
454459
Case NUTResponse.VARNOTSUPPORTED
455-
LogFile.LogTracing(varName & " is not supported by server.", LogLvl.LOG_WARNING, Me)
460+
LogFile.LogTracing(varName & " is not supported by server, trying next", LogLvl.LOG_WARNING, Me)
461+
' Continue to next variable
462+
Continue For
456463

457464
Case NUTResponse.DATASTALE
458-
LogFile.LogTracing("DATA-STALE Error Result On Retrieving " & varName & " : " & ex.LastTransaction.RawResponse, LogLvl.LOG_ERROR, Me)
459-
465+
LogFile.LogTracing("DATA-STALE Error Result On Retrieving " & varName & " : " & ex.LastTransaction.RawResponse, LogLvl.LOG_ERROR, Me)
460466
If recursing Then
461-
Return Nothing
467+
' Continue to next variable instead of returning Nothing
468+
Continue For
462469
Else
463470
Dim retryNum = 1
464471
Dim returnString As String = Nothing
465-
466472
While returnString Is Nothing AndAlso retryNum <= MAX_VAR_RETRIES
467-
LogFile.LogTracing("Attempting retry " & retryNum & " to get variable.", LogLvl.LOG_NOTICE, Me)
468-
returnString = GetUPSVar(varName, Fallback_value, True)
473+
LogFile.LogTracing("Attempting retry " & retryNum & " to get variable " & varName, LogLvl.LOG_NOTICE, Me)
474+
returnString = GetUPSVar({varName}, Fallback_value, True)
469475
retryNum += 1
470476
End While
471-
472477
If returnString IsNot Nothing Then
473478
Return returnString
479+
Else
480+
' Retry failed, continue to next variable
481+
Continue For
474482
End If
475483
End If
476-
End Select
477484

478-
If Not String.IsNullOrEmpty(Fallback_value) Then
479-
LogFile.LogTracing("Apply Fallback Value when retrieving " & varName, LogLvl.LOG_WARNING, Me)
480-
Return Fallback_value
481-
Else
482-
Throw
483-
End If
485+
Case Else
486+
LogFile.LogTracing("Error with " & varName & ", trying next", LogLvl.LOG_WARNING, Me)
487+
' Continue to next variable
488+
Continue For
489+
End Select
490+
Catch ex As Exception
491+
LogFile.LogTracing("Exception for variable " & varName & ": " & ex.Message & ", trying next", LogLvl.LOG_WARNING, Me)
492+
' Continue to next variable
493+
Continue For
484494
End Try
495+
Next
496+
497+
' If we reach here, all variables failed
498+
If Not String.IsNullOrEmpty(Fallback_value) Then
499+
LogFile.LogTracing("All variables failed, applying fallback value", LogLvl.LOG_WARNING, Me)
500+
Return Fallback_value
501+
Else
502+
LogFile.LogTracing("All variables failed and no fallback provided", LogLvl.LOG_ERROR, Me)
503+
Throw New NutException("All variables failed and no fallback provided", NUTResponse.VARNOTSUPPORTED, Nothing)
485504
End If
505+
End Function
486506

487-
Return Nothing
507+
' Overload for backward compatibility with existing code
508+
Public Function GetUPSVar(varName As String, Optional Fallback_value As Object = Nothing, Optional recursing As Boolean = False) As String
509+
Return GetUPSVar({varName}, Fallback_value, recursing)
488510
End Function
489511

490512
Public Function GetUPS_ListVar() As List(Of UPS_List_Datas)

0 commit comments

Comments
 (0)