-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecorderthread.py
More file actions
110 lines (83 loc) · 4.4 KB
/
recorderthread.py
File metadata and controls
110 lines (83 loc) · 4.4 KB
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
import datetime
import time
import urllib3.exceptions
import KiwiSDR
import traceback
class Recorder:
def connect_to_kiwi(self, name, frequency, mode, known_region, start_time,
specified_record_time=None, attempts=1,):
try:
if known_region:
region = known_region
else:
try:
region = self.stations_to_regions[name.lower()]
except KeyError:
print(f"{name} not found in stations-->region dictionary, assuming Mediterranean")
region = "Mediterranean"
print(f"{name} = {region}")
pavlov_dispatcher_link = self.kiwisdr.getLink(region, frequency, mode.lower())
self.kiwisdr.navigate(pavlov_dispatcher_link)
try:
if specified_record_time:
record_time = 60*specified_record_time
else:
record_time = 60*self.stations_to_transmission_lengths[name.lower()]
except KeyError:
print(f"RecorderThread: {name} not found in station-->transmission"
f" length dictionary; assuming 20 minutes")
record_time = 20*60
print("Recording " + name + " for " + str(int(record_time/60)) + " minutes")
try:
self.kiwisdr.record(mode, frequency, name, record_time, start_time)
except IndexError:
if attempts < 10:
print(f"RecorderThread: All kiwis busy; trying again in 15s (Attempt {attempts}/10)")
time.sleep(15)
self.connect_to_kiwi(name, frequency, mode, known_region, start_time,
specified_record_time=specified_record_time, attempts=attempts+1)
else:
print(f"Error: All kiwis busy; Giving up on recording {name} after trying to connect 10 times...")
try:
self.kiwisdr.quit()
except urllib3.exceptions.MaxRetryError:
print("Warning: Encountered MaxRetryError when trying to quit firefox;")
except OSError:
try:
self.kiwisdr.quit()
except urllib3.exceptions.MaxRetryError:
print("Warning: Encountered MaxRetryError when trying to quit firefox;")
traceback.print_exc()
def record(self, calendarmonitor):
current_time = datetime.datetime.utcnow().replace(microsecond=0)
next_station = self.next_station
start_time = next_station[4]
name, frequency, mode, known_region, start_time, specified_record_time = self.next_station
if start_time > current_time:
delta_t = (start_time - current_time).seconds
if not calendarmonitor.debug:
if delta_t - 30 > 0:
time_to_sleep = delta_t - 30
else:
time_to_sleep = 0
print(f"RecorderThread: {name} starts in {delta_t} seconds; sleeping for {time_to_sleep}")
time.sleep(time_to_sleep)
print("RecorderThread: Recording....")
self.connect_to_kiwi(name, frequency, mode, known_region, start_time, specified_record_time)
else:
print("RecorderThread: Recording....")
self.connect_to_kiwi(name, frequency, mode, known_region, start_time, specified_record_time)
else:
delta_t = (current_time - start_time).seconds
print(f"RecorderThread: {name} started {delta_t} seconds ago; recording now...")
self.connect_to_kiwi(name, frequency, mode, known_region, start_time, specified_record_time)
print(f"RecorderThread: Recording of {name} finished; Recorder thread quitting;")
return
def __init__(self, calendarmonitor, station_info, out_directory):
# station_info = [name, frequency, mode, known_region, start_time, specified_record_time]
self.calendarmonitor = calendarmonitor
self.stations_to_regions = calendarmonitor.stations_to_regions
self.stations_to_transmission_lengths = calendarmonitor.stations_to_transmission_lengths
self.kiwisdr = KiwiSDR.KiwiSDR(self.calendarmonitor.geckodriver_path, out_directory, load_time=10,
debug=self.calendarmonitor.debug)
self.next_station = station_info