Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Integrate payload format in ttnctl
Browse files Browse the repository at this point in the history
  • Loading branch information
johanstokking committed Apr 5, 2017
1 parent 12e9d8d commit f8fef74
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 122 deletions.
73 changes: 39 additions & 34 deletions ttnctl/cmd/applications_pf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import (
"github.com/spf13/cobra"
)

var applicationsPayloadFunctionsCmd = &cobra.Command{
var applicationsPayloadFormatCmd = &cobra.Command{
Use: "pf",
Short: "Show the payload functions",
Long: `ttnctl applications pf shows the payload functions for decoding,
converting and validating binary payload.`,
Short: "Show the payload format",
Long: `ttnctl applications pf shows the payload format to handle
binary payload.`,
Example: `$ ttnctl applications pf
INFO Discovering Handler...
INFO Connecting with Handler...
INFO Found Application
INFO Decoder function
INFO Found application
INFO Custom decoder function
function Decoder(bytes, port) {
var decoded = {};
if (port === 1) {
decoded.led = bytes[0];
}
return decoded;
}
INFO No converter function
INFO No validator function
INFO No encoder function
INFO No custom converter function
INFO No custom validator function
INFO No custom encoder function
`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 0, 0)
Expand All @@ -44,38 +44,43 @@ function Decoder(bytes, port) {
ctx.WithError(err).Fatal("Could not get application.")
}

ctx.Info("Found Application")
ctx.Info("Found application")

if app.Decoder != "" {
ctx.Info("Decoder function")
fmt.Println(app.Decoder)
} else {
ctx.Info("No decoder function")
}
switch app.PayloadFormat {
case "custom":
if app.Decoder != "" {
ctx.Info("Custom decoder function")
fmt.Println(app.Decoder)
} else {
ctx.Info("No custom decoder function")
}

if app.Converter != "" {
ctx.Info("Converter function")
fmt.Println(app.Converter)
} else {
ctx.Info("No converter function")
}
if app.Converter != "" {
ctx.Info("Custom converter function")
fmt.Println(app.Converter)
} else {
ctx.Info("No custom converter function")
}

if app.Validator != "" {
ctx.Info("Validator function")
fmt.Println(app.Validator)
} else {
ctx.Info("No validator function")
}
if app.Validator != "" {
ctx.Info("Custom validator function")
fmt.Println(app.Validator)
} else {
ctx.Info("No custom validator function")
}

if app.Encoder != "" {
ctx.Info("Encoder function")
fmt.Println(app.Encoder)
} else {
ctx.Info("No encoder function")
if app.Encoder != "" {
ctx.Info("Custom encoder function")
fmt.Println(app.Encoder)
} else {
ctx.Info("No custom encoder function")
}
default:
ctx.Infof("Payload format set to %s", app.PayloadFormat)
}
},
}

