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
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("")

View File

@ -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")

View File

@ -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")

View File

@ -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
}

View File

@ -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)
}