def removeSensor(sensorId): """ Remove a sensor. """ SessionLock.acquire() try: userSessionCount = SessionLock.getUserSessionCount() if userSessionCount != 0: return { STATUS: "NOK", "StatusMessage": "Active user session detected" } sensor = DbCollections.getSensors().find_one({SENSOR_ID: sensorId}) if sensor is None: return { STATUS: "NOK", "StatusMessage": "Sensor Not Found", "sensors": getAllSensors() } else: DbCollections.getSensors().remove(sensor) sensors = getAllSensors() return {STATUS: "OK", "sensors": sensors} finally: SessionLock.release()
def generateSysMessagesZipFile(emailAddress, dumpFileNamePrefix, sensorId, sessionId): dumpFileName = sessionId + "/" + dumpFileNamePrefix + ".txt" zipFileName = sessionId + "/" + dumpFileNamePrefix + ".zip" dirname = util.getPath(STATIC_GENERATED_FILE_LOCATION + sessionId) if not os.path.exists(dirname): os.makedirs(dirname) dumpFilePath = util.getPath(STATIC_GENERATED_FILE_LOCATION) + dumpFileName zipFilePath = util.getPath(STATIC_GENERATED_FILE_LOCATION) + zipFileName if os.path.exists(dumpFilePath): os.remove(dumpFilePath) if os.path.exists(zipFilePath): os.remove(zipFilePath) systemMessages = DbCollections.getSystemMessages().find( {SENSOR_ID: sensorId}) if systemMessages is None: util.debugPrint("generateZipFileForDownload: No system info found") return dumpFile = open(dumpFilePath, "a") zipFile = zipfile.ZipFile(zipFilePath, mode="w") try: for systemMessage in systemMessages: data = msgutils.getCalData(systemMessage) del systemMessage["_id"] if CAL in systemMessage and DATA_KEY in systemMessage[CAL]: del systemMessage[CAL][DATA_KEY] systemMessage[DATA_TYPE] = ASCII systemMessageString = json.dumps(systemMessage, sort_keys=False, indent=4) + "\n" length = len(systemMessageString) dumpFile.write(str(length)) dumpFile.write("\n") dumpFile.write(systemMessageString) if data is not None: dataString = str(data) dumpFile.write(dataString) dumpFile.write("\n") zipFile.write(dumpFilePath, arcname=dumpFileNamePrefix + ".txt", compress_type=zipfile.ZIP_DEFLATED) zipFile.close() session = SessionLock.getSession(sessionId) if session is None: os.remove(dumpFilePath) os.remove(zipFilePath) return url = Config.getGeneratedDataPath() + "/" + zipFileName watchForFileAndSendMail(emailAddress, url, zipFileName) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() finally: os.remove(dumpFilePath) zipFile.close()
def unfreezeRequestWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found", 403) return jsonify(SessionLock.freezeRelease(sessionId)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getSessionsWorker(sessionId): try: if not authentication.checkSessionId( sessionId, ADMIN, updateSessionTimer=False): return make_response("Session not found", 403) return jsonify(SessionLock.getSessions()) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def deleteSensor(sensorId): try: systemMessageCount = DbCollections.getSystemMessages().find({ SENSOR_ID: sensorId }).count() dataMessageCount = DbCollections.getDataMessages(sensorId).count() locationMessageCount = DbCollections.getLocationMessages().find({ SENSOR_ID: sensorId }).count() if systemMessageCount != 0 or dataMessageCount != 0 or locationMessageCount != 0: return { STATUS: "NOK", "ErrorMessage": "Please purge sensor before deleting it." } DbCollections.getSensors().remove({SENSOR_ID: sensorId}) sensors = getAllSensors() return {STATUS: "OK", "sensors": sensors} finally: SessionLock.release()
def generateSysMessagesZipFileForDownload(sensorId, sessionId): util.debugPrint("generateSysMessagesZipFileForDownload") systemMessage = DbCollections.getSystemMessages().find_one( {SENSOR_ID: sensorId}) if systemMessage is None: return {"status": "NOK", "ErrorMessage": "No data found"} else: emailAddress = SessionLock.getSession(sessionId)[USER_NAME] dumpFilePrefix = "dump-sysmessages-" + sensorId zipFileName = sessionId + "/" + dumpFilePrefix + ".zip" t = threading.Thread(target=generateSysMessagesZipFile, args=(emailAddress, dumpFilePrefix, sensorId, sessionId)) t.daemon = True t.start() url = Config.getGeneratedDataPath() + "/" + zipFileName # generateZipFile(sensorId,startTime,days,minFreq,maxFreq,dumpFileNamePrefix,sessionId) return {STATUS: "OK", "dump": zipFileName, URL: url}
def resetNoiseFloor(sensorId, noiseFloor): if SessionLock.isAcquired(): return { "status": "NOK", "StatusMessage": "Session is locked. Try again later." } SessionLock.acquire() try: dataMessages = DbCollections.getDataMessages(sensorId).find( {SENSOR_ID: sensorId}) if dataMessages is None: return {"status": "OK", "StatusMessage": "No Data Found"} SessionLock.release() for jsonData in dataMessages: DbCollections.getDataMessages(sensorId).update( {"_id": jsonData["_id"]}, {"$set": jsonData}, upsert=False) finally: SessionLock.release() return {"status": "OK", "sensors": SensorDb.getAllSensors()}
def purgeSensor(sensor): try: sensorId = sensor.getSensorId() util.debugPrint("SensorDb::purgeSensor " + sensor.getSensorId()) userSessionCount = SessionLock.getUserSessionCount() sensor = getSensorObj(sensorId) if sensor.getSensorStatus() == "ENABLED": return restartSensor(sensorId) systemMessages = DbCollections.getSystemMessages().find( {SENSOR_ID: sensorId}) # The system message can contain cal data. for systemMessage in systemMessages: msgutils.removeData(systemMessage) DbCollections.getSystemMessages().remove({SENSOR_ID: sensorId}) dataMessages = DbCollections.getDataMessages(sensorId).find( {SENSOR_ID: sensorId}) # remove data associated with data messages. for dataMessage in dataMessages: msgutils.removeData(dataMessage) DbCollections.getDataMessages(sensorId).remove({SENSOR_ID: sensorId}) DbCollections.dropDataMessages(sensorId) # remove the capture events. DbCollections.getCaptureEventDb(sensorId).remove({SENSOR_ID: sensorId}) # Location messages contain no associated data. DbCollections.getLocationMessages().remove({SENSOR_ID: sensorId}) # Clean the sensor. sensor.cleanSensorStats() for dataMessage in DbCollections.getUnprocessedDataMessages( sensorId).find(): msgutils.removeData(dataMessage) DbCollections.dropUnprocessedDataMessages(sensorId) setSensorStatus(sensorId, ENABLED) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info())
def getSensorData(ws): """ Handle sensor data streaming requests from the web browser. """ try: util.debugPrint("DataStreamng:getSensorData") global memCache if memCache is None: memCache = MemCache() token = ws.receive() print "token = ", token parts = token.split(":") if parts is None or len(parts) < 5: ws.close() return sessionId = parts[0] if not authentication.checkSessionId(sessionId, "user"): ws.close() return import SessionLock if SessionLock.findSessionByRemoteAddr(sessionId) is not None: ws.send(dumps({"status": "Streaming session already open"})) ws.close() return sensorId = parts[1] systemToDetect = parts[2] minFreq = int(parts[3]) maxFreq = int(parts[4]) util.debugPrint("sensorId " + sensorId) memCache.incrementStreamingListenerCount(sensorId) sensorObj = SensorDb.getSensorObj(sensorId) if sensorObj is None: ws.send(dumps({"status": "Sensor not found: " + sensorId})) bandName = systemToDetect + ":" + str(minFreq) + ":" + str(maxFreq) util.debugPrint("isStreamingEnabled = " + str(sensorObj.isStreamingEnabled())) lastDataMessage = memCache.loadLastDataMessage(sensorId, bandName) key = sensorId + ":" + bandName if key not in lastDataMessage or not sensorObj.isStreamingEnabled(): ws.send( dumps({ "status": "NO_DATA: Data message not found or streaming not enabled" })) else: ws.send(dumps({"status": "OK"})) util.debugPrint("DataStreaming lastDataMessage: " + str(lastDataMessage[key])) ws.send(str(lastDataMessage[key])) lastdatatime = -1 drift = 0 while True: secondsPerFrame = sensorObj.getStreamingSecondsPerFrame() lastdataseen = memCache.loadLastDataSeenTimeStamp( sensorId, bandName) if key in lastdataseen and lastdatatime != lastdataseen[key]: lastdatatime = lastdataseen[key] sensordata = memCache.loadSensorData(sensorId, bandName) memCache.incrementDataConsumedCounter(sensorId, bandName) currentTime = time.time() lastdatasent = currentTime drift = drift + (currentTime - lastdatasent) - secondsPerFrame ws.send(sensordata[key]) # If we drifted, send the last reading again to fill in. if drift < 0: drift = 0 if drift > secondsPerFrame: if APPLY_DRIFT_CORRECTION: util.debugPrint("Drift detected") ws.send(sensordata[key]) drift = 0 sleepTime = secondsPerFrame gevent.sleep(sleepTime) except: traceback.print_exc() ws.close() util.debugPrint("Error writing to websocket") finally: memCache.decrementStreamingListenerCount(sensorId)
def runGarbageCollector(sensorId): SessionLock.acquire() try: userCount = SessionLock.getUserSessionCount() if userCount != 0: return {"status": "NOK", "ErrorMessage": "Active user session detected"} sensorObj = SensorDb.getSensorObj(sensorId) if sensorObj is None: return {"status": "NOK", "ErrorMessage": "Sensor Not found"} if sensorObj.getSensorStatus() != DISABLED: return {"status": "NOK", "ErrorMessage": "Sensor is ENABLED -- DISABLE it first"} dataRetentionDuration = sensorObj.getSensorDataRetentionDurationMonths( ) dataRetentionTime = dataRetentionDuration * 30 * SECONDS_PER_DAY cur = DbCollections.getDataMessages(sensorId).find( {SENSOR_ID: sensorId}) #dataMessages = cur.sort('t', pymongo.ASCENDING) currentTime = time.time() for msg in cur: insertionTime = Message.getInsertionTime(msg) if currentTime - dataRetentionTime >= insertionTime: DbCollections.getDataMessages(sensorId).remove(msg) msgutils.removeData(msg) else: break # Now redo our book keeping summary fields. #dataMessages = cur.sort('t', pymongo.ASCENDING) locationMessages = DbCollections.getLocationMessages().find( {SENSOR_ID: sensorId}) for locationMessage in locationMessages: insertionTime = Message.getInsertionTime(locationMessage) if currentTime - dataRetentionTime >= insertionTime: DbCollections.getLocationMessages().remove(msg) else: LocationMessage.clean(locationMessage) DbCollections.getLocationMessages().update( {"_id": locationMessage["_id"]}, {"$set": locationMessage}, upsert=False) sensorObj.cleanSensorStats() # Update the summary statistics. cur = DbCollections.getDataMessages(sensorId).find( {SENSOR_ID: sensorId}) for jsonData in cur: freqRange = DataMessage.getFreqRange(jsonData) minPower = DataMessage.getMinPower(jsonData) maxPower = DataMessage.getMaxPower(jsonData) messageId = DataMessage.getLocationMessageId(jsonData) lastLocationPost = DbCollections.getLocationMessages().find_one({"_id": messageId}) 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) sensorObj.updateTime(freqRange, Message.getTime(jsonData)) sensorObj.updateDataMessageTimeStamp(Message.getTime(jsonData)) SensorDb.updateSensor(sensorObj.getJson(), False, False) DbCollections.getLocationMessages().update( {"_id": lastLocationPost["_id"]}, {"$set": lastLocationPost}, upsert=False) # Garbage collect the unprocessed data messages. cur = DbCollections.getUnprocessedDataMessages(sensorId).find({SENSOR_ID: sensorId}) if cur is not None: dataMessages = cur.sort('t', pymongo.ASCENDING) for msg in dataMessages: insertionTime = Message.getInsertionTime(msg) if currentTime - dataRetentionTime >= insertionTime: DbCollections.getUnprocessedDataMessages(sensorId).remove(msg) else: break DbCollections.dropDailyOccupancyCache(sensorId) return {"status": "OK", "sensors": SensorDb.getAllSensors()} finally: SessionLock.release()