forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
103 lines (85 loc) · 3.1 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
// 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 fields
import (
"github.com/TheThingsNetwork/ttn/core"
"github.com/TheThingsNetwork/ttn/core/adapters/mqtt"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/apex/log"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Adapter represents the interface of an application
type Adapter interface {
core.AppClient
Storage() AppStorage
SubscribeDownlink(handler core.HandlerServer) error
}
type adapter struct {
ctx log.Interface
storage AppStorage
mqtt mqtt.Adapter
}
// NewAdapter returns a new adapter that processes binary payload to fields
func NewAdapter(ctx log.Interface, storage AppStorage, next mqtt.Adapter) Adapter {
return &adapter{ctx, storage, next}
}
func (s *adapter) HandleData(context context.Context, in *core.DataAppReq, opt ...grpc.CallOption) (*core.DataAppRes, error) {
var appEUI types.AppEUI
appEUI.Unmarshal(in.AppEUI)
var devEUI types.DevEUI
devEUI.Unmarshal(in.DevEUI)
ctx := s.ctx.WithFields(&log.Fields{"AppEUI": appEUI, "DevEUI": devEUI})
req := core.DataUpAppReq{
Payload: in.Payload,
Metadata: core.ProtoMetaToAppMeta(in.Metadata...),
FPort: uint8(in.FPort),
FCnt: in.FCnt,
DevEUI: devEUI.String(),
}
functions, err := s.storage.GetFunctions(appEUI)
if err != nil {
ctx.WithError(err).Warn("Failed to get functions")
// If we can't get the functions here, just publish it anyway without fields
return new(core.DataAppRes), s.mqtt.PublishUplink(appEUI, devEUI, req)
}
if functions == nil {
// Publish when there are no payload functions set
return new(core.DataAppRes), s.mqtt.PublishUplink(appEUI, devEUI, req)
}
fields, valid, err := functions.Process(in.Payload)
if err != nil {
// If there were errors processing the payload, just publish it anyway
// without fields
ctx.WithError(err).Warn("Failed to process payload")
return new(core.DataAppRes), s.mqtt.PublishUplink(appEUI, devEUI, req)
}
if !valid {
// If the payload has been processed successfully but is not valid, it should
// not be published
ctx.Info("The processed payload is not valid")
return new(core.DataAppRes), errors.New(errors.Operational, "The processed payload is not valid")
}
req.Fields = fields
if err := s.mqtt.PublishUplink(appEUI, devEUI, req); err != nil {
return new(core.DataAppRes), errors.New(errors.Operational, "Failed to publish data")
}
return new(core.DataAppRes), nil
}
func (s *adapter) HandleJoin(context context.Context, in *core.JoinAppReq, opt ...grpc.CallOption) (*core.JoinAppRes, error) {
var appEUI types.AppEUI
appEUI.Unmarshal(in.AppEUI)
var devEUI types.DevEUI
devEUI.Unmarshal(in.DevEUI)
req := core.OTAAAppReq{
Metadata: core.ProtoMetaToAppMeta(in.Metadata...),
}
return new(core.JoinAppRes), s.mqtt.PublishActivation(appEUI, devEUI, req)
}
func (s *adapter) SubscribeDownlink(handler core.HandlerServer) error {
return s.mqtt.SubscribeDownlink(handler)
}
func (s *adapter) Storage() AppStorage {
return s.storage
}