Exemple #1
0
def get_device(deviceid):
    tenant = getTenantFromHeader(request)

    if len(tenant) == 0:
        return jsonify(status="error", message="bad request"), 400

    response = getSitewhereTenantInfo(tenant)


    if response and response.status_code is not 200:
        return jsonify(status=response.reason, message=response.content), response.status_code

    response = response.json()

    try:

        authToken = response['authenticationToken']

        url = "http://scamps.cit.ie:8888/sitewhere/api/devices/" + deviceid

        response = getDeviceInfo(url, authToken)

        print response
        print response.text
        print response.request.headers
        return jsonify(message=response.text), response.status_code
    except KeyError as ke:

        return jsonify(status="401",message="authentication token missing"), 401
Exemple #2
0
def set_status(id):

    # FIXME - should only allow role=user to set status for alerts for that customer
    # Above comment is from original code, can ignore it for now


    status_started = status_timer.start_timer()

    tenant = getTenantFromHeader(request)

    if len(tenant) == 0:
        return jsonify(status="error", message="bad request"), 400

    data = request.json

    tenant = generateDBName(tenant)

    if data and 'status' in data:
        try:
            alert = db.set_status(tenant, id=id, status=data['status'], text=data.get('text', ''))
        except Exception as e:
            return jsonify(status="error", message=str(e)), 500
    else:
        status_timer.stop_timer(status_started)
        return jsonify(status="error", message="must supply 'status' as parameter"), 400

    if alert:
        status_timer.stop_timer(status_started)
        return jsonify(status="ok")
    else:
        status_timer.stop_timer(status_started)
        return jsonify(status="error", message="not found"), 404
Exemple #3
0
def get_alerts():

    tenant = getTenantFromHeader(request)

    if len(tenant) == 0:
        return jsonify(status="error", message="bad request"), 400

    tenant = generateDBName(tenant)
    gets_started = gets_timer.start_timer()

    try:
        query, sort, _, page, limit, query_time = parse_fields(request)
    except Exception as e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400

    try:
        severity_count = db.get_counts(tenant, query=query, fields={"severity": 1}, group="severity")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    try:
        status_count = db.get_counts(tenant, query=query, fields={"status": 1}, group="status")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if limit < 1:
        return jsonify(status="error", message="page 'limit' of %s is not valid" % limit), 416

    total = sum(severity_count.values())
    pages = ((total - 1) // limit) + 1

    if total and page > pages or page < 0:
        return jsonify(status="error", message="page out of range: 1-%s" % pages), 416

    fields = dict()
    fields['history'] = {'$slice': app.config['HISTORY_LIMIT']}

    try:
        alerts = db.get_alerts(tenant, query=query, fields=fields, sort=sort, page=page, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    alert_response = list()
    if len(alerts) > 0:

        last_time = None

        for alert in alerts:
            body = alert.get_body()
            body['href'] = "%s/%s" % (request.base_url.replace('alerts', 'alert'), alert.id)

            if not last_time:
                last_time = body['lastReceiveTime']
            elif body['lastReceiveTime'] > last_time:
                last_time = body['lastReceiveTime']

            alert_response.append(body)

        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=page < pages,
            alerts=alert_response,
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=last_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on(),
        )
    else:
        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            message="not found",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=False,
            alerts=[],
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=query_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on()
        )