views.py 4.29 KB
Newer Older
Cédric Bonhomme's avatar
Cédric Bonhomme committed
1 2 3 4 5
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# ***** BEGIN LICENSE BLOCK *****
# This file is part of EV3WebController.
6
# Copyright (c) 2014-2015 Cédric Bonhomme.
Cédric Bonhomme's avatar
Cédric Bonhomme committed
7 8 9 10 11 12 13
# All rights reserved.
#
#
#
# ***** END LICENSE BLOCK *****

__author__ = "Cedric Bonhomme"
14
__version__ = "$Revision: 0.4 $"
Cédric Bonhomme's avatar
Cédric Bonhomme committed
15
__date__ = "$Date: 2014/12/15$"
16
__revision__ = "$Date: 2015/11/04 $"
17
__copyright__ = "Copyright (c) 2014-2015 Cédric Bonhomme"
Cédric Bonhomme's avatar
Cédric Bonhomme committed
18 19
__license__ = ""

20
import time
21
from flask import render_template, current_app, request, session, \
Cédric Bonhomme's avatar
Cédric Bonhomme committed
22 23 24 25
    url_for, redirect, g, send_from_directory, make_response, abort, Markup
from flask.ext.login import LoginManager, login_user, logout_user, \
    login_required, current_user, AnonymousUserMixin

26 27
from ev3.ev3dev import Motor

Cédric Bonhomme's avatar
Cédric Bonhomme committed
28 29
import conf
from web.decorators import to_response
30
from web.lib import movements
Cédric Bonhomme's avatar
Cédric Bonhomme committed
31
from web import app
32
from web.lib import movements
33
from web import right_wheel, left_wheel, button, ir_sensor, color_sensor
Cédric Bonhomme's avatar
Cédric Bonhomme committed
34 35

login_manager = LoginManager(app)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
36
login_manager.login_view = 'index'
Cédric Bonhomme's avatar
Cédric Bonhomme committed
37 38 39 40 41 42
login_manager.login_message = 'Please log in to access this page.'
login_manager.login_message_category = 'danger'

@app.errorhandler(403)
def authentication_failed(e):
    flash('You do not have enough rights.', 'danger')
Cédric Bonhomme's avatar
Cédric Bonhomme committed
43
    return redirect(url_for('index'))
Cédric Bonhomme's avatar
Cédric Bonhomme committed
44 45 46 47

@app.errorhandler(401)
def authentication_required(e):
    flash('Authenticated required.', 'info')
Cédric Bonhomme's avatar
Cédric Bonhomme committed
48
    return redirect(url_for('index'))
Cédric Bonhomme's avatar
Cédric Bonhomme committed
49 50 51 52

@login_manager.user_loader
def load_user(id):
    # Return an instance of the User model
Cédric Bonhomme's avatar
Cédric Bonhomme committed
53
    pass
Cédric Bonhomme's avatar
Cédric Bonhomme committed
54 55 56 57 58


@app.route('/move/<direction>', methods=['GET'])
@app.route('/move/<direction>/<speed>', methods=['GET'])
@to_response
59
def move(direction="forward", speed=800):
Cédric Bonhomme's avatar
Cédric Bonhomme committed
60
    """
61 62
    This endpoint manages the different 'move action': 'forward', 'backward',
    'left', 'right' and 'stop'.
Cédric Bonhomme's avatar
Cédric Bonhomme committed
63 64 65 66 67 68 69 70 71
    """
    result = {
                "action": "move",
                "direction": direction,
                "message": "OK"
            }
    return_code = 200

    if direction == 'forward':
72 73
        nb_blocks = request.args.get("blocks", None)
        if None is not nb_blocks:
74
            position = int(nb_blocks) * -1152
75 76
            result["message"] = movements.run_position_limited(left_wheel,
                                                        right_wheel, position)
77
        else:
78 79
            left_wheel.run_forever(speed * -1, regulation_mode=False)
            right_wheel.run_forever(speed * -1, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
80 81

    elif direction == 'backward':
82 83
        nb_blocks = request.args.get("blocks", None)
        if None is not nb_blocks:
84
            position = int(nb_blocks) * 1152
85 86
            result["message"] = movements.run_position_limited(left_wheel,
                                                        right_wheel, position)
87
        else:
88 89
            left_wheel.run_forever(speed * 1, regulation_mode=False)
            right_wheel.run_forever(speed * 1, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
90 91

    elif direction == 'left':
Cédric Bonhomme's avatar
Cédric Bonhomme committed
92
        speed = 600
93 94 95 96 97 98
        forever = request.args.get("forever", None)
        if None is forever:
            movements.rotate(left_wheel, right_wheel, 90, -90)
        else:
            left_wheel.run_forever(speed * -1, regulation_mode=False)
            right_wheel.run_forever(speed, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
99 100

    elif direction == 'right':
Cédric Bonhomme's avatar
Cédric Bonhomme committed
101
        speed = 600
102 103 104 105 106 107
        forever = request.args.get("forever", None)
        if None is forever:
            movements.rotate(left_wheel, right_wheel, -90, 90)
        else:
            left_wheel.run_forever(speed, regulation_mode=False)
            right_wheel.run_forever(speed * -1, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    elif direction == 'stop':
        left_wheel.stop()
        right_wheel.stop()

    else:
        result["message"], return_code = "Unknown direction", 400


    return result, return_code


@app.route('/sensor/<sensor_name>', methods=['GET'])
def sensor(sensor_name=""):
    """
    """
    if sensor_name == "ir_sensor":
        return {"distance": ir_sensor.prox}
126 127 128 129 130
    elif sensor_name == "color_sensor":
        return {"rgb": color_sensor.rgb,
                "ambiant": color_sensor.ambiant,
                "reflect": color_sensor.reflect,
                "mode": color_sensor.mode}
Cédric Bonhomme's avatar
Cédric Bonhomme committed
131 132 133 134 135 136
    elif sensor_name == "button":
        pass
    else:
        return {"message": "Unknown sensor"}, 400


Cédric Bonhomme's avatar
Cédric Bonhomme committed
137 138 139
@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')