Class: TenHsServer::Room

Inherits:
Adapter show all
Defined in:
lib/ten_hs_server/room.rb

Overview

Adapter for working with Homeseer rooms, tenHsServer has no endpoint for fetching rooms, so we need to get that from device metadata.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Room

Returns a new instance of Room.



8
9
10
# File 'lib/ten_hs_server/room.rb', line 8

def initialize name
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/ten_hs_server/room.rb', line 6

def name
  @name
end

Class Method Details

.allObject

Load all rooms.

Returns an array of Room’s.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ten_hs_server/room.rb', line 39

def all
  devices = Device.all
  rooms = []

  # Go through each device, check which room it belongs
  # do, if the room doesn't exist, add it to rooms
  devices.each do |device|
    unless rooms.find {|room| room.name == device.room}
      rooms << new(device.room)
    end
  end

  rooms
end

.find(name) ⇒ Object

Load a single room.

Returns a hash describing the room.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ten_hs_server/room.rb', line 57

def find name
  devices = Device.all
  devices_in_room = []

  # Go through each device, check which room it belongs too.
  # If the device belongs to this room, add it to devices_in_room
  devices.each do |device|
    if device.room == name
      devices_in_room << device
    end
  end

  {
    name: name,
    floor: devices_in_room.any? ? devices_in_room[0].floor : nil,
    devices: devices_in_room
  }

end

.off(devices) ⇒ Object

Turn off a device.

id - An string describing the device

Returns a true or false describing the status of the device false = off true = on



95
96
97
98
99
100
101
# File 'lib/ten_hs_server/room.rb', line 95

def off devices
  ids = devices.map { |device| device.id }
  ids = URI::encode(ids.join("."))
  response = get "?t=99&f=DeviceOff&d=#{ids}"

  parse_toggle_devices response.body
end

.on(devices) ⇒ Object

Turn on all devices in this room.

devices - An array with devices



80
81
82
83
84
85
86
# File 'lib/ten_hs_server/room.rb', line 80

def on devices
  ids = devices.map { |device| device.id }
  ids = URI::encode(ids.join("."))
  response = get "?t=99&f=DeviceOn&d=#{ids}"

  parse_toggle_devices response.body
end

Instance Method Details

#devicesObject



16
17
18
# File 'lib/ten_hs_server/room.rb', line 16

def devices
  query[:devices]
end

#floorObject



20
21
22
# File 'lib/ten_hs_server/room.rb', line 20

def floor
  query[:floor]
end

#offObject



29
30
31
32
# File 'lib/ten_hs_server/room.rb', line 29

def off
  status = self.class.off(devices)
  true
end

#onObject



24
25
26
27
# File 'lib/ten_hs_server/room.rb', line 24

def on
  status = self.class.on(devices)
  true
end

#queryObject



12
13
14
# File 'lib/ten_hs_server/room.rb', line 12

def query
  @query || @query = self.class.find(name)
end