Exemple #1
0
def process_probe_sync(probe_sync):
    """ Processes a probe sync request.

    """

    probe_id = probe_sync.probe_id

    # Persist information about the probe and this sync
    update_probe_status(probe_id, probe_sync.sync_count)

    # Persist any sensor data
    persist_sensor_data(probe_id, probe_sync.sensor_data)

    # Persist any actuator history
    # TODO...

    # Build response.  Note that curr_time will be off by remaining
    # control server processing, one way latency and probe processing.
    now = date_util.get_current_timestamp()
    response = {
        "probe_id" : probe_sync.probe_id,
        "curr_time" : now
    }

    return response
Exemple #2
0
def record_sensor_data(sensor_id, value):

    data_point = {
        "id" : sensor_id,
        "timestamp" : date_util.get_current_timestamp(),
        "value" : value
    }

    if verbose:
        print json.dumps(data_point, indent=1)

    sensor_data.append(data_point)
Exemple #3
0
def send_probe_sync_request():
    global sync_count

    url = "http://%s:%d/probe_sync" %\
        (control_server_hostname, control_server_port)
    print " **********************************************************"
    print " > Sending probe sync request to: %s with token [%s]" % (url, token)

    request_content = {
        "probe_id" : probe_id,
        "token" : token,
        "connection_attempts" : 1,
        "sensor_freq" : sensor_freq,
        "sync_freq" : sync_freq,
        "sync_count" : sync_count,
        "curr_time" : date_util.get_current_timestamp(),
        "sensor_data" : sensor_data
    }

    # Print Request
    print "---- Request ----"
    print json.dumps(request_content, indent=1)

    request = urllib2.Request(url)
    request.add_header('Content-Type', 'application/json')

    response = urllib2.urlopen(request, json.dumps(request_content))
    response_content_str = response.read()
    response_content = json.loads(response_content_str)

    # Clear sensor data
    del sensor_data[:]
    sync_count += 1

    # Print Response
    print ""
    print "---- Response ----"
    print "%s" % response_content_str
    print ""
    if verbose:
        print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>> probe_sync"
        print request
        print request.json
        print "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"

    # Validate probe sync request token
    if not probe_sync.token == token:
        print "[WARN] Connection attempt by probe '%s' from %s with invalid token." %\
            (probe_sync.probe_id, request.remote_addr)
        abort(401)

    # Compare probe's time with Control Server time.  Log if the
    # difference is significant.  If the probe's current time is 0,
    # that indicates that time hasn't yet been set on the probe.
    time_diff = date_util.get_current_timestamp() - probe_sync.curr_time
    if probe_sync.curr_time > 0 and math.fabs(time_diff) > time_diff_threshold:
        print "[WARN] Probe time is off by %ds" % time_diff

    # If the probe tried to connect to the Control Server more than
    # once, then log the the failed connection attemps
    if probe_sync.connection_attempts > 1:
        print "[WARN] Probe '%s' attemped to connect %d times" %\
            (probe_sync.probe_id, probe_sync.connection_attempts)

    response = probe_service.process_probe_sync(probe_sync)

    if verbose:
        print response
        print "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"