forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
110 lines (95 loc) · 3.01 KB
/
adapter.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
// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package mqtt
import (
"time"
"github.com/TheThingsNetwork/ttn/core"
"github.com/TheThingsNetwork/ttn/core/types"
ttnMQTT "github.com/TheThingsNetwork/ttn/mqtt"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/apex/log"
"golang.org/x/net/context"
)
const publishTimeout = 20 * time.Millisecond
// Adapter represents the interface of an application
type Adapter interface {
PublishUplink(appEUI types.AppEUI, devEUI types.DevEUI, req core.DataUpAppReq) error
PublishActivation(appEUI types.AppEUI, devEUI types.DevEUI, req core.OTAAAppReq) error
SubscribeDownlink(handler core.HandlerServer) error
}
type defaultAdapter struct {
ctx log.Interface
client ttnMQTT.Client
}
// NewAdapter returns a new MQTT handler adapter
func NewAdapter(ctx log.Interface, client ttnMQTT.Client) Adapter {
return &defaultAdapter{ctx, client}
}
// HandleData implements the core.AppClient interface
func (a *defaultAdapter) PublishUplink(appEUI types.AppEUI, devEUI types.DevEUI, req core.DataUpAppReq) error {
ctx := a.ctx.WithFields(log.Fields{
"AppEUI": appEUI,
"DevEUI": devEUI,
})
ctx.Debug("Publishing Uplink")
token := a.client.PublishUplink(appEUI, devEUI, req)
if token.WaitTimeout(publishTimeout) {
// token did not timeout: just return
if token.Error() != nil {
return errors.New(errors.Structural, token.Error())
}
} else {
// token did timeout: wait for it in background and just return
go func() {
token.Wait()
if token.Error() != nil {
ctx.WithError(token.Error()).Warn("Could not publish uplink")
}
}()
}
return nil
}
func (a *defaultAdapter) PublishActivation(appEUI types.AppEUI, devEUI types.DevEUI, req core.OTAAAppReq) error {
ctx := a.ctx.WithFields(log.Fields{
"AppEUI": appEUI,
"DevEUI": devEUI,
})
ctx.Debug("Publishing Activation")
token := a.client.PublishActivation(appEUI, devEUI, req)
if token.WaitTimeout(publishTimeout) {
// token did not timeout: just return
if token.Error() != nil {
return errors.New(errors.Structural, token.Error())
}
} else {
// token did timeout: wait for it in background and just return
go func() {
token.Wait()
if token.Error() != nil {
ctx.WithError(token.Error()).Warn("Could not publish activation")
}
}()
}
return nil
}
func (a *defaultAdapter) SubscribeDownlink(handler core.HandlerServer) error {
token := a.client.SubscribeDownlink(func(client ttnMQTT.Client, appEUI types.AppEUI, devEUI types.DevEUI, req core.DataDownAppReq) {
if len(req.Payload) == 0 {
a.ctx.Debug("Skipping empty downlink")
return
}
a.ctx.WithFields(log.Fields{
"AppEUI": appEUI,
"DevEUI": devEUI,
}).Debug("Receiving Downlink")
// Convert it to an handler downlink
handler.HandleDataDown(context.Background(), &core.DataDownHandlerReq{
Payload: req.Payload,
TTL: req.TTL,
AppEUI: appEUI.Bytes(),
DevEUI: devEUI.Bytes(),
})
})
token.Wait()
return token.Error()
}