gobot.io/x/gobot@v1.16.0/platforms/pebble/pebble_driver.go (about)

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