/
min_gw_set.go
182 lines (145 loc) · 4.03 KB
/
min_gw_set.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
package multicast
import (
"encoding/binary"
"math"
log "github.com/sirupsen/logrus"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/path"
"gonum.org/v1/gonum/graph/simple"
"github.com/brocaar/loraserver/internal/config"
"github.com/brocaar/loraserver/internal/storage"
"github.com/brocaar/lorawan"
)
// GetMinimumGatewaySet returns the minimum set of gateways to cover all
// devices.
func GetMinimumGatewaySet(rxInfoSets []storage.DeviceGatewayRXInfoSet) ([]lorawan.EUI64, error) {
g := simple.NewWeightedUndirectedGraph(0, math.Inf(1))
gwSet := getGatewaySet(rxInfoSets)
// connect all gateways
// W -999 is used so that the mst algorithm will remove the edge between
// the gateway and a device first, over removing an edge between two
// gateways.
for i, gatewayID := range gwSet {
if i == 0 {
continue
}
g.SetWeightedEdge(simple.WeightedEdge{
F: simple.Node(eui64Int64(gwSet[0])),
T: simple.Node(eui64Int64(gatewayID)),
W: -999,
})
}
// connect all devices to the gateways
addDeviceEdges(g, rxInfoSets)
dst := simple.NewWeightedUndirectedGraph(0, math.Inf(1))
path.Kruskal(dst, g)
outMap := make(map[lorawan.EUI64]struct{})
edges := dst.Edges()
for edges.Next() {
e := edges.Edge()
fromEUI := int64ToEUI64(e.From().ID())
toEUI := int64ToEUI64(e.To().ID())
fromIsGW := gwInGWSet(fromEUI, gwSet)
toIsGW := gwInGWSet(toEUI, gwSet)
// skip gateway to gateway edges
if !(fromIsGW && toIsGW) {
if fromIsGW {
outMap[fromEUI] = struct{}{}
}
if toIsGW {
outMap[toEUI] = struct{}{}
}
}
}
var outSlice []lorawan.EUI64
for k := range outMap {
outSlice = append(outSlice, k)
}
return outSlice, nil
}
func getGatewaySet(rxInfoSets []storage.DeviceGatewayRXInfoSet) []lorawan.EUI64 {
gwSet := make(map[lorawan.EUI64]struct{})
for _, rxInfoSet := range rxInfoSets {
for _, rxInfo := range rxInfoSet.Items {
gwSet[rxInfo.GatewayID] = struct{}{}
}
}
var out []lorawan.EUI64
for k := range gwSet {
out = append(out, k)
}
return out
}
func eui64Int64(eui lorawan.EUI64) int64 {
return int64(binary.BigEndian.Uint64(eui[:]))
}
func int64ToEUI64(i int64) lorawan.EUI64 {
var eui lorawan.EUI64
binary.BigEndian.PutUint64(eui[:], uint64(i))
return eui
}
func gwInGWSet(gatewayID lorawan.EUI64, gwSet []lorawan.EUI64) bool {
var found bool
for _, id := range gwSet {
if gatewayID == id {
found = true
}
}
return found
}
func addDeviceEdges(g *simple.WeightedUndirectedGraph, rxInfoSets []storage.DeviceGatewayRXInfoSet) {
for _, rxInfo := range rxInfoSets {
dr, err := config.C.NetworkServer.Band.Band.GetDataRate(rxInfo.DR)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"dr": dr,
}).Error("invalid data-data")
}
reqSNR, ok := config.SpreadFactorToRequiredSNRTable[dr.SpreadFactor]
if ok {
reqSNR += config.C.NetworkServer.NetworkSettings.InstallationMargin
}
var hasReqSNR bool
for _, item := range rxInfo.Items {
if item.LoRaSNR >= reqSNR {
hasReqSNR = true
}
}
for _, item := range rxInfo.Items {
// ignore items that do not have the min. required SNR value,
// knowning that we have items that do meet the min. req SNR.
if item.LoRaSNR < reqSNR && hasReqSNR {
continue
}
g.SetWeightedEdge(deviceGatewayEdge{
gatewayID: item.GatewayID,
devEUI: rxInfo.DevEUI,
graph: g,
})
}
}
}
type deviceGatewayEdge struct {
gatewayID lorawan.EUI64
devEUI lorawan.EUI64
graph *simple.WeightedUndirectedGraph
}
// From implements graph.Edge.
func (e deviceGatewayEdge) From() graph.Node {
return simple.Node(eui64Int64(e.devEUI))
}
// From implements graph.Edge.
func (e deviceGatewayEdge) To() graph.Node {
return simple.Node(eui64Int64(e.gatewayID))
}
// Weight implements graph.WeightedEdge.
// The returned weight is equal to 1 / number of devices covered by the
// gateway.
func (e deviceGatewayEdge) Weight() float64 {
weight := float64(1)
gwNodes := e.graph.From(eui64Int64(e.gatewayID))
if gwNodes.Len() != 0 {
weight = weight / float64(gwNodes.Len())
}
return weight
}