def getSensorLocationInfo(sensorId):
    """
    Get sensor location information. This returns all the Loc messages posted by the sensor.
    """
    util.debugPrint("getSensorLocationInfo")
    try:
        cur = DbCollections.getLocationMessages().find({SENSOR_ID: sensorId})
        cur.batch_size(20)
        retval = {}
        locations = []
        for c in cur:
            location = {}
            location['Lat'] = c['Lat']
            location['Lon'] = c['Lon']
            location['Alt'] = c['Alt']
            location['t'] = c['t']
            location['bandInfo'] = c['bandInfo']
            locations.append(location)
        retval["locations"] = locations
        retval[STATUS] = "OK"
        return retval
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        raise
    return retval
def denyAccount(email, token, urlPrefix):
    # If the admin denies the account creation,
    # The system will send the user a "we regret to inform you..." email that their account
    # was denied.
    AccountLock.acquire()
    try:
        tempAccounts = DbCollections.getTempAccounts()
        account = tempAccounts.find_one({
            ACCOUNT_EMAIL_ADDRESS: email,
            TEMP_ACCOUNT_TOKEN: token
        })
        if account is None:
            util.debugPrint(
                "Token not found for email address; invalid request")
            return False
        else:
            # remove email from tempAccounts:
            tempAccounts.remove({"_id": account["_id"]})
            # email user to let them know that their request was denied:
            t = threading.Thread(target=generateUserDenyAccountEmail,
                                 args=(email, urlPrefix))
            t.daemon = True
            t.start()
            return True
    except:
        return False
    finally:
        AccountLock.release()
def getLocationInfo():
    """
    Get all the location and system messages that we know about.
    """
    util.debugPrint("getLocationInfo")
    try:
        cur = DbCollections.getLocationMessages().find({})
        cur.batch_size(20)
        retval = {}
        locationMessages = []
        sensorIds = sets.Set()
        for c in cur:
            (c["tStartLocalTime"],
             c["tStartLocalTimeTzName"]) = timezone.getLocalTime(
                 c["t"], c[TIME_ZONE_KEY])
            del c["_id"]
            locationMessages.append(c)
            sensorIds.add(c[SENSOR_ID])
        retval["locationMessages"] = locationMessages
        systemMessages = []
        for sensorId in sensorIds:
            systemMessage = DbCollections.getSystemMessages().find_one(
                {SENSOR_ID: sensorId})
            # Issue 139
            if systemMessage is not None:
                del systemMessage["_id"]
                systemMessages.append(systemMessage)
        retval["systemMessages"] = systemMessages
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        raise
    return retval
def generateZipFileForDownload(sensorId, startTime, days, sys2detect, minFreq,
                               maxFreq, sessionId):
    """
    Prepare a zip file for download.
    """
    try:
        util.debugPrint("generateZipFileForDownload: " + sensorId + " startTime = " + str(startTime) +
                        " days " + str(days) + " sys2detect " + sys2detect + " minFreq " + str(minFreq) +
                        " maxFreq " + str(maxFreq))
        if not checkForDataAvailability(sensorId, startTime, days, sys2detect,
                                        minFreq, maxFreq):
            util.debugPrint("No data found")
            retval = {STATUS: "NOK", "StatusMessage": "No data found"}
        else:
            dumpFileNamePrefix = "dump-" + sensorId + "." + str(
                minFreq) + "." + str(maxFreq) + "." + str(
                    startTime) + "." + str(days)
            zipFileName = sessionId + "/" + dumpFileNamePrefix + ".zip"
            t = threading.Thread(target=generateZipFile,
                                 args=(sensorId, startTime, days, sys2detect,
                                       minFreq, maxFreq, dumpFileNamePrefix,
                                       sessionId))
            t.daemon = True
            t.start()
            url = Config.getGeneratedDataPath() + "/" + zipFileName
            # generateZipFile(sensorId,startTime,days,minFreq,maxFreq,dumpFileNamePrefix,sessionId)
            retval = {STATUS: "OK", "dump": zipFileName, URL: url}
        return retval
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        raise
def resetPassword(accountData):
    """
    Reset the password.
    """
    util.debugPrint("resetPassword " + str(accountData))
    userName = accountData[ACCOUNT_EMAIL_ADDRESS].strip()
    password = accountData[ACCOUNT_PASSWORD]
    privilege = accountData[ACCOUNT_PRIVILEGE]
    passwordHash = Accounts.computeMD5hash(password)
    existingAccount = DbCollections.getAccounts().find_one({
        ACCOUNT_EMAIL_ADDRESS:
        userName,
        ACCOUNT_PASSWORD:
        passwordHash
    })
    if existingAccount is None:
        return {STATUS: "NOK", ERROR_MESSAGE: "Authentication Failure"}
    elif existingAccount[ACCOUNT_LOCKED]:
        return {STATUS: "NOK", ERROR_MESSAGE: "Account Locked"}
    else:
        newPassword = accountData[ACCOUNT_NEW_PASSWORD]
        newPasswordHash = Accounts.computeMD5Hash(newPassword)
        result = Accounts.isPasswordValid(newPassword)
        if result[0] != "OK":
            return Accounts.packageReturn(result)
        else:
            existingAccount[ACCOUNT_PASSWORD] = newPasswordHash
            DbCollections.getAccounts().update({"_id": existingAccount["_id"]},
                                               {"$set": existingAccount},
                                               upsert=False)
            return {STATUS: "OK"}
Exemplo n.º 6
0
def getData(msg):
    """
    get the data associated with a data message.
    """
    fs = gridfs.GridFS(DbCollections.getSpectrumDb(), msg[SENSOR_ID] + "_data")
    messageBytes = fs.get(ObjectId(msg[Defines.DATA_KEY])).read()
    nM = int(msg["nM"])
    n = int(msg["mPar"]["n"])
    lengthToRead = nM * n  #int(nM * n)
    if lengthToRead == 0:
        util.debugPrint("No data to read")
        return None
    if msg[DATA_TYPE] == ASCII:
        powerVal = eval(messageBytes)
    elif msg[DATA_TYPE] == BINARY_INT8:
        powerVal = np.array(np.zeros(n * nM))
        for i in range(0, int(lengthToRead)):
            powerVal[i] = float(struct.unpack('b', messageBytes[i:i + 1])[0])
    elif msg[DATA_TYPE] == BINARY_INT16:
        powerVal = np.array(np.zeros(n * nM))
        for i in range(0, lengthToRead, 2):
            powerVal[i] = float(struct.unpack('h', messageBytes[i:i + 2])[0])
    elif msg[DATA_TYPE] == BINARY_FLOAT32:
        powerVal = np.array(np.zeros(n * nM))
        for i in range(0, lengthToRead, 4):
            powerVal[i] = float(struct.unpack('f', messageBytes[i:i + 4])[0])
    return list(powerVal)
def restartService(service, sessionId):
    """

    Restart specified service
    URL Path:
        sessionId the session Id of the login in session.

    URL Args: None

    Request Body:
        A String of the name of the service

    """
    try:
        util.debugPrint("restartService " + str(service))
        if not authentication.checkSessionId(sessionId, ADMIN):
            abort(403)
        util.debugPrint("passed authentication")
        if restartThisService(service):
            return jsonify({"status": "OK"})
        else:
            return jsonify({
                "status": "NOK",
                "ErrorMessage": "Unknown service"
            })
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
def thisServiceStatus(service):
    try:
        if service in SERVICE_NAMES:
            output = subprocess.Popen(["service", service, "status"],
                                      stdout=subprocess.PIPE)
            statusRawInit, errorStr = output.communicate()
            if not errorStr is None:
                util.debugPrint("Error String detected (status): " +
                                str(errorStr))
                return {STATUS: NOK, ERROR_MESSAGE: errorStr}
            statusRaw = statusRawInit.split()
            util.debugPrint("statusRaw: " + str(statusRaw))
            if "running" in statusRaw:
                return {STATUS: OK, SERVICE_STATUS: "Running"}
            elif "stopped" in statusRaw:
                return {STATUS: OK, SERVICE_STATUS: "Stopped"}
            else:
                return {STATUS: OK, SERVICE_STATUS: "UNKNOWN"}
        else:
            util.errorPrint(service + " does not match a service")
            return {
                STATUS: NOK,
                ERROR_MESSAGE: service + " does not match a service"
            }

    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
def runForensics(sensorId, algorithm, timestamp, sessionId):
    """
    Run forensics at the sensor. This just relays the command to the sensor. The sensor will post back
    after the processing is done.

    timestamp -- the timestamp of the capture.
    algorithm -- the algortithm  to appy (from the toolbox that lives on the sensor)
    sessionId -- the login session id.

    """
    try:
        if not authentication.checkSessionId(sessionId, USER):
            util.debugPrint("runForensics - request body not found")
            abort(403)
        command = {
            "sensorId": sensorId,
            "timestamp": int(timestamp),
            "algorithm": algorithm,
            "command": "analyze"
        }
        sendCommandToSensor(sensorId, json.dumps(command))
        return jsonify({STATUS: OK})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
Exemplo n.º 10
0
    def addESAgentWorker(sessionId):
        try:
            if not authentication.checkSessionId(sessionId, ADMIN):
                abort(403)
            requestStr = request.data
            agentConfig = json.loads(requestStr)
            agentName = agentConfig["agentName"]

            bad_chars = invalid_chars.intersection(agentName)
            if bad_chars:
                msg = "Invalid character in agentName: {}"
                util.debugPrint(msg.format(bad_chars))
                abort(400)

            key = agentConfig["key"]
            bad_chars = invalid_chars.intersection(key)
            if bad_chars:
                msg = "Invalid character in key: {}"
                util.debugPrint(msg.format(bad_chars))
                abort(400)

            Config.addESAgent(agentName, key)
            agents = Config.getESAgents()
            retval = {"esAgents": agents}
            retval[STATUS] = 'OK'
            return jsonify(retval)
        except:
            print "Unexpected error:", sys.exc_info()[0]
            print sys.exc_info()
            traceback.print_exc()
            util.logStackTrace(sys.exc_info())
            raise
Exemplo n.º 11
0
def log():
    if DebugFlags.debug:
        data = request.data
        jsonValue = json.loads(data)
        if "message" in jsonValue:
            message = jsonValue["message"]
        else:
            message = ""

        if "ExceptionInfo" in jsonValue:
            exceptionInfo = jsonValue["ExceptionInfo"]
        else:
            exceptionInfo = {}

        if len(exceptionInfo) != 0:
            util.errorPrint("Client Log Message: " + message)
            util.errorPrint("Client Exception Info:")
            for i in range(0, len(exceptionInfo)):
                util.errorPrint("Exception Message:")
                exceptionMessage = exceptionInfo[i]["ExceptionMessage"]
                util.errorPrint("Client Stack Trace:")
                stackTrace = exceptionInfo[i]["StackTrace"]
                util.errorPrint(exceptionMessage)
                decodeStackTrace(stackTrace)
            if "Traceback" in jsonValue:
                traceback = jsonValue["Traceback"]
                util.errorPrint("Traceback: " + traceback)
        else:
            util.debugPrint("Client Log Message: " + message)

    return "OK"
Exemplo n.º 12
0
def deleteCaptureEvents(sensorId, startDate, sessionId):
    """
    Delete the events from the capture db.
    Send a message to the sensor to do the same.
    """
    try:
        if not authentication.checkSessionId(sessionId, ADMIN):
            util.debugPrint("deleteCaptureEvents: failed authentication")
            abort(403)
        sdate = int(startDate)
        if sdate < 0:
            util.debugPrint("deleteCaptureEvents: illegal param")
            abort(400)
        else:
            CaptureDb.deleteCaptureDb(sensorId, sdate)
            global memCache
            if memCache is None:
                memCache = MemCache()
            command = json.dumps({
                "sensorId": sensorId,
                "timestamp": sdate,
                "command": "garbage_collect"
            })
            sendCommandToSensor(sensorId, command)
            return jsonify({STATUS: "OK"})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
