class Particle::Device
Domain model for one Particle
device
Constants
- ID_REGEX
- PLATFORM_IDS
Public Class Methods
# File lib/particle/device.rb, line 186 def self.claim_path "v1/devices" end
# File lib/particle/device.rb, line 182 def self.list_path "v1/devices" end
# File lib/particle/device.rb, line 11 def initialize(client, attributes) super(client, attributes) if attributes.is_a? String if attributes =~ ID_REGEX @attributes = { id: attributes } else @attributes = { name: attributes } end else # Listing all devices returns partial attributes so check if the # device was fully loaded or not @fully_loaded = true if attributes.key?(:variables) end end
# File lib/particle/device.rb, line 190 def self.provision_path "v1/devices" end
Public Instance Methods
Add a Particle
device to your account
@example Add a Photon by its id
Particle.device('f8bbe1e6e69e05c9c405ba1ca504d438061f1b0d').claim
# File lib/particle/device.rb, line 83 def claim new_device = @client.claim_device(self) self end
Compile firmware from source code for this device
@param file_paths [Array<String>] File paths to send to cloud
and flash
@return [OpenStruct] Result of flashing.
:ok => true on success :errors => String with compile errors
# File lib/particle/device.rb, line 167 def compile(file_paths) @client.compile(file_paths, device_id: id) end
# File lib/particle/device.rb, line 70 def dev_kit? product_id && PLATFORM_IDS.include?(product_id) end
Flash new firmware to this device from source code or binary
@param file_paths [Array<String>] File paths to send to cloud
and flash
@param options [Hash] Flashing options
:binary => true to skip the compile stage
@return [OpenStruct] Result of flashing.
:ok => true on success :errors => String with compile errors
# File lib/particle/device.rb, line 155 def flash(file_paths, options = {}) @client.flash_device(self, file_paths, options) end
Call a function in the firmware of a Particle
device
@param name [String] Function to run on firmware @param argument [String] Argument string to pass to the firmware function @example Call the thinker digitalWrite function
Particle.device('white_whale').function('digitalWrite', '0')
# File lib/particle/device.rb, line 111 def function(name, argument = "") @client.call_function(self, name, argument) end
# File lib/particle/device.rb, line 202 def function_path(name) path + "/#{name}" end
# File lib/particle/device.rb, line 48 def functions get_attributes unless @fully_loaded @attributes[:functions] end
# File lib/particle/device.rb, line 74 def get_attributes @loaded = @fully_loaded = true @attributes = @client.device_attributes(self) end
# File lib/particle/device.rb, line 27 def id get_attributes unless @attributes[:id] @attributes[:id] end
# File lib/particle/device.rb, line 37 def id_or_name @attributes[:id] || @attributes[:name] end
# File lib/particle/device.rb, line 32 def name get_attributes unless @attributes[:name] @attributes[:name] end
# File lib/particle/device.rb, line 198 def path "/v1/devices/#{id_or_name}" end
Ping a device to see if it is online
@return [boolean] true when online, false when offline
# File lib/particle/device.rb, line 131 def ping @client.ping_device(self) end
# File lib/particle/device.rb, line 210 def ping_path path + "/ping" end
# File lib/particle/device.rb, line 62 def platform @platform ||= Platform.new(@client, platform_id) end
# File lib/particle/device.rb, line 66 def platform_name platform.name end
# File lib/particle/device.rb, line 58 def product @product ||= dev_kit? ? nil : Product.new(@client, product_id) end
Remove a Particle
device from your account
@example Add a Photon by its id
Particle.device('f8bbe1e6e69e05c9c405ba1ca504d438061f1b0d').claim
# File lib/particle/device.rb, line 92 def remove @client.remove_device(self) end
Rename a Particle
device on your account
@param name [String] New name for the device @example Change the name of a Photon
Particle.device('blue').rename('red')
# File lib/particle/device.rb, line 101 def rename(name) @client.rename_device(self, name) end
Signal the device to start blinking the RGB LED in a rainbow pattern. Useful to identify a particular device.
@param enabled [String] Whether to enable or disable the rainbow signal @return [boolean] true when signaling, false when stopped
# File lib/particle/device.rb, line 140 def signal(enabled = true) @client.signal_device(self, enabled) end
# File lib/particle/device.rb, line 194 def update_keys_path "/v1/provisioning/#{id}" end
Update the public key for this device
@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/device.rb, line 178 def update_public_key(public_key, algorithm = 'rsa') @client.update_device_public_key(self, public_key, algorithm) end
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 @example Get the battery voltage
Particle.device('mycar').variable('battery') == 12.5
# File lib/particle/device.rb, line 123 def variable(name) @client.get_variable(self, name) end
# File lib/particle/device.rb, line 206 def variable_path(name) path + "/#{name}" end
# File lib/particle/device.rb, line 53 def variables get_attributes unless @fully_loaded @attributes[:variables] end