Пример #1
0
def get_device_telemetry(deviceID):
    dbAccess = DatabaseAccess()
    period = int(request.form.get("dataPeriod"))

    func = request.form.get("function")
    key = request.form.get("key")
    count = int(request.form.get("count"))

    if (not period) or (not func) or (not key) or (not count):
        return 'an item of data is missing'

    if not dbAccess.device_exists(deviceID):
        return 'device does not exist. no data to return.'

    flooredPeriod = period // 5
    returnedFromDB = dbAccess.telemetry_get(deviceID, flooredPeriod)
    getData = analytics.get(func, returnedFromDB, key, count)
    print(getData)
    return str(getData)
Пример #2
0
def aggregate_device_telemetry(deviceID):
    """
    returns an aggregate of a specified number of datapoints from a specified deviceID

    Form:
        dataPeriod: an integer containing the period (in seconds) that the aggregation is to carry out on

    :param
        deviceID: a string containing the ID of a device

    :return:
        'no dataPeriod present, no functions will be carried out.' will be returned if dataPeriod is missing from the form.
        'device does not exist. no data to return.' will be returned if the device does not exist in the DB.
        aggregatedDict: a dictionary of aggregated data.
    """

    dbAccess = DatabaseAccess()

    if not request.form.get('dataPeriod'):
        return 'no dataPeriod present, no functions will be carried out.'

    period = int(request.form.get("dataPeriod"))
    # floored division allows us to work out how many data points to retrieve
    flooredPeriod = period // 5

    if not dbAccess.device_exists(deviceID):
        return 'device does not exist. no data to return.'

    returnedFromDB = dbAccess.telemetry_get(deviceID, flooredPeriod)
    # Data is descending from db, not ascending. needs fixing.
    #print(returnedFromDB,"\n\n")
    aggregatedData = analytics.aggregate(returnedFromDB)
    for dict in aggregatedData:
        dict["_id"] = deviceID
    #print(aggregatedData)
    # cannot be returned in a list
    aggregatedDict = {
        i: aggregatedData[i]
        for i in range(0, len(aggregatedData))
    }
    print(aggregatedDict)
    # this data is slightly incorrect
    return aggregatedDict
Пример #3
0
def device_telemetry(deviceID):
    """
    Retrieves telemetry data up to a specified limit.

    GET: will query the PyMongo container of the relevant deviceID and return the specified number of datapoints.

    Form:
        limit: an integer value specifying how many datapoints to return.

    :param
        deviceID: a string containing deviceID.

    :return
        'no limit present, no functions will be carried out.' is displayed if limit is missing from form data.
        'device does not exist. no data to return.' is displayed if the device with the requested ID does not exist.
        telemetryDict is a dictionary of dictionaries containing requested data.
    """
    if not request.form.get('limit'):
        return 'no limit present, no functions will be carried out.'

    limit = int(request.form.get("limit"))
    dbAccess = DatabaseAccess()
    if not dbAccess.device_exists(deviceID):
        return 'device does not exist. no data to return.'

    returnedFromDB = dbAccess.telemetry_get(deviceID, limit)

    for telemetryReadings in returnedFromDB:
        telemetryReadings.update((key, deviceID)
                                 for key, value in telemetryReadings.items()
                                 if key == "_id")

    telemetryDict = {
        i: returnedFromDB[i]
        for i in range(0, len(returnedFromDB))
    }

    print(telemetryDict)
    return telemetryDict
Пример #4
0
def argget_device_telemetry(deviceID):
    dbAccess = DatabaseAccess()
    period = int(request.form.get("dataPeriod"))

    func = request.form.get("function")
    key = request.form.get("key")
    count = int(request.form.get("count"))

    if (not period) or (not func) or (not key) or (not count):
        return 'an item of data is missing'

    if not dbAccess.device_exists(deviceID):
        return 'device does not exist. no data to return.'

    flooredPeriod = period // 5
    returnedFromDB = dbAccess.telemetry_get(deviceID, flooredPeriod)
    arggetData = analytics.argget(func, returnedFromDB, key, count)
    for dict in arggetData:
        dict["_id"] = deviceID

    arggetDict = {i: arggetData[i] for i in range(0, len(arggetData))}
    print(arggetDict)
    return arggetDict