-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathharrier.py
265 lines (217 loc) · 8.66 KB
/
harrier.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
#! /usr/bin/env python
#
# Wild Find
#
#
# Copyright 2014 - 2017 Al Brown
#
# Wildlife tracking and mapping
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import Queue
import argparse
import os
import signal
import socket
import sys
import time
from wildfind.harrier import events
from wildfind.harrier.constants import GPS_AGE, GPS_RETRY, SAMPLE_RATE
from wildfind.harrier.database import Database
from wildfind.harrier.gps import Gps
from wildfind.harrier.receive import Receive
from wildfind.harrier.server import Server
from wildfind.harrier.settings import Settings
from wildfind.harrier.status import Status
from wildfind.harrier.testmode import TestMode
from wildfind.harrier.utils import ArgparseFormatter
class Harrier(object):
def __init__(self):
settings = Settings(self.__arguments())
self._settings = settings
print 'Harrier\n'
try:
print 'Host :\t\t{} ({})'.format(socket.gethostname(),
socket.gethostbyname(socket.getfqdn()))
except socket.gaierror:
pass
queue = Queue.Queue()
if settings.test:
TestMode(settings)
return
print 'Survey:\t\t{}'.format(settings.survey)
self._gps = None
self._database = Database(settings.db, queue)
self._receive = Receive(settings, queue)
self._status = Status(self._database)
self._server = Server(queue, self._status, self._database, settings)
self._isScanning = False
self._cancel = False
self._signal = signal.signal(signal.SIGINT, self.__close)
halfBand = SAMPLE_RATE / 2e6
print 'Scan range:\t{:.2f}-{:.2f}MHz'.format(settings.freq - halfBand,
settings.freq + halfBand)
if settings.delay is None:
mode = 'Remote'
else:
mode = 'Automatic, after {}s'.format(settings.delay)
print 'Scan mode:\t{}'.format(mode)
events.Post(queue).gps_open(0)
if settings.delay is not None:
events.Post(queue).scan_start()
while not self._cancel:
if not queue.empty():
self.__process_queue(settings, queue)
else:
try:
time.sleep(0.1)
except IOError:
pass
print '\nExiting...'
waiting = self._status.get_wait()
if waiting is not None:
print '(Waiting for {} to finish)'.format(self._status.get_wait())
self._cancel = True
if self._server is not None:
self._server.stop()
if self._gps is not None:
self._gps.stop()
if self._receive is not None:
self._receive.stop()
if self._database is not None:
self._database.stop()
def __arguments(self):
parser = argparse.ArgumentParser(description='Harrier',
formatter_class=ArgparseFormatter)
dirUser = os.path.expanduser('~')
parser.add_argument('-f', '--frequency', help='Centre frequency (MHz)',
type=float, required=True)
parser.add_argument('-g', '--gain', help='Gain (dB)',
type=float)
parser.add_argument('-c', '--conf', help='Configuration file',
default=os.path.join(dirUser, 'harrier.conf'))
groupNomal = parser.add_argument_group('Scan mode')
groupNomal.add_argument('-s', '--survey', help='Survey name',
type=str,
default='Survey ' + time.strftime('%c'))
groupNomal.add_argument('file', help='File', nargs='?',
default=os.path.join(dirUser, 'harrier.wfh'))
groupTest = parser.add_argument_group('Test mode')
groupTest.add_argument('-t', '--test', help='Test mode',
action='store_true')
args = parser.parse_args()
if not os.path.exists(args.conf):
error = 'Configuration file {} not found\n'.format(args.conf)
sys.stderr.write(error)
parser.exit(1)
return args
def __process_queue(self, settings, queue):
if self._cancel:
return
event = queue.get()
eventType = event.get_type()
# Start scan
if eventType == events.SCAN_START:
location = self._status.get_location()
if location is None or time.time() - location[1] > GPS_AGE:
self._status.set_status(events.STATUS_WAIT)
events.Post(queue).scan_start(1)
elif not self._isScanning:
self._receive.receive()
self._server.send_status()
# Scan finished
elif eventType == events.SCAN_DONE:
self._isScanning = False
timeStamp = event.get_arg('time')
collars = event.get_arg('collars')
if collars is not None:
self._status.set_signals(len(collars))
for collar in collars:
location = self._status.get_location()[0]
collar.lon = location[0]
collar.lat = location[1]
self._database.append_signal(timeStamp,
collar,
settings.freq,
self._settings.survey)
else:
self._status.set_signals(0)
self._server.send_signals(timeStamp, collars)
log = 'Found {} signals'.format(len(collars))
logTime = self._database.append_log(log)
self._server.send_log(logTime, log)
if settings.delay is not None:
events.Post(queue).scan_start(settings.delay)
self._server.send_status()
# Open GPS
elif eventType == events.GPS_OPEN:
if self._gps is not None:
self._gps.stop()
self._gps.join()
self._gps = None
if self._gps is None:
print '\nStarting GPS'
self._gps = Gps(settings.gps, queue)
# GPS location
elif eventType == events.GPS_LOC:
self._status.set_location(event.get_arg('location'))
self._server.send_status()
# GPS satellites
elif eventType == events.GPS_SATS:
self._status.set_sats(event.get_arg('satellites'))
self._server.send_sats()
# GPS error
elif eventType == events.GPS_ERR:
self._gps = None
error = 'GPS error: {}'.format(event.get_arg('error'))
print '\n' + error
print 'Retry in {}s'.format(GPS_RETRY)
logTime = self._database.append_log(error)
self._server.send_log(logTime, error)
self._status.clear_gps()
self._server.send_status()
events.Post(queue).gps_open(GPS_RETRY)
# Info
elif eventType == events.INFO:
self._status.clear_gps()
info = 'Info: {}'.format(event.get_arg('info'))
print '\n' + info
logTime = self._database.append_log(info)
self._server.send_log(logTime, info)
# Warning
elif eventType == events.WARN:
self._status.clear_gps()
warning = 'Warning: {}'.format(event.get_arg('warning'))
print '\n' + warning
logTime = self._database.append_log(warning)
self._server.send_log(logTime, warning)
# Error
elif eventType == events.ERR:
error = event.get_arg('error')
sys.stderr.write(error)
logTime = self._database.append_log(error)
self._server.send_log(logTime, error)
self.__close()
else:
self._status.set_status(eventType)
self._server.send_status()
def __close(self, _signal=None, _frame=None):
signal.signal(signal.SIGINT, self._signal)
self._cancel = True
if __name__ == '__main__':
try:
Harrier()
except KeyboardInterrupt:
pass