-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutNetwork.au3
175 lines (156 loc) · 3.67 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
168
169
170
171
172
173
174
#include-once
#include <nutGlobal.au3>
#include <nutGui.au3>
#include <nutOption.au3>
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
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
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("UPS " & $upsId & " doesn't exist")
EndIf
$upsVar = "0"
return -1
EndIf
$upsVar = $data
return 0
EndFunc
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("UPS " & $upsId & " doesn't exist")
EndIf
$upsVar = "0"
return -1
EndIf
$upsVar = ProcessData($data)
return 0
EndFunc
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("UPS " & $upsId & " doesn't exist")
EndIf
$upsVar = "0"
return -1
EndIf
$upsVar = ProcessData($data)
return 0
EndFunc
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("Connection failed")
return -1
Else
WriteLog("Connection Established")
return 0
EndIf
EndFunc