From 8ea333125aa90eb6c59d11e825831e161c6723ce Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 25 Jan 2017 20:19:15 +0100 Subject: [PATCH] mqtt: add driver for syntactical sugar around virtual devices Signed-off-by: deadprogram --- examples/mqtt_driver_ping.go | 41 +++++++++++++++++++ platforms/mqtt/README.md | 2 +- platforms/mqtt/mqtt_adaptor.go | 3 ++ platforms/mqtt/mqtt_driver.go | 66 ++++++++++++++++++++++++++++++ platforms/mqtt/mqtt_driver_test.go | 20 +++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 examples/mqtt_driver_ping.go create mode 100644 platforms/mqtt/mqtt_driver.go create mode 100644 platforms/mqtt/mqtt_driver_test.go diff --git a/examples/mqtt_driver_ping.go b/examples/mqtt_driver_ping.go new file mode 100644 index 00000000..c005b430 --- /dev/null +++ b/examples/mqtt_driver_ping.go @@ -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() +} diff --git a/platforms/mqtt/README.md b/platforms/mqtt/README.md index 2d00fa2d..57d366a2 100644 --- a/platforms/mqtt/README.md +++ b/platforms/mqtt/README.md @@ -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/ diff --git a/platforms/mqtt/mqtt_adaptor.go b/platforms/mqtt/mqtt_adaptor.go index ff8cb7a9..f5358b49 100644 --- a/platforms/mqtt/mqtt_adaptor.go +++ b/platforms/mqtt/mqtt_adaptor.go @@ -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)) diff --git a/platforms/mqtt/mqtt_driver.go b/platforms/mqtt/mqtt_driver.go new file mode 100644 index 00000000..d0a19b67 --- /dev/null +++ b/platforms/mqtt/mqtt_driver.go @@ -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) +} diff --git a/platforms/mqtt/mqtt_driver_test.go b/platforms/mqtt/mqtt_driver_test.go new file mode 100644 index 00000000..27d07e30 --- /dev/null +++ b/platforms/mqtt/mqtt_driver_test.go @@ -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) +}