-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.py
More file actions
224 lines (192 loc) · 7.45 KB
/
Copy pathapi.py
File metadata and controls
224 lines (192 loc) · 7.45 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from .bottle import run, route, get, request
from .colors import AMBER, BLUE, GREEN, PALETTES, RED, WHITE, Palette
from .confloader import CONFIG, conf_map, read_config_knulli, set_option
from .effects.effect_store import MODES, NOTIS
from .state import RGBState, Event, EventType
from .utilities import Color, hex_to_rgb
from copy import deepcopy
from json import dumps
STATE = RGBState.get()
presets = {
'battery_charging': [
Event(EventType.Notification, 'pulse', 3, GREEN),
],
'battery_discharging1': [
Event(EventType.Notification, 'pulse_back', 1, RED),
],
'battery_discharging2': [
Event(EventType.Notification, 'pulse_back', 1, Palette([1,0.7,0])),
],
'battery_discharging3': [
Event(EventType.Notification, 'pulse_back', 1, GREEN),
],
'battery_full': [
Event(EventType.Notification, 'pulse', 1, GREEN),
Event(EventType.Notification, 'fill', 1, GREEN),
Event(EventType.Notification, 'blink_off', 1, GREEN),
],
'battery_low1': [
Event(EventType.Notification, 'blink', 2, AMBER),
],
'battery_low2': [
Event(EventType.Notification, 'blink', 3, RED),
],
'cheevo': [
Event(EventType.Notification, 'cheevo', 1),
]
}
def run_preset_effect(preset):
print(f"[animation] preset: [{preset}]")
STATE.events.append(Event(EventType.FadeOut))
for e in presets[preset]:
STATE.events.append(deepcopy(e))
STATE.events.append(Event(EventType.FadeIn))
@route("/reload-config")
def reload_config():
read_config_knulli()
STATE.events.append(Event(EventType.LoadConfig))
return ""
@route("/set-config", method='POST')
def set_config():
req = request.body.read().decode().split(' ', maxsplit=1) # pyright: ignore[reportAttributeAccessIssue]
set_option(req[0], req[1])
STATE.events.append(Event(EventType.LoadConfig))
return f"[{req[0]}]: {req[1]}\n"
@route("/animation", method='POST')
def animation():
req = request.body.read().decode().split(";") # pyright: ignore[reportAttributeAccessIssue]
command_list = []
for com in req:
com2 = com.strip()
if com2 in presets:
run_preset_effect(com2)
else:
try:
n, c, hex_ = com2.split()
command_list.append(Event(EventType.Notification, n, int(c), Palette(hex_to_rgb(hex_))))
except Exception as e:
return "Error while processing Command:\n[name] [count] [hex_color]\n"
if len(command_list) > 0:
STATE.events.append(Event(EventType.FadeOut))
for c in command_list:
STATE.events.append(c)
STATE.events.append(Event(EventType.FadeIn))
return ""
@route("/update-battery-state", method='POST')
def battery():
req = request.body.read().decode().split() # pyright: ignore[reportAttributeAccessIssue]
last_pct = STATE.DEV.BATTERY['percentage']
cur_pct = int(req[0])
thresh_pct = CONFIG["battery.low.threshold"]
last_state = STATE.DEV.BATTERY['state']
cur_state = req[1]
if cur_state != last_state or cur_pct != last_pct:
print(f"[bat] [{last_pct}/{last_state}] -> [{cur_pct}/{cur_state}]")
if CONFIG['battery.charging'] == 'notification' and cur_state != last_state: # notification mode
if cur_state == 'Charging':
run_preset_effect('battery_charging')
if cur_state == 'Full':
run_preset_effect('battery_full')
if cur_state == 'Discharging':
if STATE.DEV.BATTERY['percentage'] < 5:
run_preset_effect('battery_discharging1')
elif STATE.DEV.BATTERY['percentage'] < 50:
run_preset_effect('battery_discharging2')
else:
run_preset_effect('battery_discharging3')
STATE.events.append(Event(EventType.RemoveLayer, 'charging'))
elif CONFIG['battery.charging'] == 'continuous' and cur_state != last_state:
if cur_state != last_state:
if cur_state == 'Charging':
STATE.events.append(Event(EventType.AddLayer, 'charging'))
else:
STATE.events.append(Event(EventType.RemoveLayer, 'charging'))
elif cur_state != last_state:
STATE.events.append(Event(EventType.RemoveLayer, 'charging'))
if CONFIG['battery.low'] == 'notification' and cur_state == 'Discharging' and cur_pct != last_pct:
if cur_pct <= thresh_pct:
run_preset_effect('battery_low1')
elif cur_pct <= 5:
run_preset_effect('battery_low2')
elif CONFIG['battery.low'] == 'continuous' and cur_state == 'Discharging' and (cur_pct != last_pct or cur_state != last_state):
if cur_pct <= thresh_pct:
STATE.events.append(Event(EventType.AddLayer, 'bat_low'))
elif cur_state != 'Discharging' or cur_pct > thresh_pct:
STATE.events.append(Event(EventType.RemoveLayer, 'bat_low'))
STATE.DEV.BATTERY['state'] = cur_state
STATE.DEV.BATTERY['percentage'] = cur_pct
@route("/update-screen-state", method='POST')
def screen():
req = request.body.read().decode() # pyright: ignore[reportAttributeAccessIssue]
try:
# Clamp value between 0 and 100 immediately
cur_pct = max(0, min(int(req), 100))
except ValueError:
print("[screen] Error: Received invalid integer payload")
return
if CONFIG['brightness.adaptive']:
# Use the cleaned variable for the comparison
if STATE._target_sc != cur_pct:
STATE._target_sc = 16 + int(cur_pct * 0.84) if cur_pct > 0 else 0
print(f"[screen] [{STATE._sc}] -> [{STATE._target_sc}]")
STATE.DEV.nuke_savestates()
STATE._idle = False
@get("/kill")
def kill():
STATE.events.append(Event(EventType.FadeOut))
#STATE.events.append(Event(EventType.Notification, 'blink_on', 1, WHITE))
#STATE.events.append(Event(EventType.Notification, 'round_back', 1, WHITE))
STATE.events.append(Event(EventType.Die))
@get("/get-settings")
def settings():
c = {}
for k, v in conf_map.items():
add = True
if "reqs" in v:
for r in v["reqs"]:
if r not in STATE.DEV.TRAITS:
add = False
if add:
c[k] = v
return dumps(c, indent=4)+"\n"
@get("/get-modes")
def get_modes():
m = {}
for k, v in MODES.items():
add = True
if "reqs" in v["metadata"]:
for r in v["metadata"]["reqs"]:
if r not in STATE.DEV.TRAITS:
add = False
if add:
m[k] = {
"name": v["metadata"]["name"]
}
return dumps(m, indent=4)+"\n"
@get("/get-animations")
def get_anim():
m = {}
for k, v in NOTIS.items():
add = True
if "reqs" in v["metadata"]:
for r in v["metadata"]["reqs"]:
if r not in STATE.DEV.TRAITS:
add = False
if add:
m[k] = {
"name": v["metadata"]["name"]
}
return dumps(m, indent=4)+"\n"
@get("/get-palettes")
def get_palettes():
p = {}
for k, v1 in PALETTES.items():
p[k] = {
"name": f"{k} ({v1[0]}, {v1[1]})"
}
return dumps(p, indent=4)+"\n"
def run_api():
print("Starting HTTP Daemon: http://localhost:1235/")
run(host='localhost', port=1235, quiet=True)
if __name__ == '__main__':
run_api()