forked from CyCoreSystems/ari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
70 lines (56 loc) · 1.69 KB
/
device.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
package ari
// DeviceState represents a communication path interacting with an
// Asterisk server for device state resources
type DeviceState interface {
Get(key *Key) *DeviceStateHandle
List(filter *Key) ([]*Key, error)
Data(key *Key) (*DeviceStateData, error)
Update(key *Key, state string) error
Delete(key *Key) error
}
// DeviceStateData is the device state for the device
type DeviceStateData struct {
// Key is the cluster-unique identifier for this device state
Key *Key `json:"key"`
State string `json:"state"`
}
// DeviceStateHandle is a representation of a device state
// that can be interacted with
type DeviceStateHandle struct {
key *Key
d DeviceState
}
// NewDeviceStateHandle creates a new deviceState handle
func NewDeviceStateHandle(key *Key, d DeviceState) *DeviceStateHandle {
return &DeviceStateHandle{
key: key,
d: d,
}
}
// ID returns the identifier for the device
func (dsh *DeviceStateHandle) ID() string {
return dsh.key.ID
}
// Key returns the key for the device
func (dsh *DeviceStateHandle) Key() *Key {
return dsh.key
}
// Data gets the device state
func (dsh *DeviceStateHandle) Data() (d *DeviceStateData, err error) {
d, err = dsh.d.Data(dsh.key)
return
}
// Update updates the device state, implicitly creating it if not exists
func (dsh *DeviceStateHandle) Update(state string) (err error) {
err = dsh.d.Update(dsh.key, state)
return
}
// Delete deletes the device state
func (dsh *DeviceStateHandle) Delete() (err error) {
err = dsh.d.Delete(dsh.key)
//NOTE: if err is not nil,
// we could replace 'd' with a version of it
// that always returns ErrNotFound. Not required, as the
// handle could "come back" at any moment via an 'Update'
return
}