Skip to content

Commit d80b5f4

Browse files
committed
Updated pub and sub commands
1 parent feae826 commit d80b5f4

File tree

5 files changed

+103
-2
lines changed

5 files changed

+103
-2
lines changed

Diff for: Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ GOLDFLAGS += -X $(GOPI).GoBuildTime=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
1414
GOFLAGS = -ldflags "-s -w $(GOLDFLAGS)"
1515

1616
install:
17-
$(GOINSTALL) $(GOFLAGS) ./cmd/mosquittosub
17+
$(GOINSTALL) $(GOFLAGS) ./cmd/mqttsub
18+
$(GOINSTALL) $(GOFLAGS) ./cmd/mqttpub
1819

1920
test:
20-
$(GOTEST) -v ./...
21+
$(GOTEST) -v ./sys/...
22+
$(GOTEST) -v ./unit/...
2123

2224
clean:
2325
$(GOCLEAN)

Diff for: cmd/mqttpub/main.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"sync"
6+
7+
// Frameworks
8+
gopi "github.com/djthorpe/gopi/v2"
9+
mosquitto "github.com/djthorpe/mosquitto"
10+
)
11+
12+
var (
13+
wg sync.WaitGroup
14+
)
15+
16+
////////////////////////////////////////////////////////////////////////////////
17+
18+
func Main(app gopi.App, args []string) error {
19+
client := app.UnitInstance("mosquitto").(mosquitto.Client)
20+
21+
// Connect to client
22+
if topic := app.Flags().GetString("topic", gopi.FLAG_NS_DEFAULT); topic == "" {
23+
return gopi.ErrBadParameter.WithPrefix("topic")
24+
} else if len(args) == 0 {
25+
return gopi.ErrHelp
26+
} else if err := client.Connect(mosquitto.MOSQ_FLAG_EVENT_ALL); err != nil {
27+
return err
28+
} else {
29+
// Wait for connect
30+
wg.Add(1)
31+
32+
// Wait for all publish acknowledgements
33+
wg.Wait()
34+
}
35+
36+
// Return success
37+
return nil
38+
}
39+
40+
func EventHandler(_ context.Context, app gopi.App, evt_ gopi.Event) {
41+
evt := evt_.(mosquitto.Event)
42+
client := app.UnitInstance("mosquitto").(mosquitto.Client)
43+
topic := app.Flags().GetString("topic", gopi.FLAG_NS_DEFAULT)
44+
qos := app.Flags().GetInt("qos", gopi.FLAG_NS_DEFAULT)
45+
46+
if evt.Type() == mosquitto.MOSQ_FLAG_EVENT_CONNECT && evt.ReturnCode() == 0 {
47+
for _, arg := range app.Flags().Args() {
48+
if id, err := client.Publish(topic, []byte(arg), mosquitto.OptQOS(qos)); err != nil {
49+
app.Log().Error(err)
50+
} else {
51+
app.Log().Info("PUBLISH:", id)
52+
wg.Add(1)
53+
}
54+
}
55+
wg.Done()
56+
}
57+
58+
if evt.Type() == mosquitto.MOSQ_FLAG_EVENT_PUBLISH {
59+
app.Log().Info("PUBACK:", evt.Id())
60+
wg.Done()
61+
}
62+
}

Diff for: cmd/mqttpub/units.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
// Frameworks
8+
gopi "github.com/djthorpe/gopi/v2"
9+
app "github.com/djthorpe/gopi/v2/app"
10+
11+
// Units
12+
_ "github.com/djthorpe/gopi/v2/unit/bus"
13+
_ "github.com/djthorpe/gopi/v2/unit/logger"
14+
_ "github.com/djthorpe/mosquitto/unit/mosquitto"
15+
)
16+
17+
var (
18+
Events = []gopi.EventHandler{
19+
gopi.EventHandler{Name: "mosquitto.Event", Handler: EventHandler},
20+
}
21+
)
22+
23+
////////////////////////////////////////////////////////////////////////////////
24+
// BOOTSTRAP
25+
26+
func main() {
27+
if app, err := app.NewCommandLineTool(Main, Events, "mosquitto"); err != nil {
28+
fmt.Fprintln(os.Stderr, err)
29+
} else {
30+
// Set flags
31+
app.Flags().FlagString("topic", "", "MQTT Topic")
32+
app.Flags().FlagInt("qos", 1, "MQTT Quality of service")
33+
34+
// Run and exit
35+
os.Exit(app.Run())
36+
}
37+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)