LED-Control/server.py
karl.hudgell d2df809ff7 retrun
2021-10-26 16:56:48 +01:00

46 lines
993 B
Python

from flask import Flask, request
import ast
import subprocess
from colormap import rgb2hex
from tenacity import retry, stop_after_attempt
app = Flask(__name__)
@retry(stop=stop_after_attempt(5))
def run_bulb_action(value):
result = subprocess.run(
[
f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n {value}"
],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
print(result)
if "Characteristic value was written successfully" not in result:
raise Exception
else:
return True
@app.route("/on")
def on():
return run_bulb_action("cc2333")
@app.route("/off")
def off():
return run_bulb_action("cc2433")
@app.route("/colour")
def colour():
rgb = list(ast.literal_eval(request.args.get("rgb")))
r = rgb[0]
g = rgb[1]
b = rgb[2]
hex = rgb2hex(r, g, b)
hex = hex.replace("#", "")
value = "56" + hex + "00f0aa"
return run_bulb_action(value)