/
misc.py
260 lines (197 loc) · 6.42 KB
/
misc.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
#
# 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 datetime
import json
from math import radians, sin, cos, asin, sqrt
import math
import os
import socket
import sys
from threading import Thread
import time
import urllib
import serial.tools.list_ports
from constants import SAMPLE_RATE, TIMESTAMP_FILE
class RemoteControl(object):
def __init__(self):
self.connected = False
self.socket = None
def __connect(self):
if not self.connected:
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(1)
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.socket.connect(('localhost', 3382))
self.connected = True
except socket.error:
self.connected = False
def __thread(self, command):
self.__connect()
if self.connected:
try:
self.socket.send(json.dumps(command))
self.socket.send('\r\n')
except socket.error:
self.socket.close()
self.connected = False
def __send(self, command):
thread = Thread(target=self.__thread, args=(command,))
thread.daemon = True
thread.start()
def tune(self, frequency):
command = {'Command': 'Set',
'Method': 'Frequency',
'Value': frequency}
self.__send(command)
def get_script_dir():
if not hasattr(sys, 'frozen'):
scriptDir = os.path.dirname(os.path.realpath(sys.argv[0]))
else:
scriptDir = sys._MEIPASS
return scriptDir
def get_resdir():
scriptDir = get_script_dir()
if os.path.isdir(os.path.join(scriptDir, 'res')):
resDir = os.path.join(scriptDir, 'res')
else:
resDir = os.path.join(scriptDir, '..', 'res')
return resDir
def get_resource_path(resource):
return os.path.join(get_resdir(), resource)
def limit(value, minimum, maximum):
return max(min(maximum, value), minimum)
def level_to_db(level):
return 10 * math.log10(level)
def db_to_level(dB):
return math.pow(10, dB / 10.0)
def next_2_to_pow(val):
val -= 1
val |= val >> 1
val |= val >> 2
val |= val >> 4
val |= val >> 8
val |= val >> 16
return val + 1
def calc_samples(dwell):
samples = dwell * SAMPLE_RATE
samples = next_2_to_pow(int(samples))
return samples
def get_dwells():
dwells = ["8 ms", 0.008,
"16 ms", 0.016,
"32 ms", 0.032,
"65 ms", 0.065,
"131 ms", 0.131,
"262 ms", 0.262,
"524 ms", 0.524,
"1 s", 1.048,
"2 s", 2.097,
"4 s", 4.194,
"8 s", 8.388]
if sys.platform == 'linux' or sys.platform == "linux2":
del dwells[-4:]
return dwells
def calc_real_dwell(dwell):
samples = calc_samples(dwell)
dwellReal = samples / SAMPLE_RATE
return (int)(dwellReal * 1000.0) / 1000.0
def nearest(value, values):
offset = [abs(value - v) for v in values]
return values[offset.index(min(offset))]
def haversine(lat1, lat2, lon1, lon2):
lat1, lat2, lon1, lon2 = map(radians, [lat1, lat2, lon1, lon2])
dlon = lon1 - lon2
dlat = lat1 - lat2
a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
b = asin(sqrt(a))
return 2 * b * 6371000
def format_precision(settings, freq=None, level=None,
units=True, fancyUnits=False):
textFreq = None
textLevel = None
if freq is not None:
prec = settings.precisionFreq
width = 4 + prec
textFreq = '{:{width}.{prec}f}'.format(freq, width=width, prec=prec)
if units or fancyUnits:
textFreq += " MHz"
if level is not None:
prec = settings.precisionLevel
width = 4 + prec
textLevel = '{:{width}.{prec}f}'.format(level, width=width, prec=prec)
if fancyUnits:
textLevel += r" $\mathsf{{dB/\sqrt{{Hz}}}}$"
elif units:
textLevel += " dB/Hz"
if textFreq and textLevel:
return (textFreq, textLevel)
if textFreq:
return textFreq
if textLevel:
return textLevel
return None
def format_time(timeStamp, withDate=False):
if timeStamp <= 1:
return 'Unknown'
if withDate:
return time.strftime('%c', time.localtime(timeStamp))
return time.strftime('%H:%M:%S', time.localtime(timeStamp))
def format_iso_time(timeStamp):
dt = datetime.datetime.utcfromtimestamp(timeStamp)
return dt.isoformat() + 'Z'
def set_version_timestamp():
scriptDir = get_script_dir()
timeStamp = str(int(time.time()))
f = open(os.path.join(scriptDir, TIMESTAMP_FILE), 'w')
f.write(timeStamp)
f.close()
def get_version_timestamp(asSeconds=False):
scriptDir = get_script_dir()
f = open(os.path.join(scriptDir, TIMESTAMP_FILE), 'r')
timeStamp = int(f.readline())
f.close()
if asSeconds:
return timeStamp
else:
return format_time(timeStamp, True)
def get_version_timestamp_repo():
f = urllib.urlopen('https://raw.github.com/EarToEarOak/RTLSDR-Scanner/master/src/version-timestamp')
timeStamp = int(f.readline())
f.close()
return timeStamp
def get_serial_ports():
ports = [port[0] for port in serial.tools.list_ports.comports()]
if len(ports) == 0:
if os.name == 'nt':
ports.append('COM1')
else:
ports.append('/dev/ttyS0')
return ports
def limit_to_ascii(text):
return ''.join([i if ord(i) < 128 else '' for i in text])
if __name__ == '__main__':
print 'Please run rtlsdr_scan.py'
exit(1)