Class: Particle::Device

Inherits:
Model
  • Object
show all
Defined in:
lib/particle/device.rb

Overview

Domain model for one Particle device

Constant Summary collapse

ID_REGEX =
/\h{24}/
PRODUCT_IDS =
{
  0 => "Core".freeze,
  6 => "Photon".freeze,
  8 => "P1".freeze,
  10 => "Electron".freeze
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

attribute_reader, #attributes, #inspect

Constructor Details

#initialize(client, attributes) ⇒ Device

Returns a new instance of Device.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/particle/device.rb', line 15

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

Class Method Details

.claim_pathObject



181
182
183
# File 'lib/particle/device.rb', line 181

def self.claim_path
  "v1/devices"
end

.list_pathObject



177
178
179
# File 'lib/particle/device.rb', line 177

def self.list_path
  "v1/devices"
end

.provision_pathObject



185
186
187
# File 'lib/particle/device.rb', line 185

def self.provision_path
  "v1/provisioning/x"
end

Instance Method Details

#change_product(product_id, should_update = false) ⇒ boolean

Change the product_id on the device. Use this carefully, it will impact what updates you receive, and can only be used for products that have given their permission

Parameters:

  • product_id (String)

    New product id

  • should_update (String) (defaults to: false)

    if the device should be immediately updated after changing the product_id

Returns:

  • (boolean)

    true on success



162
163
164
# File 'lib/particle/device.rb', line 162

def change_product(product_id, should_update = false)
  @client.change_device_product(self, product_id, should_update)
end

#claimObject

Add a Particle device to your account

Examples:

Add a Photon by its id

Particle.device('f8bbe1e6e69e05c9c405ba1ca504d438061f1b0d').claim


73
74
75
76
# File 'lib/particle/device.rb', line 73

def claim
  new_device = @client.claim_device(self)
  self
end

#compile(file_paths) ⇒ OpenStruct

Compile firmware from source code for this device

Parameters:

  • file_paths (Array<String>)

    File paths to send to cloud and flash

Returns:

  • (OpenStruct)

    Result of flashing. :ok => true on success :errors => String with compile errors



150
151
152
# File 'lib/particle/device.rb', line 150

def compile(file_paths)
  @client.compile(file_paths, device_id: id)
end

#flash(file_paths, options = {}) ⇒ OpenStruct

Flash new firmware to this device from source code or binary

Parameters:

  • file_paths (Array<String>)

    File paths to send to cloud and flash

  • options (Hash) (defaults to: {})

    Flashing options :binary => true to skip the compile stage

Returns:

  • (OpenStruct)

    Result of flashing. :ok => true on success :errors => String with compile errors



138
139
140
# File 'lib/particle/device.rb', line 138

def flash(file_paths, options = {})
  @client.flash_device(self, file_paths, options)
end

#function(name, argument = "") ⇒ Object Also known as: call

Call a function in the firmware of a Particle device

Examples:

Call the thinker digitalWrite function

Particle.device('white_whale').function('digitalWrite', '0')

Parameters:

  • name (String)

    Function to run on firmware

  • argument (String) (defaults to: "")

    Argument string to pass to the firmware function



101
102
103
# File 'lib/particle/device.rb', line 101

def function(name, argument = "")
  @client.call_function(self, name, argument)
end

#function_path(name) ⇒ Object



193
194
195
# File 'lib/particle/device.rb', line 193

def function_path(name)
  path + "/#{name}"
end

#functionsObject



50
51
52
53
# File 'lib/particle/device.rb', line 50

def functions
  get_attributes unless @fully_loaded
  @attributes[:functions]
end

#get_attributesObject



64
65
66
67
# File 'lib/particle/device.rb', line 64

def get_attributes
  @loaded = @fully_loaded = true
  @attributes = @client.device_attributes(self)
end

#idObject



31
32
33
34
# File 'lib/particle/device.rb', line 31

def id
  get_attributes unless @attributes[:id]
  @attributes[:id]
end

#id_or_nameObject



41
42
43
# File 'lib/particle/device.rb', line 41

def id_or_name
  @attributes[:id] || @attributes[:name]
end

#nameObject



36
37
38
39
# File 'lib/particle/device.rb', line 36

def name
  get_attributes unless @attributes[:name]
  @attributes[:name]
end

#pathObject



189
190
191
# File 'lib/particle/device.rb', line 189

def path
  "/v1/devices/#{id_or_name}"
end

#productObject



60
61
62
# File 'lib/particle/device.rb', line 60

def product
  PRODUCT_IDS[product_id]
end

#removeObject

Remove a Particle device from your account

Examples:

Add a Photon by its id

Particle.device('f8bbe1e6e69e05c9c405ba1ca504d438061f1b0d').claim


82
83
84
# File 'lib/particle/device.rb', line 82

def remove
  @client.remove_device(self)
end

#rename(name) ⇒ Object

Rename a Particle device on your account

Examples:

Change the name of a Photon

Particle.device('blue').rename('red')

Parameters:

  • name (String)

    New name for the device



91
92
93
# File 'lib/particle/device.rb', line 91

def rename(name)
  @client.rename_device(self, name)
end

#signal(enabled = true) ⇒ boolean

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

Parameters:

  • enabled (String) (defaults to: true)

    Whether to enable or disable the rainbow signal

Returns:

  • (boolean)

    true when signaling, false when stopped



123
124
125
# File 'lib/particle/device.rb', line 123

def signal(enabled = true)
  @client.signal_device(self, enabled)
end

#update_public_key(public_key, algorithm = 'rsa') ⇒ boolean

Update the public key for a device you own

Parameters:

  • public_key (String)

    The public key in PEM format (default format generated by openssl)

  • algorithm (String) (defaults to: 'rsa')

    The encryption algorithm for the key (default rsa)

Returns:

  • (boolean)

    true when successful



173
174
175
# File 'lib/particle/device.rb', line 173

def update_public_key(public_key, algorithm = 'rsa')
  @client.update_device_public_key(self, public_key, algorithm)
end

#variable(name) ⇒ String, Number Also known as: get

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

Examples:

Get the battery voltage

Particle.device('mycar').variable('battery') == 12.5

Parameters:

  • target (String, Device)

    A device id, name or Particle::Device object

  • name (String)

    Variable on firmware

Returns:

  • (String, Number)

    Value from the firmware variable



113
114
115
# File 'lib/particle/device.rb', line 113

def variable(name)
  @client.get_variable(self, name)
end

#variable_path(name) ⇒ Object



197
198
199
# File 'lib/particle/device.rb', line 197

def variable_path(name)
  path + "/#{name}"
end

#variablesObject



55
56
57
58
# File 'lib/particle/device.rb', line 55

def variables
  get_attributes unless @fully_loaded
  @attributes[:variables]
end