-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_process.go
143 lines (118 loc) · 3.76 KB
/
status_process.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
package main
import (
"encoding/json"
"log"
"time"
"ttnmapper-gateway-channels/types"
)
func processRawPackets() {
// Wait for a message and insert it into Postgres
for d := range rawPacketsChannel {
// The message form amqp is a json string. Unmarshal to ttnmapper uplink struct
var message types.TtnMapperUplinkMessage
if err := json.Unmarshal(d.Body, &message); err != nil {
log.Print("AMQP " + err.Error())
continue
}
// Iterate gateways. We store it flat in the database
for _, gateway := range message.Gateways {
updateTime := time.Unix(0, message.Time)
log.Print("AMQP ", "", "\t", gateway.GatewayId+"\t", updateTime)
gateway.Time = message.Time
// Ignore locations obtained from live data
gateway.Latitude = 0
gateway.Longitude = 0
gateway.Altitude = 0
updateGateway(message, gateway)
}
}
}
func updateGateway(message types.TtnMapperUplinkMessage, gateway types.TtnMapperGateway) {
gatewayStart := time.Now()
// Count number of gateways we processed
processedGateways.Inc()
// Find the database IDs for this gateway and it's antennas
antennaDb, err := getAntennaDb(gateway)
if err != nil {
failOnError(err, "Can't find antenna in DB")
}
frequencyDb, err := getFrequencyDb(message.Frequency)
if err != nil {
failOnError(err, "Can't find antenna in DB")
}
setAntennaFrequencyDb(antennaDb.ID, frequencyDb.ID)
// Prometheus stats
gatewayElapsed := time.Since(gatewayStart)
insertDuration.Observe(float64(gatewayElapsed.Nanoseconds()) / 1000.0 / 1000.0) //nanoseconds to milliseconds
}
/*
Takes a TTN Mapper Gateway and search for it in the database and return the database entry id
*/
func getAntennaDb(gateway types.TtnMapperGateway) (types.Gateway, error) {
antennaIndexer := types.AntennaIndexer{NetworkId: gateway.NetworkId, GatewayId: gateway.GatewayId, AntennaIndex: gateway.AntennaIndex}
i, ok := antennaCache.Load(antennaIndexer)
if ok {
gatewayDb := i.(types.Gateway)
return gatewayDb, nil
} else {
antennaDb := types.Gateway{NetworkId: gateway.NetworkId, GatewayId: gateway.GatewayId}
db.Where(&antennaDb).First(&antennaDb)
if antennaDb.ID == 0 {
err := db.FirstOrCreate(&antennaDb, &antennaDb).Error
if err != nil {
return antennaDb, err
}
}
antennaCache.Store(antennaIndexer, antennaDb)
return antennaDb, nil
}
}
func getFrequencyDb(frequency uint64) (types.Frequency, error) {
i, ok := frequencyCache.Load(frequency)
if ok {
frequencyDb := i.(types.Frequency)
return frequencyDb, nil
} else {
frequencyDb := types.Frequency{
Herz: frequency,
}
db.Where(&frequencyDb).First(&frequencyDb)
if frequencyDb.ID == 0 {
err := db.FirstOrCreate(&frequencyDb, &frequencyDb).Error
if err != nil {
return frequencyDb, err
}
}
antennaCache.Store(frequency, frequencyDb)
return frequencyDb, nil
}
}
func setAntennaFrequencyDb(antennaID uint, frequencyID uint) {
//antennaFrequencyDb := types.AntennaFrequency{
// AntennaID: antennaID,
// FrequencyID: frequencyID,
//}
/*
UPDATE totals
SET count = count + 1
WHERE name = 'bill';
INSERT INTO customers (name, email)
VALUES
(
'Microsoft',
'hotline@microsoft.com'
)
ON CONFLICT (name)
DO
UPDATE
SET email = EXCLUDED.email || ';' || customers.email;
db.Exec("UPDATE orders SET shipped_at=? WHERE id IN (?)", time.Now(), []int64{11,22,33})
*/
// Need to use a raw query because... ???
query := "INSERT INTO antenna_frequencies (antenna_id, frequency_id, count, last_heard) " +
"VALUES (?, ?, 1, NOW()) " +
"ON CONFLICT (antenna_id, frequency_id) DO " +
"UPDATE SET count = antenna_frequencies.count + 1, last_heard = NOW() " +
"WHERE antenna_frequencies.antenna_id = ? AND antenna_frequencies.frequency_id = ? "
db.Exec(query, antennaID, frequencyID, antennaID, frequencyID)
}