Skip to content

Commit a5be907

Browse files
committed
- Moving _Socket and _Parameter variables into their proper classes for proper OOP
- Disabled auto-connecting to UPS on first start since it locks the GUI thread on a slow connection - Cleaned up UPS_Connect subroutine for readability, improved log messages, and add a handler for lost_connection events - Cleaned up UPSDisconnect function: Handlers removed, modified to be reactive in the UI rather than controlling the backend - Disallowed changing config on the fly from external sources (a Nut_Socket object instead could be considered immutable, and must be destroyed if needing to change settings. Hopefully prevents unexpected bugs) - Add a dedicated disconnect subroutine to the Socket class. - Fixed underlying socket not being connected - Removed dedicated ConnectionStatus variable; not quite sure what its purpose was (might regret this...) - Made UPS_Device the dedicated object for handling connection monitoring logic (was competing with _Socket before)
1 parent eae2a4d commit a5be907

File tree

5 files changed

+114
-84
lines changed

5 files changed

+114
-84
lines changed

WinNUT_V2/WinNUT-Client_Common/Common_Classes.vb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ Public Class Nut_Parameter
5252
Public UPSName As String = ""
5353
Public AutoReconnect As Boolean = False
5454

55+
Public Sub New(Host As String, Port As Integer, Login As String, Password As String, UPSName As String,
56+
Optional AutoReconnect As Boolean = False)
57+
Me.Host = Host
58+
Me.Port = Port
59+
Me.Login = Login
60+
Me.Password = Password
61+
Me.UPSName = UPSName
62+
Me.AutoReconnect = AutoReconnect
63+
End Sub
64+
5565
''' <summary>
5666
''' Generate an informative String representing this Parameter object. Note password is not printed.
5767
''' </summary>

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ Imports System.IO
1515
Imports System.Windows.Forms
1616

1717
Public Class Nut_Socket
18+
1819
#Region "Properties"
19-
Public ReadOnly Property ConnectionStatus
20+
21+
Public ReadOnly Property ConnectionStatus As Boolean
2022
Get
2123
If NutSocket IsNot Nothing Then
2224
Return NutSocket.Connected
@@ -25,6 +27,7 @@ Public Class Nut_Socket
2527
End If
2628
End Get
2729
End Property
30+
2831
#End Region
2932

3033
Private LogFile As Logger
@@ -36,12 +39,11 @@ Public Class Nut_Socket
3639
Private WriterStream As StreamWriter
3740

3841
Private Nut_Parameter As Nut_Parameter
42+
Private NutConfig As Nut_Parameter
3943

4044
Private Nut_Ver As String
4145
Private Net_Ver As String
4246

43-
Private Nut_Config As Nut_Parameter
44-
4547
Public Auth_Success As Boolean = False
4648
Private ReadOnly WatchDog As New Timer
4749

@@ -53,16 +55,21 @@ Public Class Nut_Socket
5355

5456
Public Event Unknown_UPS()
5557
Public Event Socket_Broken()
56-
Public Event Socket_Deconnected()
5758

58-
Public Sub New(ByVal Nut_Config As Nut_Parameter, ByRef logger As Logger)
59+
''' <summary>
60+
''' Socket was disconnected as a part of normal operations.
61+
''' </summary>
62+
Public Event SocketDisconnected()
63+
64+
Public Sub New(Nut_Config As Nut_Parameter, ByRef logger As Logger)
5965
LogFile = logger
66+
NutConfig = Nut_Config
67+
6068
With Me.WatchDog
6169
.Interval = 1000
6270
.Enabled = False
6371
AddHandler .Tick, AddressOf Event_WatchDog
6472
End With
65-
Update_Config(Nut_Config)
6673
End Sub
6774
Public ReadOnly Property IsConnected() As Boolean
6875
Get
@@ -96,17 +103,17 @@ Public Class Nut_Socket
96103
End If
97104
End Get
98105
End Property
99-
Public Sub Update_Config(ByVal New_Nut_Config As Nut_Parameter)
100-
Me.Nut_Config = New_Nut_Config
101-
End Sub
106+
'Public Sub Update_Config(ByVal New_Nut_Config As Nut_Parameter)
107+
' Me.NutConfig = New_Nut_Config
108+
'End Sub
102109

103110
Public Function Connect() As Boolean
104111
Try
105112
'TODO: Use LIST UPS protocol command to get valid UPSs.
106-
Dim Host = Nut_Config.Host
107-
Dim Port = Nut_Config.Port
108-
Dim Login = Nut_Config.Login
109-
Dim Password = Nut_Config.Password
113+
Dim Host = NutConfig.Host
114+
Dim Port = NutConfig.Port
115+
Dim Login = NutConfig.Login
116+
Dim Password = NutConfig.Password
110117

