Skip to content

Commit 4dfc291

Browse files
committed
Better handling of missing variables
- Battery and voltage properties are now set to an invalid value (-1) when they are unavailable, rather than the previous method of setting them to a "fallback" value. Partially addresses #116 - Attempt to calculate battery charge fails and leaves a log message if the battery voltage is unavailable. - Attempt to calculate battery runtime fails if other consequential values are unavailable. This may be problematic for calculations that rely on a "good enough" default value. However, I currently don't see any scenario where that would be acceptable to someone relying on an accurate calculation. - When UPS status is on battery, log an error if battery variables are unavailable when trying to determine if a stop action is needed. Will need to follow this up and warn the user that stop actions are unavailable if battery info isn't present. - When battery runtime is unavailable to WinNUT, display localized message instead.
1 parent 7b620a4 commit 4dfc291

File tree

4 files changed

+74
-44
lines changed

4 files changed

+74
-44
lines changed

WinNUT_V2/WinNUT-Client/My Project/Resources.Designer.vb

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WinNUT_V2/WinNUT-Client/My Project/Resources.resx

+4
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ Please correct the error, or cancel the upgrade dialog to continue with the defa
367367
<data name="ups_104x104" type="System.Resources.ResXFileRef, System.Windows.Forms">
368368
<value>..\Resources\ups_104x104.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
369369
</data>
370+
<data name="VariableUnavailable" xml:space="preserve">
371+
<value>Unavailable</value>
372+
<comment>Indicate that a variable is unavailable</comment>
373+
</data>
370374
<data name="ViewLogFile_24x24" type="System.Resources.ResXFileRef, System.Windows.Forms">
371375
<value>..\Resources\ViewLogFile_24x24.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
372376
</data>

WinNUT_V2/WinNUT-Client/WinNUT.vb

+28-23
Original file line numberDiff line numberDiff line change
@@ -686,21 +686,23 @@ Public Class WinNUT
686686
Lbl_VOB.BackColor = Color.Green
687687
ActualAppIconIdx = 0
688688

689-
If Not ShutdownStatus Then
690-
If .Batt_Charge <= My.Settings.PW_BattChrgFloor Or
691-
.Batt_Runtime <= My.Settings.PW_RuntimeFloor Then
692-
LogFile.LogTracing("UPS battery has dropped below stop condition limits.",
693-
LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_LOG_SHUT_START))
694-
Shutdown_Event()
695-
Else
696-
LogFile.LogTracing(String.Format("UPS charge ({0}%) or Runtime ({1}) have not met shutdown conditions {2} or {3}.",
697-
.Batt_Charge, .Batt_Runtime, My.Settings.PW_BattChrgFloor, My.Settings.PW_RuntimeFloor),
698-
LogLvl.LOG_DEBUG, Me)
689+
If .Batt_Charge = -1 AndAlso .Batt_Runtime = -1 Then
690+
LogFile.LogTracing("Battery properties unavailable, unable to validate shutdown conditions.", LogLvl.LOG_WARNING, Me)
691+
ElseIf Not ShutdownStatus Then
692+
If .Batt_Charge <= My.Settings.PW_BattChrgFloor Or
693+
.Batt_Runtime <= My.Settings.PW_RuntimeFloor Then
694+
LogFile.LogTracing("UPS battery has dropped below stop condition limits.",
695+
LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_LOG_SHUT_START))
696+
Shutdown_Event()
697+
Else
698+
LogFile.LogTracing(String.Format("UPS charge ({0}%) or Runtime ({1}) have not met shutdown conditions {2} or {3}.",
699+
.Batt_Charge, .Batt_Runtime, My.Settings.PW_BattChrgFloor, My.Settings.PW_RuntimeFloor),
700+
LogLvl.LOG_DEBUG, Me)
701+
End If
699702
End If
700703
End If
701-
End If
702704

703-
If .UPS_Status.HasFlag(UPS_States.OVER) Then
705+
If .UPS_Status.HasFlag(UPS_States.OVER) Then
704706
Lbl_VOLoad.BackColor = Color.Red
705707
Else
706708
Lbl_VOLoad.BackColor = Color.White
@@ -730,17 +732,20 @@ Public Class WinNUT
730732
End Select
731733

