mqtt: change 'On' method function signature to match expected interface

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-01-26 07:14:43 +01:00
parent 8ea333125a
commit e0b39b1144
2 changed files with 14 additions and 4 deletions

View File

@ -14,11 +14,11 @@ func main() {
helloDriver := mqtt.NewDriver(mqttAdaptor, "hello") helloDriver := mqtt.NewDriver(mqttAdaptor, "hello")
work := func() { work := func() {
helloDriver.OnData(func(data []byte) { helloDriver.On(mqtt.Data, func(data []byte) {
fmt.Println("hello") fmt.Println("hello")
}) })
holaDriver.OnData(func(data []byte) { holaDriver.On(mqtt.Data, func(data []byte) {
fmt.Println("hola") fmt.Println("hola")
}) })

View File

@ -2,6 +2,14 @@ package mqtt
import "gobot.io/x/gobot" import "gobot.io/x/gobot"
const (
// Data event when data is available for Driver
Data = "data"
// Error event when error occurs in Driver
Error = "error"
)
// Driver for mqtt // Driver for mqtt
type Driver struct { type Driver struct {
name string name string
@ -60,7 +68,9 @@ func (m *Driver) Publish(message []byte) bool {
return m.adaptor().Publish(m.topic, message) return m.adaptor().Publish(m.topic, message)
} }
// OnData subscribes to the current device topic, and then calls the message handler function when data is received // On subscribes to data updates for the current device topic,
func (m *Driver) OnData(f func(s []byte)) bool { // and then calls the message handler function when data is received
func (m *Driver) On(n string, f func(s []byte)) bool {
// TODO: subscribe to Error updates
return m.adaptor().On(m.topic, f) return m.adaptor().On(m.topic, f)
} }