<s> import cherrypy <EOL> import jinja2 <EOL> import time <EOL> from datetime import datetime <EOL> from jinja2 import Environment , FileSystemLoader <EOL> import braubuddy <EOL> class API ( object ) : <EOL> """<STR_LIT>""" <EOL> @ cherrypy . expose <EOL> @ cherrypy . tools . json_out ( ) <EOL> def index ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> since = int ( kwargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> except ValueError : <EOL> since = <NUM_LIT:0> <EOL> try : <EOL> before = int ( kwargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> except ValueError : <EOL> before = <NUM_LIT:0> <EOL> try : <EOL> limit = int ( kwargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> except ValueError : <EOL> limit = <NUM_LIT:0> <EOL> return braubuddy . RECENT_DATA . get_datapoints ( <EOL> since = since , before = before , limit = limit ) <EOL> @ cherrypy . expose <EOL> def set ( self , temp ) : <EOL> """<STR_LIT>""" <EOL> if temp : <EOL> pass <EOL> return '<STR_LIT>' . format ( temp ) <EOL> class Dashboard ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . j2env = Environment ( <EOL> loader = FileSystemLoader ( braubuddy . TEMPLATE_DIR ) ) <EOL> @ cherrypy . expose <EOL> def index ( self ) : <EOL> '''<STR_LIT>''' <EOL> template = self . j2env . get_template ( '<STR_LIT>' ) <EOL> return template . render ( <EOL> title = cherrypy . config [ '<STR_LIT>' ] , <EOL> frequency = cherrypy . config [ '<STR_LIT>' ] , <EOL> units = braubuddy . utils . abbreviate_temp_units ( <EOL> cherrypy . config [ '<STR_LIT>' ] ) , <EOL> show_footer = cherrypy . config [ '<STR_LIT>' ] , <EOL> year = datetime . now ( ) . year , <EOL> ) <EOL> class Engine ( object ) : <EOL> """<STR_LIT>""" <EOL> def cycle ( self ) : <EOL> """<STR_LIT>""" <EOL> units = cherrypy . config [ '<STR_LIT>' ] <EOL> retry_count = cherrypy . config [ '<STR_LIT>' ] <EOL> retry_delay = cherrypy . config [ '<STR_LIT>' ] <EOL> envcontroller = cherrypy . config [ '<STR_LIT>' ] <EOL> thermometer = cherrypy . config [ '<STR_LIT>' ] <EOL> thermostat = cherrypy . config [ '<STR_LIT>' ] <EOL> current_heat , current_cool = envcontroller . get_power_levels ( ) <EOL> for i in range ( retry_count ) : <EOL> try : <EOL> current_temp = thermometer . get_temperature ( units = units ) <EOL> break <EOL> except braubuddy . thermometer . ReadError as err : <EOL> cherrypy . log . error ( err . message ) <EOL> time . sleep ( retry_delay ) <EOL> else : <EOL> cherrypy . request . app . log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( retry_count ) ) <EOL> return False <EOL> required_heat , required_cool = thermostat . get_required_state ( <EOL> current_temp , current_heat , current_cool , units = units ) <EOL> envcontroller . set_heater_level ( required_heat ) <EOL> envcontroller . set_cooler_level ( required_cool ) <EOL> target = thermostat . target <EOL> for name , output in cherrypy . request . app . config [ '<STR_LIT>' ] . iteritems ( ) : <EOL> try : <EOL> output . publish_status ( target , current_temp , current_heat , <EOL> current_cool ) <EOL> except braubuddy . output . OutputError as err : <EOL> cherrypy . log . error ( err . message ) <EOL> return True </s>
