def index(): now=datetime.datetime.now().strftime(config.DATEFORMAT) # cloud TS service status cloud='on' if util.isCloudActive() else 'off' # temperature fTemp1=util.getTemperature(0) temp1="%2.1f" % fTemp1 # external temp r=radio.radio() t=r.getTemp(2) text='N/A' if t is None else "%2.1f" % t # pressure p1=util.getPressure() # light s1=util.getLight(config.LINES['R1LINE']) # last time the ligth was switched off lastoff=util.db_getvalue(config.DB_LIGHTSOFF) if lastoff is None: loe='<no disponible>' else: loe=lastoff tData={ 'temp1':temp1, 'text':text, 'pres1':p1, 's1':s1, 'time':now, 'loe':loe, 'cloud':cloud } return render_template('index.html',**tData)
def light(): now=datetime.datetime.now() lastoff=util.db_getvalue(config.DB_LIGHTSOFF) if lastoff is None: flash('No Last Lights Out Event.') lastoff=now.strftime(config.DBDATEFORMAT) # if arrived here by submitting the form, perform the action if request.method == 'POST': value=request.form['value'] line=request.form['line'] if line not in config.LINES.keys() or value not in {'on','off'}: flash('Internal error. No lights changed.') return redirect(url_for('/')) s=True if value=='on' else False if "Apagar15" in request.form: cpid=os.fork() if cpid==0: time.sleep(15) util.setLight(config.LINES[line],s) shutdown=request.environ.get('werkzeug.server.shutdown') shutdown() elif "Apagar60" in request.form: cpid=os.fork() if cpid==0: time.sleep(60) util.setLight(config.LINES[line],s) shutdown=request.environ.get('werkzeug.server.shutdown') shutdown() else: util.setLight(config.LINES[line],s) # write switch off event to db if not s: util.db_setvalue(config.DB_LIGHTSOFF,now.strftime(config.DBDATEFORMAT)) lastoff=None s1=util.getLight(config.LINES['R1LINE']) if lastoff is None: delta=None else: delta=now-datetime.datetime.strptime(lastoff,'%Y-%m-%d %H:%M:%S') tData={ 's1':s1, 'time':now.strftime(config.DATEFORMAT), 'lastoff':util.prettydelta(delta) } return render_template('light.html',**tData)
def conf(): cloud='on' if util.isCloudActive() else 'off' now=datetime.datetime.now().strftime(config.DATEFORMAT) res=None # if arrived here by submitting the form if request.method == 'POST': # get setting for webcam resolution res=request.form['res'] util.db_setvalue(config.DB_WEBCAMRES,res) # get setting for cloud save desired state clds=request.form['cloud'] # and try to start it if it was stopped if cloud=='off' and clds=='on': ps=subprocess.Popen("sudo /etc/init.d/thingspeak start", shell=True, stdout=subprocess.PIPE) flash('ThingSpeak daemon started') elif cloud=='on' and clds=='off': ps=subprocess.Popen("sudo /etc/init.d/thingspeak stop", shell=True, stdout=subprocess.PIPE) flash('ThingSpeak daemon stopped') flash('Settings saved') else: res=util.nvl(util.db_getvalue(config.DB_WEBCAMRES),'640x480') db=util.db_get() with db: c=db.cursor() q="SELECT * FROM ACCESS ORDER BY DATETIME DESC LIMIT 100" c.execute(q) rows=c.fetchall() log='' for row in rows: log=log+row[0]+';'+row[1]+';'+row[2]+';'+row[3]+';'+row[4]+'\n' tData={ 'cloud':cloud, 'res':res, 'time':now, 'log':log } return render_template('conf.html',**tData)