github.com/CyCoreSystems/ari@v4.8.4+incompatible/client/native/device.go (about)

     1  package native
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/CyCoreSystems/ari"
     7  )
     8  
     9  // DeviceState provides the ARI DeviceState accessors for the native client
    10  type DeviceState struct {
    11  	client *Client
    12  }
    13  
    14  // Get returns the lazy handle for the given device name
    15  func (ds *DeviceState) Get(key *ari.Key) *ari.DeviceStateHandle {
    16  	return ari.NewDeviceStateHandle(ds.client.stamp(key), ds)
    17  }
    18  
    19  // List lists the current devices and returns a list of handles
    20  func (ds *DeviceState) List(filter *ari.Key) (dx []*ari.Key, err error) {
    21  
    22  	type device struct {
    23  		Name string `json:"name"`
    24  	}
    25  
    26  	if filter == nil {
    27  		filter = ds.client.stamp(ari.NewKey(ari.DeviceStateKey, ""))
    28  	}
    29  
    30  	var devices []device
    31  	err = ds.client.get("/deviceStates", &devices)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	for _, i := range devices {
    37  		k := ds.client.stamp(ari.NewKey(ari.DeviceStateKey, i.Name))
    38  		if filter.Match(k) {
    39  			dx = append(dx, k)
    40  		}
    41  	}
    42  
    43  	return
    44  }
    45  
    46  // Data retrieves the current state of the device
    47  func (ds *DeviceState) Data(key *ari.Key) (*ari.DeviceStateData, error) {
    48  	if key == nil || key.ID == "" {
    49  		return nil, errors.New("device key not supplied")
    50  	}
    51  
    52  	var data = new(ari.DeviceStateData)
    53  	if err := ds.client.get("/deviceStates/"+key.ID, data); err != nil {
    54  		return nil, dataGetError(err, "deviceState", "%v", key.ID)
    55  	}
    56  
    57  	data.Key = ds.client.stamp(key)
    58  	return data, nil
    59  }
    60  
    61  // Update updates the state of the device
    62  func (ds *DeviceState) Update(key *ari.Key, state string) error {
    63  	req := map[string]string{
    64  		"deviceState": state,
    65  	}
    66  	return ds.client.put("/deviceStates/"+key.ID, nil, &req)
    67  }
    68  
    69  // Delete deletes the device
    70  func (ds *DeviceState) Delete(key *ari.Key) error {
    71  	return ds.client.del("/deviceStates/"+key.ID, nil, "")
    72  }