@@ -11,6 +11,15 @@ Imports System.Globalization
11
11
Imports System.Windows.Forms
12
12
13
13
Public Class UPS_Device
14
+ # Region "Statics/Defaults"
15
+ Private ReadOnly INVARIANT_CULTURE = CultureInfo.InvariantCulture
16
+ Private Const CosPhi As Double = 0.6
17
+
18
+ ' How many milliseconds to wait before the Reconnect routine tries again.
19
+ Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 5000
20
+ Private Const DEFAULT_UPDATE_INTERVAL_MS As Double = 1000
21
+ # End Region
22
+
14
23
# Region "Properties"
15
24
16
25
Public ReadOnly Property Name As String
@@ -35,6 +44,10 @@ Public Class UPS_Device
35
44
End Get
36
45
End Property
37
46
47
+ ''' <summary>
48
+ ''' How often UPS data is updated, in milliseconds.
49
+ ''' </summary>
50
+ ''' <returns></returns>
38
51
Public Property PollingInterval As Integer
39
52
Get
40
53
Return Update_Data.Interval
@@ -102,16 +115,11 @@ Public Class UPS_Device
102
115
103
116
# End Region
104
117
105
- Private Const CosPhi As Double = 0.6
106
- ' How many milliseconds to wait before the Reconnect routine tries again.
107
- Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 5000
108
-
109
118
Private WithEvents Update_Data As New Timer
110
119
Private WithEvents Reconnect_Nut As New Timer
111
120
Private WithEvents Nut_Socket As Nut_Socket
112
121
113
122
Private Freq_Fallback As Double
114
- Private ciClone As CultureInfo
115
123
Public Nut_Config As Nut_Parameter
116
124
Public Retry As Integer = 0
117
125
Public MaxRetry As Integer = 30
@@ -121,13 +129,18 @@ Public Class UPS_Device
121
129
Me .LogFile = LogFile
122
130
Me .Nut_Config = Nut_Config
123
131
PollingInterval = pollInterval
124
- ciClone = CType (CultureInfo.InvariantCulture.Clone(), CultureInfo)
125
- ciClone.NumberFormat.NumberDecimalSeparator = "."
126
132
Nut_Socket = New Nut_Socket( Me .Nut_Config, LogFile)
127
133
128
134
With Reconnect_Nut
129
135
.Interval = DEFAULT_RECONNECT_WAIT_MS
130
136
.Enabled = False
137
+ AddHandler .Tick, AddressOf AttemptReconnect
138
+ End With
139
+
140
+ With Update_Data
141
+ .Interval = DEFAULT_UPDATE_INTERVAL_MS
142
+ .Enabled = False
143
+ AddHandler .Tick, AddressOf Retrieve_UPS_Datas
131
144
End With
132
145
End Sub
133
146
@@ -191,7 +204,7 @@ Public Class UPS_Device
191
204
End If
192
205
End Sub
193
206
194
- Private Sub Reconnect_Socket (sender As Object , e As EventArgs) Handles Reconnect_Nut.Tick
207
+ Private Sub AttemptReconnect (sender As Object , e As EventArgs)
195
208
Retry += 1
196
209
If Retry <= MaxRetry Then
197
210
RaiseEvent New_Retry()
@@ -250,46 +263,45 @@ Public Class UPS_Device
250
263
End Try
251
264
252
265
' Other constant values for UPS calibration.
253
- freshData.UPS_Value.Batt_Capacity = Double .Parse(GetUPSVar( "battery.capacity" , 7 ), ciClone )
254
- Freq_Fallback = Double .Parse(GetUPSVar( "output.frequency.nominal" , ( 50 + CInt (Arr_Reg_Key.Item( "FrequencySupply" )) * 10 )), ciClone )
266
+ freshData.UPS_Value.Batt_Capacity = Double .Parse(GetUPSVar( "battery.capacity" , 7 ), INVARIANT_CULTURE )
267
+ Freq_Fallback = Double .Parse(GetUPSVar( "output.frequency.nominal" , ( 50 + CInt (Arr_Reg_Key.Item( "FrequencySupply" )) * 10 )), INVARIANT_CULTURE )
255
268
256
269
LogFile.LogTracing( "Completed retrieval of basic UPS product information." , LogLvl.LOG_NOTICE, Me )
257
270
Return freshData
258
271
End Function
259
272
260
273
Private oldStatusBitmask As Integer
261
-
262
- Private Sub Retrieve_UPS_Datas() Handles Update_Data.Tick
274
+ Private Sub Retrieve_UPS_Datas(sender As Object , e As EventArgs)
263
275
LogFile.LogTracing( "Enter Retrieve_UPS_Datas" , LogLvl.LOG_DEBUG, Me )
264
276
265
277
Try
266
278
Dim UPS_rt_Status As String
267
279
268
280
If IsConnected Then
269
281
With UPS_Datas.UPS_Value
270
- .Batt_Charge = Double .Parse(GetUPSVar( "battery.charge" , 255 ), ciClone )
271
- .Batt_Voltage = Double .Parse(GetUPSVar( "battery.voltage" , 12 ), ciClone )
272
- .Batt_Runtime = Double .Parse(GetUPSVar( "battery.runtime" , 86400 ), ciClone )
273
- .Power_Frequency = Double .Parse(GetUPSVar( "input.frequency" , Double .Parse(GetUPSVar( "output.frequency" , Freq_Fallback), ciClone )), ciClone )
274
- .Input_Voltage = Double .Parse(GetUPSVar( "input.voltage" , 220 ), ciClone )
275
- .Output_Voltage = Double .Parse(GetUPSVar( "output.voltage" , .Input_Voltage), ciClone )
276
- .Load = Double .Parse(GetUPSVar( "ups.load" , 0 ), ciClone )
282
+ .Batt_Charge = Double .Parse(GetUPSVar( "battery.charge" , 255 ), INVARIANT_CULTURE )
283
+ .Batt_Voltage = Double .Parse(GetUPSVar( "battery.voltage" , 12 ), INVARIANT_CULTURE )
284
+ .Batt_Runtime = Double .Parse(GetUPSVar( "battery.runtime" , 86400 ), INVARIANT_CULTURE )
285
+ .Power_Frequency = Double .Parse(GetUPSVar( "input.frequency" , Double .Parse(GetUPSVar( "output.frequency" , Freq_Fallback), INVARIANT_CULTURE )), INVARIANT_CULTURE )
286
+ .Input_Voltage = Double .Parse(GetUPSVar( "input.voltage" , 220 ), INVARIANT_CULTURE )
287
+ .Output_Voltage = Double .Parse(GetUPSVar( "output.voltage" , .Input_Voltage), INVARIANT_CULTURE )
288
+ .Load = Double .Parse(GetUPSVar( "ups.load" , 0 ), INVARIANT_CULTURE )
277
289
278
290
' Retrieve and/or calculate output power if possible.
279
291
If _PowerCalculationMethod <> PowerMethod.Unavailable Then
280
292
Dim parsedValue As Double
281
293
282
294
Try
283
295
If _PowerCalculationMethod = PowerMethod.RealPower Then
284
- parsedValue = Double .Parse(GetUPSVar( "ups.realpower" ))
296
+ parsedValue = Double .Parse(GetUPSVar( "ups.realpower" ), INVARIANT_CULTURE )
285
297
286
298
ElseIf _PowerCalculationMethod = PowerMethod.NominalPowerCalc Then
287
- parsedValue = Double .Parse(GetUPSVar( "ups.realpower.nominal" ))
299
+ parsedValue = Double .Parse(GetUPSVar( "ups.realpower.nominal" ), INVARIANT_CULTURE )
288
300
parsedValue *= UPS_Datas.UPS_Value.Load / 100.0
289
301
290
302
ElseIf _PowerCalculationMethod = PowerMethod.VoltAmpCalc Then
291
- Dim nomCurrent = Double .Parse(GetUPSVar( "input.current.nominal" ))
292
- Dim nomVoltage = Double .Parse(GetUPSVar( "input.voltage.nominal" ))
303
+ Dim nomCurrent = Double .Parse(GetUPSVar( "input.current.nominal" ), INVARIANT_CULTURE )
304
+ Dim nomVoltage = Double .Parse(GetUPSVar( "input.voltage.nominal" ), INVARIANT_CULTURE )
293
305
294
306
parsedValue = (nomCurrent * nomVoltage * 0.8 ) * (UPS_Datas.UPS_Value.Load / 100 . 0 )
295
307
Else
@@ -331,7 +343,7 @@ Public Class UPS_Device
331
343
.UPS_Status = [Enum].Parse( GetType (UPS_States), UPS_rt_Status)
332
344
Catch ex As ArgumentException
333
345
LogFile.LogTracing( "Likely encountered an unknown/invalid UPS status. Using previous status." &
334
- vbNewLine & ex.Message, LogLvl.LOG_ERROR, Me )
346
+ vbNewLine & ex.Message, LogLvl.LOG_ERROR, Me )
335
347
End Try
336
348
337
349
' Get the difference between the old and new statuses, and filter only for active ones.
0 commit comments