-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (82 loc) · 2.53 KB
/
Copy pathmain.py
File metadata and controls
106 lines (82 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# G.O.H
# Joe Jagger
# External Imports
from govee_api_ble import GoveeDevice
import json
from pathlib import Path
from socket import gethostname
from quart import Quart,request, jsonify,send_from_directory,redirect
from uvicorn import run
# Internal Imports
from core.utils import str2bool
app = Quart(__name__)
CONFIG = json.loads(Path("./config.json").read_text("utf-8"))
try:
device = GoveeDevice(CONFIG.get("govee_mac"))
except Exception as e:
print(f"[FATAL] Failed to initalize Govee BLE API Device: {e}")
exit(1)
@app.get("/")
async def dash():
return await send_from_directory(Path("./static"),"dash.html")
@app.get("/goh/api/set_power")
async def set_power():
state = str2bool(request.args.get("state","false"))
try:
device.setPower(state)
return jsonify({
"message":f"Set lights to {state} successfully."
})
except Exception as e:
print(f"ERROR: {e}")
return jsonify({
"message":"Internal server error. Check STDOUT."
})
@app.get("/goh/api/set_brightness")
async def set_brightness():
percent = request.args.get("percent",None)
if not percent:
return jsonify({
"message":"Please pass ?percent=0-100"
})
try:
device.setBrightness(int(percent))
return jsonify({
"message":f"Set lights to {percent} successfully."
})
except Exception as e:
print(f"ERROR: {e}")
return jsonify({
"message":"Internal server error. Check STDOUT."
})
@app.get("/goh/api/set_colour")
async def set_colour():
rgb = request.args.get("rgb",None)
segments = request.args.get("segments",None)
if not rgb:
return jsonify({
"message":"Please pass ?rgb=rgb-values-here"
})
if segments:
segments = [int(x) for x in segments.split("-")]
rgb = [int(x) for x in rgb.split("-")]
if len(rgb) != 3:
return jsonify({
"message":"Failed to split rgb internally. Ensure '000-000-000' layout."
})
try:
if not segments:
device.setColor(c=rgb)
else:
device.setColor(c=rgb,segment=segments)
return jsonify({
"message":f"Set lights to {rgb} successfully."
})
except Exception as e:
print(f"ERROR: {e}")
return jsonify({
"message":"Internal server error. Check STDOUT."
})
if __name__ == "__main__":
print("GOH ASGI Starting.")
run("main:app",port=8585,host="0.0.0.0",workers=2)