Commit e6913ee0 authored by Cédric Bonhomme's avatar Cédric Bonhomme

Test of the color sensor through the API.

parent b1c5816f
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
import json import json
from functools import wraps from functools import wraps
from threading import Thread from threading import Thread
from flask import Response, request, session, jsonify, current_app from flask import Response
from web.lib.utils import default_handler from web.lib.utils import default_handler
def async(f): def async(f):
...@@ -26,17 +26,17 @@ def async(f): ...@@ -26,17 +26,17 @@ def async(f):
thr.start() thr.start()
return wrapper return wrapper
def to_response(func): def to_response(func):
"""Will cast results of func as a result, and try to extract """
a status_code for the Response object""" Cast results of func as a result, and try to extract
a status_code for the Response object.
"""
@wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
status_code = 200 status_code = 200
result = func(*args, **kwargs) result = func(*args, **kwargs)
if isinstance(result, Response): if isinstance(result, Response):
return result return result
if isinstance(result, list) and len(result) == 1:
result = result[0]
elif isinstance(result, tuple): elif isinstance(result, tuple):
result, status_code = result result, status_code = result
return Response(json.dumps(result, default=default_handler), return Response(json.dumps(result, default=default_handler),
......
...@@ -118,15 +118,15 @@ def move(direction="forward", speed=800): ...@@ -118,15 +118,15 @@ def move(direction="forward", speed=800):
@app.route('/sensor/<sensor_name>', methods=['GET']) @app.route('/sensor/<sensor_name>', methods=['GET'])
@to_response
def sensor(sensor_name=""): def sensor(sensor_name=""):
""" """
Returns the value of the selected sensor.
""" """
if sensor_name == "ir_sensor": if sensor_name == "ir_sensor":
return {"distance": ir_sensor.prox} return {"distance": ir_sensor.prox}
elif sensor_name == "color_sensor": elif sensor_name == "color_sensor":
return {"rgb": color_sensor.rgb, return {"rgb": color_sensor.rgb,
"ambiant": color_sensor.ambiant,
"reflect": color_sensor.reflect,
"mode": color_sensor.mode} "mode": color_sensor.mode}
elif sensor_name == "button": elif sensor_name == "button":
pass pass
...@@ -136,4 +136,7 @@ def sensor(sensor_name=""): ...@@ -136,4 +136,7 @@ def sensor(sensor_name=""):
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
"""
Graphical Web interface to command the robot.
"""
return render_template('index.html') return render_template('index.html')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment