class JSONThermostat

@author Frederik Feys

Attributes

current_value[RW]
range[RW]
units[RW]
wanted_value[RW]

Public Class Methods

new(json_input) click to toggle source
# File lib/json_thermostat.rb, line 10
def initialize(json_input)
  parsed = JSON.parse(json_input)
  @wanted_value = parsed['temperature']
  @range = parsed['range'] / 2.0
  @unit = parsed['unit']
end

Public Instance Methods

airco() click to toggle source

Display airco

# File lib/json_thermostat.rb, line 70
def airco
  '{"cooling":true,"heating":false}'
end
check() click to toggle source

Check if it's to hot or to cold

# File lib/json_thermostat.rb, line 58
def check
  if @current_value < @wanted_value - @range
    heating
  elsif @current_value > @wanted_value + @range
    airco
  else
    normal
  end
end
heating() click to toggle source

Display heating

# File lib/json_thermostat.rb, line 76
def heating
  '{"cooling":false,"heating":true}'
end
normal() click to toggle source

Display when it's normal

# File lib/json_thermostat.rb, line 82
def normal
  '{"cooling":false,"heating":false}'
end
unit() click to toggle source

Calculate the unit

# File lib/json_thermostat.rb, line 44
def unit
  @wanted_value = if @unit == 'fahrenheit'
                    (@wanted_value - 32.0) / 1.8
                  elsif @unit == 'kelvin'
                    (@wanted_value - 273.15)
                  elsif @unit == 'celsius'
                    @wanted_value
                  else
                    @wanted_value
                  end
end
unit_updated() click to toggle source

Update the unit

# File lib/json_thermostat.rb, line 30
def unit_updated
  @current_value = if @unit_updated == 'fahrenheit'
                     (@current_value - 32.0) / 1.8
                   elsif @unit_updated == 'kelvin'
                     (@current_value - 273.15)
                   elsif @unit_updated == 'celsius'
                     @current_value
                   else
                     @current_value
                   end
end
update(json_input) click to toggle source

Update the thermostat

# File lib/json_thermostat.rb, line 19
def update(json_input)
  parsed = JSON.parse(json_input)
  @current_value = parsed['temperature']
  @unit_updated = parsed['unit']
  unit_updated
  unit
  check
end