@@ -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,69 @@ 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
+ Try
262
+ Dim value = Single .Parse(GetUPSVar( "output.realpower" ), INVARIANT_CULTURE)
263
+ .Output_Power = value
264
+ LogFile.LogTracing( "output.power: " & value, LogLvl.LOG_DEBUG, Me )
265
+ Catch ex As Exception
266
+
267
+ End Try
268
+ End With
269
+
270
+ ' Determine optimal method for measuring power output from the UPS.
241
271
LogFile.LogTracing( "Determining best method to calculate power usage..." , LogLvl.LOG_NOTICE, Me )
272
+ ' Start with directly reading a variable from the UPS.
242
273
Try
243
- GetUPSVar( "ups.realpower" )
244
- _PowerCalculationMethod = PowerMethod.RealPower
245
- LogFile.LogTracing( "Using RealPower method to calculate power usage." , LogLvl.LOG_NOTICE, Me )
274
+ If freshData.UPS_Value.Output_Power <> Nothing Then
275
+ _PowerCalculationMethod = PowerMethod.RealOutputPower
276
+ LogFile.LogTracing( "Using RealOutputPower method." , LogLvl.LOG_NOTICE, Me )
277
+ Else
278
+ GetUPSVar( "ups.realpower" )
279
+ _PowerCalculationMethod = PowerMethod.RealPower
280
+ LogFile.LogTracing( "Using RealPower method." , LogLvl.LOG_NOTICE, Me )
281
+ End If
246
282
Catch
247
283
Try
248
284
GetUPSVar( "ups.realpower.nominal" )
249
285
GetUPSVar( "ups.load" )
250
- _PowerCalculationMethod = PowerMethod.NominalPowerCalc
251
- LogFile.LogTracing( "Using NominalPowerCalc method to calculate power usage ." , LogLvl.LOG_NOTICE, Me )
286
+ _PowerCalculationMethod = PowerMethod.RPNomLoadPct
287
+ LogFile.LogTracing( "Using RPNomLoadPct method." , LogLvl.LOG_NOTICE, Me )
252
288
Catch
253
289
Try
254
290
GetUPSVar( "input.current.nominal" )
255
291
GetUPSVar( "input.voltage.nominal" )
256
292
GetUPSVar( "ups.load" )
257
- _PowerCalculationMethod = PowerMethod.VoltAmpCalc
258
- LogFile.LogTracing( "Using VoltAmpCalc method to calculate power usage ." , LogLvl.LOG_NOTICE, Me )
293
+ _PowerCalculationMethod = PowerMethod.InputNomVALoadPct
294
+ LogFile.LogTracing( "Using InputNomVALoadPct method." , LogLvl.LOG_NOTICE, Me )
259
295
Catch
260
- _PowerCalculationMethod = PowerMethod.Unavailable
261
- LogFile.LogTracing( "Unable to find a suitable method to calculate power usage." , LogLvl.LOG_WARNING, Me )
296
+ If freshData.UPS_Value.Output_Current IsNot Nothing AndAlso
297
+ freshData.UPS_Value.Output_Voltage <> Nothing Then
298
+ _PowerCalculationMethod = PowerMethod.OutputVACalc
299
+ LogFile.LogTracing( "Using OutputVACalc method." , LogLvl.LOG_NOTICE, Me )
300
+ Else
301
+ _PowerCalculationMethod = PowerMethod.Unavailable
302
+ LogFile.LogTracing( "Unable to find a suitable method to calculate power usage." , LogLvl.LOG_WARNING, Me )
303
+ End If
262
304
End Try
263
305
End Try
264
306
End Try
@@ -283,38 +325,51 @@ Public Class UPS_Device
283
325
.Batt_Charge = Double .Parse(GetUPSVar( "battery.charge" , - 1 ), INVARIANT_CULTURE)
284
326
.Batt_Voltage = Double .Parse(GetUPSVar( "battery.voltage" , - 1 ), INVARIANT_CULTURE)
285
327
.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)
328
+ .Power_Frequency = Double .Parse(GetUPSVar( "input.frequency" , Freq_Fallback), INVARIANT_CULTURE)
287
329
.Input_Voltage = Double .Parse(GetUPSVar( "input.voltage" , - 1 ), INVARIANT_CULTURE)
288
- .Output_Voltage = Double .Parse(GetUPSVar( "output.voltage" , .Input_Voltage ), INVARIANT_CULTURE)
330
+ .Output_Voltage = Double .Parse(GetUPSVar( "output.voltage" , - 1 ), INVARIANT_CULTURE)
289
331
.Load = Double .Parse(GetUPSVar( "ups.load" , 0 ), INVARIANT_CULTURE)
290
332
291
333
' Retrieve and/or calculate output power if possible.
292
334
If _PowerCalculationMethod <> PowerMethod.Unavailable Then
293
335
Dim parsedValue As Double
294
336
295
337
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
338
+ Select Case _PowerCalculationMethod
339
+ Case PowerMethod.RealPower
340
+ parsedValue = Double .Parse(GetUPSVar( "ups.realpower" ), INVARIANT_CULTURE)
341
+
342
+ Case PowerMethod.RealOutputPower
343
+ parsedValue = Single .Parse(GetUPSVar( "output.realpower" ), INVARIANT_CULTURE)
344
+
345
+ Case PowerMethod.RPNomLoadPct
346
+ parsedValue = Double .Parse(GetUPSVar( "ups.realpower.nominal" ), INVARIANT_CULTURE)
347
+ parsedValue *= UPS_Datas.UPS_Value.Load / 100.0
348
+
349
+ Case PowerMethod.InputNomVALoadPct
350
+ Dim nomCurrent = Double .Parse(GetUPSVar( "input.current.nominal" ), INVARIANT_CULTURE)
351
+ Dim nomVoltage = Double .Parse(GetUPSVar( "input.voltage.nominal" ), INVARIANT_CULTURE)
352
+
353
+ parsedValue = nomCurrent * nomVoltage * POWER_FACTOR
354
+ parsedValue *= UPS_Datas.UPS_Value.Load / 100.0
355
+ Case PowerMethod.OutputVACalc
356
+ .Output_Current = Single .Parse(GetUPSVar( "output.current" ), INVARIANT_CULTURE)
357
+ parsedValue = .Output_Current * .Output_Voltage * POWER_FACTOR
358
+ Case Else
359
+ ' Should not trigger - something has gone wrong.
360
+ Throw New InvalidOperationException( "Reached Else case when attempting to get power output for method " & _PowerCalculationMethod)
361
+ End Select
311
362
Catch ex As FormatException
312
363
LogFile.LogTracing( "Unexpected format trying to parse value from UPS. Exception:" , LogLvl.LOG_ERROR, Me )
313
364
LogFile.LogTracing(ex.ToString(), LogLvl.LOG_ERROR, Me )
314
365
LogFile.LogTracing( "parsedValue: " & parsedValue, LogLvl.LOG_ERROR, Me )
366
+ Catch ex As Exception
367
+ LogFile.LogException(ex, Me )
315
368
End Try
316
369
317
- .Output_Power = parsedValue
370
+ ' Apply rounding to this number since calculations have extended to three decimal places.
371
+ ' TODO: Remove this round function once gauges can handle decimal places better.
372
+ .Output_Power = Math.Round(parsedValue, 1 )
318
373
End If
319
374
320
375
' Handle out-of-range battery charge
@@ -428,7 +483,6 @@ Public Class UPS_Device
428
483
LogFile.LogTracing( "Apply Fallback Value when retrieving " & varName, LogLvl.LOG_WARNING, Me )
429
484
Return Fallback_value
430
485
Else
431
- LogFile.LogTracing( "Unhandled error while getting " & varName, LogLvl.LOG_ERROR, Me )
432
486
Throw
433
487
End If
434
488
End Try
0 commit comments