111118
If Not String.IsNullOrEmpty(Host) And Not IsNothing(Port) Then
112119
Create_Socket(Host, Port)
@@ -145,16 +152,30 @@ Public Class Nut_Socket
145152
Return False
146153
End Function
147154

148-
Private Sub Create_Socket(ByVal Host As String, ByVal Port As Integer)
155+
''' <summary>
156+
''' Gracefully disconnect the socket from the NUT server.
157+
''' </summary>
158+
''' <param name="Silent">Skip raising the <see cref="SocketDisconnected"/> event if true.</param>
159+
Public Sub Disconnect(Optional Silent = False)
160+
WatchDog.Stop()
161+
Query_Data("LOGOUT")
162+
Close_Socket()
163+
164+
If Not Silent Then
165+
RaiseEvent SocketDisconnected()
166+
End If
167+
End Sub
168+
169+
Private Sub Create_Socket(Host As String, Port As Integer)
149170
Try
150171
' NutSocket = New Socket(AddressFamily.InterNetwork, ProtocolType.IP)
151172
NutSocket = New Socket(SocketType.Stream, ProtocolType.IP)
152173
LogFile.LogTracing(String.Format("Attempting TCP socket connection to {0}:{1}...", Host, Port), LogLvl.LOG_NOTICE, Me)
174+
NutSocket.Connect(Host, Port)
153175
NutTCP = New TcpClient(Host, Port)
154176
NutStream = NutTCP.GetStream
155177
ReaderStream = New StreamReader(NutStream)
156178
WriterStream = New StreamWriter(NutStream)
157-
' ConnectionStatus = True
158179
LogFile.LogTracing(String.Format("Connection established and streams ready for {0}:{1}", Host, Port), LogLvl.LOG_NOTICE, Me)
159180
Catch Excep As Exception
160181
Disconnect(True)
@@ -191,7 +212,7 @@ Public Class Nut_Socket
191212
RaiseEvent Socket_Broken()
192213
Throw New Nut_Exception(Nut_Exception_Value.SOCKET_BROKEN, VarName)
193214
Else
194-
Dim Nut_Query = Query_Data("GET DESC " & Me.Nut_Config.UPSName & " " & VarName)
215+
Dim Nut_Query = Query_Data("GET DESC " & Me.NutConfig.UPSName & " " & VarName)
195216
Select Case Nut_Query.Response
196217
Case NUTResponse.OK
197218
'LogFile.LogTracing("Process Result With " & VarName & " : " & Nut_Query.Data, LogLvl.LOG_DEBUG, Me)
@@ -434,17 +455,4 @@ Public Class Nut_Socket
434455
LogLvl.LOG_ERROR, Me)
435456
End Try
436457
End Sub
437-
438-
''' <summary>
439-
''' Gracefully disconnect the socket from the NUT server.
440-
''' </summary>
441-
''' <param name="ForceDisconnect">Skip raising the <see cref="Socket_Deconnected"/> event if true.</param>
442-
Public Sub Disconnect(Optional ByVal ForceDisconnect = False)
443-
Query_Data("LOGOUT")
444-
Close_Socket()
445-
Me.WatchDog.Stop()
446-
If Not ForceDisconnect Then
447-
RaiseEvent Socket_Deconnected()
448-
End If
449-
End Sub
450458
End Class

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Public Class UPS_Device
1212
'Private Nut_Conn As Nut_Comm
1313
' Private LogFile As Logger
1414
Private Freq_Fallback As Double
15-
Private ciClone As System.Globalization.CultureInfo
15+
Private ciClone As CultureInfo
1616
Private Const CosPhi As Double = 0.6
1717

1818
Public Nut_Config As Nut_Parameter
@@ -23,8 +23,10 @@ Public Class UPS_Device
2323
Public Retry As Integer = 0
2424
Public MaxRetry As Integer = 30
2525
Private ReadOnly Reconnect_Nut As New System.Windows.Forms.Timer
26+
2627
Private ReadOnly WatchDog As New System.Windows.Forms.Timer
27-
Private Socket_Status As Boolean = False
28+
29+
Private keepConnection As Boolean = False
2830

2931

3032
Private LogFile As Logger
@@ -84,7 +86,7 @@ Public Class UPS_Device
8486

8587
Public ReadOnly Property IsConnected() As Boolean
8688
Get
87-
Return (Me.Nut_Socket.IsConnected And Me.Socket_Status)
89+
Return (Me.Nut_Socket.IsConnected) ' And Me.keepConnection
8890
End Get
8991
End Property
9092