def watchForFileAndSendMail(emailAddress, url, uri):
    """
    Watch for the dump file to appear and send an email to the user
    after it has appeared.
    """
    for i in range(0, 100):
        filePath = util.getPath(STATIC_GENERATED_FILE_LOCATION + uri)
        if os.path.exists(filePath) and os.stat(filePath).st_size != 0:
            message = "This is an automatically generated message.\n"\
                      + "The requested data has been generated.\n"\
                      + "Please retrieve your data from the following URL: \n"\
                      + url \
                      + "\nYou must retrieve this file within 24 hours."
            util.debugPrint(message)
            SendMail.sendMail(message, emailAddress,
                              "Your Data Download Request")
            return
        else:
            util.debugPrint("Polling for file " + filePath)
            time.sleep(10)

    message = "This is an automatically generated message.\n"\
              + "Tragically, the requested data could not be generated.\n"\
              + "Sorry to have dashed your hopes into the ground.\n"
    SendMail.sendMail(message, emailAddress, "Your Data Download Request")
Exemplo n.º 14
0
def getCaptureEvents(sensorId, startDate, dayCount, sessionId):
    """
    get the capture events for a given sensor within the specified date range.


    """
    try:
        if not authentication.checkSessionId(sessionId, USER):
            util.debugPrint("getCaptureEvents: failed authentication")
            abort(403)
        try:
            sdate = int(startDate)
            dcount = int(dayCount)
        except ValueError:
            abort(400)
        if sdate < 0 or dcount < 0:
            abort(400)
        elif dcount == 0:
            abort(400)
        return jsonify(CaptureDb.getEvents(sensorId, sdate, dcount))
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
Exemplo n.º 15
0
def getDataSummary(sensorId,
                   latitude,
                   longitude,
                   altitude,
                   tmin=None,
                   dayCount=None):
    """
    Compute and return the data summary for the sensor, given its ID and position.
    """
    locationMessage = DbCollections.getLocationMessages().find_one({
        SENSOR_ID:
        sensorId,
        LON:
        longitude,
        LAT:
        latitude,
        ALT:
        altitude
    })
    if locationMessage is None:
        util.debugPrint("Location Message not found")
        return {STATUS: "NOK", ERROR_MESSAGE: "Location Message Not Found"}
    retval = getSensorDataSummary(sensorId, locationMessage)
    if retval[STATUS] == "OK":
        retval[LOCATION_MESSAGE_ID] = str(locationMessage['_id'])
        bandStats = getDataSummaryForAllBands(sensorId,
                                              locationMessage,
                                              tmin=tmin,
                                              dayCount=dayCount)
        if bandStats[STATUS] == "OK":
            retval[BAND_STATISTICS] = bandStats["bands"]
        else:
            retval = {STATUS: "NOK", ERROR_MESSAGE: bandStats[ERROR_MESSAGE]}
    return retval
Exemplo n.º 16
0
def stochasticGradientDescent(allData, trainExamples, key, numDays):
    # hyperparameters
    numIters = 200
    eta = 0.0001

    # initialize weights
    w = getInitialWeights(key, numDays)
    loss = trainLoss(allData, trainExamples, w, numDays, key)
    util.debugPrint("initial: w = %s, loss = %.4f" %
                    (util.roundDictValues(w, 4), loss))

    for t in range(1, numIters + 1):
        oldW = copy.deepcopy(w)

        for i in trainExamples:  # loop through each training example
            # loss = sF(allData, w, i)
            gradient = sdF(allData, w, i, key, numDays)

            # gradient = gradient * eta
            util.scaleDict(gradient, eta)

            # weights = weights - gradient
            util.subtractDict(w, gradient)
            # util.debugPrint("iteration %d | example %d: w = %s" % (t, i, w))

        # print weights and loss
        loss = trainLoss(allData, trainExamples, w, numDays, key)
        # util.debugPrint("iteration %d: w = %s, loss = %.4f" % (t, util.roundDictValues(w,4), loss))

        if weightsAreSame(oldW, w):
            break

    # util.debugPrint("iteration %d: w = %s, loss = %.4f" % (t, util.roundDictValues(w,4), loss))
    return w
