Пример #1
0
def get_temperatures():              
    start_timestamp, end_timestamp = _get_timestamp_query_parameters()
    try:        
        start_timestamp = float(start_timestamp)
        end_timestamp = float(end_timestamp)
        seconds = end_timestamp - start_timestamp
        print 'Asked for temperature readings from ' + misc_utils.timestamp_to_datetime(start_timestamp).strftime("%c") + ' to ' + misc_utils.timestamp_to_datetime(end_timestamp).strftime("%c") + ' (' + str(int(seconds)) + ' seconds past)...',
        all_readings = db_adapter.get_temperature_readings(int(start_timestamp), int(end_timestamp))
        print 'got ' + str(len(all_readings)) + ' result(s).'
        response.content_type = 'application/json;'                
        return json_parser.get_temperature_reading_array_as_json(all_readings)    
    except (TypeError, ValueError) as e:
        abort(400, "Malformed timestamp parameter(s): " + str(e))
Пример #2
0
def get_readings(from_timestamp, to_timestamp):
    """ returns all the temperature readings from/upto the given timestamps """
    print 'Asked for readings from ' + misc_utils.timestamp_to_datetime(from_timestamp).strftime("%c") + ' to ' + misc_utils.timestamp_to_datetime(to_timestamp).strftime("%c")
    found_temperature_readings = []    

    sql_statement = "SELECT * FROM " + TEMPERATURE_READINGS_TABLE_NAME + " WHERE timestamp BETWEEN from_unixtime(" + str(from_timestamp) + ") and from_unixtime(" + str(to_timestamp) + ")"
    cursor.execute(sql_statement);
    all_results = cursor.fetchall()
    for result in all_results:
        probe_id = result[0]
        temperature_C = result[1]                        
        timestamp = result[2].strftime("%s")            
        temperature_reading = hardware.temperature.TemperatureReading(probe_id, temperature_C, timestamp)
        found_temperature_readings.append(temperature_reading)                        
        
    return found_temperature_readings
Пример #3
0
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))
Пример #4
0
def _do_auth_check():
    """ checks the user's credential with what is in the config """        
    authorized = False
    allowed_ip = False
    ip = 'Unknown IP' 
    pretty_now_datetime = misc_utils.timestamp_to_datetime(time.time()).strftime("%c")   
    enviroment_list = request.environ
    
    if enviroment_list.get('REMOTE_ADDR') is not None:
        ip = enviroment_list.get('REMOTE_ADDR')
    if enviroment_list.get('REMOTE_ADDR') is None:        
        allowed_ip = configuration.is_ip_allowed(ip) | (ip == 'Unknown IP')    
    if not configuration.is_security_enabled(): 
        authorized = True
    elif request.auth is not None:        
        user, password = request.auth
        allowed_ip = configuration.is_ip_allowed(ip)        
        # print user + '==' + configuration.web_user() + ':' + str(user == configuration.web_user())
        # print password + '==' + configuration.web_pwd() + ':' + str(password == configuration.web_pwd())
        if (user == configuration.web_user()) & (password == configuration.web_pwd()) & (allowed_ip):    
            authorized = True    
    if authorized:
        # disabled due to spam! need  to find a smarter way for this
        #_log_and_print_security_message(pretty_now_datetime + ': Address [' + ip + '] accessed the API')
        return  # all good
    else:       
        message = pretty_now_datetime + ': Unauthorized access from [' + ip + ']'         
        if (user is not None) | (password is not None):
            message = message + ' with credentials ' + user + ':' + password
        if not allowed_ip:
            message = message + '. Reason: User was blocked due to IP restriction.'
        elif not authorized:
            message = message + '. Reason: Credentials were wrong.'
        _log_and_print_security_message(message)
        if (not authorized) | (not allowed_ip): emailer.escalate("Unauthorized access", message)
        abort(401, "This method requires basic authentication")
Пример #5
0
 def __str__(self):        
     return 'Instruction #' + self.instruction_id + ', description: "' + self.description + '", from ' + misc_utils.timestamp_to_datetime(self.from_timestamp).strftime("%c") + ' to ' + misc_utils.timestamp_to_datetime(self.to_timestamp).strftime("%c") + '. Target temperature: ' + str(self.target_temperature_C) + 'C'
Пример #6
0
 def __str__(self):
     pretty_date = misc_utils.timestamp_to_datetime(self.timestamp).strftime("%c")
     return 'From probe #' + self.probe_id + ': ' + str(self.temperature_C) + 'C/' + str(self.temperature_F) + 'F. Taken at: ' + pretty_date + "."