@@ -101,7 +103,7 @@ Public Class UPS_Device
101103
LogFile.LogTracing("WatchDog Socket report a Broken State", LogLvl.LOG_WARNING, Me)
102104
Nut_Socket.Disconnect(True)
103105
RaiseEvent Lost_Connect()
104-
Me.Socket_Status = False
106+
keepConnection = False
105107
End If
106108
End If
107109
End Sub
@@ -122,7 +124,7 @@ Public Class UPS_Device
122124
.Enabled = False
123125
AddHandler .Tick, AddressOf Event_WatchDog
124126
End With
125-
AddHandler Nut_Socket.Socket_Deconnected, AddressOf Socket_Deconnected
127+
AddHandler Nut_Socket.SocketDisconnected, AddressOf Socket_Deconnected
126128
' Connect_UPS()
127129
End Sub
128130

@@ -131,8 +133,8 @@ Public Class UPS_Device
131133
LogFile.LogTracing("Beginning connection: " & Nut_Config.ToString(), LogLvl.LOG_DEBUG, Me)
132134

133135
If Me.Nut_Socket.Connect() And Me.Nut_Socket.IsConnected Then
134-
LogFile.LogTracing("TCP Socket Created", LogLvl.LOG_NOTICE, Me)
135-
Me.Socket_Status = True
136+
' LogFile.LogTracing("TCP Socket Created", LogLvl.LOG_NOTICE, Me)
137+
keepConnection = True
136138
If Nut_Socket.IsKnownUPS(Nut_Config.UPSName) Then
137139
Me.UPS_Datas = GetUPSProductInfo()
138140
Init_Constant(Nut_Socket)
@@ -141,11 +143,11 @@ Public Class UPS_Device
141143
LogFile.LogTracing("Given UPS Name is unknown", LogLvl.LOG_NOTICE, Me)
142144
RaiseEvent Unknown_UPS()
143145
End If
144-
Me.WatchDog.Start()
146+
WatchDog.Start()
145147
'Else
146148
' If Not Reconnect_Nut.Enabled Then
147149
' RaiseEvent Lost_Connect()
148-
' Me.Socket_Status = False
150+
' Me.keepConnection = False
149151
' End If
150152
End If
151153
End Sub
@@ -156,6 +158,10 @@ Public Class UPS_Device
156158
' End If
157159
'End Sub
158160

161+
Public Sub Disconnect()
162+
163+
End Sub
164+
159165
Private Function GetUPSProductInfo() As UPS_Datas
160166
Dim UDatas As New UPS_Datas
161167
Dim UPSName = Me.Nut_Config.UPSName
@@ -377,13 +383,13 @@ Public Class UPS_Device
377383
Return StringArray(StringArray.Length - 1)
378384
End Function
379385

380-
Private Sub Socket_Deconnected()
386+
Private Sub Socket_Deconnected() Handles Nut_Socket.SocketDisconnected
381387
WatchDog.Stop()
382-
LogFile.LogTracing("TCP Socket Deconnected", LogLvl.LOG_WARNING, Me)
383-
If Not Me.Socket_Status Then
388+
LogFile.LogTracing("TCP Socket Deconnected", LogLvl.LOG_NOTICE, Me)
389+
If Not IsConnected And keepConnection Then
384390
RaiseEvent Lost_Connect()
385391
End If
386-
Me.Socket_Status = False
392+
' Me.keepConnection = False
387393
If Me.Nut_Config.AutoReconnect Then
388394
LogFile.LogTracing("Reconnection Process Started", LogLvl.LOG_NOTICE, Me)
389395
Reconnect_Nut.Enabled = True
@@ -395,6 +401,7 @@ Public Class UPS_Device
395401
LogFile.LogTracing("TCP Socket seems Broken", LogLvl.LOG_WARNING, Me)
396402
Socket_Deconnected()
397403
End Sub
404+
398405
Private Sub Reconnect_Socket(sender As Object, e As EventArgs)
399406
Me.Retry += 1
400407
If Me.Retry <= Me.MaxRetry Then

WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99

1010
Imports WinNUT_Client_Common
11-
Imports System.Threading
12-
Imports System.ComponentModel
1311

1412
Public Class List_Var_Gui
1513
Private List_Var_Datas As List(Of UPS_List_Datas)
1614
Private LogFile As Logger
17-
Private UPS_Name = WinNUT.Nut_Config.UPSName
15+
Private UPS_Name = WinNUT.UPS_Device.Nut_Config.UPSName
1816
Private Sub List_Var_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load
1917
' Me.LogFile = WinNUT.LogFile
2018
LogFile.LogTracing("Load List Var Gui", LogLvl.LOG_DEBUG, Me)

0 commit comments

Comments
 (0)