74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
from bson import json_util
|
|
from flask import Flask, request
|
|
from flask_pymongo import PyMongo
|
|
|
|
app = Flask(__name__)
|
|
app.config["MONGO_URI"] = "mongodb://mongo:27017/entities"
|
|
mongo = PyMongo(app)
|
|
|
|
|
|
@app.route('/api/v1/resources/cars', methods=['GET'])
|
|
def get_cars():
|
|
query_parameters = request.args
|
|
vin = query_parameters.get('vin')
|
|
|
|
if vin is not None:
|
|
cars = [car for car in mongo.db.cars.find({'vin': vin})]
|
|
else:
|
|
cars = [car for car in mongo.db.cars.find({})]
|
|
|
|
return json_util.dumps({'cursor': cars})
|
|
|
|
|
|
@app.route('/api/v1/resources/traffic_lights', methods=['GET'])
|
|
def get_traffic_lights():
|
|
query_parameters = request.args
|
|
tl_id = query_parameters.get('id')
|
|
|
|
if tl_id is not None:
|
|
traffic_lights = [traffic_light for traffic_light in
|
|
mongo.db.trafficLights.find({'id': tl_id})]
|
|
else:
|
|
traffic_lights = [traffic_light for traffic_light in mongo.db.trafficLights.find({})]
|
|
|
|
return json_util.dumps({'cursor': traffic_lights})
|
|
|
|
|
|
@app.route('/api/v1/resources/traffic_lights_geo', methods=['GET'])
|
|
def get_traffic_lights_geo():
|
|
query_parameters = request.args
|
|
|
|
lat = float(query_parameters.get('lat'))
|
|
lon = float(query_parameters.get('lon'))
|
|
|
|
traffic_lights = [
|
|
traffic_light for traffic_light in
|
|
mongo.db.trafficLights.aggregate([
|
|
{"$geoNear": {
|
|
"near": {
|
|
"type": "Point",
|
|
"coordinates": [lon, lat]
|
|
},
|
|
"spherical": True,
|
|
"distanceField": "calculatedRange"
|
|
}},
|
|
{"$redact": {
|
|
"$cond": {
|
|
"if": {
|
|
"$lte": ["$calculatedRange", "$range"]
|
|
},
|
|
"then": "$$KEEP",
|
|
"else": "$$PRUNE"
|
|
}
|
|
}}
|
|
]
|
|
)
|
|
if lat < traffic_light['location'][1]
|
|
]
|
|
|
|
return json_util.dumps({'cursor': traffic_lights[:1]})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run('0.0.0.0', 5002)
|