2014-05-02 17:22:05 -05:00
|
|
|
package pebble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
2014-11-28 18:10:05 -08:00
|
|
|
var _ gobot.Driver = (*PebbleDriver)(nil)
|
2014-11-16 12:25:48 -08:00
|
|
|
|
2014-05-02 17:22:05 -05:00
|
|
|
type PebbleDriver struct {
|
2014-11-28 18:10:05 -08:00
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
gobot.Commander
|
|
|
|
gobot.Eventer
|
2014-05-23 17:09:18 -05:00
|
|
|
Messages []string
|
2014-05-02 17:22:05 -05:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:26:35 -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"
|
2014-05-23 17:09:18 -05:00
|
|
|
func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
|
2014-06-11 18:59:30 -07:00
|
|
|
p := &PebbleDriver{
|
2014-11-28 18:10:05 -08:00
|
|
|
name: name,
|
|
|
|
connection: adaptor,
|
|
|
|
Messages: []string{},
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
Commander: gobot.NewCommander(),
|
2014-05-23 17:09:18 -05:00
|
|
|
}
|
2014-06-11 18:59:30 -07:00
|
|
|
|
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{} {
|
2014-06-11 18:59:30 -07:00
|
|
|
p.PublishEvent(params["name"].(string), params["data"].(string))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2014-10-16 16:26:35 -05:00
|
|
|
p.AddCommand("send_notification", func(params map[string]interface{}) interface{} {
|
2014-06-11 18:59:30 -07:00
|
|
|
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()
|
2014-06-11 18:59:30 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
return p
|
2014-05-02 17:22:05 -05:00
|
|
|
}
|
2014-11-28 18:10:05 -08:00
|
|
|
func (d *PebbleDriver) Name() string { return d.name }
|
|
|
|
func (d *PebbleDriver) Connection() gobot.Connection { return d.connection }
|
2014-10-16 16:26:35 -05:00
|
|
|
|
|
|
|
// Start returns true if driver is initialized correctly
|
2014-11-19 23:21:19 -08:00
|
|
|
func (d *PebbleDriver) Start() (errs []error) { return }
|
2014-05-02 17:22:05 -05:00
|
|
|
|
2014-10-16 16:26:35 -05:00
|
|
|
// Halt returns true if driver is halted succesfully
|
2014-11-19 23:21:19 -08:00
|
|
|
func (d *PebbleDriver) Halt() (errs []error) { return }
|
2014-05-23 17:09:18 -05:00
|
|
|
|
2014-10-16 16:26:35 -05:00
|
|
|
// PublishEvent publishes event with specified name and data in gobot
|
2014-05-23 17:09:18 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-16 16:26:35 -05:00
|
|
|
// SendNotification appends message to list of notifications to be sent to watch
|
2014-05-23 17:09:18 -05:00
|
|
|
func (d *PebbleDriver) SendNotification(message string) string {
|
|
|
|
d.Messages = append(d.Messages, message)
|
|
|
|
return message
|
|
|
|
}
|
2014-05-02 17:22:05 -05:00
|
|
|
|
2014-10-16 16:26:35 -05:00
|
|
|
// PendingMessages returns messages to be sent as notifications to pebble
|
|
|
|
// (Not intented to be used directly)
|
2014-05-23 17:09:18 -05:00
|
|
|
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
|
|
|
|
2014-05-23 17:09:18 -05:00
|
|
|
return m
|
2014-05-02 17:22:05 -05:00
|
|
|
}
|