-
Notifications
You must be signed in to change notification settings - Fork 0
/
lotw.go
131 lines (111 loc) · 2.69 KB
/
lotw.go
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
package tasks
import (
"bytes"
"fmt"
"log"
"math"
"math/rand"
"os/exec"
"path/filepath"
"sync"
"time"
"github.com/bbathe/golog/adif"
"github.com/bbathe/golog/config"
"github.com/bbathe/golog/models/qso"
"github.com/bbathe/golog/util"
)
var muxLotwUpload sync.Mutex
// QSLLotw uploads QSOs to LoTW
func QSLLotw() error {
muxLotwUpload.Lock()
defer muxLotwUpload.Unlock()
qsos, err := qso.FindQSLsToSend(qso.QSLLotw, config.LogbookServices.QSLDelay)
if err != nil {
log.Printf("%+v", err)
return err
}
if len(qsos) > 0 {
// get oldest loaded time of all these qsos
minLoadedAt := int64(math.MaxInt64)
for _, qso := range qsos {
if minLoadedAt > qso.LoadedAt {
minLoadedAt = qso.LoadedAt
}
}
// pick a number between 11 & 17
r := rand.Intn(17-11) + 11
// if batch is greater than r size or oldest is older than 3 hours, then push to lotw
if len(qsos) > r || time.Now().Add(-3*time.Hour).Unix() > minLoadedAt {
err = uploadQSOsToLoTW(qsos, false)
if err != nil {
log.Printf("%+v", err)
return err
}
}
}
return nil
}
// QSLLotwFinal uploads all QSOs to LoTW regardless of how old they are
func QSLLotwFinal() {
muxLotwUpload.Lock()
defer muxLotwUpload.Unlock()
qsos, err := qso.FindQSLsToSend(qso.QSLLotw, 0)
if err != nil {
log.Printf("%+v", err)
return
}
if len(qsos) > 0 {
err = uploadQSOsToLoTW(qsos, true)
if err != nil {
log.Printf("%+v", err)
return
}
}
}
// uploadQSOsToLoTW leverages tqsl to upload qsos to LoTW
func uploadQSOsToLoTW(qsos []qso.QSO, force bool) error {
// form working file name
fname := filepath.Join(config.WorkingDirectory, "LoTW-"+time.Now().UTC().Format("2006-Jan-02_15-04-05")+".adif")
// write qsos as adif to file
err := adif.WriteToFile(qsos, fname)
if err != nil {
log.Printf("%+v", err)
return err
}
var stdout bytes.Buffer
var stderr bytes.Buffer
// setup command execution, capturing stdout & stderr
// #nosec G204
cmd := exec.Command(
config.LogbookServices.TQSL.ExeLocation,
"--quiet",
"--batch",
"--nodate",
"--upload",
fmt.Sprintf("--location=%s", config.LogbookServices.TQSL.StationLocationName),
fname,
)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// doit!
err = cmd.Run()
if err != nil {
log.Printf("error: %+v", err)
log.Printf("stdout: %s", stdout.String())
log.Printf("stderr: %s", stderr.String())
return err
}
// set as sent in db
err = qso.UpdateQSLsToSent(qsos, qso.QSLLotw)
if err != nil {
log.Printf("%+v", err)
return err
}
// only keep the last 5 files of ours in the working directory
err = util.DeleteHistoricalFiles(5, config.WorkingDirectory, "LoTW-", ".adif")
if err != nil {
log.Printf("%+v", err)
return err
}
return nil
}