def switch_boiler(shed_state): try: roomTemp = check_temp.temp('room').get_temp() radTemp = check_temp.temp('rad').get_temp() outsideTemp = check_temp.temp('outside').get_temp() conn_string = prop('database') conn = psycopg2.connect(conn_string) cursor = conn.cursor() sql = """ delete from current_state """ cursor.execute(sql) conn.commit() sql = """ insert into current_state (state) values (%(state)s) """ cursor.execute(sql, {'state': shed_state}) conn.commit() sql = """ insert into log (room_temp, rad_temp, outside_temp, datestamp, state) values (%(roomtemp)s, %(radtemp)s, %(outsidetemp)s, (select now()), %(state)s) """ cursor.execute( sql, { 'roomtemp': roomTemp, 'radtemp': radTemp, 'outsidetemp': outsideTemp, 'state': shed_state }) conn.commit() except Exception as e: logging.debug('error in database connection in switch_boiler: %s' % e) try: pin = int(prop('gpio')) # use P1 header pin numbering convention GPIO.setmode(GPIO.BOARD) # Set up the GPIO channels - one input and one output GPIO.setwarnings(False) GPIO.setup(pin, GPIO.OUT) if shed_state == 'ON': GPIO.output(pin, GPIO.HIGH) elif shed_state == 'OFF': GPIO.output(pin, GPIO.LOW) except Exception as e: logging.debug('Error switching boiler state %s' % e)
def switch_boiler(shed_state): try: roomTemp = check_temp.temp('room').get_temp() radTemp = check_temp.temp('rad').get_temp() outsideTemp = check_temp.temp('outside').get_temp() conn_string = prop('database') conn = psycopg2.connect(conn_string) cursor = conn.cursor() sql = """ delete from current_state """ cursor.execute(sql) conn.commit() sql = """ insert into current_state (state) values (%(state)s) """ cursor.execute(sql, {'state': shed_state}) conn.commit() sql = """ insert into log (room_temp, rad_temp, outside_temp, datestamp, state) values (%(roomtemp)s, %(radtemp)s, %(outsidetemp)s, (select now()), %(state)s) """ cursor.execute(sql, {'roomtemp':roomTemp, 'radtemp':radTemp, 'outsidetemp':outsideTemp, 'state':shed_state}) conn.commit() except Exception as e: logging.debug('error in database connection in switch_boiler: %s' % e) try: pin = int(prop('gpio')) # use P1 header pin numbering convention GPIO.setmode(GPIO.BOARD) # Set up the GPIO channels - one input and one output GPIO.setwarnings(False) GPIO.setup(pin, GPIO.OUT) if shed_state == 'ON': GPIO.output(pin, GPIO.HIGH) elif shed_state == 'OFF': GPIO.output(pin, GPIO.LOW) except Exception as e: logging.debug('Error switching boiler state %s' % e)
def main(): try: try: rqstSession = request.get_cookie('pysessionid', secret=prop('cookieSecret')) except: pass if check_session(rqstSession) is True: try: roomTemp = check_temp.temp('room').get_temp() radTemp = check_temp.temp('rad').get_temp() outsideTemp = check_temp.temp('outside').get_temp() if request.forms.get('override', '').strip(): logging.debug('override') set_override() return template('main', roomTemp=roomTemp, radTemp=radTemp, outsideTemp=outsideTemp) except: pass return template('main', roomTemp=roomTemp, radTemp=radTemp, outsideTemp=outsideTemp) elif request.forms.get('override', '').strip() is '': rqstSession = request.get_cookie('pysessionid', secret=prop('cookieSecret')) username = request.forms.get('username').upper() password = request.forms.get('password').strip() logging.debug(password) if auth.passwd(username, password).check_password() == True: set_session(rqstSession) return template('main', roomTemp=roomTemp, radTemp=radTemp, outsideTemp=outsideTemp) else: return template('login') except Exception as e: logging.debug('exception in main: %s' % e) return '<p>Error</p>'
import check_temp test = check_temp.temp('room').check_temp() print(test)
def get_schedule(): while True: conn_string = prop('database') logging.debug("Connecting to database ->%s" % (conn_string)) try: conn = psycopg2.connect(conn_string) cursor = conn.cursor() except Exception as e: logging.debug("failed to connect to database -> %s" (e)) try: #get current time and day of week day = datetime.date.today() time = datetime.datetime.now().time() dayOfWeek = datetime.date.strftime(day, '%A') logging.debug(dayOfWeek) #get schedule rows from database sql = """ select s.id_shed, s.day, s.time, s.state from schedule s join template t on t.id_tmpl = s.id_tmpl where upper(s.day) = upper(%(day)s) and s.time <= %(curr_time)s and t.selected = 'Y' order by s.time desc """ cursor.execute(sql, {'day': dayOfWeek, 'curr_time': time}) shed_row = cursor.fetchone() shed_row_str = str(shed_row) logging.debug('current schedule row = %s' % shed_row_str) while shed_row is None: pre_day = day - timedelta(days=1) dayOfWeek = datetime.date.strftime(pre_day, '%A') cursor.execute(sql, {'day': dayOfWeek, 'curr_time': time}) shed_row = cursor.fetchone() shed_row_str = str(shed_row) logging.debug('current schedule row = %s' % shed_row_str) id_shed = shed_row[0] shed_day = shed_row[1] shed_time = shed_row[2] shed_state = shed_row[3] except Exception as e: logging.debug(e) try: sql = """ select state from current_state """ cursor.execute(sql) row = cursor.fetchone() curr_state = row[0] except Exception as e: logging.debug(e) curr_state = 'OFF' try: temp = check_temp.temp('room').check_temp() logging.debug('temp is: %s' % temp) logging.debug('checking override') override = check_override(id_shed) #ON 0 low on #off 1 low on except Exception as e: logging.debug('error in temp or override %s' % e) try: if shed_state == 'ON' and override == '0' and temp == 'LOW' and curr_state == 'OFF': switch_boiler('ON') logging.info( 'switching boiler on \n schedule state = on /n Override = false, temp = low' ) elif shed_state == 'OFF' and override == '1' and temp == 'LOW' and curr_state == 'OFF': switch_boiler('ON') logging.info( 'switching boiler on \n schedule state = off /n Override = true, temp = low' ) else: switch_boiler('OFF') logging.info( 'switching boiler off \n schedule state = %s /n Override = %s, temp = %s' % (shed_state, override, temp)) except Exception as e: logging.debug('error in switch section: %s' % e) t = float(prop('loopsleep')) logging.debug('sleeping for = %s seconds' % t) TIME.sleep(t)
import check_temp import psycopg2 from get_props import prop shed_state = 'ON' roomTemp = check_temp.temp('room').get_temp() radTemp = check_temp.temp('rad').get_temp() outsideTemp = check_temp.temp('outside').get_temp() conn_string = prop('database') conn = psycopg2.connect(conn_string) cursor = conn.cursor() sql = """ insert into log (room_temp, rad_temp, outside_temp, datestamp, state) values (%(roomtemp)s, %(radtemp)s, %(outsidetemp)s, (select now()), %(state)s) """ cursor.execute( sql, { 'roomtemp': roomTemp, 'radtemp': radTemp, 'outsidetemp': outsideTemp, 'state': shed_state }) conn.commit()
def get_schedule(): while True: conn_string = prop("database") logging.debug("Connecting to database ->%s" % (conn_string)) try: conn = psycopg2.connect(conn_string) cursor = conn.cursor() except Exception as e: logging.debug("failed to connect to database -> %s"(e)) try: # get current time and day of week day = datetime.date.today() time = datetime.datetime.now().time() dayOfWeek = datetime.date.strftime(day, "%A") logging.debug(dayOfWeek) # get schedule rows from database sql = """ select s.id_shed, s.day, s.time, s.state from schedule s join template t on t.id_tmpl = s.id_tmpl where upper(s.day) = upper(%(day)s) and s.time <= %(curr_time)s and t.selected = 'Y' order by s.time desc """ cursor.execute(sql, {"day": dayOfWeek, "curr_time": time}) shed_row = cursor.fetchone() shed_row_str = str(shed_row) logging.debug("current schedule row = %s" % shed_row_str) while shed_row is None: pre_day = day - timedelta(days=1) dayOfWeek = datetime.date.strftime(pre_day, "%A") cursor.execute(sql, {"day": dayOfWeek, "curr_time": time}) shed_row = cursor.fetchone() shed_row_str = str(shed_row) logging.debug("current schedule row = %s" % shed_row_str) id_shed = shed_row[0] shed_day = shed_row[1] shed_time = shed_row[2] shed_state = shed_row[3] except Exception as e: logging.debug(e) try: sql = """ select state from current_state """ cursor.execute(sql) row = cursor.fetchone() curr_state = row[0] except Exception as e: logging.debug(e) curr_state = "OFF" try: temp = check_temp.temp("room").check_temp() logging.debug("temp is: %s" % temp) logging.debug("checking override") override = check_override(id_shed) # ON 0 low on # off 1 low on except Exception as e: logging.debug("error in temp or override %s" % e) try: if shed_state == "ON" and override == "0" and temp == "LOW" and curr_state == "OFF": switch_boiler("ON") logging.info("switching boiler on \n schedule state = on /n Override = false, temp = low") elif shed_state == "OFF" and override == "1" and temp == "LOW" and curr_state == "OFF": switch_boiler("ON") logging.info("switching boiler on \n schedule state = off /n Override = true, temp = low") else: switch_boiler("OFF") logging.info( "switching boiler off \n schedule state = %s /n Override = %s, temp = %s" % (shed_state, override, temp) ) except Exception as e: logging.debug("error in switch section: %s" % e) t = float(prop("loopsleep")) logging.debug("sleeping for = %s seconds" % t) TIME.sleep(t)
import check_temp import psycopg2 from get_props import prop shed_state = 'ON' roomTemp = check_temp.temp('room').get_temp() radTemp = check_temp.temp('rad').get_temp() outsideTemp = check_temp.temp('outside').get_temp() conn_string = prop('database') conn = psycopg2.connect(conn_string) cursor = conn.cursor() sql = """ insert into log (room_temp, rad_temp, outside_temp, datestamp, state) values (%(roomtemp)s, %(radtemp)s, %(outsidetemp)s, (select now()), %(state)s) """ cursor.execute(sql, {'roomtemp':roomTemp, 'radtemp':radTemp, 'outsidetemp':outsideTemp, 'state':shed_state}) conn.commit()