-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbm_gui.py
executable file
·1470 lines (1255 loc) · 50.7 KB
/
bm_gui.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#########################################################################################
#
# gui.py - Rev. 2.0
# Copyright (C) 2021-5 by Joseph B. Attili, aa2il AT arrl DOT net
#
# Gui for dx cluster bandmap.
#
#########################################################################################
#
# 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.
#
#########################################################################################
import sys
import os
import time
import socket
import json
import platform
from datetime import datetime
from dx.spot_processing import ChallengeData
from dx.cluster_connections import *
from fileio import parse_adif, read_text_file
import webbrowser
if sys.version_info[0]==3:
from tkinter import *
import tkinter.font
else:
from Tkinter import *
import tkFont
from rig_io import bands
from cluster_feed import *
from settings import *
import logging
from utilities import freq2band, error_trap
from widgets_tk import StatusBar
from tcp_server import open_udp_client,KEYER_UDP_PORT
from bm_udp import *
from load_history import load_history
#########################################################################################
DEFAULT_BAND = '20m'
VERBOSITY=0
#########################################################################################
# Setup basic logging
logging.basicConfig(
format="%(asctime)-15s [%(levelname)s] %(funcName)s:\t(message)s",
level=logging.INFO)
#########################################################################################
# The GUI
class BandMapGUI:
def __init__(self,root,P):
# Init
self.P = P
P.nspots=0
P.SpotList=[]
P.current=[]
P.friends=[]
P.most_wanted=[]
P.corrections=[]
P.members=[]
P.qsos=[]
P.last_check=datetime.now()
if self.P.FT4:
self.FT_MODE='FT4'
else:
self.FT_MODE='FT8'
self.Ready=False
self.calls1 = []
self.old_mode = None
# UDP stuff
P.bm_udp_client=None
P.bm_udp_ntries=0
# Read "regular" logbook - need to update this
# Might need to bring this out to bandmap.py
if self.P.CWOPS and False:
if True:
print('\nCWops members worked:\n',self.P.data.cwops_worked)
self.calls1 = []
#sys.exit(0)
elif '_6' in self.P.LOG_NAME:
# For CQP
fname99=self.P.LOG_NAME.replace('_6','')
print('GUI: Reading regular log file',fname99)
logbook = parse_adif(fname99)
self.calls1 = [ qso['call'] for qso in logbook ]
self.calls1 =list( set( self.calls1) )
elif True:
# After the CQP
fname99=self.P.LOG_NAME.replace('.','_6.')
print('GUI: Reading regular log file',fname99)
logbook = parse_adif(fname99)
self.calls1 = [ qso['call'] for qso in logbook ]
self.calls1 =list( set( self.calls1) )
else:
self.calls1 = []
print('GUI: CALLS1=',self.calls1,len(self.calls1))
# Create the GUI - need to be able to distinguish between multiple copies of bandmap
if root:
self.root=Toplevel(root)
#self.hide()
else:
self.root = Tk()
if P.SERVER=="WSJT":
self.root.title("Band Map by AA2IL - " + P.SERVER)
else:
self.root.title("Band Map by AA2IL - Server " + P.SERVER)
# Move to lower left corner of screen
if P.BM_GEO==None:
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
print('Screen=',self.screen_width, self.screen_height)
w=400
h=self.screen_height
sz=str(w)+'x'+str(h)+'+'+str(self.screen_width-400)+'+0'
else:
#bandmap.py -geo 400x790+1520+240
sz=P.BM_GEO
self.root.geometry(sz)
# Add menu bar
self.create_menu_bar()
# Set band according to rig freq
self.band = StringVar(self.root)
self.ant = IntVar(self.root)
self.ant.set(-1)
self.mode = StringVar(self.root)
self.mode.set('')
self.mode2 = StringVar(self.root)
self.mode2.set(self.FT_MODE)
# Buttons
BUTframe = Frame(self.root)
BUTframe.pack()
# Buttons to select HF bands
self.Band_Buttons=[]
for bb in bands.keys():
if bb=='2m':
break
b = int( bb.split("m")[0] )
but=Radiobutton(BUTframe,
text=bb,
indicatoron = 0,
variable=self.band,
command=lambda: self.SelectBands(self.P.ALLOW_CHANGES),
value=bb)
self.Band_Buttons.append( but )
if not P.CONTEST_MODE or bands[bb]["CONTEST"]:
but.pack(side=LEFT,anchor=W)
# Another row of buttons to select mode & antenna
#ModeFrame = Frame(self.root)
#ModeFrame.pack(side=TOP)
#subFrame1 = Frame(ModeFrame,relief=RIDGE,borderwidth=2)
subFrame1 = Frame(self.toolbar,relief=FLAT,borderwidth=2,bg='red')
subFrame1.pack(side=LEFT)
if P.SERVER=="WSJT":
for m in ['FT8','FT4','MSK144']:
Radiobutton(subFrame1,
text=m,
indicatoron = 0,
variable=self.mode2,
command=lambda: self.SelectMode2(),
value=m).pack(side=LEFT,anchor=W)
else:
for m in ['CW','Data','SSB']:
Radiobutton(subFrame1,
text=m,
indicatoron = 0,
variable=self.mode,
command=lambda: self.SelectMode(),
value=m).pack(side=LEFT,anchor=W)
#subFrame2 = Frame(ModeFrame)
subFrame2 = Frame(self.toolbar,relief=FLAT,borderwidth=2,bg='green')
subFrame2.pack(side=LEFT)
for a in [1,2,3]:
Radiobutton(subFrame2,
text='Ant'+str(a),
indicatoron = 0,
variable=self.ant,
command=lambda: self.SelectAnt(),
value=a).pack(side=LEFT,anchor=W)
if False:
frm=ModeFrame
else:
frm=self.toolbar
#frm=Frame(self.toolbar,relief=RIDGE,borderwidth=1)
#frm.pack(side=LEFT)
Button(frm,text="-1",
command=lambda: self.FreqAdjust(-1) ).pack(side=LEFT,anchor=W)
Button(frm,text="+1",
command=lambda: self.FreqAdjust(+1) ).pack(side=LEFT,anchor=W)
if P.CONTEST_MODE:
Button(frm,text="<",
command=lambda: self.SetSubBand(1) ).pack(side=LEFT,anchor=W)
Button(frm,text=">",
command=lambda: self.SetSubBand(3) ).pack(side=LEFT,anchor=W)
# List box with a scroll bar
self.LBframe = Frame(self.root)
#self.LBframe.pack(side=LEFT,fill=BOTH,expand=1)
self.LBframe.pack(fill=BOTH,expand=1)
self.scrollbar = Scrollbar(self.LBframe, orient=VERTICAL)
# Select a fixed-space font
if platform.system()=='Linux':
FAMILY="monospace"
elif platform.system()=='Windows':
#FAMILY="fixed" # Doesn't come out fixed space?!
FAMILY="courier"
else:
print('GUI INIT: Unknown OS',platform.system())
sys.exit(0)
if self.P.SMALL_FONT:
SIZE=8
else:
SIZE=10
if sys.version_info[0]==3:
self.lb_font = tkinter.font.Font(family=FAMILY,size=SIZE,weight="bold")
else:
self.lb_font = tkFont.Font(family=FAMILY,size=SIZE,weight="bold")
self.lb = Listbox(self.LBframe, yscrollcommand=self.scrollbar.set,font=self.lb_font)
self.scrollbar.config(command=self.lb.yview)
self.scrollbar.pack(side=RIGHT, fill=Y)
self.lb.pack(side=LEFT, fill=BOTH, expand=1)
self.lb.bind('<<ListboxSelect>>', self.LBLeftClick)
# self.lb.bind('<2>' if aqua else '<3>', lambda e: context_menu(e, menu))
self.lb.bind('<Button-2>',self.LBCenterClick)
self.lb.bind('<Button-3>',self.LBRightClick)
# Trap the mouse wheel pseudo-events so we can handle them properly
self.lb.bind('<Button-4>',self.scroll_updown)
self.lb.bind('<Button-5>',self.scroll_updown)
self.scrollbar.bind('<Button-4>',self.scroll_updown)
self.scrollbar.bind('<Button-5>',self.scroll_updown)
# Status bar along the bottom
self.status_bar = StatusBar(self.root)
self.status_bar.setText("Howdy Ho!")
self.status_bar.pack(fill=X,side=BOTTOM)
# Make what we have so far visible
self.root.update_idletasks()
self.root.update()
# Function to actually get things going
def run(self):
# Put gui on proper desktop
if self.P.DESKTOP!=None:
cmd='wmctrl -r "'+self.root.title()+'" -t '+str(self.P.DESKTOP)
os.system(cmd)
sock = self.P.sock
if sock and sock.active:
if VERBOSITY>0:
logging.info("Calling Get band ...")
f = 1e-6*sock.get_freq(VFO=self.P.RIG_VFO) # Query rig at startup
b = freq2band(f)
self.rig_freq = 1e-3*f
else:
f = 0
b = DEFAULT_BAND # No conenction so just default
print('BM_GUI: BAND.SET band=',b,'\tf=',f)
self.band.set(b)
self.rig_band=b
print("Initial band=",b)
if sock and sock.active:
self.SelectMode('')
self.SelectAnt(-1)
self.SelectBands(True)
print('Initial server=',self.P.SERVER)
self.node.set(self.P.SERVER)
self.WatchDog()
# Callback to handle mouse wheel scrolling since Tk doesn't seem to do a very good job of it
# The jumps in Tk are much to big and I can't figure out how to adjust so do this instead
def scroll_updown(self, event):
if event.num==4:
n=-1 # int(-1*(event.delta/120))
else:
n=1 # int(-1*(event.delta/120))
#print('MOUSE WHEEL ...........................................................................................',
# n,event.num,event.delta,'\n',event)
self.lb.yview_scroll(n, "units")
return "break"
# Adjust rig freq
def FreqAdjust(self,df):
sock=self.P.sock
if VERBOSITY>0:
logging.info("Calling Get Freq ...")
self.rig_freq = sock.get_freq(VFO=self.P.RIG_VFO) / 1000.
if VERBOSITY>0:
logging.info("Calling Set Freq ...")
sock.set_freq(self.rig_freq+df,VFO=self.P.RIG_VFO)
# Set rig freq to lo or hi end of mode subband
def SetSubBand(self,iopt):
sock=self.P.sock
if sock==None:
return
b = self.band.get()
if VERBOSITY>0:
logging.info("Calling Get Mode ...")
m = sock.get_mode(VFO=self.P.RIG_VFO)
if m=='AM':
m='SSB'
if iopt==1:
print('m=',m)
frq = bands[b][m+'1'] * 1000
elif iopt==2:
frq1 = bands[b][m+'1'] * 500
frq2 = bands[b][m+'2'] * 500
frq = frq1+frq2
elif iopt==3:
frq = bands[b][m+'2'] * 1000
print("\nSetSubBand:",iopt,b,m,frq)
if VERBOSITY>0:
logging.info("Calling Set Freq ...")
sock.set_freq(float(frq/1000.),VFO=self.P.RIG_VFO)
# Callback to select antenna
def SelectAnt(self,a=None,b=None,VERBOSITY=0):
sock=self.P.sock
if sock==None:
print('SELECT ANT - No socket!')
return
if not a:
a = self.ant.get()
if a==-1:
if VERBOSITY>0:
logging.info("Calling Get Ant ...")
print("SELECT ANT: Calling Get Ant ...")
a = sock.get_ant()
self.ant.set(a)
if VERBOSITY>0:
print("SELECT ANT: Got Antenna =",a)
elif a==-2:
if VERBOSITY>0:
logging.info("Checking Ant matches Band ...")
if self.P.sock.rig_type2=='FTdx3000':
if b in ['160m','80m']:
ant=3
elif b in ['40m','20m','15m']:
ant=1
elif b in ['30m','17m','12m','10m','6m']:
ant=2
else:
ant=1
self.P.sock.set_ant(ant,VFO=self.P.RIG_VFO)
else:
print("\n%%%%%%%%%% Select Antenna: Setting Antenna =",a,"%%%%%%%%")
if VERBOSITY>0:
logging.info("Calling Set Ant ...")
sock.set_ant(a,VFO=self.P.RIG_VFO)
self.status_bar.setText("Selecting Antenna "+str(a))
# Callback to handle mode changes for WSJT-X
def SelectMode2(self,VERBOSITY=0):
if VERBOSITY>0:
print('\nSelectMode2: mode=',self.FT_MODE)
sock=self.P.sock
if sock==None:
print('SELECT MODE2 - No socket!')
return
try:
self.FT_MODE=self.mode2.get()
band = self.band.get()
frq = bands[band][self.FT_MODE] + 1
except:
error_trap('GUI->SELECT MODE2: Problem setting new config')
return
print('\n***************************************** Well well well ...',self.FT_MODE,band,frq)
self.P.ClusterFeed.tn.configure_wsjt(NewMode=self.FT_MODE)
time.sleep(.1)
sock.set_freq(frq,VFO=self.P.RIG_VFO)
# Make sure monitor is turned on also
GAIN=25
sock.set_monitor_gain(25)
return
# Callback to handle mode changes for rig
def SelectMode(self,m=None,VERBOSITY=0):
if VERBOSITY>0:
print('\nSelectMode: mode=',m)
sock=self.P.sock
if sock==None:
print('SELECT MODE - No socket!')
return
if m==None:
m = self.mode.get()
print('SelectMode-a: mode2=',m)
if m=='':
if VERBOSITY>0:
logging.info("Calling Get Mode ...")
m = sock.get_mode(VFO=self.P.RIG_VFO)
#print('SelectMode:',m)
if m==None:
return
if m=='RTTY' or m=='FT8' or m=='FT4' or m[0:3]=='PKT' or m=='DIGITIAL':
m='Data'
self.mode.set(m)
if m!=self.old_mode:
self.status_bar.setText("Mode Select: "+str(m))
self.old_mode=m
return
# Translate mode request into something that FLDIGI understands
#print('SelectMode-c:',m)
if m in ['SSB','LSB','USB']:
# buf=get_response(s,'w BY;EX1030\n'); # Audio from MIC (front)
if VERBOSITY>0:
logging.info("Calling Get Freq ...")
self.rig_freq = sock.get_freq(VFO=self.P.RIG_VFO) / 1000.
if self.rig_freq<10000:
m='LSB'
else:
m='USB'
elif m=='Data' or m=='DIGITAL':
m='RTTY'
#print("SelecteMode-d:",m)
if VERBOSITY>0:
logging.info("Calling Set Mode ...")
if not self.P.CONTEST_MODE:
sock.set_mode(m,VFO=self.P.RIG_VFO,Filter='Auto')
if m=='CW':
sock.set_if_shift(0)
# Function to collect spots for a particular band
def collect_spots(self,band,REVERSE=False,OVERRIDE=False):
P=self.P
print('COLLECT_SPOTS: nspots=',len(P.SpotList),'\tband=',band,
'\nReverse=',REVERSE,'\tOVERRIDE=',OVERRIDE,'\tCONTEST_MODE=', self.P.CONTEST_MODE)
if 'cm' in band:
iband=int( band.replace('cm','') )
else:
iband=int( band.replace('m','') )
spots=[]
for x in P.SpotList:
keep= x and x.band == iband
if self.P.DX_ONLY:
# Retain only stations outside US or SESs
keep = keep and (x.dx_station.country!='United States' or len(x.dx_call)==3 or \
x.dx_call=='WM3PEN')
if self.P.NA_ONLY:
# Retain only stations in North America
keep = keep and x.dx_station.continent=='NA'
if self.P.NEW_CWOPS_ONLY:
# Retain only cwops stations not worked yet this year
keep = keep and self.P.ClusterFeed.cwops_worked_status(x.dx_call)==1
# Retain only modes we are interested in
xm = x.mode
if xm in ['FT8','FT4','DIGITAL','JT65']:
xm='DIGI'
elif xm in ['SSB','LSB','USB','FM']:
xm='PH'
#if keep and (xm not in self.P.SHOW_MODES):
# print('COLLECT_SPOTS: Culling',xm,'spot - ', self.P.SHOW_MODES)
keep = keep and (xm in self.P.SHOW_MODES)
# Check for dupes
if keep:
match = self.P.ClusterFeed.B4(x,band)
c,c2,age=self.P.ClusterFeed.spot_color(match,x)
x.color=c
if not (self.P.SHOW_DUPES or OVERRIDE):
keep = keep and (c2!='r')
#print('COLLECT SPOTS:',x.dx_call,c,c2,keep,band,match)
#print('\tx.color=',x.color)
# Keep spots that haven't been culled
if keep:
spots.append(x)
spots.sort(key=lambda x: x.frequency, reverse=REVERSE)
print('\tNo. Collect spots=',len(spots))
return spots
# Callback to handle band changes
def SelectBands(self,allow_change=False):
P=self.P
VERBOSITY = self.P.DEBUG
#VERBOSITY = 1
if VERBOSITY>0:
print('SELECT BANDS A: nspots=',P.nspots,
'\tlen SpotList=',len(P.SpotList),
'\tlen Current=',len(P.current))
self.scrolling('SELECT BANDS A')
sock=self.P.sock
if not sock:
print('\nGUI->SELECT BANDS: Not sure why but no socket yet ????')
print('\tsock=',sock,'\n')
#return
try:
band = self.band.get()
except:
error_trap('GUI->SELECT BANDS: ????')
print('\tband=',self.band)
return
if VERBOSITY>0:
logging.info("Calling Get Band ...")
if sock:
frq2 = 1e-6*sock.get_freq(VFO=self.P.RIG_VFO)
else:
frq2=0
band2 = freq2band(frq2)
self.status_bar.setText("Band Select: "+str(band))
print("\nYou've selected ",band,' - Current rig band=',band2,\
' - allow_change=',allow_change,' - mode=',self.FT_MODE, \
flush=True)
# Check for band change
if allow_change:
b=band
if self.P.CLUSTER=='WSJT':
print('BM_GUI - Config WSJT ...',b,self.FT_MODE)
self.P.ClusterFeed.tn.configure_wsjt(NewMode=self.FT_MODE)
time.sleep(.1)
try:
new_frq = bands[b][self.FT_MODE] + 1
except:
error_trap('GUI->SELECT BANDS: Problem getting freq')
return
if VERBOSITY>0:
logging.info("Calling Set Freq and Mode ...")
print('SELECT BANDS: Setting freq=',new_frq,'and mode=',self.FT_MODE)
if sock:
sock.set_freq(new_frq,VFO=self.P.RIG_VFO)
sock.set_mode(self.FT_MODE,VFO=self.P.RIG_VFO)
else:
if band != band2 and sock:
if VERBOSITY>0:
logging.info("Calling Set Band ...")
sock.set_band(band,VFO=self.P.RIG_VFO)
# Make sure antenna selection is correct also
self.SelectAnt(-2,band)
# Extract a list of spots that are in the desired band
P.current = self.collect_spots(band)
y=self.scrolling('SELECT BANDS B')
P.GUI_BAND = self.band.get()
P.GUI_MODE = self.mode.get()
# Get latest logbook
now = datetime.utcnow().replace(tzinfo=UTC)
if (self.P.STAND_ALONE and False) or len(self.P.qsos)==0:
if self.P.LOG_NAME0:
# Log for operator if different from current callsign
# We won't keep reading this file so we set REVISIT=False
print('\nGUI: Reading log file',self.P.LOG_NAME0)
logbook = parse_adif(self.P.LOG_NAME0,REVISIT=False,verbosity=0)
self.P.qsos += logbook
print('QSOs in log=',len(logbook),len(self.P.qsos))
# Log for current callsign
# We will keep reading this file for new QSOs so we set REVISIT=True
print('\nGUI: Reading log file',self.P.LOG_NAME)
logbook = parse_adif(self.P.LOG_NAME,REVISIT=True,verbosity=0)
self.P.qsos += logbook
print('QSOs in log=',len(logbook),len(self.P.qsos))
#sys.exit(0)
if self.P.CWOPS:
self.calls = self.calls1 + [ qso['call'] for qso in self.P.qsos ]
self.calls=list( set( self.calls) )
print('No. unique calls worked:',len(self.calls))
#print(self.calls)
# Re-populate list box with spots from this band
# This seems to be the slow part
#print 'Repopulate ...',len(P.current),len(self.P.qsos)
self.lb.delete(0, END)
n=0
for x in P.current:
#pprint(vars(x))
dxcc=x.dx_station.country
if self.P.CLUSTER=='WSJT':
#print('Insert1')
self.lb.insert(END, "%4d %-10.10s %+6.6s %-17.17s %+4.4s" % \
(int(x.df),x.dx_call,x.mode,cleanup(dxcc),x.snr))
else:
#print('Insert2')
if x.mode=='CW':
val=x.wpm
else:
#val=x.snr
val=''
self.lb.insert(END, "%-6.1f %-10.19s %+6.6s %-15.15s %+4.4s" % \
(x.frequency,x.dx_call,x.mode,cleanup(dxcc),val))
# JBA - Change background colors on each list entry
self.lb.itemconfigure(END, background=P.current[n].color)
n+=1
# Reset lb view
self.LBsanity()
self.lb.yview_moveto(y)
self.scrolling('SELECT BANDS C')
if VERBOSITY>0:
print('SELECT BANDS B: nspots=',P.nspots,
'\tlen SpotList=',len(P.SpotList),
'\tlen Current=',len(P.current))
# Function to set list box view
def set_lbview(self,frq,MIDDLE=False):
dfbest=1e9
ibest=-1
idx=0
# Keep track of entry that is closest to current rig freq
for x in P.current:
df=abs( x.frequency-frq )
# print idx,x.frequency,frq,df,ibest
if df<dfbest:
dfbest=df
ibest=idx
idx=idx+1
# Make sure its visible and highlight it
if ibest>-1:
sb=self.scrollbar.get()
sz=self.lb.size()
yview=self.lb.yview()
"""
print("LBSANITY: Closest=",ibest,
'\tf=',P.current[ibest].frequency,
'\tsize=',sz,
'\tsb=',sb,
'\tyview',yview)
"""
#print('hght:',self.lb['height'])
# Use to scrollbar to determine how many lines are visible
d = yview[1]-yview[0] # Fraction of list in view
n = d*sz # No. line in view
if MIDDLE:
y = max( ibest*d/n - d/2. , 0) # Top coord so view will be centered around ibest
else:
y = max( ibest*d/n , 0) # Top coord will be centered around ibest
self.lb.yview_moveto(y)
self.lb.selection_clear(0,END)
if False:
# This was problematic
self.lb.selection_set(ibest)
# Make sure the entry closest to the rig freq is visible
def LBsanity(self):
VERBOSITY = self.P.DEBUG
#print('LBSANITY ...')
# Dont bother if using as WSJT companion
#if not CONTEST_MODE or CLUSTER=='WSJT':
if self.P.CLUSTER=='WSJT':
print('LBSANITY - WS server - nothing to do')
return
# Don't bother if not on same band as the rig
b1 = self.rig_band
b2 = self.band.get()
#print('LBSANITY: rig band=',b1,'\tband=',b2)
if b1!=b2:
if VERBOSITY>0:
print('LBSANITY - Rig on different band - nothing to do')
return
# Don't bother if user doesn't want to keep rig freq centered
if not self.P.KEEP_FREQ_CENTERED:
#if VERBOSITY>0:
# print('LBSANITY - DONT KEEP CENTERED - nothing to do')
y=self.scrolling('LBSANITY',verbosity=0)
self.lb.yview_moveto(y)
return
# Set lb view so its centered around the rig rig freq
frq = self.rig_freq
self.set_lbview(frq,True)
# Callback to clear all spots
def Clear_Spot_List(self):
P=self.P
print("\n------------- Clear Spot List -------------",self.P.CLUSTER,'\n')
P.nspots=0
P.SpotList=[];
P.current=[]
self.lb.delete(0, END)
#########################################################################################
# Watch Dog
def WatchDog(self):
#print('BM WATCH DOG ...')
P=self.P
sock = P.sock
if P.GUI_BAND==None:
P.GUI_BAND = self.band.get()
if P.GUI_MODE==None:
P.GUI_MODE = self.mode.get()
# Check if we have any new spots
Update=False
nspots=self.P.bm_q.qsize()
#print('BM WATCH DOG - There are',nspots,'new spots in the queue ...')
while nspots>0:
Update=True
entry=self.P.bm_q.get()
#print('\tentry=',entry,len(entry))
if len(entry)==1:
self.lb.delete(entry[0])
else:
if entry[1]!=None:
self.lb.insert(entry[0], entry[1])
try:
self.lb.itemconfigure(entry[0], background=entry[2])
except:
error_trap('WATCH DOG: Error in configuring item bg color ????')
print('entry=',entry)
self.P.bm_q.task_done()
nspots=self.P.bm_q.qsize()
# Check if we need to cull old spots
if Update:
self.LBsanity()
dt = (datetime.now() - self.P.last_check).total_seconds()/60 # In minutes
if dt>1:
self.cull_old_spots()
# Check for antenna or mode or band changes
# Should combine these two
if VERBOSITY>0:
logging.info("Calling Get Band & Freq ...")
if self.P.SERVER=="WSJT":
tmp = self.P.ClusterFeed.tn.wsjt_status()
#print('WatchDog:',tmp)
if all(tmp):
if not self.Ready:
print('WatchDog - Ready to go ....')
self.P.ClusterFeed.tn.configure_wsjt(NewMode=self.FT_MODE)
self.Ready=True
self.rig_freq = tmp[0]
self.rig_band = tmp[1]
self.FT_MODE = tmp[2]
else:
try:
if sock:
self.rig_freq = 1e-3*sock.get_freq(VFO=self.P.RIG_VFO)
self.rig_band = freq2band(1e-3*self.rig_freq)
except:
error_trap('WATCHDOG: Problem reading rig freq/band',True)
try:
if sock:
self.SelectAnt(-1,VERBOSITY=0)
self.SelectMode('',VERBOSITY=0)
except:
error_trap('WATCHDOG: Problem reading rig antenna/mode',True)
# Try to connect to the keyer
if self.P.BM_UDP_CLIENT:
if not self.P.bm_udp_client:
self.P.bm_udp_ntries+=1
if self.P.bm_udp_ntries<=100:
self.P.bm_udp_client=open_udp_client(self.P,KEYER_UDP_PORT,
bm_udp_msg_handler)
if self.P.bm_udp_client:
print('BM GUI->WatchDog: Opened connection to KEYER.')
self.P.bm_udp_ntries=0
else:
print('WATCHDOG: Unable to open UDP client (keyer) - too many attempts',self.P.bm_udp_ntries)
# Check if socket is dead
if sock and sock.ntimeouts>=10:
print('\tWATCHDOG: *** Too many socket timeouts - port is probably closed - giving up -> sys.exit ***\n')
sys.exit(0)
# Re-schedule to do it all over again in 1-second
self.root.update_idletasks()
self.root.update()
self.root.after(1*1000, self.WatchDog)
#########################################################################################
# Callback when an item in the listbox is selected
def LBSelect(self,value,vfo):
print('LBSelect: Tune rig to a spot - vfo=',vfo,value)
self.status_bar.setText("Spot Select "+value)
self.scrolling('LBSelect')
sock=self.P.sock
# Examine item that was selected
b=value.strip().split()
if b[2]=='FT8' or b[2]=='FT4':
b[0] = float(b[0])+1
# If we're running WSJT, tune to a particular spot
if self.P.CLUSTER=='WSJT':
df = b[0]
dx_call = b[1]
#print('\n========================= LBSelect:',b,'\n')
self.P.ClusterFeed.tn.configure_wsjt(RxDF=df,DxCall=dx_call)
return
# Note - need to set freq first so get on right band, then set the mode
# We do a second set freq since rig may offset by 700 Hz if we are in CW mode
# There must be a better way to do this but this is what we do for now
#print("LBSelect: Setting freq ",b[0])
print("LBSelect: Setting freq=',b[0],'on VFO',vfo,'\tmode=',b[2].'\tcall ",b[1])
if VERBOSITY>0:
logging.info("Calling Set Freq ...")
if sock:
sock.set_freq(float(b[0]),VFO=vfo)
if not self.P.CONTEST_MODE:
print("LBSelect: Setting mode ",b[2])
self.SelectMode(b[2])
sock.set_freq(float(b[0]),VFO=vfo)
sock.set_call(b[1])
# Make sure antenna selection is correct also
band=freq2band(0.001*float(b[0]))
self.SelectAnt(-2,band)
# Send spot info to keyer
if self.P.BM_UDP_CLIENT and self.P.bm_udp_client:
self.P.bm_udp_client.Send('Call:'+b[1]+':'+vfo)
def LBLeftClick(self,event):
print('LBLeftClick ...')
w=event.widget
if len( w.curselection() ) > 0:
index = int(w.curselection()[0])
value = w.get(index)
print('You selected item %d: "%s"' % (index, value))
self.LBSelect(value,self.P.RIG_VFO)
def LBRightClick(self,event):
print('LBRightClick ...')
index = event.widget.nearest(event.y)
value = event.widget.get(index)
print('You selected item %d: "%s"' % (index, value))
self.status_bar.setText("Right Click "+value)
#print(self.P.RIGHT_CLICK_TUNES_VFOB,self.P.SERVER)
if not self.P.RIGHT_CLICK_TUNES_VFOB or self.P.SERVER=="WSJT":
# This used to trigger a qrz call lookup
b=value.strip().split()
print("Looking up call: ",b[1])
link = 'https://www.qrz.com/db/' + b[1]
webbrowser.open_new_tab(link)
else:
# Tune VFO-B to spot freq
print("Tuning VFO B to ",value)
self.LBSelect(value,'B')
def LBCenterClick(self,event):
P=self.P
print('LBCenterClick: Delete an entry')
index = event.widget.nearest(event.y)
value = event.widget.get(index)
b=value.strip().split()
call=b[1]
print('You selected item %d: %s - %s' % (index,value,call))
self.status_bar.setText("Spot Delete "+value)
del P.current[index]
self.lb.delete(index)
#print('\nCENTER CLICK B4:',len(P.SpotList),P.SpotList)
idx=[]
i=0
for i in range(len(P.SpotList)):
x=P.SpotList[i]
if hasattr(x, 'dx_call') and x.dx_call==call:
idx.append(i)
idx.reverse()
#print('idx=',idx)
for i in idx:
x=P.SpotList.pop(i)
#print('\nCENTER CLICK AFTER:',len(P.SpotList),P.SpotList)
#########################################################################################
# For debug
def donothing(self):
win = Toplevel(self.root)
button = Button(win, text="Do nothing button")
button.pack()
# Open dialog window for basic settings
def Settings(self):
self.SettingsWin = SETTINGS_GUI(self.root,self.P)
return
# Print out log
def ShowLog(self):
print('\nLOG::::::::::',self.P.STAND_ALONE)
for qso in self.P.qsos:
print(qso)
print(' ')
return
# Select a new cluster
def SelectNode(self):
SERVER = self.node.get()
print('SelectNode:',SERVER)
if SERVER != self.P.SERVER:
self.P.SERVER = SERVER
self.P.CLUSTER = self.P.NODES[SERVER]
self.root.title("Band Map by AA2IL - Server " + SERVER)
self.Reset()
self.node.set(self.P.SERVER)
def Reset(self):
self.P.ClusterFeed.Reset_Flag.set()
#self.P.ClusterFeed.Reset()