Example #1
0
def create_app(state, scheduler):
    app = Vase(__name__)

    def _jsonify(obj={}):
        return HttpResponse(json.dumps(obj), content_type="application/json")

    def _broadcast_status(building=None):
        buildings = state.buildings
        if building:
            buildings = filter(lambda s: s.name == building, buildings)
        message = json.dumps({"status": state.__dict__()})
        BuildingsStatusEndpoint.broadcast(message, building=building)

    #
    # Static files
    #
    @app.route(path="/", methods=("GET"))
    def root(request):
        body = open(os.path.join(os.path.dirname(__file__), "static", "index.html"), "r", encoding="utf-8").read()
        return HttpResponse(body, content_type="text/html")

    @app.route(path="/html/{fname}", methods=("GET"))
    def html(request, fname):
        body = open(os.path.join(os.path.dirname(__file__), "static/html", fname), "r", encoding="utf-8").read()
        return HttpResponse(body, content_type="text/html")

    @app.route(path="/js/{fname}", methods=("GET"))
    def js(request, fname):
        body = open(os.path.join(os.path.dirname(__file__), "static/js", fname), "r", encoding="utf-8").read()
        return HttpResponse(body, content_type="application/javascript")

    @app.route(path="/css/{fname}", methods=("GET"))
    def css(request, fname):
        body = open(os.path.join(os.path.dirname(__file__), "static/css", fname), "r", encoding="utf-8").read()
        return HttpResponse(body, content_type="text/css")

    #
    # The resource buildings
    #
    @app.route(path="/api/v1/buildings/{building}/cars/{car:\d+}/buttons/{floor:\d+}", methods="POST")
    def car_buttons_floor(request, building, car: int, floor: int):
        car = int(car)
        floor = int(floor)
        print("Send the car {} to the floor {}".format(car, floor))
        state.cars_buttons_call(building, car, floor)
        scheduler.schedule_car(building, car)
        _broadcast_status(building=building)
        return _jsonify()

    @app.route(path="/api/v1/buildings/{building}/cars/{car}/buttons/toggle", methods="POST")
    def car_buttons_stop(request, building, car: int):
        car = int(car)
        print("Stop the car {}".format(car))
        state.cars_buttons_toggle(building, car)
        scheduler.schedule_car(building, car)
        _broadcast_status(building=building)
        return _jsonify()

    @app.route(path="/api/v1/buildings/{building}/floors/{floor:\d+}/buttons/call", methods="POST")
    def floors_buttons_call(request, building, floor: int):
        floor = int(floor)
        print("Call the Nearest Car on the floor {}".format(floor))
        scheduler.call_car(building, floor)
        _broadcast_status(building=building)
        return _jsonify()

    @app.route(path="/api/v1/buildings", methods="POST")
    def buildings_post(request):
        data = json.loads((yield from request.body.read()).decode("utf-8"))
        name = str(data["name"])
        n_cars = int(data["n_cars"])
        n_floors = int(data["n_floors"])
        speed = float(data["speed"])
        print('Create a building "{}" with {} floors and {} cars.'.format(name, n_floors, n_cars))
        state.create_building(name, n_floors, n_cars, speed)
        _broadcast_status()
        return _jsonify()

    @app.route(path="/api/v1/buildings/{building}", methods="DELETE")
    def buildings_delete(request, building):
        print('Destroy the building "{}".'.format(building))
        state.destroy_building(building)
        _broadcast_status()
        return _jsonify()

    @app.route(path="/api/v1/buildings/{building}/status", methods="GET")
    def buildings_status(request, building):
        return _jsonify({"status": state.buildings[building].__dict__()})

    @app.route(path="/api/v1/status", methods="GET")
    def status(request):
        return _jsonify({"status": state.__dict__()})

    #
    # WebSockets endpoint
    #
    app.endpoint(path="/ws", with_sockjs=False)(BuildingsStatusEndpoint)

    subscriptions_service.on_tick(_broadcast_status)

    return app
Example #2
0
File: wsecho.py Project: cymoo/Vase
from vase import Vase

app = Vase(__name__)


@app.route(path="/")
def hello(request):
    return "Hello Vase!"