func init() {
applicationsCmd.AddCommand(applicationsPayloadFunctionsCmd)
applicationsCmd.AddCommand(applicationsPayloadFormatCmd)
}
181 changes: 93 additions & 88 deletions ttnctl/cmd/applications_pf_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"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.`,
var applicationsPayloadFormatSetCmd = &cobra.Command{
Use: "set [decoder/converter/validator/encoder/cayennelpp] [file.js]",
Short: "Set payload format of an application",
Long: `ttnctl pf set can be used to get or set the payload format and functions of an application.
When using payload functions, you can load a file or provide them through stdin.`,
Example: `$ ttnctl applications pf set decoder
INFO Discovering Handler...
INFO Connecting with Handler...
Expand Down Expand Up @@ -70,35 +70,38 @@ Port: 1
ctx.WithError(err).Fatal("Could not get existing application.")
}

function := args[0]
format := args[0]

if len(args) == 2 {
content, err := ioutil.ReadFile(args[1])
if err != nil {
ctx.WithError(err).Fatal("Could not read function file")
}
fmt.Println(fmt.Sprintf(`
switch format {
case "decoder", "converter", "validator", "encoder":
app.PayloadFormat = "custom"
if len(args) == 2 {
content, err := ioutil.ReadFile(args[1])
if err != nil {
ctx.WithError(err).Fatal("Could not read function file")
}
fmt.Println(fmt.Sprintf(`
Function read from %s:
%s
`, args[1], string(content)))

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) {
switch format {
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", format)
}
} else {
switch format {
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 = {};
Expand All @@ -110,9 +113,9 @@ Function read from %s:
return decoded;
}
########## Write your Decoder here and end with Ctrl+D (EOF):`)
app.Decoder = readFunction(ctx)
case "converter":
fmt.Println(`function Converter(decoded, port) {
app.Decoder = readFunction(ctx)
case "converter":
fmt.Println(`function Converter(decoded, port) {
// Merge, split or otherwise
// mutate decoded fields.
var converted = decoded;
Expand All @@ -124,9 +127,9 @@ Function read from %s:
return converted;
}
########## Write your Converter here and end with Ctrl+D (EOF):`)
app.Converter = readFunction(ctx)
case "validator":
fmt.Println(`function Validator(converted, port) {
app.Converter = readFunction(ctx)
case "validator":
fmt.Println(`function Validator(converted, port) {
// Return false if the decoded, converted
// message is invalid and should be dropped.
Expand All @@ -137,9 +140,9 @@ Function read from %s:
return true;
}
########## Write your Validator here and end with Ctrl+D (EOF):`)
app.Validator = readFunction(ctx)
case "encoder":
fmt.Println(`function Encoder(object, port) {
app.Validator = readFunction(ctx)
case "encoder":
fmt.Println(`function Encoder(object, port) {
// Encode downlink messages sent as
// object to an array or buffer of bytes.
var bytes = [];
Expand All @@ -151,59 +154,61 @@ Function read from %s:
return bytes;
}
########## Write your Encoder here and end with Ctrl+D (EOF):`)
app.Encoder = readFunction(ctx)
default:
ctx.Fatalf("Function %s does not exist", function)
}
}

if skipTest, _ := cmd.Flags().GetBool("skip-test"); !skipTest {
fmt.Printf("\nDo you want to test the payload functions? (Y/n)\n")
var response string
fmt.Scanln(&response)

if strings.ToLower(response) == "y" || strings.ToLower(response) == "yes" || response == "" {
switch function {
case "decoder", "converter", "validator":
payload, err := util.ReadPayload()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the payload")
}

port, err := util.ReadPort()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the port")
}

result, err := manager.DryUplink(payload, app, uint32(port))
if err != nil {
ctx.WithError(err).Fatal("Could not set the payload function")
}

if !result.Valid {
ctx.Fatal("Could not set the payload function: Invalid result")
}
ctx.Infof("Function tested successfully. Object returned by the converter: %s", result.Fields)
case "encoder":
fields, err := util.ReadFields()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the fields")
}

port, err := util.ReadPort()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the port")
}

result, err := manager.DryDownlinkWithFields(fields, app, uint32(port))
if err != nil {
ctx.WithError(err).Fatal("Could not set the payload function")
app.Encoder = readFunction(ctx)
if skipTest, _ := cmd.Flags().GetBool("skip-test"); !skipTest {
fmt.Printf("\nDo you want to test the payload format? (Y/n)\n")
var response string
fmt.Scanln(&response)

if strings.ToLower(response) == "y" || strings.ToLower(response) == "yes" || response == "" {
switch format {
case "decoder", "converter", "validator":
payload, err := util.ReadPayload()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the payload")
}

port, err := util.ReadPort()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the port")
}

result, err := manager.DryUplink(payload, app, uint32(port))
if err != nil {
ctx.WithError(err).Fatal("Could not set the payload function")
}

if !result.Valid {
ctx.Fatal("Could not set the payload function: Invalid result")
}
ctx.Infof("Function tested successfully. Object returned by the converter: %s", result.Fields)
case "encoder":
fields, err := util.ReadFields()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the fields")
}

port, err := util.ReadPort()
if err != nil {
ctx.WithError(err).Fatal("Could not parse the port")
}

result, err := manager.DryDownlinkWithFields(fields, app, uint32(port))
if err != nil {
ctx.WithError(err).Fatal("Could not set the payload function")
}
ctx.Infof("Function tested successfully. Encoded message: %v", result.Payload)
default:
ctx.Fatalf("Function %s does not exist", format)
}
}
}
ctx.Infof("Function tested successfully. Encoded message: %v", result.Payload)
default:
ctx.Fatalf("Function %s does not exist", function)
ctx.Fatalf("Function %s does not exist", format)
}
}
default:
app.PayloadFormat = format
}

err = manager.SetApplication(app)
Expand All @@ -218,8 +223,8 @@ Function read from %s:
}

func init() {
applicationsPayloadFunctionsSetCmd.Flags().Bool("skip-test", false, "skip payload function test")
applicationsPayloadFunctionsCmd.AddCommand(applicationsPayloadFunctionsSetCmd)
applicationsPayloadFormatSetCmd.Flags().Bool("skip-test", false, "skip payload format test")
applicationsPayloadFormatCmd.AddCommand(applicationsPayloadFormatSetCmd)
}

func readFunction(ctx log.Interface) string {
Expand Down

0 comments on commit f8fef74

Please sign in to comment.