Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Smart Thermostat Raspberry PI - part receive temperature scenarios
"""
Downloads most recent temperature scenarios from cloud server.
First inserts new temperature scenarios in actscenario table.
Then deletes old scenarios from actscenario table. Leaves the last 3 versions.
Used by fcontroll() function.
"""
while True:
try:
facurtime = time.time()
favepoch = int(facurtime)
dbdac1 = dbc.cursor()
dbdac1.execute("SELECT min(vkey) FROM actscenario")
rows = dbdac1.fetchall()
maxrow = (rows)[0][0]
if (maxrow == None):
maxrow = '40b5af01'+'_'+'9999999999'+'_'+'9999999'
dbdac1.close()
#logging.info(maxrow)
with hpool.connection(timeout=3) as connectionacts:
tableacts = connectionacts.table('hactscenario')
hscanacts = tableacts.scan(row_stop=maxrow)
dbdac2 = dbc.cursor()
for key, data in hscanacts:
logging.info(str(key))
dbdac2.execute("INSERT INTO actscenario (vkey, vgroup, viepoch, vtempdif, vouttempdif, run0, run1, run2, run3, run4, run5, vscore) VALUES('%s',%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i)" % (str(key),int(data['fd:group']),int(data['fd:iepoch']),int(data['fd:tempdif']),int(data['fd:outtempdif']),int(data['fd:run0']),int(data['fd:run1']),int(data['fd:run2']),int(data['fd:run3']),int(data['fd:run4']),int(data['fd:run5']),int(data['fd:score']) ) )
dbdac2.close()
#delete all scenario except for the last 3 versions
dbdac3 = dbc.cursor()
dbdac3.execute("DELETE FROM actscenario WHERE viepoch NOT IN (SELECT distinct viepoch FROM actscenario order by viepoch desc LIMIT 3)")
dbdac3.close()
except Exception:
logging.exception("fgetactscenario")
time.sleep(650)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment