LED-Control/server.py
karl.hudgell 541fc7dc6a new try
2021-10-26 15:48:40 +01:00

62 lines
1.5 KiB
Python

from flask import Flask, request
import os
import subprocess
from colormap import rgb2hex
from tenacity import *
app = Flask(__name__)
@retry
@app.route("/on")
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"
@retry
@app.route("/off")
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"
@retry
@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:
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 request.query_string