Skip to content

Commit fb071ef

Browse files
committed
Updates to UPS data retrieval and parsing
Applying several changes to the `UPS_Device` class: - Made `Retrieve_UPS_Datas` private, and removed leftover commented code - Merged `GetPowerUsage` function into main data retrieval loop since no other code referred to it. - Wrapped power variable parsing into try block to log parsing errors. NUT and other exceptions will continue to be raised. - Reorganized some comments around battery calculations.
1 parent 80896e6 commit fb071ef

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ Public Class UPS_Device
259259

260260
Private oldStatusBitmask As Integer
261261

262-
Public Sub Retrieve_UPS_Datas() Handles Update_Data.Tick ' As UPSData
262+
Private Sub Retrieve_UPS_Datas() Handles Update_Data.Tick
263263
LogFile.LogTracing("Enter Retrieve_UPS_Datas", LogLvl.LOG_DEBUG, Me)
264+
264265
Try
265266
Dim UPS_rt_Status As String
266267

@@ -273,23 +274,51 @@ Public Class UPS_Device
273274
.Input_Voltage = Double.Parse(GetUPSVar("input.voltage", 220), ciClone)
274275
.Output_Voltage = Double.Parse(GetUPSVar("output.voltage", .Input_Voltage), ciClone)
275276
.Load = Double.Parse(GetUPSVar("ups.load", 0), ciClone)
276-
.Output_Power = If(_PowerCalculationMethod <> PowerMethod.Unavailable, GetPowerUsage(), 0)
277277

278+
' Retrieve and/or calculate output power if possible.
279+
If _PowerCalculationMethod <> PowerMethod.Unavailable Then
280+
Dim parsedValue As Double
281+
282+
Try
283+
If _PowerCalculationMethod = PowerMethod.RealPower Then
284+
parsedValue = Double.Parse(GetUPSVar("ups.realpower"))
285+
286+
ElseIf _PowerCalculationMethod = PowerMethod.NominalPowerCalc Then
287+
parsedValue = Double.Parse(GetUPSVar("ups.realpower.nominal"))
288+
parsedValue *= UPS_Datas.UPS_Value.Load / 100.0
289+
290+
ElseIf _PowerCalculationMethod = PowerMethod.VoltAmpCalc Then
291+
Dim nomCurrent = Double.Parse(GetUPSVar("input.current.nominal"))
292+
Dim nomVoltage = Double.Parse(GetUPSVar("input.voltage.nominal"))
293+
294+
parsedValue = (nomCurrent * nomVoltage * 0.8) * (UPS_Datas.UPS_Value.Load / 100.0)
295+
Else
296+
Throw New InvalidOperationException("Insufficient variables to calculate power.")
297+
End If
298+
Catch ex As FormatException
299+
LogFile.LogTracing("Unexpected format trying to parse value from UPS. Exception:", LogLvl.LOG_ERROR, Me)
300+
LogFile.LogTracing(ex.ToString(), LogLvl.LOG_ERROR, Me)
301+
LogFile.LogTracing("parsedValue: " & parsedValue, LogLvl.LOG_ERROR, Me)
302+
End Try
303+
304+
.Output_Power = parsedValue
305+
End If
306+
307+
' Handle cases of UPSs that are unable to report battery runtime or load correctly while on battery.
278308
Dim PowerDivider As Double = 0.5
279309
Select Case .Load
280310
Case 76 To 100
281311
PowerDivider = 0.4
282312
Case 51 To 75
283313
PowerDivider = 0.3
284314
End Select
315+
285316
If .Batt_Charge = 255 Then
286317
Dim nBatt = Math.Floor(.Batt_Voltage / 12)
287318
.Batt_Charge = Math.Floor((.Batt_Voltage - (11.6 * nBatt)) / (0.02 * nBatt))
288319
End If
320+
289321
If .Batt_Runtime >= 86400 Then
290-
'If Load is 0, the calculation results in infinity. This causes an exception in DataUpdated(), causing Me.Disconnect to run in the exception handler below.
291-
'Thus a connection is established, but is forcefully disconneced almost immediately. This cycle repeats on each connect until load is <> 0
292-
'(Example: I have a 0% load if only Pi, Microtik Router, Wifi AP and switches are running)
293322
.Load = If(.Load <> 0, .Load, 0.1)
294323
Dim BattInstantCurrent = (.Output_Voltage * .Load) / (.Batt_Voltage * 100)
295324
.Batt_Runtime = Math.Floor(.Batt_Capacity * 0.6 * .Batt_Charge * (1 - PowerDivider) * 3600 / (BattInstantCurrent * 100))
@@ -327,35 +356,6 @@ Public Class UPS_Device
327356
End Try
328357
End Sub
329358

330-
''' <summary>
331-
''' Attempts to get the power usage of this UPS.
332-
''' </summary>
333-
''' <returns></returns>
334-
''' <throws><see cref="NutException"/></throws>
335-
Private Function GetPowerUsage() As Double
336-
If _PowerCalculationMethod = PowerMethod.RealPower Then
337-
Return Integer.Parse(GetUPSVar("ups.realpower"))
338-
ElseIf _PowerCalculationMethod = PowerMethod.NominalPowerCalc Then
339-
Dim nomPower = GetUPSVar("ups.realpower.nominal")
340-
Try
341-
Return Integer.Parse(nomPower) * (UPS_Datas.UPS_Value.Load / 100.0)
342-
Catch ex As Exception
343-
LogFile.LogTracing("Failed to parse the nominal realpower: " & vbNewLine & ex.ToString() &
344-
vbNewLine & vbNewLine & "nomPower is: " & nomPower & " Type: " &
345-
nomPower.GetType().ToString(), LogLvl.LOG_ERROR, Me)
346-
Return 0
347-
End Try
348-
349-
ElseIf _PowerCalculationMethod = PowerMethod.VoltAmpCalc Then
350-
Dim nomCurrent = Double.Parse(GetUPSVar("input.current.nominal"))
351-
Dim nomVoltage = Double.Parse(GetUPSVar("input.voltage.nominal"))
352-
353-
Return (nomCurrent * nomVoltage * 0.8) * (UPS_Datas.UPS_Value.Load / 100.0)
354-
Else
355-
Throw New InvalidOperationException("Insufficient variables to calculate power.")
356-
End If
357-
End Function
358-
359359
Private Const MAX_VAR_RETRIES = 3
360360
Public Function GetUPSVar(varName As String, Optional Fallback_value As Object = Nothing, Optional recursing As Boolean = False) As String
361361
If Not IsConnected Then

0 commit comments

Comments
 (0)