-
Notifications
You must be signed in to change notification settings - Fork 2
/
udp.py
180 lines (146 loc) · 6.32 KB
/
udp.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
#########################################################################################
#
# udp.py - Rev. 1.0
# Copyright (C) 2022-4 by Joseph B. Attili, aa2il AT arrl DOT net
#
# UDP messaging for pySDR.
#
############################################################################################
#
# 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, either version 3 of the License, 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.
#
#########################################################################################
from tcp_server import *
from rig_io.socket_io import SetTXSplit
from utilities import freq2band
#########################################################################################
# UDP Message handler for pySDR
def udp_msg_handler(self,sock,msg):
P=self.P
id=sock.getpeername()
print('UDP MSG HANDLER: id=',id,'\tmsg=',msg.rstrip())
msgs=msg.split('\n')
for m in msgs:
mm=m.split(':')
print('UDP MSG HANDLER: m=',m,'\tmm[0]=',mm[0])
if mm[0]=='SO2V':
# Set SO2V mode
P.SO2V = (mm[1]=='ON')
P.ENABLE_AUTO_MUTE = P.DXSPLIT or P.SO2V
P.gui.MuteCB(0,not P.SO2V)
print('UDP MSG HANDLER: mm=',mm,'\tSetting SO2V',P.SO2V)
P.gui.so2v_cb.setChecked(P.SO2V)
return
elif mm[0]=='SPLIT':
# Set Split for Chasing DX
P.DXSPLIT = (mm[1]=='ON')
P.ENABLE_AUTO_MUTE = P.DXSPLIT or P.SO2V
print('UDP MSG HANDLER: mm=',mm,'\tSetting DX SPLIT',P.DXSPLIT)
P.gui.split_cb.setChecked(P.DXSPLIT)
P.gui.MuteCB(0,not P.DXSPLIT)
return
elif mm[0]=='Name':
# Query server name
if mm[1]=='?':
print('UDP MSG HANDLER: Server name query')
msg2='Name:pySDR\n'
sock.send(msg2.encode())
else:
print('UDP MSG HANDLER: Server name is',mm[1])
return
elif mm[0]=='MODE':
# Set mode
if mm[1]=='?':
print('UDP MSG HANDLER: Mode query')
msg2='MODE:'+self.P.MODE+'\n'
sock.send(msg2.encode())
else:
self.P.MODE=mm[1]
self.P.gui.ModeSelect(-1)
print('UDP MSG HANDLER: SDR mode set to',mm[1])
return
elif mm[0]=='SPOT':
# Relay to bandmap
#self.P.udp_client2.Send(m)
print('UDP MSG HANDLER: Passing on SPOT message')
return
elif mm[0]=='SpotList':
if mm[1]=='Refresh':
# Relay to bandmap
band=self.P.BAND
msg='SpotList:'+band+':?\n'
self.P.udp_client2.Send(msg)
#print('UDP MSG HANDLER: Passing on SPOT LIST REFRESH message')
elif mm[1]!='?':
band=mm[1]
self.P.NEW_SPOT_LIST=eval(mm[2])
print('UDP MSG HANDLER: New Spot List:',band,
self.P.NEW_SPOT_LIST,len(self.P.NEW_SPOT_LIST))
return
elif mm[0]=='RunFreq' and False:
# This pathway uses the bandmap as the arbiter - more up to date but very slow
if mm[1] in ['UP','DOWN']:
self.P.udp_client2.Send(m)
else:
self.P.udp_client.Send(m)
print('UDP MSG HANDLER: Relayed msg=',m)
return
elif mm[0]=='RunFreq' and mm[1] in ['UP','DOWN'] and True:
# Just use whatever spots we already have
frq=float(mm[2])
band = freq2band(1e-3*frq)
print('UDP MSG HANDLER: RunFreq - frq=',frq,'\tband=',band)
spots = self.P.gui.Spots
spots.sort(key=lambda x: x.freq, reverse=(mm[1]=='DOWN'))
#print('spots=',spots)
flast=None
MIN_DF=1e-3*500
for x in spots:
f = x.freq
if not flast:
flast = f
df = abs( f - flast )
print(x.call,'\t',flast,'\t',f,'\t',df)
if df>MIN_DF:
if (mm[1]=='UP' and flast>=frq and f>frq) or \
(mm[1]=='DOWN' and flast<=frq and f<frq):
frq2=0.5*(f+flast)
msg='RunFreq:TRY:'+str(frq2)
print('UDP MSG HANDLER: RunFreq - Suggested freq=',frq2,
'\nSending msg=',msg)
#self.P.udp_server.Broadcast(msg)
sock.send(msg.encode())
return
flast = f
print('UDP MSG HANDLER: RunFreq - Unable to suggest a freq')
return
elif mm[0]=='SpotFreq' and mm[1] in ['UP','DOWN'] and True:
# Just use whatever spaots we already have
frq=float(mm[2])
band = freq2band(1e-3*frq)
print('UDP MSG HANDLER: SpotFreq - frq=',frq,'\tband=',band)
spots = self.P.gui.Spots
spots.sort(key=lambda x: x.freq, reverse=(mm[1]=='DOWN'))
#print('spots=',spots)
for x in spots:
f = x.freq
print(x.call,'\t',f)
if (mm[1]=='UP' and f>frq) or \
(mm[1]=='DOWN' and f<frq):
msg='SpotFreq:TRY:'+str(f)+':'+x.call+':A'
print('UDP MSG HANDLER: SpotFreq - Suggested freq=',f,
'\nSending msg=',msg)
#self.P.udp_server.Broadcast(msg)
sock.send(msg.encode())
return
print('UDP MSG HANDLER: SpotFreq - Unable to find a spot freq')
return
print('UDP MSG HANDLER: Not sure what to do with this',mm)