Class: JSONThermostat

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

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ JSONThermostat

Returns a new instance of JSONThermostat.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/json_thermostat.rb', line 4

def initialize(settings)
  @settings = JSON.parse(settings)
  if @settings['unit']
    @thermostat = Thermostat.new(
      @settings['temperature'], @settings['range'], @settings['unit']
    )
  else
    @thermostat = Thermostat.new(
      @settings['temperature'], @settings['range'], 'celsius'
    )
  end
end

Instance Method Details

#update(measurement) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/json_thermostat.rb', line 17

def update(measurement)
  @measurement = JSON.parse(measurement)
  @thermostat.current_value = @measurement['temperature']
  if @measurement['unit']
    @thermostat.run(@measurement['unit'])
  else
    @thermostat.run('celsius')
  end

  JSON.generate(cooling: @thermostat.airco, heating: @thermostat.heating)
end