Class: Thermostat

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

Overview

Determines if the heating should be turned on

Examples:

message = { wanted_value: 21.0, range: 1.0 }
thermo = Thermostat.new(message)
thermo.update_current(18)
thermo.update_thermo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Thermostat

Initializes the thermostat with wanted value, range and unit

Parameters:

  • args (Hash)

Options Hash (args):

  • :wanted_value (Numeric)

    the wanted value

  • :range (Numeric)

    the range that the value may defer

  • :unit (String)

    the unit of the value



15
16
17
18
# File 'lib/thermostat.rb', line 15

def initialize(args)
  @wanted_value = args[:wanted_value]
  @range = args[:range]
end

Instance Attribute Details

#aircoObject

Returns the value of attribute airco.



9
10
11
# File 'lib/thermostat.rb', line 9

def airco
  @airco
end

#current_valueObject

Returns the value of attribute current_value.



8
9
10
# File 'lib/thermostat.rb', line 8

def current_value
  @current_value
end

#heatingObject

Returns the value of attribute heating.



9
10
11
# File 'lib/thermostat.rb', line 9

def heating
  @heating
end

#rangeObject

Returns the value of attribute range.



8
9
10
# File 'lib/thermostat.rb', line 8

def range
  @range
end

#wanted_valueObject

Returns the value of attribute wanted_value.



8
9
10
# File 'lib/thermostat.rb', line 8

def wanted_value
  @wanted_value
end

Instance Method Details

#return_aircoboolean

Return if airco should be turned on

Returns:

  • (boolean)

    return if airco should be turned on



46
47
48
# File 'lib/thermostat.rb', line 46

def return_airco
  current_value > (wanted_value + range)
end

#return_heatingboolean

Return if heating should be turned on

Returns:

  • (boolean)

    return if heating should be turned on



51
52
53
# File 'lib/thermostat.rb', line 51

def return_heating
  current_value < (wanted_value - range)
end

#update_current(current_value) ⇒ Object

Update the current value with given value

Parameters:

  • current_value (Numeric)


33
34
35
# File 'lib/thermostat.rb', line 33

def update_current(current_value)
  @current_value = current_value
end

#update_range(range) ⇒ Object

Update the range



21
22
23
# File 'lib/thermostat.rb', line 21

def update_range(range)
  @range = range
end

#update_thermoString

Give return if the airco or heater should be turned on

Returns:

  • (String)

    return visually if airco or heating should be turned on



39
40
41
42
43
# File 'lib/thermostat.rb', line 39

def update_thermo
  airco = return_airco
  heating = return_heating
  "{\"cooling\":#{airco},\"heating\":#{heating}}"
end

#update_wanted(wanted_value) ⇒ Object

Update the wanted value with given value

Parameters:

  • wanted_value (Numeric)


27
28
29
# File 'lib/thermostat.rb', line 27

def update_wanted(wanted_value)
  @wanted_value = wanted_value
end