class JSONThermostat

processes the values and returns whether the airconditioning or heater should be active or not

Attributes

current_value[RW]
range[RW]
set_cooler[RW]
set_heater[RW]
unit[RW]
wanted_value[RW]

Public Class Methods

new(testjson) click to toggle source
# File lib/json_thermostat.rb, line 6
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

controls whether the temperature is warm, cold or pleasant

# File lib/json_thermostat.rb, line 22
def check
  if @current_value < @wanted_value - @range
    warm
  elsif @current_value > @wanted_value + @range
    cold
  else
    pleasant
  end
end
cold() click to toggle source

returns json file with active cooling and inactive heating

# File lib/json_thermostat.rb, line 65
def cold
  JSON.generate(cooling: true, heating: false)
end
pleasant() click to toggle source

returns json file with inactive cooling and inactive heating

# File lib/json_thermostat.rb, line 61
def pleasant
  JSON.generate(cooling: false, heating: false)
end
temp_to_fahrenheit(value) click to toggle source

converts celcius to fahrenheit

# File lib/json_thermostat.rb, line 56
def temp_to_fahrenheit(value)
  value = (value - 32.0) / 1.8
  value
end
temp_to_kelvin(value) click to toggle source

converts celcius to kelvin

# File lib/json_thermostat.rb, line 51
def temp_to_kelvin(value)
  value -= 273.2
  value
end
unit_controlmeasurement() click to toggle source

controls whether the given unit is celsius, fahrenheid or kelvin and converts the current value

# File lib/json_thermostat.rb, line 32
def unit_controlmeasurement
  if @unitmeasurement == 'celsius'
  elsif @unitmeasurement == 'fahrenheit'
    @current_value = temp_to_fahrenheit(@current_value)
  elsif @unitmeasurement == 'kelvin'
    @current_value = temp_to_kelvin(@current_value)
  end
end
unit_controlsetting() click to toggle source

controls whether the given unit is celsius, fahrenheid or kelvin and converts the wanted value and range

# File lib/json_thermostat.rb, line 41
def unit_controlsetting
  if @unitsetting == 'celsius'
  elsif @unitsetting == 'fahrenheit'
    @wanted_value = temp_to_fahrenheit(@wanted_value)
    @range = range / 1.8
  elsif @unitsetting == 'kelvin'
    @wanted_value = temp_to_kelvin(@wanted_value)
  end
end
update(testjson) click to toggle source

updates the json file with the calculated values

# File lib/json_thermostat.rb, line 13
def update(testjson)
  parsed           = JSON.parse(testjson)
  @current_value   = parsed['temperature']
  @unitmeasurement = parsed['unit']
  unit_controlsetting
  unit_controlmeasurement
  check
end
warm() click to toggle source

returns json file with inactive cooling and active heating

# File lib/json_thermostat.rb, line 69
def warm
  JSON.generate(cooling: false, heating: true)
end