Beispiel #1
0
def recomputeOccupancies(sensorId):
    sensorObj = SensorDb.getSensorObj(sensorId)
    if sensorObj.getSensorStatus() == PURGING:
        return {"status": "NOK", "ErrorMessage": "Sensor is PURGING"}
    elif sensorObj.getSensorStatus() == RECOMPUTING:
        return {"status": "NOK", "ErrorMessage": "Sensor is RECOMPUTING"}
    else:
        SensorDb.setSensorStatus(sensorId, RECOMPUTING)

    return {"status": "OK", "sensors": SensorDb.getAllSensors()}
Beispiel #2
0
def recomputeOccupanciesWorker(sensorId):
    # Clean out the summary stats.
    util.debugPrint("recomputeOccupanciesWorker " + sensorId)
    sensorObj = SensorDb.getSensorObj(sensorId)
    if sensorObj is None:
        return
    try:
        sensorObj.cleanSensorStats()
        cur = DbCollections.getDataMessages(sensorId).find()
        if cur is None or cur.count() == 0:
            return
        locationMessages = DbCollections.getLocationMessages().find(
            {SENSOR_ID: sensorId})
        for locationMessage in locationMessages:
            lid = locationMessage["_id"]
            LocationMessage.clean(locationMessage)
            util.debugPrint("Location Message " +
                            json.dumps(locationMessage, indent=4))
            DbCollections.getLocationMessages().update(
                {"_id": lid}, {"$set": locationMessage}, upsert=False)

        for jsonData in cur:
            freqRange = DataMessage.getFreqRange(jsonData)
            # TODO -- recompute the occupancies. for data message.
            DataMessage.resetThreshold(jsonData)
            dataMsgId = jsonData["_id"]
            del jsonData["_id"]
            DbCollections.getDataMessages(sensorId).update({"_id": dataMsgId},
                                                           {"$set": jsonData},
                                                           upsert=False)
            minPower = DataMessage.getMinPower(jsonData)
            maxPower = DataMessage.getMaxPower(jsonData)
            lastLocationPost = msgutils.getLocationMessage(jsonData)
            if DataMessage.getMeasurementType(jsonData) == FFT_POWER:
                minOccupancy = DataMessage.getMinOccupancy(jsonData)
                maxOccupancy = DataMessage.getMaxOccupancy(jsonData)
                meanOccupancy = DataMessage.getMeanOccupancy(jsonData)
                sensorObj.updateMinOccupancy(freqRange, minOccupancy)
                sensorObj.updateMaxOccupancy(freqRange, maxOccupancy)
                sensorObj.updateOccupancyCount(freqRange, meanOccupancy)
                LocationMessage.updateMaxBandOccupancy(lastLocationPost,
                                                       freqRange, maxOccupancy)
                LocationMessage.updateMinBandOccupancy(lastLocationPost,
                                                       freqRange, minOccupancy)
                LocationMessage.updateOccupancySum(lastLocationPost, freqRange,
                                                   meanOccupancy)
            else:
                occupancy = DataMessage.getOccupancy(jsonData)
                sensorObj.updateMinOccupancy(freqRange, occupancy)
                sensorObj.updateMaxOccupancy(freqRange, occupancy)
                sensorObj.updateOccupancyCount(freqRange, occupancy)
                LocationMessage.updateMaxBandOccupancy(lastLocationPost,
                                                       freqRange, occupancy)
                LocationMessage.updateMinBandOccupancy(lastLocationPost,
                                                       freqRange, occupancy)
                LocationMessage.updateOccupancySum(lastLocationPost, freqRange,
                                                   occupancy)
            DbCollections.getLocationMessages().update(
                {"_id": lastLocationPost["_id"]}, {"$set": lastLocationPost},
                upsert=False)
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
    finally:
        SensorDb.setSensorStatus(sensorId, ENABLED)