from flask import Flask, request
import os
import sys
from colormap import rgb2hex

app = Flask(__name__)


@app.route("/on")
def on():
    os.system(
        f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n cc2333"
    )
    return "On"


@app.route("/off")
def off():
    os.system(
        f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n cc2433"
    )
    return "Off"


@app.route("/colour")
def hello():
    # a = request.query_string
    r = int(request.args.get("r"))
    g = int(request.args.get("g"))
    b = int(request.args.get("b"))
    hex = rgb2hex(r, g, b)
    hex = hex.replace("#", "")

    str = "56" + hex + "00f0aa"
    try:
      subprocess = os.Popen("echo Hello World", shell=True, stdout=os.PIPE)
      subprocess_return = subprocess.stdout.read()
      print(subprocess_return)
    except print(0):
        pass



    os.system(
        f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n {str}"
    )
    return request.query_string
