This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathcheck_gw.py
executable file
·208 lines (185 loc) · 4.51 KB
/
check_gw.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import time
import ssl
import getopt
import datetime
import json
import paho.mqtt.client as paho
import json
topic = None
mode = None
switched = False
def usage():
print "check-gw -m mode -h hostname/address -p port -t topic [-u userId -w password] [-c cafile] [-s]"
def aged(msg):
fmt = "%s %-14s %-40s %s"
topic = str(msg.topic)
payload = str(msg.payload)
try:
data = json.loads(payload)
except:
try:
values = payload.split(',')
try:
tstamp = int(values[1], 16)
except:
tstamp = 0
if tstamp > time.time() - 6 * 1.5 * 3600:
return 0
else:
return 1
except:
print fmt % (retained, tstamp, topic, payload)
return 2
if type(data) is not dict:
print fmt % (retained, tstamp, topic, payload)
return
time_str = None
if '_type' in data:
if data['_type'] == 'location':
if 'tst' in data:
try:
tstamp = int(data['tst'])
except:
tstamp = 0
if tstamp > time.time() - 6 * 1.5 * 3600:
return 0
else:
return 1
else:
return 2
else:
return 2
else:
return 2
def on_connect(mosq, userdata, rc):
global returnValue
if rc != 0:
sys.exit(3)
else:
if mode == 'status':
mosq.subscribe('%s/status' % topic)
elif mode == 'gpio7':
mosq.subscribe('%s/gpio/7' % topic)
elif mode == 'vbatt':
mosq.subscribe('%s/voltage/batt' % topic)
elif mode == 'vext':
mosq.subscribe('%s/voltage/ext' % topic)
elif mode == 'age':
mosq.subscribe('%s' % topic)
else:
usage()
returnValue = 3
def on_disconnect(mosq, userdata, rc):
global returnValue
print "GW disconnect %d" % rc
if returnValue == -1:
returnValue = 2
def on_subscribe(client, userdata, mid, granted_qos):
pass
def on_message(mosq, userdata, message):
global returnValue
print message.payload
if mode == 'status':
if message.payload == "0":
returnValue = 2
elif message.payload == "1":
returnValue = 0
else:
if switched:
returnValue = 0
else:
returnValue = 1
elif mode == 'gpio7':
if message.payload == "0":
returnValue = 1
else:
returnValue = 0
elif mode == 'vbatt':
try:
voltage = float(message.payload)
except:
voltage = 0
if voltage > 3.6:
returnValue = 0
else:
returnValue = 1
elif mode == 'vext':
try:
voltage = float(message.payload)
except:
voltage = 0
if voltage >= 12.0:
returnValue = 0
else:
if switched:
returnValue = 0
else:
returnValue = 1
elif mode == 'age':
returnValue = aged(message)
else:
returnValue = 3
def main(argv):
global returnValue
clientID = "check-gw-%d" % os.getpid()
host = 'localhost'
userId = None
password = None
port = 1883
cafile = None
try:
opts, args = getopt.getopt(argv, "sm:h:p:u:w:t:c:", ["switched", "mode", "host", "port", "userId", "password", "topic", "cafile"])
except getopt.GetoptError as e:
usage()
print "GW getopt 3"
sys.exit(3)
for opt, arg in opts:
if opt in ('-h', '--host'):
host = arg
if opt in ('-p', '--port'):
port = arg
if opt in ('-u', '--userId'):
userId = arg
if opt in ('-w', '--password'):
password = arg
if opt in ('-t', '--topic'):
global topic
topic = arg
if opt in ('-c', '--cafile'):
cafile = arg
if opt in ('-m', '--mode'):
global mode
mode = arg
if opt in ('-s', '--switched'):
global switched
switched = True
mosq = paho.Client(clientID, clean_session=True, userdata=None)
mosq.on_connect = on_connect
mosq.on_disconnect = on_disconnect
mosq.on_subscribe = on_subscribe
mosq.on_message = on_message
if userId != None:
mosq.username_pw_set(userId,password)
if cafile != None:
mosq.tls_set(cafile)
mosq.tls_insecure_set(True)
try:
mosq.connect(host, port, 60)
except:
print "GW connect exception 2"
sys.exit(2)
timeout = time.time() + 3
while returnValue == -1:
if time.time() > timeout:
print "GW timeout"
returnValue = 2
mosq.loop()
mosq.disconnect()
exit(returnValue)
returnValue = -1
if __name__ == '__main__':
main(sys.argv[1:])