working rewrite

This commit is contained in:
karl.hudgell 2021-10-26 16:55:30 +01:00
父節點 c1b460ab2d
當前提交 8f511b7da9
共有 2 個檔案被更改,包括 17 行新增38 行删除

查看文件

@ -1,3 +1,4 @@
ast
black==21.9b0 black==21.9b0
click==8.0.3 click==8.0.3
colorama==0.4.4 colorama==0.4.4

查看文件

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