def update_database(): last_data = {'readings': {'temp': 0, 'humidity': 0}, 'parameters': None} while 1: update_time = timezone.now() data = (yield) # Round current temperature and humidity readings temperature_rounded = round(data["readings"]["temp"]) humidity_rounded = round(data["readings"]["humidity"]) # Compare rounded current readings against the rounded previous readings if (round(last_data["readings"]["temp"]) != temperature_rounded) or ( round(last_data["readings"]["humidity"]) != humidity_rounded): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=temperature_rounded, humidity=humidity_rounded) else: reading_record.time = update_time reading_record.temperature = temperature_rounded reading_record.humidity = humidity_rounded reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, source=0, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data
def update_database(): last_data = {'readings': {'temp': 0, 'humidity': 0}, 'parameters': None} while 1: update_time = timezone.now() data = (yield) if (round(last_data["readings"]["temp"]) != round( data["readings"]["temp"])) or (round( last_data["readings"]["humidity"]) != round( data["readings"]["humidity"])): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=data["readings"]["temp"], humidity=data["readings"]["humidity"]) else: reading_record.time = update_time reading_record.temperature = data["readings"]["temp"] reading_record.humidity = data["readings"]["humidity"] reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data
def update_database(): last_data = {'readings':{'temp':0,'humidity':0}, 'parameters':None} while 1: update_time = timezone.now() data = (yield) if (round(last_data["readings"]["temp"]) != round(data["readings"]["temp"])) or (round(last_data["readings"]["humidity"]) != round(data["readings"]["humidity"])): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=data["readings"]["temp"], humidity=data["readings"]["humidity"]) else: reading_record.time = update_time reading_record.temperature = data["readings"]["temp"] reading_record.humidity = data["readings"]["humidity"] reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data
def update_database(): last_data = {'readings':{'temp':0,'humidity':0}, 'parameters':None} while 1: update_time = timezone.now() data = (yield) # Round current temperature and humidity readings temperature_rounded = round(data["readings"]["temp"]) humidity_rounded = round(data["readings"]["humidity"]) # Compare rounded current readings against the rounded previous readings if (round(last_data["readings"]["temp"]) != temperature_rounded) or (round(last_data["readings"]["humidity"]) != humidity_rounded): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=temperature_rounded, humidity=humidity_rounded) else: reading_record.time = update_time reading_record.temperature = temperature_rounded reading_record.humidity = humidity_rounded reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, source=0, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data