-
Notifications
You must be signed in to change notification settings - Fork 144
/
cli.py
261 lines (228 loc) · 9.29 KB
/
cli.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
#
# rtlsdr_scan
#
# http://eartoearoak.com/software/rtlsdr-scanner
#
# Copyright 2012 - 2015 Al Brown
#
# A frequency scanning GUI for the OsmoSDR rtl-sdr library at
# http://sdr.osmocom.org/trac/wiki/rtl-sdr
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, or (at your option)
# any later version.
#
# 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
from collections import OrderedDict
import os
import sys
from threading import Thread
import threading
import time
from urlparse import urlparse
from constants import SAMPLE_RATE
from devices import DeviceRTL, get_devices_rtl
from events import Event, post_event, EventThread
from file import save_plot, export_plot, ScanInfo, File
from location import ThreadLocation
from misc import nearest, calc_real_dwell, next_2_to_pow
from scan import ThreadScan, anaylse_data, update_spectrum
from settings import Settings
class Cli(object):
def __init__(self, pool, args):
start = args.start
end = args.end
sweeps = args.sweeps
gain = args.gain
dwell = args.dwell
nfft = args.fft
lo = args.lo
index = args.index
remote = args.remote
directory, filename = os.path.split(args.file)
_null, ext = os.path.splitext(args.file)
self.lock = threading.Lock()
self.stepsTotal = 0
self.steps = 0
self.spectrum = OrderedDict()
self.locations = OrderedDict()
self.settings = Settings(load=False)
self.queueNotify = Queue.Queue()
self.queueScan = Queue.Queue()
self.queueLocation = Queue.Queue()
self.threadLocation = None
error = None
if end <= start:
error = "Start should be lower than end"
elif dwell <= 0:
error = "Dwell should be positive"
elif nfft <= 0:
error = "FFT bins should be positive"
elif ext != ".rfs" and File.get_type_index(ext) == -1:
error = "File extension should be "
error += File.get_type_pretty(File.Types.SAVE)
error += File.get_type_pretty(File.Types.PLOT)
else:
device = DeviceRTL()
if remote is None:
self.settings.devicesRtl = get_devices_rtl()
count = len(self.settings.devicesRtl)
if index > count - 1:
error = "Device not found ({} devices in total):\n".format(count)
for device in self.settings.devicesRtl:
error += "\t{}: {}\n".format(device.indexRtl,
device.name)
else:
device.isDevice = False
url = urlparse('//' + remote)
if url.hostname is not None:
device.server = url.hostname
else:
error = "Invalid hostname"
if url.port is not None:
device.port = url.port
else:
device.port = 1234
self.settings.devicesRtl.append(device)
index = len(self.settings.devicesRtl) - 1
if args.conf is not None:
if os.path.exists(args.conf):
error = self.settings.load_conf(args.conf)
else:
error = 'Cannot find {}'.format(args.conf)
if end - 1 < start:
end = start + 1
if remote is None:
if len(self.settings.devicesRtl):
gain = nearest(gain, self.settings.devicesRtl[index].gains)
else:
error = 'No devices found'
if error is not None:
print "Error: {}".format(error)
exit(1)
self.settings.start = start
self.settings.stop = end
self.settings.dwell = calc_real_dwell(dwell)
self.settings.scanDelay = args.delay
self.settings.nfft = nfft
self.settings.devicesRtl[index].gain = gain
self.settings.devicesRtl[index].lo = lo
print "{} - {}MHz".format(start, end)
print "{} Sweeps".format(sweeps)
print "{}dB Gain".format(gain)
print "{}s Dwell".format(self.settings.dwell)
print "{} FFT points".format(nfft)
print "{}MHz LO".format(lo)
if remote is not None:
print remote
else:
print self.settings.devicesRtl[index].name
if len(self.settings.devicesGps):
self.threadLocation = ThreadLocation(self.queueLocation,
self.settings.devicesGps[0])
if not self.__gps_wait():
self.__gps_stop()
exit(1)
self.__scan(sweeps, self.settings, index, pool)
fullName = os.path.join(directory, filename)
if ext == ".rfs":
scanInfo = ScanInfo()
scanInfo.set_from_settings(self.settings)
save_plot(fullName, scanInfo, self.spectrum, self.locations)
else:
exportType = File.get_type_index(ext)
export_plot(fullName, exportType, self.spectrum)
self.__gps_stop()
print "Done"
def __gps_wait(self):
print '\nWaiting for GPS fix: {}'.format(self.settings.devicesGps[0].get_serial_desc())
while True:
if not self.queueLocation.empty():
status = self.__process_event(self.queueLocation, None)
if status == Event.LOC:
return True
elif status == Event.LOC_ERR:
return False
def __gps_stop(self):
if self.threadLocation and self.threadLocation.isAlive():
self.threadLocation.stop()
def __scan(self, sweeps, settings, index, pool):
samples = settings.dwell * SAMPLE_RATE
samples = next_2_to_pow(int(samples))
for sweep in range(0, sweeps):
print '\nSweep {}:'.format(sweep + 1)
threadScan = ThreadScan(self.queueNotify, self.queueScan, None,
settings, index, samples, False)
while threadScan.isAlive() or self.steps > 0:
if not self.queueNotify.empty():
self.__process_event(self.queueNotify, pool)
if not self.queueLocation.empty():
self.__process_event(self.queueLocation, pool)
if self.settings.scanDelay > 0 and sweep < sweeps - 1:
print '\nDelaying {}s'.format(self.settings.scanDelay)
time.sleep(self.settings.scanDelay)
print ""
print ""
def __process_event(self, queue, pool):
event = queue.get()
status = event.data.get_status()
arg1 = event.data.get_arg1()
arg2 = event.data.get_arg2()
if status == Event.STARTING:
print "Starting"
elif status == Event.STEPS:
self.stepsTotal = (arg1 + 1) * 2
self.steps = self.stepsTotal
elif status == Event.INFO:
if arg2 != -1:
self.settings.devicesRtl[self.settings.indexRtl].tuner = arg2
elif status == Event.DATA:
cal = self.settings.devicesRtl[self.settings.indexRtl].calibration
freq, scan = self.queueScan.get()
pool.apply_async(anaylse_data, (freq, scan, cal,
self.settings.nfft,
self.settings.overlap,
"Hamming"),
callback=self.__on_process_done)
self.__progress()
elif status == Event.ERROR:
print "Error: {}".format(arg2)
exit(1)
elif status == Event.PROCESSED:
offset = self.settings.devicesRtl[self.settings.indexRtl].offset
Thread(target=update_spectrum, name='Update',
args=(self.queueNotify, self.lock, self.settings.start,
self.settings.stop, arg1,
arg2, offset, self.spectrum, False,)).start()
elif status == Event.UPDATED:
self.__progress()
elif status == Event.LOC:
if len(self.spectrum) > 0:
self.locations[max(self.spectrum)] = (arg2[0],
arg2[1],
arg2[2])
elif status == Event.LOC_ERR:
print '{}'.format(arg2)
exit(1)
return status
def __on_process_done(self, data):
timeStamp, freq, scan = data
post_event(self.queueNotify, EventThread(Event.PROCESSED, freq,
(timeStamp, scan)))
def __progress(self):
self.steps -= 1
comp = (self.stepsTotal - self.steps) * 100 / self.stepsTotal
sys.stdout.write("\r{0:.1f}%".format(comp))
if __name__ == '__main__':
print 'Please run rtlsdr_scan.py'
exit(1)