1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

Add constructor clientid parameter and set proper datatype for On callback function

This commit is contained in:
Adrian Zankich 2014-11-06 12:28:04 -08:00
parent cbc5546dbb
commit ce7181741f
5 changed files with 28 additions and 23 deletions

View File

@ -1,25 +1,26 @@
package main package main
import ( import (
"time"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata" "github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio" "github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/mqtt" "github.com/hybridgroup/gobot/platforms/mqtt"
"time"
) )
func main() { func main() {
gbot := gobot.NewGobot() gbot := gobot.NewGobot()
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://localhost:1883") mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://localhost:1883", "blinker")
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0") firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "13") led := gpio.NewLedDriver(firmataAdaptor, "led", "13")
work := func() { work := func() {
mqttAdaptor.On("lights/on", func(data interface{}) { mqttAdaptor.On("lights/on", func(data []byte) {
led.On() led.On()
}) })
mqttAdaptor.On("lights/off", func(data interface{}) { mqttAdaptor.On("lights/off", func(data []byte) {
led.Off() led.Off()
}) })
data := []byte("") data := []byte("")

View File

@ -2,21 +2,22 @@ package main
import ( import (
"fmt" "fmt"
"time"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/mqtt" "github.com/hybridgroup/gobot/platforms/mqtt"
"time"
) )
func main() { func main() {
gbot := gobot.NewGobot() gbot := gobot.NewGobot()
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://0.0.0.0:1883") mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://0.0.0.0:1883", "pinger")
work := func() { work := func() {
mqttAdaptor.On("hello", func(data interface{}) { mqttAdaptor.On("hello", func(data []byte) {
fmt.Println("hello") fmt.Println("hello")
}) })
mqttAdaptor.On("hola", func(data interface{}) { mqttAdaptor.On("hola", func(data []byte) {
fmt.Println("hola") fmt.Println("hola")
}) })
data := []byte("o") data := []byte("o")

View File

@ -2,7 +2,7 @@
Gobot (http://gobot.io/) is a library for robotics and physical computing using Go Gobot (http://gobot.io/) is a library for robotics and physical computing using Go
This repository contains the Gobot adaptor for the MQTT machine to machine message broker (http://getpebble.com/). This repository contains the Gobot adaptor for the MQTT machine to machine message broker (http://mqtt.org/).
## Installing ## Installing
@ -28,13 +28,13 @@ import (
func main() { func main() {
gbot := gobot.NewGobot() gbot := gobot.NewGobot()
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://0.0.0.0:1883") mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://0.0.0.0:1883", "pinger")
work := func() { work := func() {
mqttAdaptor.On("hello", func(data interface{}) { mqttAdaptor.On("hello", func(data []byte) {
fmt.Println("hello") fmt.Println("hello")
}) })
mqttAdaptor.On("hola", func(data interface{}) { mqttAdaptor.On("hola", func(data []byte) {
fmt.Println("hola") fmt.Println("hola")
}) })
data := []byte("o") data := []byte("o")

View File

@ -7,24 +7,26 @@ import (
type MqttAdaptor struct { type MqttAdaptor struct {
gobot.Adaptor gobot.Adaptor
Host string Host string
client *mqtt.MqttClient clientID string
client *mqtt.MqttClient
} }
// NewMqttAdaptor creates a new mqtt adaptor with specified name // NewMqttAdaptor creates a new mqtt adaptor with specified name, host and client id
func NewMqttAdaptor(name string, host string) *MqttAdaptor { func NewMqttAdaptor(name string, host string, clientID string) *MqttAdaptor {
return &MqttAdaptor{ return &MqttAdaptor{
Adaptor: *gobot.NewAdaptor( Adaptor: *gobot.NewAdaptor(
name, name,
"MqttAdaptor", "MqttAdaptor",
), ),
Host: host, Host: host,
clientID: clientID,
} }
} }
// Connect returns true if connection to mqtt is established // Connect returns true if connection to mqtt is established
func (a *MqttAdaptor) Connect() bool { func (a *MqttAdaptor) Connect() bool {
opts := createClientOptions("sub", a.Host) opts := createClientOptions(a.clientID, a.Host)
a.client = mqtt.NewClient(opts) a.client = mqtt.NewClient(opts)
a.client.Start() a.client.Start()
return true return true
@ -60,7 +62,7 @@ func (a *MqttAdaptor) Publish(topic string, message []byte) bool {
} }
// Subscribe to a topic, and then call the message handler function when data is received // Subscribe to a topic, and then call the message handler function when data is received
func (a *MqttAdaptor) On(event string, f func(s interface{})) bool { func (a *MqttAdaptor) On(event string, f func(s []byte)) bool {
if a.client == nil { if a.client == nil {
return false return false
} }

View File

@ -2,12 +2,13 @@ package mqtt
import ( import (
"fmt" "fmt"
"github.com/hybridgroup/gobot"
"testing" "testing"
"github.com/hybridgroup/gobot"
) )
func initTestMqttAdaptor() *MqttAdaptor { func initTestMqttAdaptor() *MqttAdaptor {
return NewMqttAdaptor("mqtt", "localhost:1883") return NewMqttAdaptor("mqtt", "localhost:1883", "client")
} }
func TestMqttAdaptorConnect(t *testing.T) { func TestMqttAdaptorConnect(t *testing.T) {
@ -35,7 +36,7 @@ func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) { func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor() a := initTestMqttAdaptor()
gobot.Assert(t, a.On("hola", func(data interface{}) { gobot.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola") fmt.Println("hola")
}), false) }), false)
} }
@ -43,7 +44,7 @@ func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
func TestMqttAdaptorOnWhenConnected(t *testing.T) { func TestMqttAdaptorOnWhenConnected(t *testing.T) {
a := initTestMqttAdaptor() a := initTestMqttAdaptor()
a.Connect() a.Connect()
gobot.Assert(t, a.On("hola", func(data interface{}) { gobot.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola") fmt.Println("hola")
}), true) }), true)
} }