Skip to content

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 4 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cli/ping/ping.go
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)
}
18 changes: 18 additions & 0 deletions cli/root.go
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)
}
}
117 changes: 0 additions & 117 deletions command/ping.go

This file was deleted.

95 changes: 95 additions & 0 deletions command/ping/ping.go
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
}
16 changes: 0 additions & 16 deletions command/root.go

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion utils/topic.go → internal/mqtt/topic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package mqtt

import (
"regexp"
Expand Down
4 changes: 2 additions & 2 deletions main.go
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()
}