Skip to content

Instantly share code, notes, and snippets.

@kgrz

kgrz/app.rb Secret

Last active April 1, 2016 03:21
Show Gist options
  • Save kgrz/9320d1682e682a9930c30e34c979a306 to your computer and use it in GitHub Desktop.
Save kgrz/9320d1682e682a9930c30e34c979a306 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'json'
enable :sessions
get '/' do
require 'pry'; binding.pry
@temp = session[:temp] || 20
erb :index
end
post '/temperature' do
p params[:temp]
session[:temp] = params[:temp].to_i
JSON.dump(temp: session[:temp])
end
get '/temperature' do
p session[:temp]
if session[:temp]
JSON.generate({temp: session[:temp]})
else
JSON.generate({temp: 20})
end
end
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<section class="display">
<h1>Thermostat</h1>
<input id='city' type='text' placeholder="city name">
<button id='submit'>Submit</button>
<p id='weather'></p>
<h1 id="temperature"><%= @temp %></h1>
<h4 id="psm"></h4>
<form action='/temperature' method='post'>
<button class="icon icon-up" id="up" name='temp'></button>
<button class="icon icon-down"id="down" name='temp'></button>
<button id="reset" name='temp'>reset</button>
<button id="switchpowersaving">PSM</button>
</form>
</section>
<script src="jquery.js"></script>
<script src="thermostat.js"></script>
<script src="interface.js"></script>
<!-- <script src="weatherAPI.js"></script> -->
</body>
</html>
$(document).ready(function() {
var thermostat = new Thermostat();
var temp = thermostat.temperature;
updateTemperature();
$('#up').click(function(event) {
event.preventDefault();
event.stopPropagation();
thermostat.increaseTemperature();
updateTemperature();
});
$('#down').click(function() {
event.preventDefault();
event.stopPropagation();
thermostat.decreaseTemperature();
updateTemperature();
});
$('#reset').click(function() {
thermostat.tempReset();
updateTemperature();
});
$('#switchpowersaving').click(function() {
thermostat.switchModePowerSaving();
updateTemperature();
});
function updateTemperature() {
$.ajax({
type: 'POST',
url: 'http://localhost:4567/temperature',
dataType: 'json',
success: function(data){
temp = data.temp;
$('#temperature').text(thermostat.currentTemperature() + "\xB0C");
$('#up').attr('value', thermostat.currentTemperature());
$('#down').attr('value', thermostat.currentTemperature());
$('#reset').attr('value', thermostat.currentTemperature());
$('.display').css('background-color', thermostat.displayColor());
$('#psm').text(thermostat.displayPowerSaveMode());
},
error: function(){
alert('Error loading temp');
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment