@@ -259,8 +259,9 @@ Public Class UPS_Device
259
259
260
260
Private oldStatusBitmask As Integer
261
261
262
- Public Sub Retrieve_UPS_Datas() Handles Update_Data.Tick ' As UPSData
262
+ Private Sub Retrieve_UPS_Datas() Handles Update_Data.Tick
263
263
LogFile.LogTracing( "Enter Retrieve_UPS_Datas" , LogLvl.LOG_DEBUG, Me )
264
+
264
265
Try
265
266
Dim UPS_rt_Status As String
266
267
@@ -273,23 +274,51 @@ Public Class UPS_Device
273
274
.Input_Voltage = Double .Parse(GetUPSVar( "input.voltage" , 220 ), ciClone)
274
275
.Output_Voltage = Double .Parse(GetUPSVar( "output.voltage" , .Input_Voltage), ciClone)
275
276
.Load = Double .Parse(GetUPSVar( "ups.load" , 0 ), ciClone)
276
- .Output_Power = If (_PowerCalculationMethod <> PowerMethod.Unavailable, GetPowerUsage(), 0 )
277
277
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.
278
308
Dim PowerDivider As Double = 0.5
279
309
Select Case .Load
280
310
Case 76 To 100
281
311
PowerDivider = 0.4
282
312
Case 51 To 75
283
313
PowerDivider = 0.3
284
314
End Select
315
+
285
316
If .Batt_Charge = 255 Then
286
317
Dim nBatt = Math.Floor(.Batt_Voltage / 12 )
287
318
.Batt_Charge = Math.Floor((.Batt_Voltage - ( 11.6 * nBatt)) / ( 0 . 02 * nBatt))
288
319
End If
320
+
289
321
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)
293
322
.Load = If (.Load <> 0 , .Load, 0 . 1 )
294
323
Dim BattInstantCurrent = (.Output_Voltage * .Load) / (.Batt_Voltage * 100 )
295
324
.Batt_Runtime = Math.Floor(.Batt_Capacity * 0.6 * .Batt_Charge * ( 1 - PowerDivider) * 3600 / (BattInstantCurrent * 100 ))
@@ -327,35 +356,6 @@ Public Class UPS_Device
327
356
End Try
328
357
End Sub
329
358
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
-
359
359
Private Const MAX_VAR_RETRIES = 3
360
360
Public Function GetUPSVar(varName As String , Optional Fallback_value As Object = Nothing , Optional recursing As Boolean = False ) As String
361
361
If Not IsConnected Then
0 commit comments