Class: JSONThermostat

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = '') ⇒ JSONThermostat

The [initialize] method creates a JSONThermostat

Examples:

JSONThermostat.new('{"temperature": 20.0, "range": 1.0, "unit": "celcius"}')

Parameters:

  • settings (String) (defaults to: '')

    a json object containing the following attributes

    • temperature [Double] the wanted temperature

    • range [Double] the lower and upper margin around temperature

    • unit [String, nil] optionally an unit can be given, defaults to celcius



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/json_thermostat.rb', line 16

def initialize(settings = '')
  json = JSON.parse(settings)
  temp, range = 0.0
  if json.include? 'unit'
    temp = Convert.convert_absolute(input: json['temperature'], from_unit: json['unit'], to_unit: 'celsius')
    range = Convert.convert_relative(input: json['range'], from_unit: json['unit'], to_unit: 'celsius')
  else
    temp = json['temperature']
    range = json['range']
  end
  @thermostat = Thermostat.new(range: range, temp_wanted: temp, temp_curr: temp)
end

Instance Attribute Details

#thermostatObject (readonly)

Returns the internal thermostat

Returns:

  • returns the internal thermostat



7
8
9
# File 'lib/json_thermostat.rb', line 7

def thermostat
  @thermostat
end

Instance Method Details

#update(data = '') ⇒ String

The [update] method updates the JSONThermostat with a new temperature

Examples:

thermostat.update('{"temperature": 15}')
#returns: '{"cooling": false, "heating": true}'

Parameters:

  • data (String) (defaults to: '')

    a json object containing the temperature input

    • temperature [Double] the new current temperature

    • unit [String, nil] optionally an unit can be given, defaults to celcius

Returns:

  • (String)

    a json object containing the temperature control status

    • cooling [Bool] the status of the airo

    • heating [Bool] the status of the heater



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/json_thermostat.rb', line 39

def update(data = '')
  json = JSON.parse(data)
  temp = 0.0
  temp = if json.include? 'unit'
           Convert.convert_absolute(input: json['temperature'], from_unit: json['unit'], to_unit: 'celsius')
         else
           json['temperature']
         end
  @thermostat.temp_curr = temp
  @thermostat.check_temp
  JSON.generate(cooling: @thermostat.airco, heating: @thermostat.heater)
end