@app.endpoint(path="/ws/echo", with_sockjs=False)
class EchoEndpoint:
    """
    WebSocket endpoint
    Has the following attributes:
    `bag` - a dictionary that is shared between all instances of this endpoint
    `transport` - used to send messages into the websocket
    """
    def on_connect(self):
        print("You are successfully connected")

    def on_message(self, message):
        self.transport.send(message)

    def on_close(self, exc=None):
        print("Connection closed")

if __name__ == '__main__':
    app.run()
Example #3
0
def create_app(state, scheduler):
    app = Vase(__name__)

    def _jsonify(obj={}):
        return HttpResponse(json.dumps(obj), content_type="application/json")

    def _broadcast_status(building=None):
        buildings = state.buildings
        if building:
            buildings = filter(lambda s: s.name == building, buildings)
        message = json.dumps({'status': state.__dict__()})
        BuildingsStatusEndpoint.broadcast(message, building=building)

    #
    # Static files
    #
    @app.route(path="/", methods=("GET"))
    def root(request):
        body = open(os.path.join(os.path.dirname(__file__), 'static',
                                 'index.html'),
                    'r',
                    encoding='utf-8').read()
        return HttpResponse(body, content_type="text/html")

    @app.route(path="/html/{fname}", methods=("GET"))
    def html(request, fname):
        body = open(os.path.join(os.path.dirname(__file__), 'static/html',
                                 fname),
                    'r',
                    encoding='utf-8').read()
        return HttpResponse(body, content_type="text/html")

    @app.route(path="/js/{fname}", methods=("GET"))
    def js(request, fname):
        body = open(os.path.join(os.path.dirname(__file__), 'static/js',
                                 fname),
                    'r',
                    encoding='utf-8').read()
        return HttpResponse(body, content_type="application/javascript")

    @app.route(path="/css/{fname}", methods=("GET"))
    def css(request, fname):
        body = open(os.path.join(os.path.dirname(__file__), 'static/css',
                                 fname),
                    'r',
                    encoding='utf-8').read()
        return HttpResponse(body, content_type="text/css")

    #
    # The resource buildings
    #
    @app.route(
        path="/api/v1/buildings/{building}/cars/{car:\d+}/buttons/{floor:\d+}",
        methods="POST")
    def car_buttons_floor(request, building, car: int, floor: int):
        car = int(car)
        floor = int(floor)
        print("Send the car {} to the floor {}".format(car, floor))
        state.cars_buttons_call(building, car, floor)
        scheduler.schedule_car(building, car)
        _broadcast_status(building=building)
        return _jsonify()

    @app.route(path="/api/v1/buildings/{building}/cars/{car}/buttons/toggle",
               methods="POST")
    def car_buttons_stop(request, building, car: int):
        car = int(car)
        print("Stop the car {}".format(car))
        state.cars_buttons_toggle(building, car)
        scheduler.schedule_car(building, car)
        _broadcast_status(building=building)
        return _jsonify()

    @app.route(
        path="/api/v1/buildings/{building}/floors/{floor:\d+}/buttons/call",
        methods="POST")
    def floors_buttons_call(request, building, floor: int):
        floor = int(floor)
        print("Call the Nearest Car on the floor {}".format(floor))
        scheduler.call_car(building, floor)
        _broadcast_status(building=building)
        return _jsonify()

    @app.route(path="/api/v1/buildings", methods="POST")
    def buildings_post(request):
        data = json.loads((yield from request.body.read()).decode('utf-8'))
        name = str(data['name'])
        n_cars = int(data['n_cars'])
        n_floors = int(data['n_floors'])
        speed = float(data['speed'])
        print("Create a building \"{}\" with {} floors and {} cars.".format(
            name, n_floors, n_cars))
        state.create_building(name, n_floors, n_cars, speed)
        _broadcast_status()
        return _jsonify()

    @app.route(path="/api/v1/buildings/{building}", methods="DELETE")
    def buildings_delete(request, building):
        print("Destroy the building \"{}\".".format(building))
        state.destroy_building(building)
        _broadcast_status()
        return _jsonify()

    @app.route(path="/api/v1/buildings/{building}/status", methods="GET")
    def buildings_status(request, building):
        return _jsonify({'status': state.buildings[building].__dict__()})

    @app.route(path="/api/v1/status", methods="GET")
    def status(request):
        return _jsonify({'status': state.__dict__()})

    #
    # WebSockets endpoint
    #
    app.endpoint(path="/ws", with_sockjs=False)(BuildingsStatusEndpoint)

    subscriptions_service.on_tick(_broadcast_status)

    return app