-
-
Notifications
You must be signed in to change notification settings - Fork 4
Restructure commands #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ping | ||
|
||
import ( | ||
"github.com/bcmi-labs/iot-cloud-cli/command/ping" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
host string | ||
username string | ||
password string | ||
thingID string | ||
troubleshooting bool | ||
) | ||
|
||
func NewCommand() *cobra.Command { | ||
pingCommand := &cobra.Command{ | ||
Use: "ping", | ||
Short: "Ping Arduino IoT Cloud", | ||
Long: "Ping Arduino IoT Cloud", | ||
RunE: runPingCommand, | ||
} | ||
|
||
pingCommand.Flags().StringVarP(&host, "host", "b", "tcps://mqtts-up.iot.arduino.cc:8884", "Broker endpoint (required)") | ||
pingCommand.Flags().StringVarP(&username, "username", "u", "", "Username (required)") | ||
pingCommand.Flags().StringVarP(&password, "password", "p", "", "Password (required)") | ||
pingCommand.Flags().StringVarP(&thingID, "thing_id", "t", "", "Thing ID (required)") | ||
|
||
pingCommand.Flags().BoolVar(&troubleshooting, "troubleshooting", false, "Enable troubleshooting mode (full logs from the MQTT client)") | ||
|
||
pingCommand.MarkFlagRequired("username") | ||
pingCommand.MarkFlagRequired("password") | ||
pingCommand.MarkFlagRequired("thing_id") | ||
|
||
return pingCommand | ||
} | ||
|
||
func runPingCommand(cmd *cobra.Command, args []string) error { | ||
params := &ping.PingParams{ | ||
Host: host, | ||
Username: username, | ||
Password: password, | ||
ThingID: thingID, | ||
Troubleshooting: troubleshooting, | ||
} | ||
|
||
return ping.Ping(params) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/bcmi-labs/iot-cloud-cli/cli/ping" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Execute() { | ||
rootCmd := &cobra.Command{} | ||
rootCmd.AddCommand(ping.NewCommand()) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package ping | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"math/rand" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/bcmi-labs/iot-cloud-cli/internal/mqtt" | ||
"github.com/bcmi-labs/iot-cloud-cli/internal/properties" | ||
paho "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
type PingParams struct { | ||
Host string | ||
Username string | ||
Password string | ||
ThingID string | ||
Troubleshooting bool | ||
} | ||
|
||
func Ping(params *PingParams) error { | ||
if params.Troubleshooting { | ||
paho.ERROR = log.New(os.Stdout, "[ERROR] ", 0) | ||
paho.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0) | ||
paho.WARN = log.New(os.Stdout, "[WARN] ", 0) | ||
paho.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0) | ||
} | ||
|
||
mqttAdapter := mqtt.NewAdapterWithAuth( | ||
params.Host, | ||
params.Username, | ||
params.Username, | ||
params.Password, | ||
) | ||
|
||
err := mqttAdapter.Connect() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Connected to Arduino IoT Cloud") | ||
|
||
inboundTopic := fmt.Sprintf("/a/t/%s/e/i", params.ThingID) | ||
outboundTopic := fmt.Sprintf("/a/t/%s/e/o", params.ThingID) | ||
|
||
// Subscribing to the thing inbound topic to received new properties | ||
// values from the cloud. | ||
ok, _ := mqttAdapter.On(inboundTopic, func(msg paho.Message) { | ||
fmt.Println("received a message", msg) | ||
}) | ||
fmt.Println("Subscribed", ok) | ||
|
||
// Sending new random values (in the 0-100 range) to the thing specified | ||
// using the flags | ||
go func() { | ||
for { | ||
randomValue := rand.Intn(100) | ||
|
||
property, err := properties.NewInteger("counter", randomValue) | ||
if err != nil { | ||
fmt.Println("Failed to encode property value", err) | ||
} | ||
|
||
// Publishing a new random value to the outbound topic of the thing | ||
err = mqttAdapter.Publish(outboundTopic, property) | ||
if err != nil { | ||
fmt.Println("Failed to send property to Arduino IoT Cloud", err) | ||
} | ||
fmt.Println("Property value sent successfully", randomValue) | ||
|
||
wait := 3 | ||
time.Sleep(time.Duration(wait) * time.Second) | ||
} | ||
}() | ||
|
||
// Wait for a sigterm signal (CTRL+C) to disconnect from the broker | ||
// and say good bye | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
// blocking here waiting for a signal from the terminal 😪 | ||
<-c | ||
|
||
mqttAdapter.Disconnect() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Disconnected from Arduino IoT Cloud.") | ||
fmt.Println("Completed successfully.") | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package mqtt | ||
|
||
import ( | ||
"regexp" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package main | ||
|
||
import "github.com/bcmi-labs/iot-cloud-cli/command" | ||
import "github.com/bcmi-labs/iot-cloud-cli/cli" | ||
|
||
func main() { | ||
command.Execute() | ||
cli.Execute() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.