module Particle::Client::Devices

Client methods for the Particle device API

@see docs.particle.io/reference/api/#devices

Public Instance Methods

call_function(target, name, argument = "") click to toggle source

Call a function in the firmware of a Particle device

@param target [String, Device] A device id, name or {Device} object @param name [String] Function to run on firmware @param argument [String] Argument string to pass to the firmware function @return [Integer] Return value from the firmware function

# File lib/particle/client/devices.rb, line 85
def call_function(target, name, argument = "")
  result = post(device(target).function_path(name), arg: argument)
  result[:return_value]
end
claim_device(target) click to toggle source

Add a Particle device to your account

@param target [String, Device] A device id or {Device} object.

You can't claim a device by name

@return [Device] A device object to interact with

# File lib/particle/client/devices.rb, line 45
def claim_device(target)
  result = post(Device.claim_path, id: device(target).id_or_name)
  device(result[:id])
end
device(target) click to toggle source

Create a domain model for a Particle device

@param target [String, Hash, Device] A device id, name, hash of attributes or {Device} object @return [Device] A device object to interact with

# File lib/particle/client/devices.rb, line 15
def device(target)
  if target.is_a? Device
    target
  else
    Device.new(self, target)
  end
end
device_attributes(target) click to toggle source

Get information about a Particle device

@param target [String, Device] A device id, name or {Device} object @return [Hash] The device attributes

# File lib/particle/client/devices.rb, line 36
def device_attributes(target)
  get(device(target).path)
end
devices() click to toggle source

List all Particle devices on the account

@return [Array<Device>] List of Particle devices to interact with

# File lib/particle/client/devices.rb, line 26
def devices
  get(Device.list_path).map do |attributes|
    device(attributes)
  end
end
get_variable(target, name) click to toggle source

Get the value of a variable in the firmware of a Particle device

@param target [String, Device] A device id, name or {Device} object @param name [String] Variable on firmware @return [String, Number] Value from the firmware variable

# File lib/particle/client/devices.rb, line 95
def get_variable(target, name)
  result = get(device(target).variable_path(name))
  result[:result]
end
ping_device(target) click to toggle source

Ping a device to see if it is online

@param target [String, Device] A device id, name or {Device} object @return [boolean] true when online, false when offline

# File lib/particle/client/devices.rb, line 104
def ping_device(target)
  result = put(device(target).ping_path)
  result[:online]
end
provision_device(params) click to toggle source

Create a new Particle device

@param product [String] A product id @return [Device] A device object to interact with

# File lib/particle/client/devices.rb, line 54
def provision_device(params)
  product_id = params[:product_id] or raise ArgumentError.new("product_id parameter is required")
  result = post(Device.provision_path, product_id: product_id)
  device(result[:device_id])
end
remove_device(target) click to toggle source

Remove a Particle device from your account

@param target [String, Device] A device id, name or {Device} object @return [boolean] true for success

# File lib/particle/client/devices.rb, line 64
def remove_device(target)
  result = delete(device(target).path)
  result[:ok]
end
rename_device(target, name) click to toggle source

Rename a Particle device in your account

@param target [String, Device] A device id, name or {Device} object @param name [String] New name for the device @return [boolean] true for success

# File lib/particle/client/devices.rb, line 74
def rename_device(target, name)
  result = put(device(target).path, name: name)
  result[:name] == name
end
signal_device(target, enabled = true) click to toggle source

Signal the device to start blinking the RGB LED in a rainbow pattern. Useful to identify a particular device.

@param target [String, Device] A device id, name or {Device} object @param enabled [String] Whether to enable or disable the rainbow signal @return [boolean] true when signaling, false when stopped

# File lib/particle/client/devices.rb, line 115
def signal_device(target, enabled = true)
  result = put(device(target).path, signal: enabled ? '1' : '0')
  result[:signaling]
end
update_device_public_key(target, public_key, algorithm = 'rsa') click to toggle source

Update the public key for a device you own

@param target [String, Device] A device id, name or {Device} object @param public_key [String] The public key in PEM format (default

format generated by openssl)

@param algorithm [String] The encryption algorithm for the key

(default rsa)

@return [boolean] true when successful

# File lib/particle/client/devices.rb, line 128
def update_device_public_key(target, public_key, algorithm = 'rsa')
  result = post(device(target).update_keys_path,
                deviceID: device(target).id,
                publicKey: public_key,
                algorithm: algorithm)
  !result.empty?
end