github.com/CyCoreSystems/ari@v4.8.4+incompatible/device.go (about) 1 package ari 2 3 // DeviceState represents a communication path interacting with an 4 // Asterisk server for device state resources 5 type DeviceState interface { 6 Get(key *Key) *DeviceStateHandle 7 8 List(filter *Key) ([]*Key, error) 9 10 Data(key *Key) (*DeviceStateData, error) 11 12 Update(key *Key, state string) error 13 14 Delete(key *Key) error 15 } 16 17 // DeviceStateData is the device state for the device 18 type DeviceStateData struct { 19 // Key is the cluster-unique identifier for this device state 20 Key *Key `json:"key"` 21 22 State string `json:"state"` 23 } 24 25 // DeviceStateHandle is a representation of a device state 26 // that can be interacted with 27 type DeviceStateHandle struct { 28 key *Key 29 d DeviceState 30 } 31 32 // NewDeviceStateHandle creates a new deviceState handle 33 func NewDeviceStateHandle(key *Key, d DeviceState) *DeviceStateHandle { 34 return &DeviceStateHandle{ 35 key: key, 36 d: d, 37 } 38 } 39 40 // ID returns the identifier for the device 41 func (dsh *DeviceStateHandle) ID() string { 42 return dsh.key.ID 43 } 44 45 // Key returns the key for the device 46 func (dsh *DeviceStateHandle) Key() *Key { 47 return dsh.key 48 } 49 50 // Data gets the device state 51 func (dsh *DeviceStateHandle) Data() (d *DeviceStateData, err error) { 52 d, err = dsh.d.Data(dsh.key) 53 return 54 } 55 56 // Update updates the device state, implicitly creating it if not exists 57 func (dsh *DeviceStateHandle) Update(state string) (err error) { 58 err = dsh.d.Update(dsh.key, state) 59 return 60 } 61 62 // Delete deletes the device state 63 func (dsh *DeviceStateHandle) Delete() (err error) { 64 err = dsh.d.Delete(dsh.key) 65 //NOTE: if err is not nil, 66 // we could replace 'd' with a version of it 67 // that always returns ErrNotFound. Not required, as the 68 // handle could "come back" at any moment via an 'Update' 69 return 70 }