monitoring/ffmap/web/api.py

62 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2015-10-07 16:19:07 +02:00
from ffmap.routertools import *
from ffmap.maptools import *
from ffmap.dbtools import FreifunkDB
2015-11-11 15:27:50 +01:00
from ffmap.stattools import record_global_stats
2015-10-07 16:19:07 +02:00
from flask import Blueprint, request, make_response, redirect, url_for
2015-10-07 16:19:07 +02:00
from pymongo import MongoClient
from bson.json_util import dumps as bson2json
import json
api = Blueprint("api", __name__)
db = FreifunkDB().handle()
2015-10-07 16:19:07 +02:00
# map ajax
2015-10-07 16:19:07 +02:00
@api.route('/get_nearest_router')
def get_nearest_router():
2015-11-03 16:03:14 +01:00
res_router = db.routers.find_one(
{"position": {"$near": {"$geometry": {
2015-10-07 16:19:07 +02:00
"type": "Point",
"coordinates": [float(request.args.get("lng")), float(request.args.get("lat"))]
2015-11-03 16:03:14 +01:00
}}}},
{
"hostname": 1,
"neighbours": 1,
"position": 1,
}
)
2015-10-07 16:19:07 +02:00
r = make_response(bson2json(res_router))
r.mimetype = 'application/json'
return r
# router by mac (link from router webui)
@api.route('/get_router_by_mac/<mac>')
def get_router_by_mac(mac):
res_routers = db.routers.find({"netifs.mac": mac.lower()}, {"_id": 1})
if res_routers.count() != 1:
2016-02-06 11:42:11 +01:00
return redirect(url_for("router_list", q="netifs.mac:%s" % mac))
else:
return redirect(url_for("router_info", dbid=next(res_routers)["_id"]))
2015-10-07 16:19:07 +02:00
@api.route('/alfred', methods=['GET', 'POST'])
def alfred():
#set_alfred_data = {65: "hallo", 66: "welt"}
set_alfred_data = {}
r = make_response(json.dumps(set_alfred_data))
if request.method == 'POST':
alfred_data = request.get_json()
if alfred_data:
# load router status xml data
for mac, xml in alfred_data.get("64", {}).items():
2015-11-03 16:03:14 +01:00
import_nodewatcher_xml(mac, xml)
r.headers['X-API-STATUS'] = "ALFRED data imported"
detect_offline_routers()
delete_orphaned_routers()
2015-11-11 15:27:50 +01:00
record_global_stats()
update_mapnik_csv()
2015-10-07 16:19:07 +02:00
r.mimetype = 'application/json'
return r