def test_modify_instruction(self):
     instruction = Instruction(1, 99, 1, time.time() - 200)
     brew_logic.store_instruction_for_unique_time(instruction)        
     json = '{ "description" : "modified description", "target_temperature_C" : "15.5", "from_timestamp" : "1000"}'        
     response = self._call_PUT_with_credentials_and_body('http://localhost:8080/chestfreezer/api/instruction/1', json, 'application/json')[0]
     assert(response.status == 204)        
     assert(db_adapter.get_instruction_by_id('1').from_timestamp == 1000)
     assert(db_adapter.get_instruction_by_id('1').description == 'modified description')
     assert(db_adapter.get_instruction_by_id('1').target_temperature_C == 15.5)
 def test_add_instruction_overlap(self):
     instruction1 = Instruction(1, 10, time.time() - 600, time.time(), 'overlap instruction 1')
     instruction2 = Instruction(2, 30, time.time() - 1200, time.time() - 400, 'overlap instruction 2')
     brew_logic.store_instruction_for_unique_time(instruction1)
     try:        
         brew_logic.store_instruction_for_unique_time(instruction2)
         assert(False)
     except InstructionException:
         # all good, test succeed
         pass
 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 test_add_modify_delete_instruction(self):
     instruction = Instruction(None, 10, time.time() - 600, time.time(), 'Test Instruction')        
     brew_logic.store_instruction_for_unique_time(instruction)        
     instructions = db_adapter.get_all_instructions()
     assert(len(instructions) == 1)        
     new_description = 'New Description, Bla Bla Bla'
     old_instruction = db_adapter.get_all_instructions()[0]
     old_instruction.description = new_description        
     brew_logic.store_instruction_for_unique_time(old_instruction)
     instructions = db_adapter.get_all_instructions()        
     assert(len(instructions) == 1)
     assert(instructions[0].description == new_description)
     db_adapter.delete_instruction(old_instruction.instruction_id)
     assert(len(db_adapter.get_all_instructions()) == 0)
def create_instruction():
    if _get_parameter_value('instruction_id') is not None:
        abort(400, 'Instruction ID cannot be set directly.')
    target_temperature_C = float(_get_parameter_value("target_temperature_C", True))
    from_timestamp = _get_timestamp_parameter('from_timestamp', True)
    to_timestamp = _get_timestamp_parameter('to_timestamp', True)
    description = _get_parameter_value('description')
    try:
        new_instruction = Instruction(None, target_temperature_C, from_timestamp, to_timestamp, description)
        before_ids = db_adapter.get_all_instruction_ids()
        brew_logic.store_instruction_for_unique_time(new_instruction)
        after_ids = db_adapter.get_all_instruction_ids()    
        new_id = list(set(after_ids) - set(before_ids))[0]
        response.status = 201
        return '{ "instruction_id" : "' + new_id + '" }'  # not really RESTful, maybe will fix at some point
    except InstructionException as e:        
        response.status = 400
        return str(e) # this is the correct way to return errors it seems
 def test_apply_instruction(self):
     temperature_probes._last_master_reading = 24.565
     brew_logic.configuration.set_instruction_interval_seconds(0.1)
     brew_logic.configuration.set_control_temperature_interval_seconds(0.5)                
     brew_logic.start_instruction_thread()
     brew_logic.start_temperature_control_thread()        
     instruction = Instruction(1, 10, time.time(), time.time() + 600, 'Test Instruction')        
     brew_logic.store_instruction_for_unique_time(instruction)
     time.sleep(1)
     assert(brew_logic.freezer_state)
     assert(not brew_logic.heater_state)
     temperature_probes._last_master_reading = 9.4  # now devices should stop
     time.sleep(1)        
     assert(not brew_logic.freezer_state)
     assert(brew_logic.heater_state)
     temperature_probes._last_master_reading = 10.5  # now devices should stop
     time.sleep(1)
     assert(not brew_logic.freezer_state)
     assert(not brew_logic.heater_state)
def modify_instruction(instruction_id):
    print 'Updating instruction #' + instruction_id + '...'
    original_instruction = _get_instruction(instruction_id)
    if _get_parameter_value('instruction_id') is not None:
        abort(400, 'Instruction ID cannot be modified')    
    try:
        if _get_parameter_value('target_temperature_C') is not None:
                original_instruction.target_temperature_C = float(_get_parameter_value('target_temperature_C'))            
        original_instruction.set_from_timestamp_safe(_get_timestamp_parameter('from_timestamp'))        
        original_instruction.set_to_timestamp_safe(_get_timestamp_parameter('to_timestamp'))        
        if _get_parameter_value('description') is not None:
            original_instruction.description = str(_get_parameter_value('description')).strip()        
    except (ValueError, TypeError):
        abort(400, 'One or more of the parameter values where malformed')
    try:        
        print str(original_instruction)
        brew_logic.store_instruction_for_unique_time(original_instruction)        
        print 'Modified instruction #' + instruction_id
        response.status = 204
    except InstructionException as e:
        response.status = 400
        return str(e)         
 def test_get_instructions(self):
     instruction1 = Instruction(1, 15, time.time() - 600, time.time() - 100, 'Test instruction 15C')
     instruction2 = Instruction(2, 0, time.time() - 1200, time.time() - 600, 'Test instruction 0C')
     instruction3 = Instruction(3, 7, time.time() - 50, time.time() + 2000, 'Test instruction 7C')
     brew_logic.store_instruction_for_unique_time(instruction1)
     brew_logic.store_instruction_for_unique_time(instruction2)
     brew_logic.store_instruction_for_unique_time(instruction3)
     response, content = self._call_GET_with_credentials('http://localhost:8080/chestfreezer/api/instruction')
     assert(response.status == 200)
     data = json.loads(content)        
     assert(len(data) == 3)
     response, content = self._call_GET_with_credentials('http://localhost:8080/chestfreezer/api/instruction?now')
     assert(response.status == 200)        
     data = json.loads(content)                        
     assert(len(data) == 5)  # its not a list, its one element / 5 fields
     assert(data.get('instruction_id') == instruction3.instruction_id)
     start = str(int(time.time() - 1000))
     end = str(int(time.time() - 200))
     response, content = self._call_GET_with_credentials('http://localhost:8080/chestfreezer/api/instruction?start=' + start + '&end=' + end)        
     assert(response.status == 200)
     data = json.loads(content)
     assert(len(data) == 2)
     assert(data[0].get('instruction_id') == instruction1.instruction_id)
     assert(data[1].get('instruction_id') == instruction2.instruction_id)
 def test_delete_instruction(self):
     instruction = Instruction(1, 15, time.time() - 600, time.time() + 6000, 'Bla bla')
     brew_logic.store_instruction_for_unique_time(instruction)        
     response = self._call_DELETE_with_credentials('http://localhost:8080/chestfreezer/api/instruction/1')[0]
     assert(response.status == 204)        
     assert(len(db_adapter.get_all_instructions()) == 0)