Skip to content

Commit e2a2d2e

Browse files
committed
UPS Status update changes, reactivate stop system
- Removed Shutdown_Condition and Stop_Shutdown events in favor of one StatusesChanged event - Rewrote the status updating portion of Retrieve_UPS_Datas to try and make it more efficient, and allow the client to handle changes in status. - Moved handling of status changes into WinNUT.vb, and reactivated disabled stop system.
1 parent a7bbc42 commit e2a2d2e

File tree

2 files changed

+93
-100
lines changed

2 files changed

+93
-100
lines changed

WinNUT_V2/WinNUT-Client/WinNUT.vb

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ Public Class WinNUT
3737

3838
'Object for UPS management
3939
Public WithEvents UPS_Device As UPS_Device
40-
' Public Nut_Socket As Nut_Socket
41-
' Public Nut_Config As New Nut_Parameter
42-
' Private Device_Data As UPSData
43-
44-
' ^--- Shall be referenced from inside UPS_Device object
4540

4641
Private AutoReconnect As Boolean
4742
Private UPS_Retry As Integer = 0
@@ -665,16 +660,16 @@ Public Class WinNUT
665660
UPS_Status = "OL"
666661
UPS_OutPower = .Output_Power
667662

668-
If (.UPS_Status And UPS_States.OL) Then
663+
If .UPS_Status.HasFlag(UPS_States.OL) Then
669664
Lbl_VOL.BackColor = Color.Green
670665
Lbl_VOB.BackColor = Color.White
671666
ActualAppIconIdx = AppIconIdx.IDX_OL
672-
ElseIf (.UPS_Status And UPS_States.OB) Then
667+
ElseIf .UPS_Status.HasFlag(UPS_States.OB) Then
673668
Lbl_VOL.BackColor = Color.Yellow
674669
Lbl_VOB.BackColor = Color.Green
675670
ActualAppIconIdx = 0
676671
End If
677-
If (.UPS_Status And UPS_States.OVER) Then
672+
If .UPS_Status.HasFlag(UPS_States.OVER) Then
678673
Lbl_VOLoad.BackColor = Color.Red
679674
Else
680675
Lbl_VOLoad.BackColor = Color.White
@@ -699,31 +694,27 @@ Public Class WinNUT
699694
' Lbl_VOLoad.BackColor = Color.White
700695
' End If
701696

697+
LogFile.LogTracing("Updating battery icons based on charge percent: " & UPS_BattCh & "%", LogLvl.LOG_DEBUG, Me)
698+
702699
Select Case UPS_BattCh
703700
Case 76 To 100
704701
Lbl_VBL.BackColor = Color.White
705702
ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_100
706-
LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me)
707703
Case 51 To 75
708704
Lbl_VBL.BackColor = Color.White
709705
ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_75
710-
LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me)
711706
Case 40 To 50
712707
Lbl_VBL.BackColor = Color.White
713708
ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_50
714-
LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me)
715709
Case 26 To 39
716710
Lbl_VBL.BackColor = Color.Red
717711
ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_50
718-
LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me)
719712
Case 11 To 25
720713
Lbl_VBL.BackColor = Color.Red
721714
ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_25
722-
LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me)
723715
Case 0 To 10
724716
Lbl_VBL.BackColor = Color.Red
725717
ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_0
726-
LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me)
727718
End Select
728719

729720
' Calculate and display estimated remaining time on battery.
@@ -1041,9 +1032,44 @@ Public Class WinNUT
10411032
End If
10421033
End Sub
10431034

