forked from 0x9900/AutoFT8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmit.py
195 lines (166 loc) · 4.91 KB
/
transmit.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
#
# BSD 3-Clause License
#
# Copyright (c) 2021, Fred W6BSD
# All rights reserved.
# See licence file for more information.
#
import logging
import operator
import re
import socket
import threading
import time
from datetime import datetime
from importlib import import_module
import wsjtx
from config import Config
LOG = logging.getLogger('Transmit')
# LOG.setLevel(logging.DEBUG)
class Transmit(threading.Thread):
def __init__(self, status, period, daemon=None):
config = Config()
if isinstance(period, range):
period = list(period)
super().__init__(daemon=daemon)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.period = period
self.status = status
self._killed = False
# import the selector from plugins
*module_name, class_name = config.select_method.split('.')
module_name = '.'.join(['plugins'] + module_name)
module = import_module(module_name)
klass = getattr(module, class_name)
self.call_selector = klass(config, status.db)
self.call = config.call
self.follow_frequency = config.get('follow_frequency', True)
def wait(self):
while True:
for _ in range(12):
time.sleep(1)
if self._killed:
return
while datetime.utcnow().second not in self.period:
time.sleep(.25)
if self._killed:
break
break
def shutdown(self):
LOG.info('Transmit thread killed')
self._killed = True
def stop_transmit(self, flag):
LOG.debug('Stop transmit')
if not self.status.ip_wsjt:
return
stop_pkt = wsjtx.WSHaltTx()
stop_pkt.tx = flag
try:
self.sock.sendto(stop_pkt.raw(), self.status.ip_wsjt)
except:
logging.error(self.status.ip_wsjt)
raise
def reply(self, call):
packet = wsjtx.WSReply()
packet.call = call['call']
packet.Time = call['Time']
packet.SNR = call['SNR']
packet.DeltaTime = call['DeltaTime']
packet.DeltaFrequency = call['DeltaFrequency']
packet.Mode = call['Mode']
packet.Message = call['Message']
if self.follow_frequency:
packet.Modifiers = wsjtx.Modifiers.SHIFT
LOG.debug('Transmiting %s', packet)
self.sock.sendto(packet.raw(), self.status.ip_wsjt)
def run(self):
# Wait for the very end of the sequence
while True:
LOG.info(self.status)
self.wait()
if self._killed:
break
LOG.info(self.status)
if self.status.is_pause():
self.stop_transmit(True)
self.status.xmit = 0
self.status.call = ''
while self.status.is_pause():
if self._killed:
return
time.sleep(.5)
continue
if self.is_incontact(self.status.call):
LOG.info('is_incontact')
self.status.call = ''
call = self.is_inprogress(self.status.call)
if call:
LOG.info('is_inprogress: %s', call['Message'])
self.reply(call)
self.status.call = call['call']
continue
call = self.run_pileup()
if call:
LOG.info('run_pileup: %s', call['Message'])
self.reply(call)
self.status.call = call['call']
continue
self.status.xmit -= 1
if not self.status.call or not self.status.xmit:
call = self.call_selector.get()
if call:
LOG.info('%s: %s', self.call_selector, call['Message'])
self.reply(call)
self.status.call = call['call']
self.status.xmit = self.status.max_tries
self.status.db.black.update_one(
{"call": call['call']},
{"$set": {"time": Transmit.timestamp(), "logged": False}},
upsert=True)
continue
else:
LOG.critical('Stop Transmit')
# Exit
self.sock.close()
def is_incontact(self, call):
exp = re.compile(r'{}|CQ'.format(self.call))
request = {
"call": call,
"to": {"$not": exp},
"timestamp": {"$gt": Transmit.timestamp() - 15},
}
return bool(self.status.db.calls.count_documents(request))
def is_inprogress(self, call):
request = {
"call": call,
"timestamp": {"$gt": Transmit.timestamp() - 15},
}
if not call:
return
LOG.debug('req = %s', request)
record = self.status.db.calls.find_one(request)
if not record or record['to'] not in ('CQ', self.call):
record = None
return record
def run_pileup(self):
calls = []
request = {
"to": self.call,
"timestamp": {"$gt": Transmit.timestamp() - 15}
}
LOG.debug('req = %s', request)
record = self.status.db.calls.find(request)
for call in record:
coef = Transmit.coefficient(call['distance'], call['SNR'])
calls.append((coef, call))
if not calls:
return
calls.sort(key=operator.itemgetter(0))
_, call = calls.pop()
return call
@staticmethod
def coefficient(distance, snr):
return distance * 10**(snr/10)
@staticmethod
def timestamp():
return int(datetime.utcnow().timestamp())