Exemplo n.º 17
0
def startStreamingServer(port):
    """
    Start the streaming server and accept connections.
    """
    global memCache
    if memCache is None:
        memCache = MemCache()
    soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    l_onoff = 1
    l_linger = 0
    soc.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                   struct.pack('ii', l_onoff, l_linger))
    portAssigned = False
    for p in range(port, port + 10, 2):
        try:
            print 'Trying port ', p
            soc.bind(('0.0.0.0', p))
            soc.listen(10)
            socketServerPort = p

            memCache.setSocketServerPort(p)
            portAssigned = True
            util.debugPrint("DataStreaming: Bound to port " + str(p))
            break
        except:
            print sys.exc_info()
            traceback.print_exc()
            util.debugPrint("DataStreaming: Bind failed - retry")
    if portAssigned:
        global occupancyQueue
        socketServer = startSocketServer(soc, socketServerPort)
        socketServer.start()
    else:
        util.errorPrint(
            "DataStreaming: Streaming disabled on worker - no port found.")
def deleteAccount(emailAddress):

    AccountLock.acquire()
    try:
        numAdmin = numAdminAccounts()
        util.debugPrint("delete account.")
        accounts = DbCollections.getAccounts()
        account = accounts.find_one({ACCOUNT_EMAIL_ADDRESS: emailAddress})
        if account is None:
            util.debugPrint("Cannot delete account, email not found " +
                            emailAddress)
            retVal = ["INVALUSER", "Account not found."]
        elif numAdmin == 1 and account[ACCOUNT_PRIVILEGE] == ADMIN:
            util.debugPrint("Cannot delete account, last admin account.")
            retVal = [
                "LASTADMIN",
                "Last admin account, cannot perform operation or there would be no admin accounts left."
            ]
        else:
            util.debugPrint("Removing account.")
            accounts.remove({"_id": account["_id"]})
            util.debugPrint("account deleted: " + emailAddress)
            retVal = ["OK", ""]
    except:
        retVal = [
            "NOK",
            "There was an issue on the server, please check the system logs."
        ]
    finally:
        AccountLock.release()
    return packageAccountsReturn(retVal)
 def loadLastDataMessage(self, sensorId, bandName):
     key = str(STREAMING_LAST_DATA_MESSAGE + sensorId + ":" +
               bandName).encode("UTF-8")
     util.debugPrint("loadLastDataMessage: " + key)
     lastDataMessage = self.mc.get(key)
     if lastDataMessage is not None:
         self.lastDataMessage[sensorId + ":" + bandName] = lastDataMessage
     return self.lastDataMessage
Exemplo n.º 20
0
def disconnectSensor(sensorId):
    """
    Send a sensor a command to exit.

    URL Path:
        sensorId -- the session ID of the login session.

    URL Args: None

    Request Body:
    Contains authentication information for the agent that is authorized
    to arm and disarm the sensor:

        - agentName: Name of the agent to arm/disarm sensor.
        - key  : password of the agent to arm/disarm the sensor.

    HTTP Return Codes:

        - 200 OK: invocation was successful.
        - 403 Forbidden: authentication failure
        - 400 Bad request: Sensor is not a streaming sensor.

    Example Invocation:

   ::

       params = {}
       params["agentName"] = "NIST_ESC"
       params["key"] = "ESC_PASS"
       r = requests.post("https://"+ host + ":" + str(443) + "/sensorcontrol/disconnectSensor/" + self.sensorId + "/LTE:70315780:713315880",data=json.dumps(params),verify=False)


    """
    try:
        util.debugPrint("disconnectSensor: sensorId " + sensorId)
        requestStr = request.data
        if requestStr is None:
            abort(400)
        accountData = json.loads(requestStr)
        if not authentication.authenticateSensorAgent(accountData):
            abort(403)
        sensorConfig = SensorDb.getSensorObj(sensorId)
        if sensorConfig is None:
            abort(404)
        if not sensorConfig.isStreamingEnabled():
            abort(400)
        sendCommandToSensor(
            sensorId, json.dumps({
                "sensorId": sensorId,
                "command": "exit"
            }))
        return jsonify({STATUS: OK})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
