def follow_instructions(): global has_escalated has_escalated = False while True: global instruction_thread_in_waiting instruction_thread_in_waiting = False try: instructions = database.db_adapter.get_all_instructions() if len(instructions) > 1: pretty_date = misc_utils.get_storeable_datetime_timestamp(time.time()) instructions_string = ',\n'.join(map(str, instructions)) message = "More than one instruction for time " + pretty_date + ":\n" + instructions_string if not has_escalated: emailer.escalate("Instruction error", message) global has_escalated has_escalated = True print message elif len(instructions) == 1: global current_instruction_id instruction = instructions[0] if (current_instruction_id != instruction.instruction_id): current_instruction_id = instruction.instruction_id global instruction_target_temperature_C instruction_target_temperature_C = instruction.target_temperature_C print 'Applying instruction #' + instruction.instruction_id + ', setting target temperature to: ' + str(instruction_target_temperature_C) + "C" elif len(instructions) == 0: global current_instruction_id current_instruction_id = None except Exception as e: print 'Error while looking at instructions:\n' + str(e) instruction_thread_in_waiting = True time.sleep(configuration.instruction_interval_seconds())
def _store_instruction(instruction): """ stores or updates the given instruction. This is not safe! Do not call this directly, unless you know what you are doing. Use the brew_logic method that safely stores instructions """ from_timestamp = misc_utils.get_storeable_datetime_timestamp(instruction.from_timestamp) to_timestamp = misc_utils.get_storeable_datetime_timestamp(instruction.to_timestamp) cursor.execute("SELECT * FROM " + INSTRUCTIONS_TABLE_NAME + " WHERE instruction_id='" + instruction.instruction_id + "'") results = cursor.fetchall() if len(results) == 0: if _is_memory_db(): insert_sql = "INSERT INTO " + INSTRUCTIONS_TABLE_NAME + " VALUES ('" + str(instruction.instruction_id) + "','" + str(instruction.target_temperature_C) + "','" + from_timestamp + "','" + to_timestamp + "','" + instruction.description + "')" else: insert_sql = "INSERT INTO " + INSTRUCTIONS_TABLE_NAME + " (instruction_target_temperature_C,from_timestamp,to_timestamp,description) VALUES ('" + str(instruction.target_temperature_C) + "','" + from_timestamp + "','" + to_timestamp + "','" + instruction.description + "')" cursor.execute(insert_sql) elif len(results) == 1: update_sql = "UPDATE " + INSTRUCTIONS_TABLE_NAME + " SET instruction_target_temperature_C='" + str(instruction.target_temperature_C) + "',from_timestamp='" + from_timestamp + "',to_timestamp='" + to_timestamp + "',description='" + instruction.description + "'" cursor.execute(update_sql) db.commit()
def store_temperatures(temperature_readings): """ stores the given list of temperature readings """ for temperature_reading in temperature_readings: probe_id = temperature_reading.probe_id temperature_C = temperature_reading.temperature_C timestamp = misc_utils.get_storeable_datetime_timestamp(temperature_reading.timestamp) sql_statement = "INSERT INTO " + TEMPERATURE_READINGS_TABLE_NAME + " VALUES ('" + probe_id + "','" + str(temperature_C) + "','" + timestamp + "')" cursor.execute(sql_statement) db.commit()