This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
applications_pf_set.go
170 lines (144 loc) · 4.22 KB
/
applications_pf_set.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
// 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 cmd
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/TheThingsNetwork/ttn/api/handler"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/apex/log"
"github.com/spf13/cobra"
)
var applicationsPayloadFunctionsSetCmd = &cobra.Command{
Use: "set [decoder/converter/validator/encoder] [file.js]",
Short: "Set payload functions of an application",
Long: `ttnctl pf set can be used to get or set payload functions of an application.
The functions are read from the supplied file or from STDIN.`,
Example: `$ ttnctl applications pf set decoder
INFO Discovering Handler...
INFO Connecting with Handler...
function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};
// if (port === 1) {
// decoded.led = bytes[0];
// }
return decoded;
}
########## Write your Decoder here and end with Ctrl+D (EOF):
function Decoder(bytes, port) {
var decoded = {};
// if (port === 1) {
// decoded.led = bytes[0];
// }
return decoded;
}
INFO Updated application AppID=test
`,
Run: func(cmd *cobra.Command, args []string) {
appID := util.GetAppID(ctx)
conn, manager := util.GetHandlerManager(ctx, appID)
defer conn.Close()
app, err := manager.GetApplication(appID)
if err != nil && strings.Contains(err.Error(), "not found") {
app = &handler.Application{AppId: appID}
} else if err != nil {
ctx.WithError(err).Fatal("Could not get existing application.")
}
if len(args) == 0 {
cmd.UsageFunc()(cmd)
return
}
function := args[0]
if len(args) == 2 {
content, err := ioutil.ReadFile(args[1])
if err != nil {
ctx.WithError(err).Fatal("Could not read function file")
}
switch function {
case "decoder":
app.Decoder = string(content)
case "converter":
app.Converter = string(content)
case "validator":
app.Validator = string(content)
case "encoder":
app.Encoder = string(content)
default:
ctx.Fatalf("Function %s does not exist", function)
}
} else {
switch function {
case "decoder":
fmt.Println(`function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};
// if (port === 1) {
// decoded.led = bytes[0];
// }
return decoded;
}
########## Write your Decoder here and end with Ctrl+D (EOF):`)
app.Decoder = readFunction()
case "converter":
fmt.Println(`function Converter(decoded, port) {
// Merge, split or otherwise
// mutate decoded fields.
var converted = decoded;
// if (port === 1 && (converted.led === 0 || converted.led === 1)) {
// converted.led = Boolean(converted.led);
// }
return converted;
}
########## Write your Converter here and end with Ctrl+D (EOF):`)
app.Converter = readFunction()
case "validator":
fmt.Println(`function Validator(converted, port) {
// Return false if the decoded, converted
// message is invalid and should be dropped.
// if (port === 1 && typeof converted.led !== 'boolean') {
// return false;
// }
return true;
}
########## Write your Validator here and end with Ctrl+D (EOF):`)
app.Validator = readFunction()
case "encoder":
fmt.Println(`function Encoder(object, port) {
// Encode downlink messages sent as
// object to an array or buffer of bytes.
var bytes = [];
// if (port === 1) {
// bytes[0] = object.led ? 1 : 0;
// }
return bytes;
}
########## Write your Encoder here and end with Ctrl+D (EOF):`)
app.Encoder = readFunction()
default:
ctx.Fatalf("Function %s does not exist", function)
}
}
err = manager.SetApplication(app)
if err != nil {
ctx.WithError(err).Fatal("Could not update application")
}
ctx.WithFields(log.Fields{
"AppID": appID,
}).Infof("Updated application")
},
}
func readFunction() string {
content, err := ioutil.ReadAll(os.Stdin)
if err != nil {
ctx.WithError(err).Fatal("Could not read function from STDIN.")
}
return strings.TrimSpace(string(content))
}
func init() {
applicationsPayloadFunctionsCmd.AddCommand(applicationsPayloadFunctionsSetCmd)
}