1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00

80 lines
2.0 KiB
Go
Raw Normal View History

2014-05-02 17:22:05 -05:00
package pebble
import (
"github.com/hybridgroup/gobot"
)
type PebbleDriver struct {
gobot.Driver
Messages []string
2014-05-02 17:22:05 -05:00
}
// NewPebbleDriver creates a new pebble driver with specified name
// Adds following events:
// button - Sent when a pebble button is pressed
// accel - Pebble watch acceleromenter data
// tab - When a pebble watch tap event is detected
// And the following API commands:
// "publish_event"
// "send_notification"
// "pending_message"
func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
p := &PebbleDriver{
2014-07-07 17:35:59 -07:00
Driver: *gobot.NewDriver(
name,
"PebbleDriver",
adaptor,
),
Messages: []string{},
}
2014-07-07 17:35:59 -07:00
p.AddEvent("button")
p.AddEvent("accel")
p.AddEvent("tap")
2014-07-31 13:56:50 -05:00
p.AddCommand("publish_event", func(params map[string]interface{}) interface{} {
p.PublishEvent(params["name"].(string), params["data"].(string))
return nil
})
p.AddCommand("send_notification", func(params map[string]interface{}) interface{} {
p.SendNotification(params["message"].(string))
return nil
})
2014-07-31 13:56:50 -05:00
p.AddCommand("pending_message", func(params map[string]interface{}) interface{} {
return p.PendingMessage()
})
return p
2014-05-02 17:22:05 -05:00
}
// Start returns true if driver is initialized correctly
func (d *PebbleDriver) Start() bool { return true }
2014-05-02 17:22:05 -05:00
// Halt returns true if driver is halted succesfully
func (d *PebbleDriver) Halt() bool { return true }
// PublishEvent publishes event with specified name and data in gobot
func (d *PebbleDriver) PublishEvent(name string, data string) {
2014-07-07 17:35:59 -07:00
gobot.Publish(d.Event(name), data)
2014-05-02 17:22:05 -05:00
}
// SendNotification appends message to list of notifications to be sent to watch
func (d *PebbleDriver) SendNotification(message string) string {
d.Messages = append(d.Messages, message)
return message
}
2014-05-02 17:22:05 -05:00
// PendingMessages returns messages to be sent as notifications to pebble
// (Not intented to be used directly)
func (d *PebbleDriver) PendingMessage() string {
if len(d.Messages) < 1 {
return ""
}
m := d.Messages[0]
d.Messages = d.Messages[1:]
2014-05-02 17:22:05 -05:00
return m
2014-05-02 17:22:05 -05:00
}