Commit 4b423549 authored by Mattia Bondanza's avatar Mattia Bondanza

Prima versione. Sembra andare.

parent 1c247c69
#!/usr/bin/python
import commands
import json
#---- SETTINGS ----#
rain_limit = 1.0
cloud_limit = 50
#------------------#
def change_led_status( led, status ):
"""Status can be "on", "off", "heartbeat", "blink". If status does not match any of this strings
the function just print an error and return without doing anythig."""
leds = [ "/sys/class/leds/tdw89x0:green:dsl/",
"/sys/class/leds/tdw89x0:green:internet/",
"/sys/class/leds/ath9k-phy0/",
"/sys/class/leds/tdw89x0:green:wps/" ]
#print "Opening ", leds[led]+"brightness"
bright = open( leds[led]+"brightness", "wr" )
trigger = open( leds[led]+"trigger", "wr" )
if status == "on":
trigger.write( "none" )
bright.write( "1" )
if status == "off":
trigger.write( "none" )
bright.write( "0" )
if status == "blink":
trigger.write( "timer" )
bright.write( "1" )
if status == "heartbeat":
trigger.write( "heartbeat" )
bright.write( "1" )
bright.close()
trigger.close()
body = commands.getoutput("curl \'api.openweathermap.org/data/2.5/forecast?q=pisa,it&APPID=c5ebd88f4494c472f3032e3426b6f0c6\' -s")
parsed = json.loads(body)
cloud_perc_average = 0
rain_average = 0
print "Date\t\t\tDescription\tCloud %\tRain"
for i in range(len(parsed["list"])):
print parsed["list"][i]["dt_txt"], "\t", parsed["list"][i]["weather"][0]["description"], "\t", parsed["list"][i]["clouds"]["all"],
cloud_perc_average += parsed["list"][i]["clouds"]["all"]
if len(parsed["list"][i]["rain"]) > 0:
print "\t", parsed["list"][i]["rain"]["3h"]
rain_average += parsed["list"][i]["rain"]["3h"]
else :
print "\t0"
if i % 8 == 7 and i < 32:
cloud_perc_average /= 8
rain_average /= 8
if rain_average > rain_limit:
change_led_status( int( i / 8 ), "blink" )
print "blink"
elif cloud_perc_average > cloud_limit:
change_led_status( int( i / 8 ), "off" )
print "off"
else:
change_led_status( int( i / 8 ), "on" )
print "on"
if cloud_perc_average > cloud_limit:
print "\x1b[37;1m",
print "Average clouds in the last 24h: ", cloud_perc_average, "\x1b[0m"
if rain_average > rain_limit:
print "\x1b[34m",
print "Average rain in the last 24h: ", rain_average, "\n", "\x1b[0m"
rain_average = 0
cloud_perc_average = 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment