-
Notifications
You must be signed in to change notification settings - Fork 0
/
downlink.go
139 lines (114 loc) · 3.31 KB
/
downlink.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package handler
import (
"time"
pb_broker "github.com/TheThingsNetwork/ttn/api/broker"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/apex/log"
)
func (h *handler) EnqueueDownlink(appDownlink *types.DownlinkMessage) (err error) {
appID, devID := appDownlink.AppID, appDownlink.DevID
ctx := h.Ctx.WithFields(log.Fields{
"AppID": appID,
"DevID": devID,
})
start := time.Now()
defer func() {
if err != nil {
ctx.WithError(err).Warn("Could not enqueue downlink")
} else {
ctx.WithField("Duration", time.Now().Sub(start)).Debug("Enqueued downlink")
}
}()
dev, err := h.devices.Get(appID, devID)
if err != nil {
return err
}
// Clear redundant fields
appDownlink.AppID = ""
appDownlink.DevID = ""
dev.StartUpdate()
dev.NextDownlink = appDownlink
err = h.devices.Set(dev)
if err != nil {
return err
}
h.mqttEvent <- &types.DeviceEvent{
AppID: appID,
DevID: devID,
Event: types.DownlinkScheduledEvent,
}
return nil
}
func (h *handler) HandleDownlink(appDownlink *types.DownlinkMessage, downlink *pb_broker.DownlinkMessage) error {
appID, devID := appDownlink.AppID, appDownlink.DevID
ctx := h.Ctx.WithFields(log.Fields{
"AppID": appID,
"DevID": devID,
"AppEUI": downlink.AppEui,
"DevEUI": downlink.DevEui,
})
var err error
defer func() {
if err != nil {
h.mqttEvent <- &types.DeviceEvent{
AppID: appID,
DevID: devID,
Event: types.DownlinkErrorEvent,
Data: types.ErrorEventData{Error: err.Error()},
}
ctx.WithError(err).Warn("Could not handle downlink")
}
}()
dev, err := h.devices.Get(appID, devID)
if err != nil {
return err
}
// Get Processors
processors := []DownlinkProcessor{
h.ConvertFieldsDown,
h.ConvertToLoRaWAN,
}
ctx.WithField("NumProcessors", len(processors)).Debug("Running Downlink Processors")
downlink.Trace = downlink.Trace.WithEvent("process downlink")
// Run Processors
for _, processor := range processors {
err = processor(ctx, appDownlink, downlink, dev)
if err == ErrNotNeeded {
err = nil
return nil
} else if err != nil {
return err
}
}
h.status.downlink.Mark(1)
ctx.Debug("Send Downlink")
downlink.Trace = downlink.Trace.WithEvent(trace.ForwardEvent, "broker", h.ttnBrokerID)
h.downlink <- downlink
downlinkConfig := types.DownlinkEventConfigInfo{}
if downlink.DownlinkOption.ProtocolConfig != nil {
if lorawan := downlink.DownlinkOption.ProtocolConfig.GetLorawan(); lorawan != nil {
downlinkConfig.Modulation = lorawan.Modulation.String()
downlinkConfig.DataRate = lorawan.DataRate
downlinkConfig.BitRate = uint(lorawan.BitRate)
downlinkConfig.FCnt = uint(lorawan.FCnt)
}
}
if gateway := downlink.DownlinkOption.GatewayConfig; gateway != nil {
downlinkConfig.Frequency = uint(downlink.DownlinkOption.GatewayConfig.Frequency)
downlinkConfig.Power = int(downlink.DownlinkOption.GatewayConfig.Power)
}
h.mqttEvent <- &types.DeviceEvent{
AppID: appDownlink.AppID,
DevID: appDownlink.DevID,
Event: types.DownlinkSentEvent,
Data: types.DownlinkEventData{
Payload: downlink.Payload,
GatewayID: downlink.DownlinkOption.GatewayId,
Config: downlinkConfig,
},
}
return nil
}