732734
' Calculate and display estimated remaining time on battery.
733-
' TODO: overflow exception?
734-
Dim iSpan As TimeSpan = TimeSpan.FromSeconds(UPS_BattRuntime)
735-
LogFile.LogTracing("Calculated estimated remaining battery time: " & iSpan.ToString(), LogLvl.LOG_DEBUG, Me)
736-
737-
' Format the TimeSpan using a standard format (g = 0:00:00)
738-
' https://docs.microsoft.com/en-us/dotnet/api/system.timespan.tostring
739-
Lbl_VRTime.Text = iSpan.ToString("g")
740-
'Lbl_VRTime.Text = iSpan.Hours.ToString.PadLeft(2, "0"c) & ":" &
741-
'iSpan.Minutes.ToString.PadLeft(2, "0"c) & ":" &
742-
'iSpan.Seconds.ToString.PadLeft(2, "0"c)
743-
'End If
735+
If UPS_BattRuntime >= 0 AndAlso UPS_BattRuntime <= 86400 Then
736+
Dim iSpan As TimeSpan = TimeSpan.FromSeconds(UPS_BattRuntime)
737+
LogFile.LogTracing("Calculated estimated remaining battery time: " & iSpan.ToString(), LogLvl.LOG_DEBUG, Me)
738+
739+
' Format the TimeSpan using a standard format (g = 0:00:00)
740+
' https://docs.microsoft.com/en-us/dotnet/api/system.timespan.tostring
741+
Lbl_VRTime.Text = iSpan.ToString("g")
742+
'Lbl_VRTime.Text = iSpan.Hours.ToString.PadLeft(2, "0"c) & ":" &
743+
'iSpan.Minutes.ToString.PadLeft(2, "0"c) & ":" &
744+
'iSpan.Seconds.ToString.PadLeft(2, "0"c)
745+
'End If
746+
Else
747+
Lbl_VRTime.Text = My.Resources.VariableUnavailable
748+
End If
744749

745750
LogFile.LogTracing("Update Dial", LogLvl.LOG_DEBUG, Me)
746751
AG_InV.Value1 = UPS_InputV

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

+33-21
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Public Class UPS_Device
264264
End Try
265265

266266
' Other constant values for UPS calibration.
267-
freshData.UPS_Value.Batt_Capacity = Double.Parse(GetUPSVar("battery.capacity", 7), INVARIANT_CULTURE)
267+
freshData.UPS_Value.Batt_Capacity = Double.Parse(GetUPSVar("battery.capacity", -1), INVARIANT_CULTURE)
268268
Freq_Fallback = Double.Parse(GetUPSVar("output.frequency.nominal", Freq_Fallback), INVARIANT_CULTURE)
269269

270270
LogFile.LogTracing("Completed retrieval of basic UPS product information.", LogLvl.LOG_NOTICE, Me)
@@ -280,11 +280,11 @@ Public Class UPS_Device
280280

281281
If IsConnected Then
282282
With UPS_Datas.UPS_Value
283-
.Batt_Charge = Double.Parse(GetUPSVar("battery.charge", 255), INVARIANT_CULTURE)
284-
.Batt_Voltage = Double.Parse(GetUPSVar("battery.voltage", 12), INVARIANT_CULTURE)
285-
.Batt_Runtime = Double.Parse(GetUPSVar("battery.runtime", 86400), INVARIANT_CULTURE)
283+
.Batt_Charge = Double.Parse(GetUPSVar("battery.charge", -1), INVARIANT_CULTURE)
284+
.Batt_Voltage = Double.Parse(GetUPSVar("battery.voltage", -1), INVARIANT_CULTURE)
285+
.Batt_Runtime = Double.Parse(GetUPSVar("battery.runtime", -1), INVARIANT_CULTURE)
286286
.Power_Frequency = Double.Parse(GetUPSVar("input.frequency", Double.Parse(GetUPSVar("output.frequency", Freq_Fallback), INVARIANT_CULTURE)), INVARIANT_CULTURE)
287-
.Input_Voltage = Double.Parse(GetUPSVar("input.voltage", 220), INVARIANT_CULTURE)
287+
.Input_Voltage = Double.Parse(GetUPSVar("input.voltage", -1), INVARIANT_CULTURE)
288288
.Output_Voltage = Double.Parse(GetUPSVar("output.voltage", .Input_Voltage), INVARIANT_CULTURE)
289289
.Load = Double.Parse(GetUPSVar("ups.load", 0), INVARIANT_CULTURE)
290290

