-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
364 lines (328 loc) · 9.3 KB
/
types.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package uiot
import (
"context"
"fmt"
"sync"
proto "github.com/TrevorFarrelly/u-iot/proto"
"google.golang.org/grpc"
)
// Device represents a device's location and functionality within a home.
type Device struct {
Name string
Type Type
Room Room
Funcs map[string]*Func
// these fields are set on creation/receipt of a new device
remote bool
addr string
port int
}
// create a local Device struct from the protobuf representation
func deviceFromProto(p *proto.DevInfo) *Device {
// build device struct
dev := &Device{
Name: p.Meta.Name,
Type: Type(p.Meta.Type),
Room: Room(p.Meta.Room),
Funcs: make(map[string]*Func),
remote: true,
addr: p.Addr,
port: int(p.Port),
}
// add all functions to map
for _, f := range p.Funcs {
dev.Funcs[f.Name] = &Func{}
// add all params to function
for _, p := range f.Params {
dev.Funcs[f.Name].Params = append(dev.Funcs[f.Name].Params, Param{int(p.Min), int(p.Max)})
}
}
return dev
}
// AddFunction adds a function f to device d. Remote devices can call this function
// using the provided name and params, p.
func (d *Device) AddFunction(name string, f func(...int), p ...Param) error {
if d.remote {
return fmt.Errorf("Cannot modify remote device")
}
d.Funcs[name] = &Func{f, p}
return nil
}
// CallFunc calls a remote device's function with the provided parameter values.
// Parameter checking is handled internally, i.e. if the provided parameters are
// outside the range specified in AddFunction, CallFunc will return an error.
func (d Device) CallFunc(name string, p ...int) error {
// get function from device
f, ok := d.Funcs[name]
if !ok {
return fmt.Errorf("device %s does not have function %s", d.Name, name)
}
// check parameters
if len(p) != len(f.Params) {
return fmt.Errorf("%s expects %d parameters, but %d were provided", name, len(f.Params), len(p))
}
var convP []uint32
for i, param := range f.Params {
if p[i] < param.Min || p[i] > param.Max {
return fmt.Errorf("%d is out of range for parameter %d: %d-%d", p[i], i, param.Min, param.Max)
}
convP = append(convP, uint32(p[i]))
}
// call remote function
addr := fmt.Sprintf("%s:%d", d.addr, d.port)
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
return err
}
client := proto.NewDeviceClient(conn)
ctx := context.Background()
_, err = client.CallFunc(ctx, &proto.FuncCall{Name: name, Params: convP})
if err != nil {
return err
}
return nil
}
// get the protobuf representation of this device
func (d Device) asProto() *proto.DevInfo {
// construct device info
pd := &proto.DevInfo{
Port: uint32(d.port),
Addr: d.addr,
Meta: &proto.Meta{
Type: uint32(d.Type),
Room: uint32(d.Room),
Name: d.Name,
},
}
// add all functions to this device
for name, f := range d.Funcs {
pf := &proto.Func{
Name: name,
}
// add all parameters to this function
for _, p := range f.Params {
pf.Params = append(pf.Params, &proto.Param{
Min: uint32(p.Min),
Max: uint32(p.Max),
})
}
pd.Funcs = append(pd.Funcs, pf)
}
return pd
}
// returns the IP and port combo for this device
func (d Device) getFullAddress() string {
return fmt.Sprintf("%s:%d", d.addr, d.port)
}
// String returns a string representing this device, in the form (type, room)
// name: func func func...
func (d Device) String() string {
ret := fmt.Sprintf("(%s, %s) %s:", d.Type, d.Room, d.Name)
for name, f := range d.Funcs {
ret += fmt.Sprintf(" %s%s", name, f)
}
return ret
}
// Func represents a function that a device performs. F will be nil in any remote
// device.
type Func struct {
F func(...int)
Params []Param
}
// String returns the string representation of the function, in the form (param, param, param...)
func (f Func) String() string {
ret := fmt.Sprintf("( ")
for _, p := range f.Params {
ret += fmt.Sprintf("%s ", p)
}
ret += ")"
return ret
}
// Param represents a parameter to a function, with specific minimum and maximum values.
type Param struct {
Min int
Max int
}
func (p Param) String() string {
return fmt.Sprintf("%d-%d", p.Min, p.Max)
}
// Network contains a list of all devices on the network, updated dynamically
// as devices connect and disconnect.
type Network struct {
mux sync.Mutex
devs []*Device
eventing bool
event chan *Event
}
// GetDevice gets a device named name from the network.
func (n Network) GetDevice(name string) (*Device, error) {
n.mux.Lock()
defer n.mux.Unlock()
for _, dev := range n.devs {
if dev.Name == name {
return dev, nil
}
}
return nil, fmt.Errorf("%s was not found on the network", name)
}
// GetDevices returns all known devices from the network.
func (n Network) GetDevices() []*Device {
return n.devs
}
// GetMatching gets all devices with the provide room and type.
func (n Network) GetMatching(r Room, t Type) []*Device {
// initialize return array
ret := []*Device{}
// iterate over all devices
for _, d := range n.GetDevices() {
t_match := true
r_match := true
// if type is set and is not a match, skip it
if t != -1 && d.Type != t {
t_match = false
}
// if room is set and is not a match, skip it
if r != -1 && d.Room != r {
r_match = false
}
// if both type and room match, add it to return list
if t_match && r_match {
ret = append(ret, d)
}
}
return ret
}
// CallAll calls a function on all devices that match the provided room and type.
func (n Network) CallAll(r Room, t Type, name string, p ...int) error {
// initialize error array
errs := []error{}
// iterate over all matching devices
for _, d := range n.GetMatching(r, t) {
// call function, adding error to array if we get one
if err := d.CallFunc(name, p...); err != nil {
errs = append(errs, err)
}
}
// if we encountered any errors, return one containing all of them
if len(errs) != 0 {
ret := fmt.Sprintf("could not call '%s' on %d device(s):", name, len(errs))
for _, err := range errs {
ret += fmt.Sprintf("\n %v", err)
}
return fmt.Errorf(ret)
}
return nil
}
// Add a new known device to the network
func (n *Network) addDevice(new *Device) error {
n.mux.Lock()
defer n.mux.Unlock()
// determine if new device is already known by comparing its IP and port to other known devices
for _, dev := range n.devs {
if dev.getFullAddress() == new.getFullAddress() {
return fmt.Errorf("%s (addr %s) already exists on the network", new.Name, new.getFullAddress())
}
}
// add device to list
n.devs = append(n.devs, new)
// send event if it's enabled
if n.eventing {
n.event <- &Event{Connect, new}
}
return nil
}
// Remove a device from the network
func (n *Network) removeDevice(old *Device) error {
n.mux.Lock()
defer n.mux.Unlock()
// remove the device from the network if we have it
for i, dev := range n.devs {
if dev.getFullAddress() == old.getFullAddress() {
// swap device to remove with the device at the end of the list
n.devs[len(n.devs)-1], n.devs[i] = n.devs[i], n.devs[len(n.devs)-1]
n.devs = n.devs[:len(n.devs)-1]
// send event if it's enabled
if n.eventing {
n.event <- &Event{Disconnect, old}
}
return nil
}
}
return fmt.Errorf("%s (addr %s) does not exist on the network", old.Name, old.getFullAddress())
}
// EnableEvents provides access to the Event channel, allowing a device to detect
// when a remote device connects or disconnects from the network.
func (n *Network) EnableEvents() chan *Event {
n.eventing = true
return n.event
}
// Event represents a change in state in the network. Used in the Network struct,
// specifically when the user has enabled eventing via network.EnableEvents().
type Event struct {
Type EventType
Dev *Device
}
// EventType represents the different types of events that are supported by the
// eventing interface.
type EventType int
const (
Connect EventType = iota
Disconnect
)
func (t EventType) String() string {
return [...]string{"Connect", "Disconnect"}[t]
}
// Type represents the various types of devices that can exist on the network.
type Type int
const (
Light Type = iota
Outlet
Speaker
Screen
Controller
OtherType
)
// TypeFromString converts a type string (in the same form as one returned from
// type.String()) into a valid Type. Supports wildcard (*) for use in network.CallAll().
func TypeFromString(s1 string) (Type, error) {
if s1 == "*" {
return -1, nil
}
for i, s2 := range [...]string{"Light", "Outlet", "Speaker", "Screen", "Controller", "Other"} {
if s1 == s2 {
return Type(i), nil
}
}
return -1, fmt.Errorf("%s is not a valid device type", s1)
}
func (t Type) String() string {
return [...]string{"Light", "Outlet", "Speaker", "Screen", "Controller", "Other"}[t]
}
// Room represents various rooms a device could be placed in.
type Room int
const (
Living Room = iota
Dining
Bed
Bath
Kitchen
Foyer
Closet
OtherRoom
)
// RoomFromString converts a room string (in the same form as one returned from
// room.String()) into a valid Room. Supports wildcard (*) for use in network.CallAll().
func RoomFromString(s string) (Room, error) {
if s == "*" {
return -1, nil
}
for i, s2 := range [...]string{"LivingRoom", "DiningRoom", "Bedroom", "Bathroom", "Kitchen", "Foyer", "Closet", "Other"} {
if s == s2 {
return Room(i), nil
}
}
return -1, fmt.Errorf("%s is not a valid device room", s)
}
func (r Room) String() string {
return [...]string{"LivingRoom", "DiningRoom", "Bedroom", "Bathroom", "Kitchen", "Foyer", "Closet", "Other"}[r]
}