forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
163 lines (138 loc) · 3.52 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package gobot
import (
"fmt"
"time"
)
// DriverInterface defines Driver expected behaviour
type DriverInterface interface {
Start() bool
Halt() bool
Adaptor() AdaptorInterface
SetInterval(time.Duration)
Interval() time.Duration
SetName(string)
Name() string
Pin() string
SetPin(string)
Command(string) func(map[string]interface{}) interface{}
Commands() map[string]func(map[string]interface{}) interface{}
AddCommand(string, func(map[string]interface{}) interface{})
Events() map[string]*Event
Event(string) *Event
AddEvent(string)
Type() string
ToJSON() *JSONDevice
}
type Driver struct {
adaptor AdaptorInterface
interval time.Duration
pin string
name string
commands map[string]func(map[string]interface{}) interface{}
events map[string]*Event
driverType string
}
// NewDriver returns a Driver with specified parameters
// and sets driver pin, adaptor and interval
func NewDriver(name string, driverType string, v ...interface{}) *Driver {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
d := &Driver{
driverType: driverType,
name: name,
interval: 10 * time.Millisecond,
commands: make(map[string]func(map[string]interface{}) interface{}),
events: make(map[string]*Event),
adaptor: nil,
pin: "",
}
for i := range v {
switch v[i].(type) {
case string:
d.pin = v[i].(string)
case AdaptorInterface:
d.adaptor = v[i].(AdaptorInterface)
case time.Duration:
d.interval = v[i].(time.Duration)
}
}
return d
}
// Adaptor returns driver adaptor
func (d *Driver) Adaptor() AdaptorInterface {
return d.adaptor
}
// SetInterval defines driver interval duration.
func (d *Driver) SetInterval(t time.Duration) {
d.interval = t
}
// Interval current driver interval duration
func (d *Driver) Interval() time.Duration {
return d.interval
}
// SetName sets driver name.
func (d *Driver) SetName(s string) {
d.name = s
}
// Name returns driver name.
func (d *Driver) Name() string {
return d.name
}
// Pin returns driver pin
func (d *Driver) Pin() string {
return d.pin
}
// SetPin defines driver pin
func (d *Driver) SetPin(pin string) {
d.pin = pin
}
// Type returns driver type
func (d *Driver) Type() string {
return d.driverType
}
// Events returns driver events map
func (d *Driver) Events() map[string]*Event {
return d.events
}
// Event returns an event by name if exists
func (d *Driver) Event(name string) *Event {
e, ok := d.events[name]
if ok {
return e
} else {
panic(fmt.Sprintf("Unknown Driver Event: %v", name))
}
}
// AddEvents adds a new event by name
func (d *Driver) AddEvent(name string) {
d.events[name] = NewEvent()
}
// Command retrieves a command by name
func (d *Driver) Command(name string) func(map[string]interface{}) interface{} {
return d.commands[name]
}
// Commands returns a map of driver commands
func (d *Driver) Commands() map[string]func(map[string]interface{}) interface{} {
return d.commands
}
// AddCommand links specified command name to `f`
func (d *Driver) AddCommand(name string, f func(map[string]interface{}) interface{}) {
d.commands[name] = f
}
// ToJSON returns JSON Driver represnentation including adaptor and commands
func (d *Driver) ToJSON() *JSONDevice {
jsonDevice := &JSONDevice{
Name: d.Name(),
Driver: d.Type(),
Commands: []string{},
Connection: "",
}
if d.Adaptor() != nil {
jsonDevice.Connection = d.Adaptor().ToJSON().Name
}
for command := range d.Commands() {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}
return jsonDevice
}