-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (164 loc) · 4.7 KB
/
main.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
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
package main
import (
"bufio"
"fmt"
_ "github.com/mattn/go-sqlite3"
"os/exec"
"strconv"
"strings"
"time"
"xorm.io/xorm"
)
const sweepAlias = "hackrf_sweep"
// frequencyStringToInt parses a string into an integer representing hertz
func frequencyStringToInt(x string) (num int) {
var err error
num, err = strconv.Atoi(strings.Split(x, ".")[0])
errPanic(err)
return
}
// calculateBinRange calculates the highest and lowest frequencies in a bin
func calculateBinRange(hzLow int, hzHigh int, hzBinWidth int, binNum int) (low, high int) {
low = hzLow + (binNum * hzBinWidth)
high = low + hzBinWidth
if high > hzHigh {
high = hzHigh
}
return
}
// constructSweepArgs constructs arguments array for the scanRow call
// todo: default bin size to 1000000 (1 million hertz)
// todo: high and low limits
// todo: sample rate
// one-shot mode (single scanRow)
// bin width in hertz
func constructSweepArgs(oneShot bool, binSize int) (arguments []string) {
if oneShot {
arguments = append(arguments, "-1")
}
arguments = append(arguments, fmt.Sprintf("-w %v", binSize))
return
}
// errPanic panics if passed an error otherwise just save me from repeating this damn code
// todo: eventually this should probably handle errors...
func errPanic(err error) {
if err != nil {
panic(err)
}
}
// scanRow scans a row from the provided scanner and parses it
// breaks one row with multiple bin values into multiple rows with one bin each
// Append extracted rows to row array
func scanRow(scanner *bufio.Scanner, lat float64, lon float64, alt float64) (rows []Sample) {
var row = strings.Split(scanner.Text(), ", ")
var numBins = len(row) - 6
var samples = frequencyStringToInt(row[5])
for i := 0; i < numBins; i++ {
var low, high = calculateBinRange(
frequencyStringToInt(row[2]),
frequencyStringToInt(row[3]),
frequencyStringToInt(row[4]),
i)
var binRowIndex = i + 6
parsedTime, err := time.Parse(time.RFC3339, row[0]+"T"+row[1]+"Z")
errPanic(err)
decibels, err := strconv.ParseFloat(row[binRowIndex], 64)
errPanic(err)
insertRow := Sample{
HzLow: low,
HzHigh: high,
Decibels: decibels,
N: samples,
Timestamp: parsedTime,
AltitudeMeters: alt,
Latitude: lat,
Longitude: lon,
}
rows = append(rows, insertRow)
}
return rows
}
type Sample struct {
Id int64 `xorm:"pk autoincr"`
HzLow int `xorm:"index"`
HzHigh int `xorm:"index"`
Decibels float64
Latitude float64 `xorm:"index"`
Longitude float64 `xorm:"index"`
AltitudeMeters float64
N int
Timestamp time.Time `xorm:"index"`
}
// setupEngine creates a new orm engine and syncs the tables
func setupEngine() (engine *xorm.Engine) {
engine, err := xorm.NewEngine("sqlite3", "./test.db")
errPanic(err)
err = engine.Sync2(new(Sample)) // Set up db tables
errPanic(err)
return
}
// setupCommand creates a hackrf_sweep command and the stdout scanner
func setupCommand() (cmd *exec.Cmd, scanner *bufio.Scanner) {
cmd = exec.Command(sweepAlias, constructSweepArgs(false, 1000000)...)
out, err := cmd.StdoutPipe()
errPanic(err)
scanner = bufio.NewScanner(out)
return
}
// insertSampleRows inserts rows of samples in a transaction
//func insertSampleRows(engine *xorm.Engine, rows []Sample) {
//sess := engine.NewSession()
//defer sess.Close()
//_, err := sess.Insert(rows)
//errPanic(err)
//err = sess.Commit()
//errPanic(err)
//}
// todo: check that there is a hackrf plugged in
// todo: wait for gpsd tpv
// todo: use buffer channels like an adult
func main() {
var (
err error
rows int
start = time.Now()
laps int // the lap number starting when updated
lapLimit = 10
lat = new(float64)
lon = new(float64)
alt = new(float64)
)
// Set up command and db engine
cmd, scanner := setupCommand()
//engine := setupEngine()
//gps, err := gpsd.Dial(gpsd.DefaultAddress)
//errPanic(err)
//gps.Subscribe("TPV", func(r interface{}) {
// tpv := r.(*gpsd.TPVReport)
// lat = &tpv.Lat
// lon = &tpv.Lon
// alt = &tpv.Alt
//})
err = cmd.Start() // Start (async) command
errPanic(err)
//
//gps.Run()
//defer gps.Close()
for scanner.Scan() {
newRows := scanRow(scanner, *lat, *lon, *alt)
//insertSampleRows(engine, newRows)
if newRows[0].HzLow == 0 { // todo: update 0 to lower limit when implemented
laps = laps + 1
if laps > 1 {
go logLaps(time.Since(start).Milliseconds(), rows, *lat, *lon)
}
if laps > lapLimit {
break
}
}
rows = rows + len(newRows)
}
}
func logLaps(milliseconds int64, rows int, lat float64, lon float64) {
fmt.Println(fmt.Sprintf("%d samples processed in %g seconds. current location: %f,%f", rows, float64(milliseconds)/1000, lat, lon))
}