Skip to content

Commit 0870b71

Browse files
committed
UPS connection logging improvements
- Adjust spacing in connection error string - Nut_Socket.vb: Enhanced error raising so Nut_Exceptions are passed as-is - Handle NUT errors at the UPS level. Currently only informative logging is happening here, extra actions taken as a result yet.
1 parent ce5be39 commit 0870b71

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

WinNUT_V2/WinNUT-Client_Common/Common_Enums.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Public Enum NUTResponse
100100
End Enum
101101

102102
Public Enum Nut_Exception_Value
103-
<StringValue("Unable to create connection :")>
103+
<StringValue("Unable to create connection: ")>
104104
CONNECT_ERROR
105105
<StringValue("Invalid Username.")>
106106
INVALID_USERNAME

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Public Class Nut_Socket
3838
Public Event OnNotice(Message As String, NoticeLvl As LogLvl, sender As Object)
3939
'Public Event OnError(Excep As Exception, NoticeLvl As LogLvl, sender As Object, ReportToGui As Boolean)
4040
Public Event OnError(Excep As Exception, NoticeLvl As LogLvl, sender As Object)
41+
Public Event OnNUTException(ex As Nut_Exception, NoticeLvl As LogLvl, sender As Object)
4142

4243
Public Event Unknown_UPS()
4344
Public Event Socket_Broken()
@@ -94,10 +95,11 @@ Public Class Nut_Socket
9495
Dim Port = Me.Nut_Config.Port
9596
Dim Login = Me.Nut_Config.Login
9697
Dim Password = Me.Nut_Config.Password
97-
Dim Response As Boolean = False
98+
9899
If Not String.IsNullOrEmpty(Host) And Not IsNothing(Port) Then
99100
If Not Create_Socket(Host, Port) Then
100-
Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR)
101+
' Don't duplicate/override Create_Socket's error throwing functionality.
102+
' Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR)
101103
Disconnect()
102104
Else
103105
If Not AuthLogin(Login, Password) Then
@@ -113,15 +115,22 @@ Public Class Nut_Socket
113115
If Nut_Query.Response = NUTResponse.OK Then
114116
Me.Net_Ver = Nut_Query.Data
115117
End If
116-
Response = True
118+
Return True
117119
End If
118120
End If
119-
Return Response
121+
122+
Catch nutEx As Nut_Exception
123+
' Handle NUT exceptions specifically, without variable boxing/unboxing
124+
RaiseEvent OnNUTException(nutEx, LogLvl.LOG_ERROR, Me)
125+
Return False
120126
Catch Excep As Exception
121127
RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me)
122128
Return False
129+
Finally
130+
123131
End Try
124132
End Function
133+
125134
Private Function Create_Socket(ByVal Host As String, ByVal Port As Integer) As Boolean
126135
Try
127136
Me.NutSocket = New Socket(AddressFamily.InterNetwork, ProtocolType.IP)
@@ -131,7 +140,7 @@ Public Class Nut_Socket
131140
Me.WriterStream = New StreamWriter(NutStream)
132141
Me.ConnectionStatus = True
133142
Catch Excep As Exception
134-
RaiseEvent OnError(New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me)
143+
RaiseEvent OnNUTException(New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me)
135144
Me.ConnectionStatus = False
136145
End Try
137146
Return Me.ConnectionStatus

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Imports System.Globalization
1111
Public Class UPS_Device
1212
'Private Nut_Conn As Nut_Comm
13-
Private LogFile As Logger
13+
' Private LogFile As Logger
1414
Private Freq_Fallback As Double
1515
Private ciClone As System.Globalization.CultureInfo
1616
Private Const CosPhi As Double = 0.6
@@ -107,7 +107,7 @@ Public Class UPS_Device
107107
End Sub
108108

109109
Public Sub New(ByVal Nut_Config As Nut_Parameter, ByRef LogFile As Logger)
110-
Me.LogFile = LogFile
110+
' Me.LogFile = LogFile
111111
Me.Nut_Config = Nut_Config
112112
Me.ciClone = CType(CultureInfo.InvariantCulture.Clone(), CultureInfo)
113113
Me.ciClone.NumberFormat.NumberDecimalSeparator = "."
@@ -410,4 +410,8 @@ Public Class UPS_Device
410410
RaiseEvent Deconnected()
411411
End If
412412
End Sub
413+
414+
Private Sub NUTSocketError(nutEx As Nut_Exception, NoticeLvl As LogLvl, sender As Object) Handles Nut_Socket.OnNUTException
415+
LogFile.LogTracing("[" & Nut_Config.UPSName & "] " & nutEx.ToString(), LogLvl.LOG_WARNING, Nut_Socket)
416+
End Sub
413417
End Class

0 commit comments

Comments
 (0)