1044-
Private Sub Shutdown_Event() Handles UPS_Device.Shutdown_Condition
1035+
Private Sub HandleUPSStatusChange(sender As UPS_Device, newStatuses As UPS_States) Handles UPS_Device.StatusesChanged
1036+
LogFile.LogTracing("Handling new UPS status(es)...", LogLvl.LOG_DEBUG, Me)
1037+
Dim statusString As String = ""
1038+
1039+
With sender.UPS_Datas.UPS_Value
1040+
For Each status In [Enum].GetValues(GetType(UPS_States))
1041+
If newStatuses.HasFlag(status) Then
1042+
statusString &= [Enum].GetName(GetType(UPS_States), status) & " "
1043+
' Determine if we need to initiate the stop procedure.
1044+
If status = UPS_States.FSD Then
1045+
LogFile.LogTracing("Full Shut Down imposed by the NUT server.", LogLvl.LOG_NOTICE, Me, StrLog.Item(AppResxStr.STR_LOG_NUT_FSD))
1046+
Shutdown_Event()
1047+
ElseIf status = UPS_States.OB And
1048+
(.Batt_Charge <= Arr_Reg_Key.Item("ShutdownLimitBatteryCharge") Or
1049+
.Batt_Runtime <= Arr_Reg_Key.Item("ShutdownLimitUPSRemainTime")) And
1050+
Not ShutdownStatus Then
1051+
LogFile.LogTracing("UPS battery has dropped below stop condition limits.", LogLvl.LOG_NOTICE, Me, WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_SHUT_START))
1052+
Shutdown_Event()
1053+
ElseIf status = UPS_States.OL AndAlso ShutdownStatus Then
1054+
LogFile.LogTracing("UPS returned online during a pre-shutdown event.", LogLvl.LOG_NOTICE, Me)
1055+
Stop_Shutdown_Event()
1056+
End If
1057+
End If
1058+
Next
1059+
End With
1060+
1061+
LogFile.LogTracing("Finished handling new UPS statuses: " & statusString, LogLvl.LOG_NOTICE, Me)
1062+
End Sub
1063+
1064+
''' <summary>
1065+
''' Track if the Shutdown_Gui is counting down to activate the Shutdown_Action.
1066+
''' </summary>
1067+
Private ShutdownStatus = False
1068+
Private Sub Shutdown_Event()
1069+
ShutdownStatus = True
1070+
10451071
If Arr_Reg_Key.Item("ImmediateStopAction") Then
1046-
' UPSDisconnect()
1072+
LogFile.LogTracing("Immediately stopping due to shutdown event.", LogLvl.LOG_NOTICE, Me)
10471073
UPS_Device.Disconnect()
10481074
Shutdown_Action()
10491075
Else
@@ -1054,13 +1080,15 @@ Public Class WinNUT
10541080
End If
10551081
End Sub
10561082

1057-
Private Sub Stop_Shutdown_Event() Handles UPS_Device.Stop_Shutdown
1083+
Private Sub Stop_Shutdown_Event()
1084+
ShutdownStatus = False
10581085
Shutdown_Gui.Shutdown_Timer.Stop()
10591086
Shutdown_Gui.Shutdown_Timer.Enabled = False
10601087
Shutdown_Gui.Grace_Timer.Stop()
10611088
Shutdown_Gui.Grace_Timer.Enabled = False
10621089
Shutdown_Gui.Hide()
10631090
Shutdown_Gui.Close()
1091+
LogFile.LogTracing("Stop condition cancelled.", LogLvl.LOG_NOTICE, Me, WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_SHUT_STOP))
10641092
End Sub
10651093

10661094
Public Sub Shutdown_Action()

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 48 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ Imports System.Windows.Forms
1313
Public Class UPS_Device
1414
#Region "Properties"
1515

16+
Public ReadOnly Property Name As String
17+
Get
18+
Return Nut_Config.UPSName
19+
End Get
20+
End Property
21+
1622
Public ReadOnly Property IsConnected As Boolean
1723
Get
1824
Return (Nut_Socket.IsConnected) ' And Me.Socket_Status
@@ -34,6 +40,16 @@ Public Class UPS_Device
3440
End Set
3541
End Property
3642

43+
Private upsData As UPSData
44+
Public Property UPS_Datas As UPSData
45+
Get
46+
Return upsData
47+
End Get
48+
Private Set(value As UPSData)
49+
upsData = value
50+
End Set
51+
End Property
52+
3753
#End Region
3854
Private Const CosPhi As Double = 0.6
3955
' How many milliseconds to wait before the Reconnect routine tries again.
@@ -103,28 +119,13 @@ Public Class UPS_Device
103119
'Private NutStream As System.Net.Sockets.NetworkStream
104120
'Private ReaderStream As System.IO.StreamReader
105121
'Private WriterStream As System.IO.StreamWriter
106-
'Private ShutdownStatus As Boolean = False
107122
'Private Follow_FSD As Boolean = False
108123
'Private Unknown_UPS_Name As Boolean = False
109124
'Private Invalid_Data As Boolean = False
110125
'Private Invalid_Auth_Data As Boolean = False
111126

112127
#Region "Properties"
113-
Private upsData As UPSData
114-
Public Property UPS_Datas As UPSData
115-
Get
116-
Return upsData
117-
End Get
118-
Private Set(value As UPSData)
119-
upsData = value
120-
End Set
121-
End Property
122128

123-
Public ReadOnly Property Name As String
124-
Get
125-
Return Nut_Config.UPSName
126-
End Get
127-
End Property
128129
#End Region
129130

130131
' Public Event Unknown_UPS()
@@ -139,8 +140,14 @@ Public Class UPS_Device
139140
Public Event ConnectionError(innerException As Exception)
140141
Public Event EncounteredNUTException(ex As NutException, sender As Object)
141142
Public Event New_Retry()
142-
Public Event Shutdown_Condition()
143-
Public Event Stop_Shutdown()
143+
' Public Event Shutdown_Condition()
144+
' Public Event Stop_Shutdown()
145+
146+
''' <summary>
147+
''' Raise an event when a status code is added to the UPS that wasn't there before.
148+
''' </summary>
149+
''' <param name="newStatuses">The bitmask of status flags that are currently set on the UPS.</param>
150+
Public Event StatusesChanged(sender As UPS_Device, newStatuses As UPS_States)
144151

145152
Public Sub New(ByRef Nut_Config As Nut_Parameter, ByRef LogFile As Logger, pollInterval As Integer)
146153
Me.LogFile = LogFile
@@ -291,6 +298,8 @@ Public Class UPS_Device
291298
Return freshData
292299
End Function
293300

301+
Private oldStatusBitmask As Integer
302+
294303
Public Sub Retrieve_UPS_Datas() Handles Update_Data.Tick ' As UPSData
295304
LogFile.LogTracing("Enter Retrieve_UPS_Datas", LogLvl.LOG_DEBUG, Me)
296305
Try
@@ -304,6 +313,7 @@ Public Class UPS_Device
304313
' UPS_Datas = GetUPSProductInfo()
305314
' End Select
306315
'End With
316+
307317
With UPS_Datas.UPS_Value
308318
.Batt_Charge = Double.Parse(GetUPSVar("battery.charge", 255), ciClone)
309319
.Batt_Voltage = Double.Parse(GetUPSVar("battery.voltage", 12), ciClone)
@@ -312,7 +322,7 @@ Public Class UPS_Device
312322
.Input_Voltage = Double.Parse(GetUPSVar("input.voltage", 220), ciClone)
313323
.Output_Voltage = Double.Parse(GetUPSVar("output.voltage", .Input_Voltage), ciClone)
314324
.Load = Double.Parse(GetUPSVar("ups.load", 100), ciClone)
315-
UPS_rt_Status = GetUPSVar("ups.status", "OL")
325+
UPS_rt_Status = GetUPSVar("ups.status")
316326
.Output_Power = Double.Parse((GetUPSVar("ups.realpower.nominal", 0)), ciClone)
317327
If .Output_Power = 0 Then
318328
.Output_Power = Double.Parse((GetUPSVar("ups.power.nominal", 0)), ciClone)
@@ -344,70 +354,26 @@ Public Class UPS_Device
344354
Dim BattInstantCurrent = (.Output_Voltage * .Load) / (.Batt_Voltage * 100)
345355
.Batt_Runtime = Math.Floor(.Batt_Capacity * 0.6 * .Batt_Charge * (1 - PowerDivider) * 3600 / (BattInstantCurrent * 100))
346356
End If
347-
Dim StatusArr = UPS_rt_Status.Trim().Split(" ")
348-
.UPS_Status = 0
349-
For Each State In StatusArr
350-
Select Case State
351-
Case "OL"
352-
LogFile.LogTracing("UPS is On Line", LogLvl.LOG_NOTICE, Me)
353-
.UPS_Status = .UPS_Status Or UPS_States.OL
354-
'If Not Update_Nut.Interval = Me.Delay Then
355-
' Update_Nut.Stop()
356-
' Update_Nut.Interval = Me.Delay
357-
' Update_Nut.Start()
358-
'End If
359-
'If ShutdownStatus Then
360-
' LogFile.LogTracing("Stop condition Canceled", LogLvl.LOG_NOTICE, Me, WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_SHUT_STOP))
361-
' ShutdownStatus = False
362-
' RaiseEvent Stop_Shutdown()
363-
'End If
364-
Case "OB"
365-
LogFile.LogTracing("UPS is On Battery", LogLvl.LOG_NOTICE, Me)
366-
.UPS_Status = .UPS_Status Or UPS_States.OB
367-
'If Update_Nut.Interval = Me.Delay Then
368-
' Update_Nut.Stop()
369-
' Update_Nut.Interval = If((Math.Floor(Me.Delay / 5) < 1000), 1000, Math.Floor(Me.Delay / 5))
370-
' Update_Nut.Start()
371-
'End If
372-
'If ((Me.BattCh <= Me.Low_Batt Or Me.BattRuntime <= Me.Backup_Limit) And Not ShutdownStatus) Then
373-
' LogFile.LogTracing("Stop condition reached", LogLvl.LOG_NOTICE, Me, WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_SHUT_START))
374-
' RaiseEvent Shutdown_Condition()
375-
' ShutdownStatus = True
376-
'End If
377-
Case "LB", "HB"
378-
LogFile.LogTracing("High/Low Battery on UPS", LogLvl.LOG_NOTICE, Me)
379-
.UPS_Status = .UPS_Status Or UPS_States.LBHB
380-
Case "CHRG"
381-
LogFile.LogTracing("Battery is Charging on UPS", LogLvl.LOG_NOTICE, Me)
382-
.UPS_Status = .UPS_Status Or UPS_States.CHRG
383-
Case "DISCHRG"
384-
LogFile.LogTracing("Battery is Discharging on UPS", LogLvl.LOG_NOTICE, Me)
385-
.UPS_Status = .UPS_Status Or UPS_States.DISCHRG
386-
Case "FSD"
387-
LogFile.LogTracing("Stop condition imposed by the NUT server", LogLvl.LOG_NOTICE, Me, WinNUT_Globals.StrLog.Item(AppResxStr.STR_LOG_NUT_FSD))
388-
.UPS_Status = .UPS_Status Or UPS_States.FSD
389-
RaiseEvent Shutdown_Condition()
390-
'ShutdownStatus = True
391-
Case "BYPASS"
392-
LogFile.LogTracing("UPS bypass circuit is active - no battery protection is available", LogLvl.LOG_NOTICE, Me)
393-
.UPS_Status = .UPS_Status Or UPS_States.BYPASS
394-
Case "CAL"
395-
LogFile.LogTracing("UPS is currently performing runtime calibration (on battery)", LogLvl.LOG_NOTICE, Me)
396-
.UPS_Status = .UPS_Status Or UPS_States.CAL
397-
Case "OFF"
398-
LogFile.LogTracing("UPS is offline and is not supplying power to the load", LogLvl.LOG_NOTICE, Me)
399-
.UPS_Status = .UPS_Status Or UPS_States.OFF
400-
Case "OVER"
401-
LogFile.LogTracing("UPS is overloaded", LogLvl.LOG_NOTICE, Me)
402-
.UPS_Status = .UPS_Status Or UPS_States.OVER
403-
Case "TRIM"
404-
LogFile.LogTracing("UPS is trimming incoming voltage", LogLvl.LOG_NOTICE, Me)
405-
.UPS_Status = .UPS_Status Or UPS_States.TRIM
406-
Case "BOOST"
407-
LogFile.LogTracing("UPS is boosting incoming voltage", LogLvl.LOG_NOTICE, Me)
408-
.UPS_Status = .UPS_Status Or UPS_States.BOOST
409-
End Select
410-
Next
357+
358+
' Prepare the status string for Enum parsing by replacing spaces with commas.
359+
UPS_rt_Status = UPS_rt_Status.Replace(" ", ",")
360+
Try
361+
.UPS_Status = [Enum].Parse(GetType(UPS_States), UPS_rt_Status)
362+
Catch ex As ArgumentException
363+
LogFile.LogTracing("Likely encountered an unknown/invalid UPS status. Using previous status." &
364+
vbNewLine & ex.Message, LogLvl.LOG_ERROR, Me)
365+
End Try
366+
367+
' Get the difference between the old and new statuses, and filter only for active ones.
368+
Dim statusDiff = (oldStatusBitmask Xor .UPS_Status) And .UPS_Status
369+
370+
If statusDiff = 0 Then
371+
LogFile.LogTracing("UPS statuses have not changed since last update, skipping.", LogLvl.LOG_DEBUG, Me)
372+
Else
373+
LogFile.LogTracing("UPS statuses have CHANGED, updating...", LogLvl.LOG_NOTICE, Me)
374+
oldStatusBitmask = .UPS_Status
375+
RaiseEvent StatusesChanged(Me, statusDiff)
376+
End If
411377
End With
412378
RaiseEvent DataUpdated()
413379
End If
@@ -419,7 +385,6 @@ Public Class UPS_Device
419385
'Me.Disconnect(True)
420386
'Enter_Reconnect_Process(Excep, "Error When Retrieve_UPS_Data : ")
421387
End Try
422-
' Return Me.UPSData
423388
End Sub
424389

425390
Private Const MAX_VAR_RETRIES = 3

0 commit comments

Comments
 (0)