-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
297 lines (247 loc) · 9.43 KB
/
application.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
from __future__ import print_function
import config_stuff
import serial
import signal
import requests
import time
import traceback
import read_page
import random
import urllib
from subprocess import check_output
from datetime import datetime
from page_log import Logger, NullLogger
from status_api import StatusLog
from arena_api import ArenaAPI
from actions import perform
DEFAULT_PORT = '/dev/serial0'
DEFAULT_BAUD = 9600
DEFAULT_TIMEOUT = 5. * 60
ROLLOFF_SEC = [5, 15, 30, 60, 120, 240]
class SilentPushover(object):
def send_message(self, *args, **kwargs):
pass
class PagerPI(object):
debug = False
verbose = False
stop = False
startup_attempt = 0
needs_startup = True
need_sleep = None
ip_addresses = "UNSET"
ROLLOFF_SEC = ROLLOFF_SEC
def __init__(self, pager=None, override_config=None, pagerrc=None):
# A list of pager messages that we have received but not
# logged to the status server.
self.messages = []
# A map from traceback to a list of timestamps of when the
# errors occurred that have not yet been sent to the status
# server.
self.errors = {}
if pagerrc is None:
pagerrc = ['..', 'pagerrc.json']
# load configuration data.
self.config = config_stuff.configure(pagerrc, override_config or {})
# Actions requested by the server, yet to be performed.
self.actions = []
self.perform = perform
# The serial device that we are reading pager messages from.
self.pager = pager
if pager is None:
self.pager = serial.Serial(
port=self.config.get('port', DEFAULT_PORT),
baudrate=self.config.get('baud', DEFAULT_BAUD),
timeout=self.config.get('timeout', DEFAULT_TIMEOUT)
)
# An object that sends messages to the status service.
self.status_log = StatusLog(self)
# An object that sends PDD messages to Arena.
self.arena_api = ArenaAPI(self)
# Application metrics.
self.status = {'alert_messages': 0,
'other_messages': 0,
'last_read_time': None}
# An object that can send messages via pushover.
try:
import pushover
self.pushover = pushover.Client()
self.public_pushover = pushover.Client(profile="Public")
except Exception:
self.pushover = SilentPushover()
self.public_pushover = self.pushover
@property
def log(self):
if self.config.get('silent'):
return NullLogger()
log_factory = self.config.get('log_factory', Logger)
return log_factory(self.verbose, self.config.get('lineFile'))
def startup(self):
"""Perform startup tasks
"""
try:
# report our IP address via pushover.
self.send_addresses()
# report to the status server that we have started.
self.status_log.startup()
except Exception as e:
self.on_exception(e)
# don't try to start up again for this many seconds.
if self.startup_attempt < len(self.ROLLOFF_SEC):
self.need_sleep = self.ROLLOFF_SEC[self.startup_attempt]
self.startup_attempt += 1
else:
self.need_sleep = self.ROLLOFF_SEC[-1]
raise
else:
self.startup_attempt = 0
self.needs_startup = False
def main_once(self):
# Perform any pending requested actions.
actions = self.actions
self.actions = []
for action in actions:
try:
self.perform(self, action)
except Exception as e:
self.on_exception(e)
if self.needs_startup:
# Connect to the server to report our version and state.
self.startup()
# Open the serial port.
if not self.pager.is_open:
try:
pager.open()
except Exception as e:
self.need_sleep = 5
raise Exception("Failed to open serial port: %s" %
(e.message,))
# read one line from the pager receiver
try:
data = self.pager.readline()
except Exception as e:
self.pager.close()
raise
if data:
self.status['last_read_time'] = datetime.now()
self.log.pager_log_all(data)
# parse & handle the data that we read. this will create
# a pdd request in Arena if the message is an alert.
self.handle_serial_data(data)
elif self.verbose:
print('No data within timeout period')
# notify the status server of our activity.
try:
resp = self.status_log.message(self.messages, self.errors)
except Exception as exception:
self.on_exception(exception)
self.needs_startup = True
else:
print(resp)
if resp.get('errors', None):
self.log.report_server_error(resp)
self.needs_startup = True
else:
self.messages = []
self.errors = {}
def main(self):
while not self.stop:
try:
self.main_once()
except Exception as exception:
self.on_exception_main(exception)
if self.need_sleep is not None:
time.sleep(self.need_sleep)
self.need_sleep = None
def shutdown_handler(self, signum, frame):
print("Received shutdown signal")
raise _SHUTDOWN
def send_addresses(self):
ip_addresses = check_output(["hostname", "-I"]).strip()
if ip_addresses != self.ip_addresses:
print("Sending IP Addresses [", ip_addresses, "]")
self.pushover.send_message(ip_addresses, title="My IP")
self.ip_addresses = ip_addresses
def handle_serial_data(self, pager_message):
alert = read_page.read_alert_message(self, pager_message)
if alert:
if self.debug and alert['lat'] is None:
self.make_random_geo(alert)
if self.verbose:
read_page.show_alert_message(alert)
else:
print('alert message: %r' % (pager_message,))
print('alert message[message]: %r' % (alert['message'],))
self.on_alert_message(alert)
else:
self.on_unhandled_message(pager_message)
print('other message: %r' % (pager_message,))
def make_random_geo(self, alert):
"""For debugging the Arena integration.
Generate a random location and whether it is an aircraft message.
"""
if self.verbose:
print("NO Geo Coords - going random!")
alert['lat'] = -37.616+random.uniform(-1, 1)
alert['lon'] = 144.420+random.uniform(-1, 1)
if random.randint(0,9) > 5:
if self.verbose:
print("Random aircraft message generated!")
alert['aircraftMsg'] = 1
def send_public_message(self, alert):
self.public_pushover.send_message(alert['message'],
title="CFA Alert")
def on_alert_message(self, alert):
self.messages.append({'ts' : str(datetime.now()),
'type' : 'alert',
'message' : alert['message']})
self.send_public_message(alert)
if alert['lat'] is not None:
self.arena_api.record_pdd(alert)
self.status['alert_messages'] += 1
def on_unhandled_message(self, message):
self.messages.append({'ts' : str(datetime.now()),
'type' : 'pager_message',
'message' : read_page.clean_message(message)})
self.status['other_messages'] += 1
def on_exception_main(self, exception):
"""Report an exception at the top level.
Useful hook for tests, which should probably fail immediately
if any unexpected exceptions occur.
"""
self.on_exception(exception)
def on_exception(self, exception):
"""Report an exception.
Print the exception to the console and log it for later
sending to the status service.
"""
exception_text = traceback.format_exception_only(type(exception),
exception)
now = datetime.now()
self.errors.setdefault(''.join(exception_text), []).append(
{'ts' : str(now)})
self.log.report_exception(now, exception)
class _Shutdown(BaseException):
"""A request for shutdown of the pager service.
Note that this is a BaseException, which means it will not be
caught by the usual exception machinery.
"""
_SHUTDOWN = _Shutdown("TERM signal received")
def main(debug=False, verbose=True, no_pushover=False):
print("PagerPI Start")
if verbose:
print(datetime.now().isoformat())
try:
pagerpi = PagerPI()
pagerpi.debug = debug
pagerpi.verbose = verbose
if no_pushover:
pagerpi.send_addresses = lambda: None
signal.signal(signal.SIGTERM, pagerpi.shutdown_handler)
pagerpi.main()
except (_Shutdown, KeyboardInterrupt):
if debug:
import traceback
traceback.print_exc()
pass
if __name__ == '__main__':
main()