@@ -317,24 +317,36 @@ Public Class UPS_Device
317317
.Output_Power = parsedValue
318318
End If
319319

320-
' Handle cases of UPSs that are unable to report battery runtime or load correctly while on battery.
321-
Dim PowerDivider As Double = 0.5
322-
Select Case .Load
323-
Case 76 To 100
324-
PowerDivider = 0.4
325-
Case 51 To 75
326-
PowerDivider = 0.3
327-
End Select
328-
329-
If .Batt_Charge = 255 Then
330-
Dim nBatt = Math.Floor(.Batt_Voltage / 12)
331-
.Batt_Charge = Math.Floor((.Batt_Voltage - (11.6 * nBatt)) / (0.02 * nBatt))
320+
' Handle out-of-range battery charge
321+
If .Batt_Charge > 0 AndAlso .Batt_Charge <= 100 Then
322+
If .Batt_Voltage > 0 Then
323+
Dim nBatt = Math.Floor(.Batt_Voltage / 12)
324+
.Batt_Charge = Math.Floor((.Batt_Voltage - (11.6 * nBatt)) / (0.02 * nBatt))
325+
Else
326+
LogFile.LogTracing("Unable to calculate UPS Batt_Charge: Batt_Voltage (" & .Batt_Voltage & ") out of range.", LogLvl.LOG_WARNING, Me)
327+
End If
332328
End If
333329

334-
If .Batt_Runtime >= 86400 Then
335-
.Load = If(.Load <> 0, .Load, 0.1)
336-
Dim BattInstantCurrent = (.Output_Voltage * .Load) / (.Batt_Voltage * 100)
337-
.Batt_Runtime = Math.Floor(.Batt_Capacity * 0.6 * .Batt_Charge * (1 - PowerDivider) * 3600 / (BattInstantCurrent * 100))
330+
' Handle cases of UPSs that are unable to report battery runtime or load correctly while on battery.
331+
If .Batt_Runtime = -1 Then
332+
If .Output_Voltage = -1 OrElse .Batt_Voltage = -1 OrElse .Batt_Capacity = -1 OrElse .Batt_Charge = -1 Then
333+
LogFile.LogTracing("Unable to calculate battery runtime, missing UPS variables.", LogLvl.LOG_WARNING, Me)
334+
LogFile.LogTracing(String.Format("Output_Voltage: {0}, Batt_Voltage: {1}, Batt_Capacity: {2}, Batt_Charge: {3}",
335+
.Output_Voltage, .Batt_Voltage, .Batt_Capacity, .Batt_Charge), LogLvl.LOG_WARNING, Me)
336+
337+
Else
338+
Dim PowerDivider As Double = 0.5
339+
Select Case .Load
340+
Case 76 To 100
341+
PowerDivider = 0.4
342+
Case 51 To 75
343+
PowerDivider = 0.3
344+
End Select
345+
346+
.Load = If(.Load <> 0, .Load, 0.1)
347+
Dim BattInstantCurrent = (.Output_Voltage * .Load) / (.Batt_Voltage * 100)
348+
.Batt_Runtime = Math.Floor(.Batt_Capacity * 0.6 * .Batt_Charge * (1 - PowerDivider) * 3600 / (BattInstantCurrent * 100))
349+
End If
338350
End If
339351

340352
UPS_rt_Status = GetUPSVar("ups.status", UPS_States.None)

0 commit comments

Comments
 (0)