forked from gawindx/WinNUT-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutNetwork.au3
167 lines (152 loc) · 3.92 KB
/
nutNetwork.au3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Global $socket = 0
Func ProcessData($data)
Local $strs
$strs = StringSplit($data, '"')
If UBound($strs) < 2 Then ;ERROR string returned or other unexpected condition
Return -1 ;return -1 which means the something bad happened and value
EndIf ;returned from NUT is unusable
If StringLeft($strs[2], 1) == "0" Then
Return StringTrimLeft($strs[2], 1)
Else
Return $strs[2]
EndIf
EndFunc ;==>ProcessData
Func CheckErr($upsresp)
Local $strs
If StringLeft($upsresp, 3) == "ERR" Then
$strs = StringSplit($upsresp, " ")
If UBound($strs) < 2 Then
Return "Uknown Error"
EndIf
Return $strs[2]
Else
Return "OK"
EndIf
EndFunc ;==>CheckErr
Func ListUPSVars($upsId, ByRef $upsVar)
Local $sendstring, $sent, $data
If $socket == 0 Then
$upsVar = "0"
Return -1
EndIf
$sendstring = "LIST VAR " & $upsId & @CRLF
$sent = TCPSend($socket, $sendstring)
If $sent == 0 Then ;connection lost
$errorstring = __("Connection lost")
WriteLog($errorstring)
$socket = 0
$upsVar = "0"
Return -1
EndIf
Sleep(500)
$data = TCPRecv($socket, 4096)
If $data == "" Then ;connection lost
$errorstring = __("Connection lost")
WriteLog($errorstring)
$socket = 0
$upsVar = "0"
Return -1
EndIf
$err = CheckErr($data)
If $err <> "OK" Then
$errorstring = $err
If StringInStr($errorstring, "UNKNOWN-UPS") <> 0 Then
WriteLog(StringFormat(__("UPS %s doesn't exist"), $upsId))
EndIf
$upsVar = "0"
Return -1
EndIf
$upsVar = $data
Return 0
EndFunc ;==>ListUPSVars
Func GetUPSDescVar($upsId, $varName, ByRef $upsVar)
Local $sendstring, $sent, $data
If $socket == 0 Then
$upsVar = "0"
Return -1
EndIf
$sendstring = "GET DESC " & $upsId & " " & $varName & @CRLF
$sent = TCPSend($socket, $sendstring)
If $sent == 0 Then ;connection lost
$errorstring = __("Connection lost")
WriteLog($errorstring)
$socket = 0
$upsVar = "0"
Return -1
EndIf
$data = TCPRecv($socket, 4096)
If $data == "" Then ;connection lost
$errorstring = __("Connection lost")
WriteLog($errorstring)
$socket = 0
$upsVar = "0"
Return -1
EndIf
$err = CheckErr($data)
If $err <> "OK" Then
$errorstring = $err
If StringInStr($errorstring, "UNKNOWN-UPS") <> 0 Then
WriteLog(StringFormat(__("UPS %s doesn't exist"), $upsId))
EndIf
$upsVar = "0"
Return -1
EndIf
$upsVar = ProcessData($data)
Return 0
EndFunc ;==>GetUPSDescVar
Func GetUPSVar($upsId, $varName, ByRef $upsVar)
Local $sendstring, $sent, $data
If $socket == 0 Then
$upsVar = "0"
Return -1
EndIf
$sendstring = "GET VAR " & $upsId & " " & $varName & @CRLF
$sent = TCPSend($socket, $sendstring)
If $sent == 0 Then ;connection lost
$errorstring = __("Connection lost")
WriteLog($errorstring)
$socket = 0
$upsVar = "0"
Return -1
EndIf
$data = TCPRecv($socket, 4096)
If $data == "" Then ;connection lost
$errorstring = __("Connection lost")
WriteLog($errorstring)
$socket = 0
$upsVar = "0"
Return -1
EndIf
$err = CheckErr($data)
If $err <> "OK" Then
$errorstring = $err
If StringInStr($errorstring, "UNKNOWN-UPS") <> 0 Then
WriteLog(StringFormat(__("UPS %s doesn't exist"), $upsId))
EndIf
$upsVar = "0"
Return -1
EndIf
$upsVar = ProcessData($data)
Return 0
EndFunc ;==>GetUPSVar
Func ConnectServer()
If $socket <> 0 Then ;already connected
WriteLog(__("Disconnecting from server"))
TCPSend($socket, "LOGOUT")
TCPCloseSocket($socket) ;disconnect first
$socket = 0
EndIf
Opt("TCPTimeout", 10)
WriteLog(__("Connecting to NUT Server"))
$ipaddr = TCPNameToIP(GetOption("ipaddr"))
$socket = TCPConnect($ipaddr, GetOption("port"))
If $socket == -1 Then ;connection failed
$haserror = 1
$errorstring = __("Connection failed")
WriteLog($errorstring)
Return -1
Else
WriteLog(__("Connection Established"))
Return 0
EndIf
EndFunc ;==>ConnectServer