49 lines
1.0 KiB
Python
49 lines
1.0 KiB
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():
|
|
res = run_bulb_action("cc2333")
|
|
return {"result": res}
|
|
|
|
|
|
@app.route("/off")
|
|
def off():
|
|
res = run_bulb_action("cc2433")
|
|
return {"result": res}
|
|
|
|
|
|
@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"
|
|
res = run_bulb_action(value)
|
|
return {"result": res}
|