class VirtualOuluServer(object): def __init__(self, traffic_manager=None): super(VirtualOuluServer, self).__init__() if traffic_manager is not None: self.traffic_manager = traffic_manager else: self.traffic_manager = TrafficManager() self.traffic_manager.start() def get_vehicles_positions(self): return {'time': self.traffic_manager.simulating_time, 'vehicles_positions': self.traffic_manager.vehicles_positions, 'congested_places': self.traffic_manager.congested_places} def delete_congestion(self, congestion_id): self.traffic_manager.remove_congestion(congestion_id) return True def add_congestion(self, lat, lgn): congestion = self.traffic_manager.add_congestion(lat, lgn) return congestion['id'] def update_congestion(self, congestion_id, lat, lng): self.traffic_manager.update_congestion(congestion_id, lat, lng) return congestion_id
app = Flask(__name__) cors = CORS(app) @app.route('/vehicles_positions.php') def vehicles_positions(): return Response(json.dumps({'time': traffic_manager.simulating_time, 'vehicles_positions': traffic_manager.vehicles_positions, 'congested_places': traffic_manager.congested_places}), 200, mimetype='application/json') @app.route('/delete_congestion/<congestion_id>') def delete_congestion(congestion_id): traffic_manager.remove_congestion(congestion_id) return Response(json.dumps({'success': True, 'congestion_id': congestion_id}), 202, mimetype='application/json') @app.route('/congest_edge/<lat>/<lon>') def congest_edge(lat, lon): congestion = traffic_manager.add_congestion(lat, lon) return Response(json.dumps({'success': True, 'id': congestion['id']}), 200, mimetype='application/json') @app.route('/update_congest/<congestion_id>/<lat>/<lng>') def update_congest(congestion_id, lat, lng): traffic_manager.update_congestion(congestion_id, lat, lng) return Response(json.dumps({'success': True, 'id': congestion_id}), 200, mimetype='application/json') @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': traffic_manager.start() app.run(debug=False, host='0.0.0.0', port=5000)