mqtt: add driver for syntactical sugar around virtual devices
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
6c027af8d0
commit
8ea333125a
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/platforms/mqtt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mqttAdaptor := mqtt.NewAdaptor("tcp://test.mosquitto.org:1883", "pinger")
|
||||
holaDriver := mqtt.NewDriver(mqttAdaptor, "hola")
|
||||
helloDriver := mqtt.NewDriver(mqttAdaptor, "hello")
|
||||
|
||||
work := func() {
|
||||
helloDriver.OnData(func(data []byte) {
|
||||
fmt.Println("hello")
|
||||
})
|
||||
|
||||
holaDriver.OnData(func(data []byte) {
|
||||
fmt.Println("hola")
|
||||
})
|
||||
|
||||
data := []byte("o")
|
||||
gobot.Every(1*time.Second, func() {
|
||||
helloDriver.Publish(data)
|
||||
})
|
||||
|
||||
gobot.Every(5*time.Second, func() {
|
||||
holaDriver.Publish(data)
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("mqttBot",
|
||||
[]gobot.Connection{mqttAdaptor},
|
||||
work,
|
||||
)
|
||||
|
||||
robot.Start()
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
MQTT is an Internet of Things connectivity protocol featuring a lightweight publish/subscribe messaging transport. It is useful for its small code footprint and minimal network bandwidth usage.
|
||||
|
||||
This repository contains the Gobot adaptor/drivers to connect to MQTT servers. It uses the Paho MQTT Golang client package (https://eclipse.org/paho/) created and maintained by the Eclipse Foundation (https://github.com/eclipse) thank you!
|
||||
This repository contains the Gobot adaptor/driver to connect to MQTT servers. It uses the Paho MQTT Golang client package (https://eclipse.org/paho/) created and maintained by the Eclipse Foundation (https://github.com/eclipse) thank you!
|
||||
|
||||
For more info about the MQTT machine to machine messaging standard, go to http://mqtt.org/
|
||||
|
||||
|
|
|
@ -41,6 +41,9 @@ func (a *Adaptor) Name() string { return a.name }
|
|||
// SetName sets the MQTT Adaptor's name
|
||||
func (a *Adaptor) SetName(n string) { a.name = n }
|
||||
|
||||
// Port returns the Host name
|
||||
func (a *Adaptor) Port() string { return a.Host }
|
||||
|
||||
// Connect returns true if connection to mqtt is established
|
||||
func (a *Adaptor) Connect() (err error) {
|
||||
a.client = paho.NewClient(createClientOptions(a.clientID, a.Host, a.username, a.password))
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package mqtt
|
||||
|
||||
import "gobot.io/x/gobot"
|
||||
|
||||
// Driver for mqtt
|
||||
type Driver struct {
|
||||
name string
|
||||
topic string
|
||||
connection gobot.Connection
|
||||
gobot.Eventer
|
||||
gobot.Commander
|
||||
}
|
||||
|
||||
// NewDriver returns a new Gobot MQTT Driver
|
||||
func NewDriver(a *Adaptor, topic string) *Driver {
|
||||
m := &Driver{
|
||||
name: "MQTT",
|
||||
topic: topic,
|
||||
connection: a,
|
||||
Eventer: gobot.NewEventer(),
|
||||
Commander: gobot.NewCommander(),
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Name returns name for the Driver
|
||||
func (m *Driver) Name() string { return m.name }
|
||||
|
||||
// Name sets name for the Driver
|
||||
func (m *Driver) SetName(name string) { m.name = name }
|
||||
|
||||
// Connection returns Connections used by the Driver
|
||||
func (m *Driver) Connection() gobot.Connection {
|
||||
return m.connection
|
||||
}
|
||||
|
||||
func (m *Driver) adaptor() *Adaptor {
|
||||
return m.Connection().(*Adaptor)
|
||||
}
|
||||
|
||||
// Start starts the Driver
|
||||
func (m *Driver) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Halt halts the Driver
|
||||
func (m *Driver) Halt() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Topic returns the current topic for the Driver
|
||||
func (m *Driver) Topic() string { return m.topic }
|
||||
|
||||
// SetTopic sets the current topic for the Driver
|
||||
func (m *Driver) SetTopic(topic string) { m.topic = topic }
|
||||
|
||||
// Publish a message to the current device topic
|
||||
func (m *Driver) Publish(message []byte) bool {
|
||||
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
|
||||
func (m *Driver) OnData(f func(s []byte)) bool {
|
||||
return m.adaptor().On(m.topic, f)
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package mqtt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/gobottest"
|
||||
)
|
||||
|
||||
var _ gobot.Driver = (*Driver)(nil)
|
||||
|
||||
func TestMqttDriver(t *testing.T) {
|
||||
d := NewDriver(initTestMqttAdaptor(), "/test/topic")
|
||||
|
||||
gobottest.Assert(t, d.Name(), "MQTT")
|
||||
gobottest.Assert(t, d.Connection().Name(), "MQTT")
|
||||
|
||||
gobottest.Assert(t, d.Start(), nil)
|
||||
gobottest.Assert(t, d.Halt(), nil)
|
||||
}
|
Loading…
Reference in New Issue