Skip to content

Commit 3eb2509

Browse files
author
Paolo Calao
authored
Restructure commands (#6)
* Restructure commands organization * Restructure mqtt package * Split ping in cli and command * Rename ping params
1 parent 9d49b38 commit 3eb2509

File tree

8 files changed

+164
-136
lines changed

8 files changed

+164
-136
lines changed

cli/ping/ping.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ping
2+
3+
import (
4+
"github.com/bcmi-labs/iot-cloud-cli/command/ping"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var (
9+
host string
10+
username string
11+
password string
12+
thingID string
13+
troubleshooting bool
14+
)
15+
16+
func NewCommand() *cobra.Command {
17+
pingCommand := &cobra.Command{
18+
Use: "ping",
19+
Short: "Ping Arduino IoT Cloud",
20+
Long: "Ping Arduino IoT Cloud",
21+
RunE: runPingCommand,
22+
}
23+
24+
pingCommand.Flags().StringVarP(&host, "host", "b", "tcps://mqtts-up.iot.arduino.cc:8884", "Broker endpoint (required)")
25+
pingCommand.Flags().StringVarP(&username, "username", "u", "", "Username (required)")
26+
pingCommand.Flags().StringVarP(&password, "password", "p", "", "Password (required)")
27+
pingCommand.Flags().StringVarP(&thingID, "thing_id", "t", "", "Thing ID (required)")
28+
29+
pingCommand.Flags().BoolVar(&troubleshooting, "troubleshooting", false, "Enable troubleshooting mode (full logs from the MQTT client)")
30+
31+
pingCommand.MarkFlagRequired("username")
32+
pingCommand.MarkFlagRequired("password")
33+
pingCommand.MarkFlagRequired("thing_id")
34+
35+
return pingCommand
36+
}
37+
38+
func runPingCommand(cmd *cobra.Command, args []string) error {
39+
params := &ping.Params{
40+
Host: host,
41+
Username: username,
42+
Password: password,
43+
ThingID: thingID,
44+
Troubleshooting: troubleshooting,
45+
}
46+
47+
return ping.Ping(params)
48+
}

cli/root.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/bcmi-labs/iot-cloud-cli/cli/ping"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func Execute() {
12+
rootCmd := &cobra.Command{}
13+
rootCmd.AddCommand(ping.NewCommand())
14+
15+
if err := rootCmd.Execute(); err != nil {
16+
fmt.Fprintln(os.Stderr, err)
17+
}
18+
}

command/ping.go

-117
This file was deleted.

command/ping/ping.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package ping
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"math/rand"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
12+
"github.com/bcmi-labs/iot-cloud-cli/internal/mqtt"
13+
"github.com/bcmi-labs/iot-cloud-cli/internal/properties"
14+
paho "github.com/eclipse/paho.mqtt.golang"
15+
)
16+
17+
type Params struct {
18+
Host string
19+
Username string
20+
Password string
21+
ThingID string
22+
Troubleshooting bool
23+
}
24+
25+
func Ping(params *Params) error {
26+
if params.Troubleshooting {
27+
paho.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
28+
paho.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
29+
paho.WARN = log.New(os.Stdout, "[WARN] ", 0)
30+
paho.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
31+
}
32+
33+
mqttAdapter := mqtt.NewAdapterWithAuth(
34+
params.Host,
35+
params.Username,
36+
params.Username,
37+
params.Password,
38+
)
39+
40+
err := mqttAdapter.Connect()
41+
if err != nil {
42+
return err
43+
}
44+
fmt.Println("Connected to Arduino IoT Cloud")
45+
46+
inboundTopic := fmt.Sprintf("/a/t/%s/e/i", params.ThingID)
47+
outboundTopic := fmt.Sprintf("/a/t/%s/e/o", params.ThingID)
48+
49+
// Subscribing to the thing inbound topic to received new properties
50+
// values from the cloud.
51+
ok, _ := mqttAdapter.On(inboundTopic, func(msg paho.Message) {
52+
fmt.Println("received a message", msg)
53+
})
54+
fmt.Println("Subscribed", ok)
55+
56+
// Sending new random values (in the 0-100 range) to the thing specified
57+
// using the flags
58+
go func() {
59+
for {
60+
randomValue := rand.Intn(100)
61+
62+
property, err := properties.NewInteger("counter", randomValue)
63+
if err != nil {
64+
fmt.Println("Failed to encode property value", err)
65+
}
66+
67+
// Publishing a new random value to the outbound topic of the thing
68+
err = mqttAdapter.Publish(outboundTopic, property)
69+
if err != nil {
70+
fmt.Println("Failed to send property to Arduino IoT Cloud", err)
71+
}
72+
fmt.Println("Property value sent successfully", randomValue)
73+
74+
wait := 3
75+
time.Sleep(time.Duration(wait) * time.Second)
76+
}
77+
}()
78+
79+
// Wait for a sigterm signal (CTRL+C) to disconnect from the broker
80+
// and say good bye
81+
c := make(chan os.Signal, 1)
82+
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
83+
84+
// blocking here waiting for a signal from the terminal 😪
85+
<-c
86+
87+
mqttAdapter.Disconnect()
88+
if err != nil {
89+
return err
90+
}
91+
92+
fmt.Println("Disconnected from Arduino IoT Cloud.")
93+
fmt.Println("Completed successfully.")
94+
return nil
95+
}

command/root.go

-16
This file was deleted.
File renamed without changes.

utils/topic.go renamed to internal/mqtt/topic.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package mqtt
22

33
import (
44
"regexp"

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

3-
import "github.com/bcmi-labs/iot-cloud-cli/command"
3+
import "github.com/bcmi-labs/iot-cloud-cli/cli"
44

55
func main() {
6-
command.Execute()
6+
cli.Execute()
77
}

0 commit comments

Comments
 (0)