Class: DeviceDBComms::Device

Inherits:
Shared
  • Object
show all
Defined in:
lib/devicedb_comms/device.rb

Instance Method Summary collapse

Methods inherited from Shared

#get, #parse_response, #post, #put, #to_query

Constructor Details

#initialize(url = nil, pem_path = nil) ⇒ Device

Backward compatibility with version <= 0.0.14 TODO Remove arguments



8
9
10
11
# File 'lib/devicedb_comms/device.rb', line 8

def initialize(url = nil, pem_path = nil)
  @applications = {}
  super(url, pem_path)
end

Instance Method Details

#action(device_id, type, body, tries = 1) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/devicedb_comms/device.rb', line 54

def action(device_id, type, body, tries = 1)
  rtn = { 'error' => 'No tries' }
  n = 0
  while n < tries && rtn.has_key?('error')
    rtn = post("/devices/#{device_id}/new_action", { action_type: type, action_body: body })
    n += 1
  end
  rtn
end

#clear_application(device_id) ⇒ Object



38
39
40
# File 'lib/devicedb_comms/device.rb', line 38

def clear_application(device_id)
  set_application(device_id, nil)
end

#edit(device_id, device_params) ⇒ Object



21
22
23
# File 'lib/devicedb_comms/device.rb', line 21

def edit(device_id, device_params)
  put("/devices/#{device_id}", device: device_params)
end

#find(device_id) ⇒ Object



13
14
15
# File 'lib/devicedb_comms/device.rb', line 13

def find(device_id)
  get("/devices/#{device_id}")
end

#find_disconnected_by_type(type) ⇒ Object



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

def find_disconnected_by_type(type)
  {
    'devices' => []
  }.merge(post("/devices/search", { device_type: type, hive_id: -1, status: 'idle' }))
end

#get_application(device_id) ⇒ Object



42
43
44
# File 'lib/devicedb_comms/device.rb', line 42

def get_application(device_id)
  get("/devices/#{device_id}/application")['application']
end

#hive_connect(device_id, hive_id) ⇒ Object



46
47
48
# File 'lib/devicedb_comms/device.rb', line 46

def hive_connect(device_id, hive_id)
  post("/hives/#{hive_id}/devices/#{device_id}/connect")
end

#hive_disconnect(device_id) ⇒ Object



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

def hive_disconnect(device_id)
  put("/hives/devices/#{device_id}/disconnect")
end

#poll(device_id, status = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/devicedb_comms/device.rb', line 25

def poll(device_id, status=nil)
  params = {}
  if @applications[device_id]
    params['application'] = @applications[device_id]
  end
  post("/devices/#{device_id}/poll" + ( "/#{status}" unless status.nil? ).to_s, params)
end

#register(device_params) ⇒ Object



17
18
19
# File 'lib/devicedb_comms/device.rb', line 17

def register(device_params)
  post("/devices/register", device_params)
end

#set_application(device_id, name = nil) ⇒ Object



33
34
35
36
# File 'lib/devicedb_comms/device.rb', line 33

def set_application(device_id, name=nil)
  @applications[device_id] = name
  poll(device_id)
end