Added initial location lookup and connection to openweathermap

This commit is contained in:
josh@noisymime.org 2013-10-21 20:13:35 +11:00
parent 57b2f6a757
commit 1e0b7aef60
3 changed files with 49 additions and 2 deletions

20
ospi.py
View File

@ -82,6 +82,25 @@ def CPU_temperature():
except:
pass
def get_temperature():
"""Returns the temperature given the stored location info"""
import urllib2
weatherBaseURL = "http://api.openweathermap.org/data/2.5/weather?"
weatherFormat = "units=metric&"
weatherURL = weatherBaseURL + weatherFormat
latitude = gv.sd['loc'].split(",")[0]
longitude = gv.sd['loc'].split(",")[1]
queryURL = weatherURL + "lat=" + str(latitude) + "&lon=" + str(longitude)
response = urllib2.urlopen(queryURL)
decoder = json.JSONDecoder()
temperature = decoder.decode(response.read())["main"]["temp"]
return (str(temperature))
def log_run():
"""add run data to csv file - most recent first."""
if gv.lg:
@ -536,6 +555,7 @@ class home:
else:
try:
homepg += '<script>var cputemp='+str(float(CPU_temperature()))+'; var tempunit="C";</script>\n'
homepg += '<script>var temp='+str(float(get_temperature()))+'; var tempunit="C";</script>\n'
except ValueError:
pass
homepg += '<script src=\"'+baseurl()+'/static/scripts/java/svc1.8.3/home.js\"></script>'

View File

@ -33,7 +33,8 @@ else w("<b>Firmware version</b>: "+(ver/10>>0)+"."+(ver%10)+"<br>");
w("<b>Device time</b>: "+datestr(devt*1000)+"<br>");
if (typeof cputemp === 'undefined') cputemp="";
w("<b>CPU Temp</b>: <span id='heat' onmouseover='bluebg(this)' onmouseout='nobg(this)' style='cursor:pointer' onclick='toggle(tt)' title='Click to toggle Celsius <> Fahrenheit'>"+cputemp+"&deg;"+tempunit+"</span><hr>");
w("<b>CPU Temp</b>: <span id='heat' onmouseover='bluebg(this)' onmouseout='nobg(this)' style='cursor:pointer' onclick='toggle(tt)' title='Click to toggle Celsius <> Fahrenheit'>"+cputemp+"&deg;"+tempunit+"</span><br>");
w("<b>Location Temp</b>: <span id='heat' onmouseover='bluebg(this)' onmouseout='nobg(this)' style='cursor:pointer' onclick='toggle(tt)' title='Click to toggle Celsius <> Fahrenheit'>"+temp+"&deg;"+tempunit+"</span><hr>");
w("<script type=\"text/javascript\" src=\""+baseurl+"/static/scripts/java/svc1.8.3/"+((sd['mm'])?"manualmode.js":"progmode.js")+"\"></script>");
// print status and other information
w("<br><b>Operation</b>: "+(sd['en']?("on").fontcolor("green"):("OFF").fontcolor("red")));

View File

@ -29,6 +29,25 @@ function ftoggle() {
}
document.getElementById("tooltips").innerHTML = (state?"Hide Tooltips":"Show Tooltips");
}
function lookupLocation() {
var x=document.getElementsByName("oloc")[0];
x.value="Looking up location...";
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.value="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
var x=document.getElementsByName("oloc")[0];
x.value=position.coords.latitude + "," + position.coords.longitude;
}
w("<div align=\"center\" style=\"background-color:#EEEEEE;border:2px solid gray;padding:5px 10px;width:240px;border-radius:10px;box-shadow:3px 3px 2px #888888;\">");
w("<b>Set Options</b>:<br><font size=2>(Hover on each option to see tooltip)</font></div>");
w("<p></p>");
@ -46,7 +65,14 @@ for(oid=0;oid<opts.length;oid++){
if(datatype == "boolean") {
w("<p title=\""+tooltip+"\"><b>"+label+":</b> <input type=checkbox "+(value>0?"checked":"")+" name=o"+name+">");
} else if (datatype == "string") {
w("<p title=\""+tooltip+"\"><b>"+label+":</b> <input type=text size=31 maxlength=31 value='"+value+"' name=o"+name+">");
switch (name) {
case "loc":
w("<p title=\""+tooltip+"\"><b>"+label+":</b> <input type=text size=31 maxlength=31 value='"+value+"' name=o"+name+">");
w("<button id=\"location\" style=\"height:24\" onclick=\"lookupLocation();return false;\">Lookup Location</button>");
break;
default:
w("<p title=\""+tooltip+"\"><b>"+label+":</b> <input type=text size=31 maxlength=31 value='"+value+"' name=o"+name+">");
}
} else {
switch (name) {
case "tz":