gobot.io/x/gobot/v2@v2.1.0/platforms/pebble/pebble_driver.go (about) 1 package pebble 2 3 import ( 4 "gobot.io/x/gobot/v2" 5 ) 6 7 type Driver struct { 8 name string 9 connection gobot.Connection 10 gobot.Commander 11 gobot.Eventer 12 Messages []string 13 } 14 15 // NewDriver creates a new pebble driver 16 // Adds following events: 17 // 18 // button - Sent when a pebble button is pressed 19 // accel - Pebble watch acceleromenter data 20 // tab - When a pebble watch tap event is detected 21 // 22 // And the following API commands: 23 // 24 // "publish_event" 25 // "send_notification" 26 // "pending_message" 27 func NewDriver(adaptor *Adaptor) *Driver { 28 p := &Driver{ 29 name: "Pebble", 30 connection: adaptor, 31 Messages: []string{}, 32 Eventer: gobot.NewEventer(), 33 Commander: gobot.NewCommander(), 34 } 35 36 p.AddEvent("button") 37 p.AddEvent("accel") 38 p.AddEvent("tap") 39 40 p.AddCommand("publish_event", func(params map[string]interface{}) interface{} { 41 p.PublishEvent(params["name"].(string), params["data"].(string)) 42 return nil 43 }) 44 45 p.AddCommand("send_notification", func(params map[string]interface{}) interface{} { 46 p.SendNotification(params["message"].(string)) 47 return nil 48 }) 49 50 p.AddCommand("pending_message", func(params map[string]interface{}) interface{} { 51 return p.PendingMessage() 52 }) 53 54 return p 55 } 56 func (d *Driver) Name() string { return d.name } 57 func (d *Driver) SetName(n string) { d.name = n } 58 func (d *Driver) Connection() gobot.Connection { return d.connection } 59 60 // Start returns true if driver is initialized correctly 61 func (d *Driver) Start() (err error) { return } 62 63 // Halt returns true if driver is halted successfully 64 func (d *Driver) Halt() (err error) { return } 65 66 // PublishEvent publishes event with specified name and data in gobot 67 func (d *Driver) PublishEvent(name string, data string) { 68 d.Publish(d.Event(name), data) 69 } 70 71 // SendNotification appends message to list of notifications to be sent to watch 72 func (d *Driver) SendNotification(message string) string { 73 d.Messages = append(d.Messages, message) 74 return message 75 } 76 77 // PendingMessages returns messages to be sent as notifications to pebble 78 // (Not intended to be used directly) 79 func (d *Driver) PendingMessage() string { 80 if len(d.Messages) < 1 { 81 return "" 82 } 83 m := d.Messages[0] 84 d.Messages = d.Messages[1:] 85 86 return m 87 }