hybridgroup.gobot/platforms/pebble/pebble_driver.go

89 lines
2.3 KiB
Go
Raw Permalink Normal View History

2014-05-03 06:22:05 +08:00
package pebble
import (
"gobot.io/x/gobot/v2"
2014-05-03 06:22:05 +08:00
)
type Driver struct {
name string
connection gobot.Connection
gobot.Commander
gobot.Eventer
Messages []string
2014-05-03 06:22:05 +08:00
}
// NewDriver creates a new pebble driver
// 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 NewDriver(adaptor *Adaptor) *Driver {
p := &Driver{
name: "Pebble",
connection: adaptor,
Messages: []string{},
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
}
2014-07-08 08:35:59 +08:00
p.AddEvent("button")
p.AddEvent("accel")
p.AddEvent("tap")
//nolint:forcetypeassert // ok here
2014-08-01 02:56:50 +08:00
p.AddCommand("publish_event", func(params map[string]interface{}) interface{} {
p.PublishEvent(params["name"].(string), params["data"].(string))
return nil
})
//nolint:forcetypeassert // ok here
p.AddCommand("send_notification", func(params map[string]interface{}) interface{} {
p.SendNotification(params["message"].(string))
return nil
})
2014-08-01 02:56:50 +08:00
p.AddCommand("pending_message", func(params map[string]interface{}) interface{} {
return p.PendingMessage()
})
return p
2014-05-03 06:22:05 +08:00
}
func (d *Driver) Name() string { return d.name }
func (d *Driver) SetName(n string) { d.name = n }
func (d *Driver) Connection() gobot.Connection { return d.connection }
// Start returns true if driver is initialized correctly
func (d *Driver) Start() error { return nil }
2014-05-03 06:22:05 +08:00
2016-07-14 00:44:47 +08:00
// Halt returns true if driver is halted successfully
func (d *Driver) Halt() error { return nil }
// PublishEvent publishes event with specified name and data in gobot
func (d *Driver) PublishEvent(name string, data string) {
d.Publish(d.Event(name), data)
2014-05-03 06:22:05 +08:00
}
// SendNotification appends message to list of notifications to be sent to watch
func (d *Driver) SendNotification(message string) string {
d.Messages = append(d.Messages, message)
return message
}
2014-05-03 06:22:05 +08:00
// PendingMessages returns messages to be sent as notifications to pebble
2016-07-14 00:44:47 +08:00
// (Not intended to be used directly)
func (d *Driver) PendingMessage() string {
if len(d.Messages) < 1 {
return ""
}
m := d.Messages[0]
d.Messages = d.Messages[1:]
2014-05-03 06:22:05 +08:00
return m
2014-05-03 06:22:05 +08:00
}