<repo_name>cazwazacz/thermostat<file_sep>/public/src/Thermostat.js
var SETTINGS = {
  initialTemp: 20,
  step: 1,
  min: 10,
  PSMax: 25,
  max: 32,
  lowUsage: 18,
  mediumUsage: 25
}

function Thermostat (settings = SETTINGS) {
  this.settings = settings;
  this._temp = this.settings.initialTemp;
  this._isPowerSavingMode = true;
}


Thermostat.prototype.temp = function () {
  return this._temp;
};

Thermostat.prototype.up = function () {
  this._temp = Math.min(this._temp + this.settings.step, this.max());
};

Thermostat.prototype.down = function () {
  this._temp = Math.max(this._temp - this.settings.step, this.min());
};

Thermostat.prototype.min = function () {
  return this.settings.min;
};

Thermostat.prototype.isPowerSavingMode = function () {
  return this._isPowerSavingMode;
};

Thermostat.prototype.max = function () {
  return this.isPowerSavingMode() ? this.settings.PSMax : this.settings.max;
};

Thermostat.prototype.reset = function () {
  this._temp = this.settings.initialTemp;
};

Thermostat.prototype.usage = function () {
  if (this._temp < this.settings.lowUsage) {
    return "low-usage";
  } else if (this._temp < this.settings.mediumUsage) {
    return "medium-usage";
  } else {
    return "high-usage";
  }
};

Thermostat.prototype.switchMode = function () {
  this._isPowerSavingMode = !this._isPowerSavingMode;
  this._temp = Math.min(this._temp, this.settings.PSMax);
};
<file_sep>/public/index.js

$(document).ready(function() {

  function postingTemp (temp) {
    $.post('/temperature', {temperature: temp}, function(data) {
    });
  }

  var thermostat = new Thermostat();
  var url = 'http://api.openweathermap.org/data/2.5/weather';
  var key = 'f93adfeb902ecab387b8b3b6b636cf26';

  function assemble_url(city, key) {
    return url + '?q=' + city + '&appid=' + key;
  };

  $.ajaxSetup({
    'error': function() { alert('Chosen city does not exist') }
  });

  function getLocalTemp (city = 'London') {
    $.getJSON(assemble_url(city, key), function(data) {
        $("#localTemp").html(data.main.temp);
        $("#cityName").html(data.name);
    });
  };

  function update_ps_color() {
    var color = thermostat.isPowerSavingMode() ? "green" : "red";
    $("#psm").css("background-color", color);
  };

  function update_ps_text() {
    var text = thermostat.isPowerSavingMode() ? "On" : "Off";
    $("#psm_state").html(text);
  };

  function getTempFromApi() {
    $.get('/temperature', function(data) {
      if (data.temperature === null) {
        $('#temp').html(thermostat.tempe);
      } else {
        $('#temp').html(data.temperature);
      }
    });
  }

  function getCityFromApi() {
    $.get('/temperature', function(data) {
      if (data.city === null) {
        $('#cityName').html('London');
        getLocalTemp();
      } else {
        $('#cityName').html(data.city);
        getLocalTemp(data.city);
      }
    });
  }

  getTempFromApi();
  getCityFromApi();

  function update_temperature() {
    $("#temp").html(thermostat.temp());
  };

  function usageColor() {
    $('#temp').removeClass();
    $('#temp').addClass(thermostat.usage());
  }

  function update() {
    update_temperature();
    update_ps_text();
    update_ps_color();
    usageColor();
    postingTemp(thermostat.temp());
  };

  function postCity(city) {
    $.post('/city', {city: city}, function(data) {
    })
  }

  update();

  $('#city_button').click(function() {
    var city = $('#city').val();
    getLocalTemp(city);
    postCity(city);
  });

  $("#down").click(function() {
    thermostat.down();
  });

  $("#up").click(function() {
    thermostat.up();
  });

  $("#reset").click(function() {
    thermostat.reset();
  });

  $("#psm").click(function() {
    thermostat.switchMode();
  });

  $("button").click(update);
});
