/
framelog.go
214 lines (177 loc) · 5.74 KB
/
framelog.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
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
package framelog
import (
"context"
"fmt"
"time"
proto "github.com/golang/protobuf/proto"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/loraserver/api/gw"
"github.com/brocaar/lorawan"
)
const (
gatewayFrameLogUplinkPubSubKeyTempl = "lora:ns:gw:%s:pubsub:frame:uplink"
gatewayFrameLogDownlinkPubSubKeyTempl = "lora:ns:gw:%s:pubsub:frame:downlink"
deviceFrameLogUplinkPubSubKeyTempl = "lora:ns:device:%s:pubsub:frame:uplink"
deviceFrameLogDownlinkPubSubKeyTempl = "lora:ns:device:%s:pubsub:frame:downlink"
)
// FrameLog contains either an uplink or downlink frame.
type FrameLog struct {
UplinkFrame *gw.UplinkFrameSet
DownlinkFrame *gw.DownlinkFrame
}
// LogUplinkFrameForGateways logs the given frame to all the gateway pub-sub keys.
func LogUplinkFrameForGateways(p *redis.Pool, uplinkFrameSet gw.UplinkFrameSet) error {
c := p.Get()
defer c.Close()
c.Send("MULTI")
for _, rx := range uplinkFrameSet.RxInfo {
var id lorawan.EUI64
copy(id[:], rx.GatewayId)
frameLog := gw.UplinkFrameSet{
PhyPayload: uplinkFrameSet.PhyPayload,
TxInfo: uplinkFrameSet.TxInfo,
RxInfo: []*gw.UplinkRXInfo{rx},
}
b, err := proto.Marshal(&frameLog)
if err != nil {
return errors.Wrap(err, "marshal uplink frame-set error")
}
key := fmt.Sprintf(gatewayFrameLogUplinkPubSubKeyTempl, id)
c.Send("PUBLISH", key, b)
}
_, err := c.Do("EXEC")
if err != nil {
return errors.Wrap(err, "publish frame to gateway channel error")
}
return nil
}
// LogDownlinkFrameForGateway logs the given frame to the gateway pub-sub key.
func LogDownlinkFrameForGateway(p *redis.Pool, frame gw.DownlinkFrame) error {
var id lorawan.EUI64
copy(id[:], frame.TxInfo.GatewayId)
c := p.Get()
defer c.Close()
key := fmt.Sprintf(gatewayFrameLogDownlinkPubSubKeyTempl, id)
b, err := proto.Marshal(&frame)
if err != nil {
return errors.Wrap(err, "marshal downlink frame error")
}
_, err = c.Do("PUBLISH", key, b)
if err != nil {
return errors.Wrap(err, "publish frame to gateway channel error")
}
return nil
}
// LogDownlinkFrameForDevEUI logs the given frame to the device pub-sub key.
func LogDownlinkFrameForDevEUI(p *redis.Pool, devEUI lorawan.EUI64, frame gw.DownlinkFrame) error {
c := p.Get()
defer c.Close()
key := fmt.Sprintf(deviceFrameLogDownlinkPubSubKeyTempl, devEUI)
b, err := proto.Marshal(&frame)
if err != nil {
return errors.Wrap(err, "marshal downlink frame error")
}
_, err = c.Do("PUBLISH", key, b)
if err != nil {
return errors.Wrap(err, "publish frame to device channel error")
}
return nil
}
// LogUplinkFrameForDevEUI logs the given frame to the pub-sub key of the given DevEUI.
func LogUplinkFrameForDevEUI(p *redis.Pool, devEUI lorawan.EUI64, frame gw.UplinkFrameSet) error {
c := p.Get()
defer c.Close()
b, err := proto.Marshal(&frame)
if err != nil {
return errors.Wrap(err, "marshal uplink frame error")
}
key := fmt.Sprintf(deviceFrameLogUplinkPubSubKeyTempl, devEUI)
_, err = c.Do("PUBLISH", key, b)
if err != nil {
return errors.Wrap(err, "publish frame to device channel error")
}
return nil
}
// GetFrameLogForGateway subscribes to the uplink and downlink frame logs
// for the given gateway and sends this to the given channel.
func GetFrameLogForGateway(ctx context.Context, p *redis.Pool, gatewayID lorawan.EUI64, frameLogChan chan FrameLog) error {
uplinkKey := fmt.Sprintf(gatewayFrameLogUplinkPubSubKeyTempl, gatewayID)
downlinkKey := fmt.Sprintf(gatewayFrameLogDownlinkPubSubKeyTempl, gatewayID)
return getFrameLogs(ctx, p, uplinkKey, downlinkKey, frameLogChan)
}
// GetFrameLogForDevice subscribes to the uplink and downlink frame logs
// for the given device and sends this to the given channel.
func GetFrameLogForDevice(ctx context.Context, p *redis.Pool, devEUI lorawan.EUI64, frameLogChan chan FrameLog) error {
uplinkKey := fmt.Sprintf(deviceFrameLogUplinkPubSubKeyTempl, devEUI)
downlinkKey := fmt.Sprintf(deviceFrameLogDownlinkPubSubKeyTempl, devEUI)
return getFrameLogs(ctx, p, uplinkKey, downlinkKey, frameLogChan)
}
func getFrameLogs(ctx context.Context, p *redis.Pool, uplinkKey, downlinkKey string, frameLogChan chan FrameLog) error {
c := p.Get()
defer c.Close()
psc := redis.PubSubConn{Conn: c}
if err := psc.Subscribe(uplinkKey, downlinkKey); err != nil {
return errors.Wrap(err, "subscribe error")
}
done := make(chan error, 1)
go func() {
for {
switch v := psc.Receive().(type) {
case redis.Message:
fl, err := redisMessageToFrameLog(v, uplinkKey, downlinkKey)
if err != nil {
log.WithError(err).Error("decode message error")
} else {
frameLogChan <- fl
}
case redis.Subscription:
if v.Count == 0 {
done <- nil
return
}
case error:
done <- v
return
}
}
}()
// todo: make this a config value?
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
loop:
for {
select {
case <-ticker.C:
if err := psc.Ping(""); err != nil {
log.WithError(err).Error("subscription ping error")
break loop
}
case <-ctx.Done():
break loop
case err := <-done:
return err
}
}
if err := psc.Unsubscribe(); err != nil {
return errors.Wrap(err, "unsubscribe error")
}
return <-done
}
func redisMessageToFrameLog(msg redis.Message, uplinkKey, downlinkKey string) (FrameLog, error) {
var fl FrameLog
if msg.Channel == uplinkKey {
fl.UplinkFrame = &gw.UplinkFrameSet{}
if err := proto.Unmarshal(msg.Data, fl.UplinkFrame); err != nil {
return fl, errors.Wrap(err, "unmarshal uplink frame-set error")
}
}
if msg.Channel == downlinkKey {
fl.DownlinkFrame = &gw.DownlinkFrame{}
if err := proto.Unmarshal(msg.Data, fl.DownlinkFrame); err != nil {
return fl, errors.Wrap(err, "unmarshal downlink frame error")
}
}
return fl, nil
}