From ce7181741fefd330ad6e0d25d88cd2216325139f Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Thu, 6 Nov 2014 12:28:04 -0800 Subject: [PATCH] Add constructor clientid parameter and set proper datatype for On callback function --- examples/mqtt_firmata_blink.go | 9 +++++---- examples/mqtt_ping.go | 9 +++++---- platforms/mqtt/README.md | 8 ++++---- platforms/mqtt/mqtt_adaptor.go | 16 +++++++++------- platforms/mqtt/mqtt_adaptor_test.go | 9 +++++---- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/examples/mqtt_firmata_blink.go b/examples/mqtt_firmata_blink.go index d0d34e60..cbe75f06 100644 --- a/examples/mqtt_firmata_blink.go +++ b/examples/mqtt_firmata_blink.go @@ -1,25 +1,26 @@ package main import ( + "time" + "github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot/platforms/firmata" "github.com/hybridgroup/gobot/platforms/gpio" "github.com/hybridgroup/gobot/platforms/mqtt" - "time" ) func main() { gbot := gobot.NewGobot() - mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://localhost:1883") + mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://localhost:1883", "blinker") firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0") led := gpio.NewLedDriver(firmataAdaptor, "led", "13") work := func() { - mqttAdaptor.On("lights/on", func(data interface{}) { + mqttAdaptor.On("lights/on", func(data []byte) { led.On() }) - mqttAdaptor.On("lights/off", func(data interface{}) { + mqttAdaptor.On("lights/off", func(data []byte) { led.Off() }) data := []byte("") diff --git a/examples/mqtt_ping.go b/examples/mqtt_ping.go index fbb5fd7e..60d9e013 100644 --- a/examples/mqtt_ping.go +++ b/examples/mqtt_ping.go @@ -2,21 +2,22 @@ package main import ( "fmt" + "time" + "github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot/platforms/mqtt" - "time" ) func main() { 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() { - mqttAdaptor.On("hello", func(data interface{}) { + mqttAdaptor.On("hello", func(data []byte) { fmt.Println("hello") }) - mqttAdaptor.On("hola", func(data interface{}) { + mqttAdaptor.On("hola", func(data []byte) { fmt.Println("hola") }) data := []byte("o") diff --git a/platforms/mqtt/README.md b/platforms/mqtt/README.md index bbd5e50c..ebed479b 100644 --- a/platforms/mqtt/README.md +++ b/platforms/mqtt/README.md @@ -2,7 +2,7 @@ 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 @@ -28,13 +28,13 @@ import ( func main() { 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() { - mqttAdaptor.On("hello", func(data interface{}) { + mqttAdaptor.On("hello", func(data []byte) { fmt.Println("hello") }) - mqttAdaptor.On("hola", func(data interface{}) { + mqttAdaptor.On("hola", func(data []byte) { fmt.Println("hola") }) data := []byte("o") diff --git a/platforms/mqtt/mqtt_adaptor.go b/platforms/mqtt/mqtt_adaptor.go index ce00ecf4..f54cb880 100644 --- a/platforms/mqtt/mqtt_adaptor.go +++ b/platforms/mqtt/mqtt_adaptor.go @@ -7,24 +7,26 @@ import ( type MqttAdaptor struct { gobot.Adaptor - Host string - client *mqtt.MqttClient + Host string + clientID string + client *mqtt.MqttClient } -// NewMqttAdaptor creates a new mqtt adaptor with specified name -func NewMqttAdaptor(name string, host string) *MqttAdaptor { +// NewMqttAdaptor creates a new mqtt adaptor with specified name, host and client id +func NewMqttAdaptor(name string, host string, clientID string) *MqttAdaptor { return &MqttAdaptor{ Adaptor: *gobot.NewAdaptor( name, "MqttAdaptor", ), - Host: host, + Host: host, + clientID: clientID, } } // Connect returns true if connection to mqtt is established func (a *MqttAdaptor) Connect() bool { - opts := createClientOptions("sub", a.Host) + opts := createClientOptions(a.clientID, a.Host) a.client = mqtt.NewClient(opts) a.client.Start() 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 -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 { return false } diff --git a/platforms/mqtt/mqtt_adaptor_test.go b/platforms/mqtt/mqtt_adaptor_test.go index cc222ea3..c9614d1b 100644 --- a/platforms/mqtt/mqtt_adaptor_test.go +++ b/platforms/mqtt/mqtt_adaptor_test.go @@ -2,12 +2,13 @@ package mqtt import ( "fmt" - "github.com/hybridgroup/gobot" "testing" + + "github.com/hybridgroup/gobot" ) func initTestMqttAdaptor() *MqttAdaptor { - return NewMqttAdaptor("mqtt", "localhost:1883") + return NewMqttAdaptor("mqtt", "localhost:1883", "client") } func TestMqttAdaptorConnect(t *testing.T) { @@ -35,7 +36,7 @@ func TestMqttAdaptorPublishWhenConnected(t *testing.T) { func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) { a := initTestMqttAdaptor() - gobot.Assert(t, a.On("hola", func(data interface{}) { + gobot.Assert(t, a.On("hola", func(data []byte) { fmt.Println("hola") }), false) } @@ -43,7 +44,7 @@ func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) { func TestMqttAdaptorOnWhenConnected(t *testing.T) { a := initTestMqttAdaptor() a.Connect() - gobot.Assert(t, a.On("hola", func(data interface{}) { + gobot.Assert(t, a.On("hola", func(data []byte) { fmt.Println("hola") }), true) }