Class: Inels::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/inels/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ips) ⇒ Controller

Returns a new instance of Controller.



6
7
8
9
# File 'lib/inels/controller.rb', line 6

def initialize ips
  @multi_client = MultiClient.new(ips)
  @schedule = ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates/schedule.erb')))
end

Instance Attribute Details

#multi_clientObject (readonly)

Returns the value of attribute multi_client.



11
12
13
# File 'lib/inels/controller.rb', line 11

def multi_client
  @multi_client
end

#scheduleObject (readonly)

Returns the value of attribute schedule.



12
13
14
# File 'lib/inels/controller.rb', line 12

def schedule
  @schedule
end

Instance Method Details

#get_device(id, client: nil, verbose: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/inels/controller.rb', line 40

def get_device id, client: nil, verbose: false
  client ||= multi_client.client_for_id('devices', id)
  device_state = client.api_get("devices/#{id}/state")
  if verbose
    device = client.api_get("devices/#{id}")

    device_state['valves'] = device['heating devices'].map do |valve|
      client.api_get("devices/#{valve['id']}/state")
    end
  end
  device_state
end

#list_devicesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/inels/controller.rb', line 14

def list_devices
  room_states = multi_client.clients.map do |client|
    rooms = client.api_get('rooms')
    rooms.keys.map do |room_id| 
      room = client.api_get("rooms/#{room_id}")
      devices = room['devices'].keys.map do |device_id|
        device = client.api_get("devices/#{device_id}")
        case device['device info']['product type']
        when 'HeatCoolArea'
          device['room_name'] = room['room info']['label']
          device['ip'] = client.ip
          {
            id: device['id'],
            name: device['device info']['label'],
            product_type: device['device info']['product type'],
            room_name: room['room info']['label'],
            ip: client.ip
          }
        else
          nil
        end
      end.compact
    end
  end.flatten
end

#set_correction(id, correction, client: nil) ⇒ Object



59
60
61
62
# File 'lib/inels/controller.rb', line 59

def set_correction id, correction, client: nil
  client ||= multi_client.client_for_id('devices', id)
  client.api_put("devices/#{id}", {correction: correction}.to_json)
end

#set_power(id, power, client: nil) ⇒ Object



64
65
66
67
# File 'lib/inels/controller.rb', line 64

def set_power id, power, client: nil
  client ||= multi_client.client_for_id('devices', id)
  client.api_put("devices/#{id}", {power: power}.to_json)
end

#set_temperature(id, temperature, client: nil) ⇒ Object



53
54
55
56
57
# File 'lib/inels/controller.rb', line 53

def set_temperature id, temperature, client: nil
  client ||= multi_client.client_for_id('devices', id)
  schedule_id = client.api_get("devices/#{id}")['schedule']
  client.api_post('temperature/schedules', schedule.result(binding))
end