Skip to content

Commit

Permalink
added automatic wsgi support checking and fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
1n5aN1aC committed Dec 14, 2015
1 parent 7505bf5 commit 0731fcd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Supports](https://img.shields.io/badge/platform-raspberry%20pi-lightgrey.svg?style=plastic)](https://www.raspberrypi.org/) [![Supports](https://img.shields.io/badge/supports-mysql%20or%20sqlite-lightgrey.svg?style=plastic)](https://github.com/1n5aN1aC/rainmeter-pi/blob/master/framework/settings.py) [![License](https://img.shields.io/badge/license-cc--by--sa-green.svg?style=plastic)](http://creativecommons.org/licenses/by-sa/4.0/)
[![Supports](https://img.shields.io/badge/platform-raspberry%20pi-lightgrey.svg?style=plastic)](https://www.raspberrypi.org/) [![Supports](https://img.shields.io/badge/supports-mysql%20or%20sqlite-lightgrey.svg?style=plastic)](https://github.com/1n5aN1aC/rainmeter-pi/blob/master/framework/settings.py#L29) [![License](https://img.shields.io/badge/license-cc--by--sa-green.svg?style=plastic)](http://creativecommons.org/licenses/by-sa/4.0/)

# Rainmeter-pi
Raspberry-pi powered Weather Station
Expand Down
6 changes: 3 additions & 3 deletions framework/http_reset_rain.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def reset_rain():
now = Table_Now.get(1)
now.sync()
now.Out_Rain_Since_Reset = 0

return "reset!"

# If file ran directly, reset the rain as well.
# We use this to be able to support wsgi OR straight python.
Expand All @@ -18,7 +20,5 @@ def reset_rain():
print "Content-Type: text/html;charset=utf-8"
print

reset_rain()

# Print the Data
print "reset!"
print reset_rain()
16 changes: 16 additions & 0 deletions framework/http_reset_rain.wsgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!python
from http_reset_rain import *

#
# This is requested by the index page every time it needs to update the data.
# It will Return all the current weather data in a json-file, formated for web-display.
# That means this is called very often, and should be reletively effecient
def application(environ, start_response):
status = '200 OK'
output = reset_rain()

response_headers = [('Content-type', 'application/json;charset=utf-8'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]
28 changes: 26 additions & 2 deletions js/value_refresher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
// This requests the current data via local json request.
// It then makes a callback to update() to handle the data.
function getTheJSON() {
jQuery.getJSON("/status", function(data) { update(data); } )
url = "/framework/http_status.py"
if (window.wsgi == true) {
url = "/status"
}
jQuery.getJSON(url, function(data) { update(data); } )
}

// update() handles actually updating the vales on the page from the json results
Expand Down Expand Up @@ -61,12 +65,32 @@ function update(data) {

// Handles when the user clicks the button to reset the rainfall.
function reset_rain() {
url = "/framework/http_reset_rain.py"
if (window.wsgi == true) {
url = "/reset_rain"
}
$.ajax({
url: "/framework/http_reset_rain.py",
url: url,
});
}

// If the server responds to /status, we know wgsi is supported.
function check_wsgi() {
$.ajax({
type: "HEAD",
url: "/status",
success: function() {
window.wsgi = true;
},
error: function() {
window.wsgi = false;
}
});
}

document.addEventListener("DOMContentLoaded", function(event) {
//Check if the webserver is set up to support wsgi (much faster)
check_wsgi()
//Update it now!
getTheJSON()
// Set up a permanent call to getTheJSON() every X seconds.
Expand Down

0 comments on commit 0731fcd

Please sign in to comment.