Class: Neura::Client::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/neura-client/device.rb

Instance Method Summary collapse

Constructor Details

#initialize(device_id, device) ⇒ Device

Returns a new instance of Device.



5
6
7
8
# File 'lib/neura-client/device.rb', line 5

def initialize(device_id, device)
  @id = device_id
  @device = device
end

Instance Method Details

#fire_event(event = {}) ⇒ Object



14
15
16
# File 'lib/neura-client/device.rb', line 14

def fire_event(event = {})
  HTTParty.post("#{Neura::Client::HOST}/external_api/v1/nodes/#{@id}/event", body: {event: attributes}, headers: Neura::Client.headers)
end

#get_pending_actionsObject



18
19
20
21
# File 'lib/neura-client/device.rb', line 18

def get_pending_actions
  result = HTTParty.get("#{Neura::Client::HOST}/external_api/v1/nodes/#{@id}/pending_actions", headers: Neura::Client.headers)
  [*result["pending_actions"]].map{|pa| Neura::Client::PendingAction.new(pa["pending_action"])}
end

#perform_pending_actionsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/neura-client/device.rb', line 23

def perform_pending_actions
  actions = get_pending_actions
  actions.each do |action|
    if @device.perform!(action)
      action.done!
    else
      action.reject!
    end
  end
end

#update_attributes(attributes) ⇒ Object



10
11
12
# File 'lib/neura-client/device.rb', line 10

def update_attributes(attributes)
  HTTParty.post("#{Neura::Client::HOST}/external_api/v1/nodes/#{@id}/update", body: {attributes: attributes}, headers: Neura::Client.headers)
end