Exemplo n.º 21
0
def getSensorDataSummary(sensorId, locationMessage):
    sensor = SensorDb.getSensorObj(sensorId)
    if sensor is None:
        return {STATUS: NOK, ERROR_MESSAGE: "Sensor Not found"}
    measurementType = sensor.getMeasurementType()
    tzId = locationMessage[TIME_ZONE_KEY]
    acquisitionCount = LocationMessage.getMessageCount(locationMessage)
    util.debugPrint("AquistionCount " + str(acquisitionCount))
    if acquisitionCount == 0:
        return {
            "status": "OK",
            "minOccupancy": 0,
            "tStartReadings": 0,
            "tStartLocalTime": 0,
            "tStartLocalTimeFormattedTimeStamp": "UNKNOWN",
            "tStartDayBoundary": 0,
            "tEndDayBoundary": 0,
            "tEndReadings": 0,
            "tEndLocalTimeFormattedTimeStamp": "UNKNOWN",
            "maxOccupancy": 0,
            "measurementType": measurementType,
            "isStreamingEnabled": sensor.isStreamingEnabled(),
            "sensorStatus": sensor.getSensorStatus(),
            COUNT: 0
        }

    minTime = LocationMessage.getFirstDataMessageTimeStamp(locationMessage)
    maxTime = LocationMessage.getLastDataMessageTimeStamp(locationMessage)

    tStartDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        minTime, tzId)
    (minLocalTime,
     tStartLocalTimeTzName) = timezone.getLocalTime(minTime, tzId)

    tEndDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        maxTime, tzId)

    tstampMin = timezone.formatTimeStampLong(minTime, tzId)
    tstampMax = timezone.formatTimeStampLong(maxTime, tzId)
    retval = {
        "status": "OK",
        "maxOccupancy": 0,
        "minOccupancy": 0,
        "tStartReadings": minTime,
        "tStartLocalTime": minLocalTime,
        "tStartLocalTimeFormattedTimeStamp": tstampMin,
        "tStartDayBoundary": tStartDayBoundary,
        "tEndDayBoundary": tEndDayBoundary,
        "tEndReadings": maxTime,
        "tEndLocalTimeFormattedTimeStamp": tstampMax,
        "measurementType": measurementType,
        "isStreamingEnabled": sensor.isStreamingEnabled(),
        "sensorStatus": sensor.getSensorStatus(),
        COUNT: acquisitionCount
    }

    return retval
def generateUserDenyAccountEmail(emailAddress, serverUrlPrefix):
    """
    Generate and send email. This is a thread since the SMTP timeout is 30 seconds
    """
    message = "This is an automatically generated message from the Spectrum Monitoring System.\n"\
              + "We regret to inform you that your request for a new account from the CAC Measured Spectrum Occupancy Database was denied.\n"\
              + "Please contact the system administrator for more information.\n"
    util.debugPrint(message)
    SendMail.sendMail(message, emailAddress, "Account information")
