class App
The App
class, contains the logic to ceep the room temperature within a desired value
Attributes
Public Class Methods
Initialises the App
class @param wanted_temperature
[wanted_temperature] the desired room temperature @param range [range] the range in wich the room temperature must stay from the desired temperature @param thermostat [thermostat] an instance from the class Thermostat
@param update_speed
[update_speed] the speed at wich the app will run
# File lib/app.rb, line 11 def initialize(args) @wanted_temperature = args[:wanted_temperature] @range = get_range(input_range: args[:range]) @thermostat = args[:thermostat] @update_speed = args[:update_speed] || 1 end
Public Instance Methods
Checks if the thermostat must cool down to reach the desired temperature @param temperature [temperature] the current room temperature @return [Boolean] true if the thermostat had to cool down if not, false
# File lib/app.rb, line 49 def cool_thermostat(args) if temperature_out_of_range(temperature: args[:temperature]) && temperature_to_high(temperature: args[:temperature]) thermostat.cooling(value: range) true else false end end
says the thermostat if it must heat up or cool down to reach the desired temperature @return [Struct] contains what actions the thermostat took
# File lib/app.rb, line 38 def correct_temperature cooling = cool_thermostat(temperature: thermostat_temperature) heating = heat_thermostat(temperature: thermostat_temperature) output = Struct.new(:cooling, :heating) output.new(cooling, heating) end
gets the range value used in calculations @param input_range [input_range] the range in wich the room temperature must stay from the desired temperature @return [number] the actual range
# File lib/app.rb, line 31 def get_range(args) args[:input_range] / 2.0 end
Checks if the thermostat must heat up to reach the desired temperature @param temperature [temperature] the current room temperature @return [Boolean] true if the thermostat had to warm up if not, false
# File lib/app.rb, line 63 def heat_thermostat(args) if temperature_out_of_range(temperature: args[:temperature]) && temperature_to_low(temperature: args[:temperature]) thermostat.heating(value: range) true else false end end
Starts the application
# File lib/app.rb, line 19 def run loop do print("\ntermostaat temperatuur: #{thermostat_temperature}") correct_temperature sleep(update_speed) end end
Checks if the temperature is out of range @param temperature [temperature] the current room temperature @return [Boolean] true if the thermostat is out of range else, false
# File lib/app.rb, line 76 def temperature_out_of_range(args) (args[:temperature] - wanted_temperature).abs > range end
Checks if the temperature is to high @param temperature [temperature] the current room temperature @return [Boolean] true if the temperature is to high else, false
# File lib/app.rb, line 90 def temperature_to_high(args) args[:temperature] > wanted_temperature end
Checks if the temperature is to low @param temperature [temperature] the current room temperature @return [Boolean] true if the temperature is to low else, false
# File lib/app.rb, line 83 def temperature_to_low(args) args[:temperature] < wanted_temperature end
gets the current temperature of the thermostat @return [number] the current thermostat temperature
# File lib/app.rb, line 96 def thermostat_temperature thermostat.temperature end