forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpebble_driver.go
88 lines (75 loc) · 2.25 KB
/
pebble_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package pebble
import (
"gobot.io/x/gobot/v2"
)
type Driver struct {
name string
connection gobot.Connection
gobot.Commander
gobot.Eventer
Messages []string
}
// 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(),
}
p.AddEvent("button")
p.AddEvent("accel")
p.AddEvent("tap")
//nolint:forcetypeassert // ok here
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
})
p.AddCommand("pending_message", func(params map[string]interface{}) interface{} {
return p.PendingMessage()
})
return p
}
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 }
// 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)
}
// 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
}
// PendingMessages returns messages to be sent as notifications to pebble
// (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:]
return m
}