Exemplo n.º 23
0
def generateSpectrumForFFTPower(msg, milisecOffset, sessionId):
    chWidth = Config.getScreenConfig()[CHART_WIDTH]
    chHeight = Config.getScreenConfig()[CHART_HEIGHT]

    startTime = msg["t"]
    nM = int(msg["nM"])
    n = int(msg["mPar"]["n"])
    measurementDuration = int(msg["mPar"]["td"])
    miliSecondsPerMeasurement = float(
        measurementDuration * MILISECONDS_PER_SECOND) / float(nM)
    powerVal = np.array(msgutils.getData(msg))
    spectrogramData = np.transpose(powerVal.reshape(nM, n))
    col = int(milisecOffset / miliSecondsPerMeasurement)
    util.debugPrint("Col = " + str(col))
    spectrumData = spectrogramData[:, col]
    maxFreq = msg["mPar"]["fStop"]
    minFreq = msg["mPar"]["fStart"]
    nSteps = len(spectrumData)
    freqDelta = float(maxFreq - minFreq) / float(1E6) / nSteps
    freqArray = [
        float(minFreq) / float(1E6) + i * freqDelta for i in range(0, nSteps)
    ]
    plt.figure(figsize=(chWidth, chHeight))
    plt.scatter(freqArray, spectrumData, color='red', label='Signal Power')
    # TODO -- fix this when the sensor is calibrated.
    wnI = msg[NOISE_FLOOR]
    noiseFloorData = [wnI for i in range(0, len(spectrumData))]
    plt.scatter(freqArray, noiseFloorData, color='black', label="Noise Floor")
    xlabel = "Freq (MHz)"
    ylabel = "Power (dBm)"
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    locationMessage = DbCollections.getLocationMessages().find_one(
        {"_id": ObjectId(msg["locationMessageId"])})
    t = msg["t"] + milisecOffset / float(MILISECONDS_PER_SECOND)
    tz = locationMessage[TIME_ZONE_KEY]
    title = "Spectrum at " + timezone.formatTimeStampLong(t, tz)
    plt.title(title)
    spectrumFile = sessionId + "/" + msg[SENSOR_ID] + "." + str(
        startTime) + "." + str(milisecOffset) + ".spectrum.png"
    spectrumFilePath = util.getPath(
        STATIC_GENERATED_FILE_LOCATION) + spectrumFile
    plt.savefig(spectrumFilePath, pad_inches=0, dpi=100)
    plt.clf()
    plt.close()
    # plt.close("all")
    retval = {
        "status": "OK",
        "spectrum": Config.getGeneratedDataPath() + "/" + spectrumFile,
        "freqArray": freqArray,
        "spectrumData": spectrumData.tolist(),
        "noiseFloorData": noiseFloorData,
        "title": title,
        "xlabel": xlabel,
        "ylabel": ylabel
    }
    return retval
Exemplo n.º 24
0
def decodeStackTrace(stackTrace):
    lines = stackTrace.split()
    for line in lines:
        pieces = line.split(":")
        if pieces[0] in gwtSymbolMap:
            print gwtSymbolMap.get(pieces[0])
            file = gwtSymbolMap.get(pieces[0])["file"]
            lineNo = gwtSymbolMap.get(pieces[0])["line"]
            util.debugPrint(file + ": " + lineNo + ": " + pieces[1])
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 generateUserAccountPendingAuthorizationEmail(emailAddress,
                                                 serverUrlPrefix):
    """
    Generate and send email. This is a thread since the SMTP timeout is 30 seconds
    """
    message = "This is an automatically generated message from the Spectrum Monitoring System.\n"\
              + "You requested a new account for the CAC Measured Spectrum Occupancy Database.\n"\
              + "Your request has been sent to the system administrator for authorization.\n"
    util.debugPrint(message)
    SendMail.sendMail(message, emailAddress, "Account pending authorization")
def generateChangePasswordEmail(emailAddress, serverUrlPrefix):
    """
    Generate and send email. This is a thread since the SMTP timeout is 30 seconds
    """
    message = "This is an automatically generated message from the Spectrum Monitoring System.\n"\
        + "Your password has been changed to value you entered into " + str(serverUrlPrefix + "/spectrumbrowser") + "\n"\
        + "If you did not originate the change password request, please contact the system administrator.\n"

    util.debugPrint(message)
    SendMail.sendMail(message, emailAddress, "change password link")
Exemplo n.º 28
0
def getResourceData(ws):
    """
    Web-browser websocket connection handler.
    """
    util.debugPrint("getResourceData")
    try:
        ResourceDataStreaming.getResourceData(ws)
    except:
        util.logStackTrace(sys.exc_info())
        traceback.print_exc()
        raise
Exemplo n.º 29
0
 def removeSession(self, sessionId):
     self.acquire()
     try:
         util.debugPrint("removeSession: " + sessionId)
         activeSessions = self.mc.get(SESSIONS)
         self.mc.delete(SESSIONS)
         if sessionId in activeSessions:
             del activeSessions[sessionId]
         self.mc.add(SESSIONS, activeSessions)
     finally:
         self.release()
Exemplo n.º 30
0
 def unlockAccountWorker(emailAddress, sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             abort(403)
         util.debugPrint("unlockAccount")
         return jsonify(AccountsManagement.unlockAccount(emailAddress))
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise