LED-Control/server.py
2021-10-26 16:49:39 +01:00

68 lines
1.7 KiB
Python

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