2021-10-25 20:04:55 +01:00
|
|
|
from flask import Flask, request
|
|
|
|
import os
|
2021-10-26 15:32:50 +01:00
|
|
|
import subprocess
|
2021-10-25 20:04:55 +01:00
|
|
|
from colormap import rgb2hex
|
2021-10-26 15:52:27 +01:00
|
|
|
from tenacity import wait_exponential, retry, stop_after_attempt
|
|
|
|
|
2021-10-25 20:04:55 +01:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
2021-10-26 15:52:27 +01:00
|
|
|
@retry(wait=wait_exponential(multiplier=2, min=2, max=30), stop=stop_after_attempt(5))
|
2021-10-26 10:39:04 +01:00
|
|
|
@app.route("/on")
|
2021-10-26 10:40:10 +01:00
|
|
|
def on():
|
2021-10-26 15:48:40 +01:00
|
|
|
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"
|
2021-10-26 10:39:04 +01:00
|
|
|
|
|
|
|
|
2021-10-26 15:52:27 +01:00
|
|
|
@retry(wait=wait_exponential(multiplier=2, min=2, max=30), stop=stop_after_attempt(5))
|
2021-10-26 10:39:04 +01:00
|
|
|
@app.route("/off")
|
2021-10-26 10:40:10 +01:00
|
|
|
def off():
|
2021-10-26 15:48:40 +01:00
|
|
|
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"
|
2021-10-25 20:04:55 +01:00
|
|
|
|
|
|
|
|
2021-10-26 15:52:27 +01:00
|
|
|
@retry(wait=wait_exponential(multiplier=2, min=2, max=30), stop=stop_after_attempt(5))
|
2021-10-26 10:33:37 +01:00
|
|
|
@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"
|
2021-10-26 15:54:18 +01:00
|
|
|
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
|
|
|
|
except:
|
|
|
|
raise Exception
|