def test_instructions_for_times(self):
     instruction1 = Instruction(1, 10, time.time() - 599, time.time() - 100, 'Test instruction low')
     instruction2 = Instruction(2, 30, time.time() - 1200, time.time() - 600, 'Test instruction high')
     brew_logic.store_instruction_for_unique_time(instruction1)        
     brew_logic.store_instruction_for_unique_time(instruction2)        
     assert(len(db_adapter.get_all_instructions()) == 2)        
     assert(len(db_adapter.get_instructions()) == 0)        
     results = db_adapter.get_instructions(time.time() - 300, time.time() - 300)        
     assert(instruction1.target_temperature_C == results[0].target_temperature_C)        
     results = db_adapter.get_instructions(time.time() - 1300, time.time() - 650)        
     assert(instruction2.target_temperature_C == results[0].target_temperature_C)        
     results = db_adapter.get_instructions(time.time() - 700, time.time() - 200)
     assert(len(results) == 2)
def get_instructions():    
    if not request.query_string:        
        print 'Returning all instructions...'
        allInstructions = db_adapter.get_instructions(0,2147483648); #end of unix time
        response.content_type = 'application/json;'
        return json_parser.get_instruction_array_as_json(allInstructions)
    if (request.query_string is not None) & ('now' in request.query_string):
        print 'Returning instruction for current time...'
        try:
            return json_parser.get_instruction_as_json(db_adapter.get_instructions()[0])
        except IndexError:
            # no current instruction
            response.status = 204
            return ''
    start_timestamp, end_timestamp = _get_timestamp_query_parameters()
    try:
        print 'Asked for instructions from ' + misc_utils.timestamp_to_datetime(float(start_timestamp)).strftime("%c") + ' to ' + misc_utils.timestamp_to_datetime(float(end_timestamp)).strftime("%c") + '...',
        all_instructions = db_adapter.get_instructions(int(start_timestamp), int(end_timestamp))
        print 'got ' + str(len(all_instructions)) + ' result(s).'
        response.content_type = 'application/json;'                        
        return json_parser.get_instruction_array_as_json(all_instructions)    
    except (TypeError, ValueError) as e:
        abort(400, "Malformed timestamp parameter(s): " + str(e))