class JSONThermostat

Attributes

current_value[RW]
range[RW]
set_airco[RW]
set_heating[RW]
unit[RW]
wanted_value[RW]

Public Class Methods

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

Public Instance Methods

check() click to toggle source
# File lib/json_thermostat.rb, line 23
def check
  if @current_value < @wanted_value - @range
    heat
  elsif @current_value > @wanted_value + @range
    cool
  else
    normal
  end
end
cool() click to toggle source
# File lib/json_thermostat.rb, line 62
def cool
  JSON.generate(cooling: true, heating: false)
end
heat() click to toggle source
# File lib/json_thermostat.rb, line 66
def heat
  JSON.generate(cooling: false, heating: true)
end
normal() click to toggle source
# File lib/json_thermostat.rb, line 70
def normal
  JSON.generate(cooling: false, heating: false)
end
temp_from_fahrenheit(value) click to toggle source
# File lib/json_thermostat.rb, line 57
def temp_from_fahrenheit(value)
  value = (value - 32.0) / 1.8
  value
end
temp_from_kelvin(value) click to toggle source
# File lib/json_thermostat.rb, line 52
def temp_from_kelvin(value)
  value -= 273.2
  value
end
unit_controlmeasurement() click to toggle source
# File lib/json_thermostat.rb, line 43
def unit_controlmeasurement
  if @unitmeasurement == 'celsius'
  elsif @unitmeasurement == 'kelvin'
    @current_value = temp_from_kelvin(@current_value)
  elsif @unitmeasurement == 'fahrenheit'
    @current_value = temp_from_fahrenheit(@current_value)
  end
end
unit_controlsetting() click to toggle source
# File lib/json_thermostat.rb, line 33
def unit_controlsetting
  if @unitsetting == 'celsius'
  elsif @unitsetting == 'kelvin'
    @wanted_value = temp_from_kelvin(@wanted_value)
  elsif @unitsetting == 'fahrenheit'
    @wanted_value = temp_from_fahrenheit(@wanted_value)
    @range = range / 1.8
  end
end
update(testjson) click to toggle source
# File lib/json_thermostat.rb, line 14
def update(testjson)
  parsed           = JSON.parse(testjson)
  @current_value   = parsed['temperature']
  @unitmeasurement = parsed['unit']
  unit_controlsetting
  unit_controlmeasurement
  check
end