class JSONThermostat

Takes in JSON strings from external sources like sencors and passes the information to the application

Attributes

app[RW]
converter[RW]
settings[RW]
thermostat[RW]

Public Class Methods

new(settings_json) click to toggle source

Initialises the JSONThermostat class @param json [String] a json string containing the thermostat settings, of the form {temperature: 293.15, range: 1.0, unit: “kelvin”} unit is optional and will default to celsius

# File lib/json_thermostat.rb, line 14
def initialize(settings_json)
  @settings = get_parsed_json(json: settings_json)
  @thermostat = Thermostat.new
  @converter = UnitConverter.new(desired_unit: settings['unit'])
  @app = App.new(
    wanted_temperature: settings['temperature'],
    range: settings['range'],
    thermostat: thermostat
  )
end

Public Instance Methods

get_parsed_json(args) click to toggle source

Parses a JSON string to a JSON object @param json [json] the json string @return [JSON] the resulting JSON object

# File lib/json_thermostat.rb, line 50
def get_parsed_json(args)
  JSON.parse(args[:json])
end
update(sensordata_json) click to toggle source

takes in a JSON string from outside sources and updates the application @param sensordata_json [sensordata_json] the JSON string of the form: {temperature: 293.15, unit: “kelvin”} with the unit defaulting to celsius if not specified @return [JSON] the resulting webpage of the form {cooling: Boolean, heating: Boolean}

# File lib/json_thermostat.rb, line 31
def update(sensordata_json)
  json = get_parsed_json(json: sensordata_json)
  update_temperature(temperature: json['temperature'], unit: json['unit'])
  output = app.correct_temperature
  JSON.generate(cooling: output.cooling, heating: output.heating)
end
update_temperature(args) click to toggle source

updates the temperature of the thermostat @param temperature [temperature] the request object

# File lib/json_thermostat.rb, line 40
def update_temperature(args)
  thermostat.temperature = converter.convert(
    value: args[:temperature],
    input_unit: args[:unit]
  )
end