import redis
from rq import Queue, Connection
from flask import Flask, render_template, Blueprint, jsonify, request
import tasks
import rq_dashboard
from wingnut import Wingnut

app = Flask(
        __name__,
        template_folder="./templates",
        static_folder="./static",
    )

app.config.from_object(rq_dashboard.default_settings)
app.register_blueprint(rq_dashboard.blueprint, url_prefix="/rq")

@app.route("/", methods=["GET"])
def home():
    return render_template("main/home.html")

@app.route("/tasks", methods=["POST"])
def run_task():
    task_type = request.form["type"]
    with Connection(redis.from_url("redis://localhost:6379")):
        q = Queue()
        task = q.enqueue(tasks.create_task, task_type)
    response_object = {
        "status": "success",
        "data": {
            "task_id": task.get_id()
        }
    }
    return jsonify(response_object), 202

@app.route("/mode", methods=["POST"])
def set_mode():
    task_type = request.form["type"]
    with Connection(redis.from_url("redis://localhost:6379")):
        q = Queue("mode")
        task = q.enqueue(tasks.set_mode, task_type)
    response_object = {
        "status": "success",
        "data": {
            "task_id": task.get_id()
        }
    }
    return jsonify(response_object), 202

@app.route("/tasks/<task_id>", methods=["GET"])
def get_status(task_id):
    with Connection(redis.from_url("redis://localhost:6379")):
        q = Queue()
        task = q.fetch_job(task_id)
    if task:
        response_object = {
            "status": "success",
            "data": {
                "task_id": task.get_id(),
                "task_status": task.get_status(),
                "task_result": task.result,
            },
        }
    else:
        response_object = {"status": "error"}
    return jsonify(response_object)

@app.route("/configuration", methods=["GET"])
def get_configuration():
    wingnut = Wingnut()
    response_object = {
        "status": "success",
        "data": {
            "servoPin": wingnut.servoPin,
            "leftMotorPin1": wingnut.leftMotorPin1,
            "leftMotorPin1": wingnut.leftMotorPin2,
            "leftMotorEnablePin": wingnut.leftMotorEnablePin,
            "rightMotorPin1": wingnut.rightMotorPin1,
            "rightMotorPin1": wingnut.rightMotorPin2,
            "rightMotorEnablePin": wingnut.rightMotorEnablePin,
            "sonarTriggerPin": wingnut.sonarTriggerPin,
            "sonarEchoPin": wingnut.sonarEchoPin
        }
    }
    return jsonify(response_object)

@app.route("/diagnostics", methods=["GET"])
def get_diagnostics():
    r = redis.Redis()
    diagnostics = {}
    diagnostics["power_level"] = r.get("power_level").decode("utf-8")
    diagnostics["temperature"] = r.get("temperature").decode("utf-8")
    diagnostics["free_memory_mb"] = r.get("free_memory_mb").decode("utf-8")
    diagnostics["free_disk_space"] = r.get("free_disk_space").decode("utf-8")
    response_object = {
        "status": "success",
        "data": {
            "diagnostics": diagnostics
        }
    }
    return jsonify(response_object)

if __name__ == "__main__":
    app.run(host="0.0.0.0",debug=1)



    

[(8, 'flask.Flask', 'Flask', False, 'from flask import Flask, render_template, Blueprint, jsonify, request\n'), (19, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, Blueprint, jsonify, request\n'), (65, 'flask.jsonify', 'jsonify', False, 'from flask import Flask, render_template, Blueprint, jsonify, request\n'), (69, 'wingnut.Wingnut', 'Wingnut', False, 'from wingnut import Wingnut\n'), (84, 'flask.jsonify', 'jsonify', False, 'from flask import Flask, render_template, Blueprint, jsonify, request\n'), (88, 'redis.Redis', 'redis.Redis', False, 'import redis\n'), (100, 'flask.jsonify', 'jsonify', False, 'from flask import Flask, render_template, Blueprint, jsonify, request\n'), (25, 'rq.Queue', 'Queue', False, 'from rq import Queue, Connection\n'), (33, 'flask.jsonify', 'jsonify', False, 'from flask import Flask, render_template, Blueprint, jsonify, request\n'), (39, 'rq.Queue', 'Queue', False, 'from rq import Queue, Connection\n'), (47, 'flask.jsonify', 'jsonify', False, 'from flask import Flask, render_template, Blueprint, jsonify, request\n'), (52, 'rq.Queue', 'Queue', False, 'from rq import Queue, Connection\n'), (24, 'redis.from_url', 'redis.from_url', False, 'import redis\n'), (38, 'redis.from_url', 'redis.from_url', False, 'import redis\n'), (51, 'redis.from_url', 'redis.from_url', False, 'import redis\n')]