@@ -13,7 +13,7 @@ Imports System.Windows.Forms
13
13
Public Class UPS_Device
14
14
# Region "Statics/Defaults"
15
15
Private ReadOnly INVARIANT_CULTURE = CultureInfo.InvariantCulture
16
- Private Const CosPhi As Double = 0.6
16
+ Private Const POWER_FACTOR = 0.8
17
17
18
18
' How many milliseconds to wait before the Reconnect routine tries again.
19
19
Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 5000
@@ -238,27 +238,57 @@ Public Class UPS_Device
238
238
Trim(GetUPSVar( "ups.serial" , "Unknown" )),
239
239
Trim(GetUPSVar( "ups.firmware" , "Unknown" )))
240
240
241
+ With freshData.UPS_Value
242
+ LogFile.LogTracing( "Initializing other well-known UPS variables..." , LogLvl.LOG_DEBUG, Me )
243
+ Try
244
+ Dim value = Single .Parse(GetUPSVar( "output.current" ), INVARIANT_CULTURE)
245
+ .Output_Current = value
246
+ LogFile.LogTracing( "output.current: " & value, LogLvl.LOG_DEBUG, Me )
247
+ Catch ex As Exception
248
+ If ex.GetType() <> GetType (NutException) Then
249
+ LogFile.LogException(ex, Me )
250
+ End If
251
+ End Try
252
+ Try
253
+ Dim value = Single .Parse(GetUPSVar( "output.voltage" ), INVARIANT_CULTURE)
254
+ .Output_Voltage = value
255
+ LogFile.LogTracing( "output.voltage: " & value, LogLvl.LOG_DEBUG, Me )
256
+ Catch ex As Exception
257
+ If ex.GetType() <> GetType (NutException) Then
258
+ LogFile.LogException(ex, Me )
259
+ End If
260
+ End Try
261
+ End With
262
+
263
+ ' Determine optimal method for measuring power output from the UPS.
241
264
LogFile.LogTracing( "Determining best method to calculate power usage..." , LogLvl.LOG_NOTICE, Me )
265
+ ' Start with directly reading a variable from the UPS.
242
266
Try
243
267
GetUPSVar( "ups.realpower" )
244
268
_PowerCalculationMethod = PowerMethod.RealPower
245
- LogFile.LogTracing( "Using RealPower method to calculate power usage ." , LogLvl.LOG_NOTICE, Me )
269
+ LogFile.LogTracing( "Using RealPower method." , LogLvl.LOG_NOTICE, Me )
246
270
Catch
247
271
Try
248
272
GetUPSVar( "ups.realpower.nominal" )
249
273
GetUPSVar( "ups.load" )
250
- _PowerCalculationMethod = PowerMethod.NominalPowerCalc
251
- LogFile.LogTracing( "Using NominalPowerCalc method to calculate power usage ." , LogLvl.LOG_NOTICE, Me )
274
+ _PowerCalculationMethod = PowerMethod.RPNomLoadPct
275
+ LogFile.LogTracing( "Using RPNomLoadPct method." , LogLvl.LOG_NOTICE, Me )
252
276
Catch
253
277
Try
254
278
GetUPSVar( "input.current.nominal" )
255
279
GetUPSVar( "input.voltage.nominal" )
256
280
GetUPSVar( "ups.load" )
257
- _PowerCalculationMethod = PowerMethod.VoltAmpCalc
258
- LogFile.LogTracing( "Using VoltAmpCalc method to calculate power usage ." , LogLvl.LOG_NOTICE, Me )
281
+ _PowerCalculationMethod = PowerMethod.InputNomVALoadPct
282
+ LogFile.LogTracing( "Using InputNomVALoadPct method." , LogLvl.LOG_NOTICE, Me )
259
283
Catch
260
- _PowerCalculationMethod = PowerMethod.Unavailable
261
- LogFile.LogTracing( "Unable to find a suitable method to calculate power usage." , LogLvl.LOG_WARNING, Me )
284
+ If freshData.UPS_Value.Output_Current IsNot Nothing AndAlso
285
+ freshData.UPS_Value.Output_Voltage <> Nothing Then
286
+ _PowerCalculationMethod = PowerMethod.OutputVACalc
287
+ LogFile.LogTracing( "Using OutputVACalc method." , LogLvl.LOG_NOTICE, Me )
288
+ Else
289
+ _PowerCalculationMethod = PowerMethod.Unavailable
290
+ LogFile.LogTracing( "Unable to find a suitable method to calculate power usage." , LogLvl.LOG_WARNING, Me )
291
+ End If
262
292
End Try
263
293
End Try
264
294
End Try
@@ -283,35 +313,43 @@ Public Class UPS_Device
283
313
.Batt_Charge = Double .Parse(GetUPSVar( "battery.charge" , - 1 ), INVARIANT_CULTURE)
284
314
.Batt_Voltage = Double .Parse(GetUPSVar( "battery.voltage" , - 1 ), INVARIANT_CULTURE)
285
315
.Batt_Runtime = Double .Parse(GetUPSVar( "battery.runtime" , - 1 ), INVARIANT_CULTURE)
286
- .Power_Frequency = Double .Parse(GetUPSVar( "input.frequency" , Double .Parse(GetUPSVar( "output.frequency" , Freq_Fallback), INVARIANT_CULTURE) ), INVARIANT_CULTURE)
316
+ .Power_Frequency = Double .Parse(GetUPSVar( "input.frequency" , Freq_Fallback), INVARIANT_CULTURE)
287
317
.Input_Voltage = Double .Parse(GetUPSVar( "input.voltage" , - 1 ), INVARIANT_CULTURE)
288
- .Output_Voltage = Double .Parse(GetUPSVar( "output.voltage" , .Input_Voltage ), INVARIANT_CULTURE)
318
+ .Output_Voltage = Double .Parse(GetUPSVar( "output.voltage" , - 1 ), INVARIANT_CULTURE)
289
319
.Load = Double .Parse(GetUPSVar( "ups.load" , 0 ), INVARIANT_CULTURE)
290
320
291
321
' Retrieve and/or calculate output power if possible.
292
322
If _PowerCalculationMethod <> PowerMethod.Unavailable Then
293
323
Dim parsedValue As Double
294
324
295
325
Try
296
- If _PowerCalculationMethod = PowerMethod.RealPower Then
297
- parsedValue = Double .Parse(GetUPSVar( "ups.realpower" ), INVARIANT_CULTURE)
298
-
299
- ElseIf _PowerCalculationMethod = PowerMethod.NominalPowerCalc Then
300
- parsedValue = Double .Parse(GetUPSVar( "ups.realpower.nominal" ), INVARIANT_CULTURE)
301
- parsedValue *= UPS_Datas.UPS_Value.Load / 100.0
302
-
303
- ElseIf _PowerCalculationMethod = PowerMethod.VoltAmpCalc Then
304
- Dim nomCurrent = Double .Parse(GetUPSVar( "input.current.nominal" ), INVARIANT_CULTURE)
305
- Dim nomVoltage = Double .Parse(GetUPSVar( "input.voltage.nominal" ), INVARIANT_CULTURE)
306
-
307
- parsedValue = (nomCurrent * nomVoltage * 0.8 ) * (UPS_Datas.UPS_Value.Load / 100 . 0 )
308
- Else
309
- Throw New InvalidOperationException( "Insufficient variables to calculate power." )
310
- End If
326
+ Select Case _PowerCalculationMethod
327
+ Case PowerMethod.RealPower
328
+ parsedValue = Double .Parse(GetUPSVar( "ups.realpower" ), INVARIANT_CULTURE)
329
+
330
+ Case PowerMethod.RPNomLoadPct
331
+ parsedValue = Double .Parse(GetUPSVar( "ups.realpower.nominal" ), INVARIANT_CULTURE)
332
+ parsedValue *= UPS_Datas.UPS_Value.Load / 100.0
333
+
334
+ Case PowerMethod.InputNomVALoadPct
335
+ Dim nomCurrent = Double .Parse(GetUPSVar( "input.current.nominal" ), INVARIANT_CULTURE)
336
+ Dim nomVoltage = Double .Parse(GetUPSVar( "input.voltage.nominal" ), INVARIANT_CULTURE)
337
+
338
+ parsedValue = nomCurrent * nomVoltage * POWER_FACTOR
339
+ parsedValue = UPS_Datas.UPS_Value.Load / 100.0
340
+ Case PowerMethod.OutputVACalc
341
+ .Output_Current = Single .Parse(GetUPSVar( "output.current" ), INVARIANT_CULTURE)
342
+ parsedValue = .Output_Current * .Output_Voltage * POWER_FACTOR
343
+ Case Else
344
+ ' Should not trigger - something has gone wrong.
345
+ Throw New InvalidOperationException( "Insufficient variables to calculate power." )
346
+ End Select
311
347
Catch ex As FormatException
312
348
LogFile.LogTracing( "Unexpected format trying to parse value from UPS. Exception:" , LogLvl.LOG_ERROR, Me )
313
349
LogFile.LogTracing(ex.ToString(), LogLvl.LOG_ERROR, Me )
314
350
LogFile.LogTracing( "parsedValue: " & parsedValue, LogLvl.LOG_ERROR, Me )
351
+ Catch ex As Exception
352
+ LogFile.LogException(ex, Me )
315
353
End Try
316
354
317
355
.Output_Power = parsedValue
@@ -428,7 +466,6 @@ Public Class UPS_Device
428
466
LogFile.LogTracing( "Apply Fallback Value when retrieving " & varName, LogLvl.LOG_WARNING, Me )
429
467
Return Fallback_value
430
468
Else
431
- LogFile.LogTracing( "Unhandled error while getting " & varName, LogLvl.LOG_ERROR, Me )
432
469
Throw
433
470
End If
434
471
End Try
0 commit comments