-
Notifications
You must be signed in to change notification settings - Fork 0
/
dry_run.go
101 lines (81 loc) · 2.66 KB
/
dry_run.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
// 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 (
"encoding/json"
pb "github.com/TheThingsNetwork/ttn/api/handler"
"github.com/TheThingsNetwork/ttn/core/handler/functions"
"github.com/TheThingsNetwork/ttn/utils/errors"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711"
)
// DryUplink converts the uplink message payload by running the payload
// functions that are provided in the DryUplinkMessage, without actually going to the network.
// This is helpful for testing the payload functions without having to save them.
func (h *handlerManager) DryUplink(ctx context.Context, in *pb.DryUplinkMessage) (*pb.DryUplinkResult, error) {
app := in.App
logger := functions.NewEntryLogger()
flds := ""
valid := true
if app != nil && app.Decoder != "" {
functions := &UplinkFunctions{
Decoder: app.Decoder,
Converter: app.Converter,
Validator: app.Validator,
Logger: logger,
}
fields, val, err := functions.Process(in.Payload, uint8(in.Port))
if err != nil {
return nil, err
}
valid = val
marshalled, err := json.Marshal(fields)
if err != nil {
return nil, err
}
flds = string(marshalled)
}
return &pb.DryUplinkResult{
Payload: in.Payload,
Fields: flds,
Valid: valid,
Logs: logger.Logs,
}, nil
}
// DryDownlink converts the downlink message payload by running the payload
// functions that are provided in the DryDownlinkMessage, without actually going to the network.
// This is helpful for testing the payload functions without having to save them.
func (h *handlerManager) DryDownlink(ctx context.Context, in *pb.DryDownlinkMessage) (*pb.DryDownlinkResult, error) {
app := in.App
if in.Payload != nil {
if in.Fields != "" {
return nil, errors.NewErrInvalidArgument("Downlink", "Both Fields and Payload provided")
}
return &pb.DryDownlinkResult{
Payload: in.Payload,
}, nil
}
if in.Fields == "" {
return nil, errors.NewErrInvalidArgument("Downlink", "Neither Fields nor Payload provided")
}
if app == nil || app.Encoder == "" {
return nil, errors.NewErrInvalidArgument("Encoder", "Not specified")
}
logger := functions.NewEntryLogger()
functions := &DownlinkFunctions{
Encoder: app.Encoder,
Logger: logger,
}
var parsed map[string]interface{}
err := json.Unmarshal([]byte(in.Fields), &parsed)
if err != nil {
return nil, errors.NewErrInvalidArgument("Fields", err.Error())
}
payload, _, err := functions.Process(parsed, uint8(in.Port))
if err != nil {
return nil, err
}
return &pb.DryDownlinkResult{
Payload: payload,
Logs: logger.Logs,
}, nil
}