forked from juggie/AlarmServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarmserver.py
executable file
·332 lines (283 loc) · 13 KB
/
alarmserver.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/python
## Alarm Server
## Supporting Envisalink 2DS/3
## Contributors: https://github.com/juggie/AlarmServer/graphs/contributors
## Compatibility: https://github.com/juggie/AlarmServer/wiki/Compatibility
##
## This code is under the terms of the GPL v3 license.
import asyncore, asynchat
import ConfigParser
import datetime
import os, socket, string, sys, httplib, urllib, urlparse, ssl
import json
import hashlib
import time
import getopt
import logging
logger = logging.getLogger('alarmserver')
logger.setLevel(logging.DEBUG)
# Console handler
# Prints all messages (debug level)
ch = logging.StreamHandler();
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter(
fmt='%(asctime)s %(name)s %(levelname)s: %(message)s',
datefmt='%b %d %H:%M:%S')
ch.setFormatter(formatter);
# add handlers to logger
logger.addHandler(ch)
import HTTPChannel
import Envisalink
LOGTOFILE = False
class CodeError(Exception): pass
MAXPARTITIONS=16
MAXZONES=128
MAXALARMUSERS=47
CONNECTEDCLIENTS={}
def to_chars(string):
chars = []
for char in string:
chars.append(ord(char))
return chars
def get_checksum(code, data):
return ("%02X" % sum(to_chars(code)+to_chars(data)))[-2:]
#currently supports pushover notifications, more to be added
#including email, text, etc.
#to be fixed!
def send_notification(config, message):
if config.PUSHOVER_ENABLE == True:
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": "qo0nwMNdX56KJl0Avd4NHE2onO4Xff",
"user": config.PUSHOVER_USERTOKEN,
"message": str(message),
}), { "Content-type": "application/x-www-form-urlencoded" })
class AlarmServerConfig():
def __init__(self, configfile):
self._config = ConfigParser.ConfigParser()
self._config.read(configfile)
self.LOGURLREQUESTS = self.read_config_var('alarmserver', 'logurlrequests', True, 'bool')
self.HTTPSPORT = self.read_config_var('alarmserver', 'httpsport', 8111, 'int')
self.CERTFILE = self.read_config_var('alarmserver', 'certfile', 'server.crt', 'str')
self.KEYFILE = self.read_config_var('alarmserver', 'keyfile', 'server.key', 'str')
self.MAXEVENTS = self.read_config_var('alarmserver', 'maxevents', 10, 'int')
self.MAXALLEVENTS = self.read_config_var('alarmserver', 'maxallevents', 100, 'int')
self.ENVISALINKHOST = self.read_config_var('envisalink', 'host', 'envisalink', 'str')
self.ENVISALINKPORT = self.read_config_var('envisalink', 'port', 4025, 'int')
self.ENVISALINKPASS = self.read_config_var('envisalink', 'pass', 'user', 'str')
self.ENABLEPROXY = self.read_config_var('envisalink', 'enableproxy', True, 'bool')
self.ENVISALINKPROXYPORT = self.read_config_var('envisalink', 'proxyport', self.ENVISALINKPORT, 'int')
self.ENVISALINKPROXYPASS = self.read_config_var('envisalink', 'proxypass', self.ENVISALINKPASS, 'str')
self.PUSHOVER_ENABLE = self.read_config_var('pushover', 'enable', False, 'bool')
self.PUSHOVER_USERTOKEN = self.read_config_var('pushover', 'enable', False, 'bool')
self.ALARMCODE = self.read_config_var('envisalink', 'alarmcode', 1111, 'int')
self.EVENTTIMEAGO = self.read_config_var('alarmserver', 'eventtimeago', True, 'bool')
self.LOGFILE = self.read_config_var('alarmserver', 'logfile', '', 'str')
global LOGTOFILE
if self.LOGFILE == '':
LOGTOFILE = False
else:
LOGTOFILE = True
self.PARTITIONNAMES={}
for i in range(1, MAXPARTITIONS+1):
partition = self.read_config_var('alarmserver', 'partition'+str(i), False, 'str', True)
if partition: self.PARTITIONNAMES[i] = partition
self.ZONENAMES={}
for i in range(1, MAXZONES+1):
zone = self.read_config_var('alarmserver', 'zone'+str(i), False, 'str', True)
if zone: self.ZONENAMES[i] = zone
self.ALARMUSERNAMES={}
for i in range(1, MAXALARMUSERS+1):
user = self.read_config_var('alarmserver', 'user'+str(i), False, 'str', True)
if user: self.ALARMUSERNAMES[i] = user
if self.PUSHOVER_USERTOKEN == False and self.PUSHOVER_ENABLE == True: self.PUSHOVER_ENABLE = False
def defaulting(self, section, variable, default, quiet = False):
if quiet == False:
print('Config option '+ str(variable) + ' not set in ['+str(section)+'] defaulting to: \''+str(default)+'\'')
def read_config_var(self, section, variable, default, type = 'str', quiet = False):
try:
if type == 'str':
return self._config.get(section,variable)
elif type == 'bool':
return self._config.getboolean(section,variable)
elif type == 'int':
return int(self._config.get(section,variable))
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
self.defaulting(section, variable, default, quiet)
return default
class AlarmServer(asyncore.dispatcher):
def __init__(self, config):
# Call parent class's __init__ method
asyncore.dispatcher.__init__(self)
# Create Envisalink client object
self._envisalinkclient = Envisalink.Client(config, CONNECTEDCLIENTS)
#Store config
self._config = config
logger.info('AlarmServer on HTTPS port {}'.format(config.HTTPSPORT))
# Create socket and listen on it
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.bind(("", config.HTTPSPORT))
self.listen(5)
def handle_accept(self):
# Accept the connection
conn, addr = self.accept()
if (config.LOGURLREQUESTS):
logger.debug('Incoming web connection from %s' % repr(addr))
try:
HTTPChannel.HTTPChannel(self, ssl.wrap_socket(conn, server_side=True, certfile=config.CERTFILE, keyfile=config.KEYFILE, ssl_version=ssl.PROTOCOL_TLSv1), addr)
except ssl.SSLError:
logger.warning('Failed https connection, attempted with http')
return
def handle_request(self, channel, method, request, header):
if (config.LOGURLREQUESTS):
logger.debug('Web request: '+str(method)+' '+str(request))
query = urlparse.urlparse(request)
query_array = urlparse.parse_qs(query.query, True)
if query.path == '/':
channel.pushfile('index.html');
elif query.path == '/api':
channel.pushok(json.dumps(self._envisalinkclient._alarmstate))
elif query.path == '/api/alarm/arm':
channel.pushok(json.dumps({'response' : 'Request to arm received'}))
self._envisalinkclient.send_command('030', '1')
elif query.path == '/api/alarm/stayarm':
channel.pushok(json.dumps({'response' : 'Request to arm in stay received'}))
self._envisalinkclient.send_command('031', '1')
elif query.path == '/api/alarm/armwithcode':
channel.pushok(json.dumps({'response' : 'Request to arm with code received'}))
self._envisalinkclient.send_command('033', '1' + str(query_array['alarmcode'][0]))
elif query.path == '/api/pgm':
channel.pushok(json.dumps({'response' : 'Request to trigger PGM'}))
#self._envisalinkclient.send_command('020', '1' + str(query_array['pgmnum'][0]))
self._envisalinkclient.send_command('071', '1' + "*7" + str(query_array['pgmnum'][0]))
time.sleep(1)
self._envisalinkclient.send_command('071', '1' + str(query_array['alarmcode'][0]))
elif query.path == '/api/alarm/disarm':
channel.pushok(json.dumps({'response' : 'Request to disarm received'}))
if 'alarmcode' in query_array:
self._envisalinkclient.send_command('040', '1' + str(query_array['alarmcode'][0]))
else:
self._envisalinkclient.send_command('040', '1' + str(self._config.ALARMCODE))
elif query.path == '/api/refresh':
channel.pushok(json.dumps({'response' : 'Request to refresh data received'}))
self._envisalinkclient.send_command('001', '')
elif query.path == '/api/config/eventtimeago':
channel.pushok(json.dumps({'eventtimeago' : str(self._config.EVENTTIMEAGO)}))
elif query.path == '/img/glyphicons-halflings.png':
channel.pushfile('glyphicons-halflings.png')
elif query.path == '/img/glyphicons-halflings-white.png':
channel.pushfile('glyphicons-halflings-white.png')
elif query.path == '/favicon.ico':
channel.pushfile('favicon.ico')
else:
if len(query.path.split('/')) == 2:
try:
with open(sys.path[0] + os.sep + 'ext' + os.sep + query.path.split('/')[1]) as f:
f.close()
channel.pushfile(query.path.split('/')[1])
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
channel.pushstatus(404, "Not found")
channel.push("Content-type: text/html\r\n")
channel.push("File not found")
channel.push("\r\n")
else:
if (config.LOGURLREQUESTS):
logger.warning("Invalid file requested")
channel.pushstatus(404, "Not found")
channel.push("Content-type: text/html\r\n")
channel.push("\r\n")
def handle_error(self):
logger.exception('AlarmServer exception')
class ProxyChannel(asynchat.async_chat):
def __init__(self, server, proxypass, sock, addr):
asynchat.async_chat.__init__(self, sock)
self.server = server
self.set_terminator("\r\n")
self._buffer = []
self._server = server
self._clientMD5 = hashlib.md5(str(addr)).hexdigest()
self._straddr = str(addr)
self._proxypass = proxypass
self._authenticated = False
self.send_command('5053')
def collect_incoming_data(self, data):
# Append incoming data to the buffer
self._buffer.append(data)
def found_terminator(self):
line = "".join(self._buffer)
self._buffer = []
self.handle_line(line)
def handle_line(self, line):
logger.info('PROXY REQ < '+line)
if self._authenticated == True:
self._server._envisalinkclient.send_command(line, '', False)
else:
self.send_command('500005')
expectedstring = '005' + self._proxypass + get_checksum('005', self._proxypass)
if line == ('005' + self._proxypass + get_checksum('005', self._proxypass)):
logger.info('Proxy User Authenticated')
CONNECTEDCLIENTS[self._straddr]=self
self._authenticated = True
self.send_command('5051')
else:
logger.warning('Proxy User Authentication failed')
self.send_command('5050')
self.close()
def send_command(self, data, checksum = True):
if checksum == True:
to_send = data+get_checksum(data, '')+'\r\n'
else:
to_send = data+'\r\n'
self.push(to_send)
def handle_close(self):
logger.info('Proxy connection from %s closed' % self._straddr)
if self._straddr in CONNECTEDCLIENTS: del CONNECTEDCLIENTS[self._straddr]
self.close()
def handle_error(self):
logger.exception('Proxy connection from %s errored' % self._straddr)
if self._straddr in CONNECTEDCLIENTS: del CONNECTEDCLIENTS[self._straddr]
self.close()
def usage():
print 'Usage: '+sys.argv[0]+' -c <configfile>'
def main(argv):
try:
opts, args = getopt.getopt(argv, "hc:", ["help", "config="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-c", "--config"):
global conffile
conffile = arg
if __name__=="__main__":
conffile='alarmserver.cfg'
main(sys.argv[1:])
logger.info('Using configuration file %s' % conffile)
config = AlarmServerConfig(conffile)
if LOGTOFILE:
# File handler. Only print INFO and above
fh = logging.FileHandler(config.LOGFILE)
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('-'*30)
logger.info('Alarm Server Starting')
server = AlarmServer(config)
proxy = Envisalink.Proxy(config, server)
try:
while True:
asyncore.loop(timeout=2, count=1)
# insert scheduling code here.
except KeyboardInterrupt:
print "Crtl+C pressed. Shutting down."
logger.info('Shutting down from Ctrl+C')
server.shutdown(socket.SHUT_RDWR)
server.close()
sys.exit()