-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
410 lines (330 loc) · 13.7 KB
/
app.py
File metadata and controls
410 lines (330 loc) · 13.7 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
from flask import Flask, request, jsonify
import requests
import json
import threading
from sub.scheduler import *
from sub.ruleEngine import *
from dotenv import load_dotenv
import os, sys
from libs.device_binding import enforce_mac_binding
from libs.edit import deleteItem, file_changed_request, putItem, update_env_file # type: ignore
from wifi_config.api import create_wifi_blueprint
from wifi_config.bootstrap import ensure_bootstrap_ap, watch_disconnection_and_start_ap
load_dotenv(dotenv_path='.env')
if not enforce_mac_binding():
raise SystemExit(1)
res_file_path= os.environ.get('res_file_path')
cert_file_path= os.environ.get('cert_file_path')
schedules_file_path = os.environ.get('schedules_file_path')
rules_file_path = os.environ.get('rules_file_path')
rooms_file_path = os.environ.get('rooms_file_path')
devices_file_path = os.environ.get('devices_file_path')
notifications_file_path = os.environ.get('notifications_file_path')
HA_host = os.environ.get('HA_host')
hass_token = os.environ.get('hass_token')
def config():
if not os.path.exists(res_file_path):
os.makedirs(res_file_path)
print(f"폴더 생성: {res_file_path}")
if not os.path.exists(cert_file_path):
os.makedirs(cert_file_path)
print(f"폴더 생성: {cert_file_path}")
file_list = [schedules_file_path, rules_file_path, rooms_file_path, devices_file_path, notifications_file_path]
for f in file_list:
if not os.path.exists(f):
with open(f, 'w') as f:
json.dump([], f)
print(f"{f} 파일이 생성되었습니다.")
app = Flask(__name__)
app.register_blueprint(create_wifi_blueprint())
def _start_wifi_bootstrap_thread() -> None:
def _run() -> None:
result = ensure_bootstrap_ap()
print(
"[WIFI][BOOTSTRAP] result "
f"reason={result.get('reason')} started={result.get('started')}"
)
threading.Thread(target=_run, daemon=True, name="wifi-bootstrap").start()
_start_wifi_bootstrap_thread()
def _start_wifi_watchdog_thread() -> None:
threading.Thread(
target=watch_disconnection_and_start_ap,
daemon=True,
name="wifi-ap-watchdog",
).start()
_start_wifi_watchdog_thread()
@app.route('/test', methods=['POST'])
def test():
return '@@@', 200
@app.route('/local/api/config/ha/cert', methods=["POST","DELETE", "PUT"])
def configHACert():
hass_token = request.json["hass_token"]
env_file_path = '.env'
update_env_file(env_file_path, 'hass_token', hass_token)
return 'Success', 200
@app.route('/local/api/config/aws/cert', methods=["POST","DELETE", "PUT"])
def configAwsCert():
root_ca = request.json["root_ca"]
certificate = request.json["certificate"]
private_key = request.json["private_key"]
# 업데이트된 데이터를 JSON 파일에 다시 저장5
with open('./cert/root-CA.crt', 'w', encoding='utf-8') as file:
file.write(root_ca)
with open('./cert/matterHub.cert.pem', 'w', encoding='utf-8') as file:
file.write(certificate)
with open('./cert/matterHub.private.key', 'w', encoding='utf-8') as file:
file.write(private_key)
return 'Success', 200
@app.route('/local/api/config/aws/id', methods=["POST","DELETE", "PUT"])
def configAwsId():
matterhub_id = request.json["matterhub_id"]
certificate = request.json["certificate"]
private_key = request.json["private_key"]
env_file_path = '.env'
update_env_file(env_file_path, 'matterhub_id', matterhub_id)
return 'Success', 200
@app.route('/webhook', methods=['POST'])
def webhook():
return jsonify({
"status": "disabled",
"message": "webhook update is disabled; use reverse tunnel maintenance workflow",
}), 410
@app.route('/local/api', methods=["POST","DELETE", "PUT", "GET"])
def home():
headers = {"Authorization": f"Bearer {hass_token}"}
response = requests.get(f"{HA_host}/api/", headers=headers)
return str(response.json())
@app.route('/local/api/services')
def services():
headers = {"Authorization": f"Bearer {hass_token}"}
response = requests.get(f"{HA_host}/api/services", headers=headers)
return jsonify(response.json())
@app.route('/local/api/states')
def states():
"""
HA /api/states 프록시.
- HA 쪽에서 JSON 이 아닌 응답(HTML, 에러페이지 등)을 주면 json()에서 예외가 나므로
이를 잡아서 에러 내용을 그대로 반환하고, HTTP 상태코드를 함께 노출한다.
"""
headers = {"Authorization": f"Bearer {hass_token}"}
resp = requests.get(f"{HA_host}/api/states", headers=headers)
try:
data = resp.json()
except Exception as e: # JSONDecodeError, ValueError 등
# 디버깅을 위해 앞부분 텍스트를 로그/응답에 남긴다.
text_snippet = resp.text[:200] if resp.text else ""
return jsonify({
"error": "ha_states_invalid_json",
"message": str(e),
"status_code": resp.status_code,
"body_snippet": text_snippet,
}), 502
return jsonify(data)
@app.route('/local/api/states/<entity_id>')
def statesEntityId(entity_id):
headers = {"Authorization": f"Bearer {hass_token}"}
response = requests.get(f"{HA_host}/api/states/{entity_id}", headers=headers)
return jsonify(response.json())
@app.route('/local/api/devices/<entity_id>/command', methods=["POST"])
def device_command(entity_id):
headers = {"Authorization": f"Bearer {hass_token}"}
body = {
"entity_id": entity_id
}
_r = {**request.json}
_r.pop('domain')
_r.pop('service')
merged_dict = {**body, **_r}
print(merged_dict)
response = requests.post(f"{HA_host}/api/services/{request.json['domain']}/{request.json['service']}", data=json.dumps(merged_dict), headers=headers)
return jsonify(response.json())
@app.route('/local/api/devices/<entity_id>/status', methods=["GET"])
def device_status(entity_id):
headers = {"Authorization": f"Bearer {hass_token}"}
response = requests.get(f"{HA_host}/api/states/{entity_id}", headers=headers)
return jsonify(response.json())
@app.route('/local/api/devices/<entity_id>/services', methods=["GET"])
def device_services(entity_id):
target_entity = entity_id
target_domain = target_entity.split('.')[0]
url = f"{HA_host}/api/services"
headers = {"Authorization": f"Bearer {hass_token}"}
response = requests.get(url, headers=headers)
all_domain = json.loads(response.content)
for d in all_domain:
if(d['domain'] == target_domain):
switch_services = d['services']
return jsonify(switch_services)
return jsonify({})
@app.route('/local/api/devices', methods=["POST","DELETE", "PUT", "GET"])
def devices():
try:
with open(devices_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
except FileNotFoundError:
data = [] # 파일이 없으면 빈 리스트로 초기화
if request.method == "POST":
new_data = request.json
data.append(new_data)
if request.method == "DELETE":
target_value = request.json['entity_id']
data = deleteItem(data, "entity_id", target_value)
if request.method == "PUT":
target_value = request.json['entity_id']
data = putItem(data, "entity_id", target_value, request.json)
if request.method == "GET":
pass
# 업데이트된 데이터를 JSON 파일에 다시 저장5
with open(devices_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
schedule_config(one_time)
return jsonify(data)
@app.route('/local/api/schedules', methods=["POST","DELETE", "PUT", "GET"])
def schdules():
# 파일에서 기존 데이터 읽기
try:
with open(schedules_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
except FileNotFoundError:
data = [] # 파일이 없으면 빈 리스트로 초기화
if request.method == "POST":
new_data = request.json
new_data.setdefault('activate', True)
data.append(new_data)
if request.method == "DELETE":
target_value = request.json['id']
data = deleteItem(data, "id", target_value)
if request.method == "PUT":
target_value = request.json['id']
data = putItem(data, "id", target_value, request.json)
if request.method == "GET":
pass
# 업데이트된 데이터를 JSON 파일에 다시 저장5
with open(schedules_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
if(request.method!="GET"):
schedule_config(one_time)
return jsonify(data)
@app.route('/local/api/schedules/<schedule_id>', methods=["POST","DELETE", "PUT", "GET"])
def schdules_id(schedule_id):
# 파일에서 기존 데이터 읽기
try:
with open(schedules_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
except FileNotFoundError:
data = [] # 파일이 없으면 빈 리스트로 초기화
if request.method == "POST":
new_data = request.json
new_data.setdefault('activate', True)
data.append(new_data)
if request.method == "DELETE":
data = deleteItem(data, "id", schedule_id)
if request.method == "PUT":
data = putItem(data, "id", schedule_id, request.json)
if request.method == "GET":
pass
# 업데이트된 데이터를 JSON 파일에 다시 저장5
with open(schedules_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
if(request.method!="GET"):
schedule_config(one_time)
return jsonify(data)
@app.route('/local/api/rules', methods=["POST","DELETE", "PUT", "GET"])
def rules():
try:
with open(rules_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
except FileNotFoundError:
data = [] # 파일이 없으면 빈 리스트로 초기화
if request.method == "POST":
new_data = request.json
new_data.setdefault('activate', True)
data.append(new_data)
if request.method == "DELETE":
target_value = request.json['id']
data = deleteItem(data, "id", target_value)
if request.method == "PUT":
target_value = request.json['id']
data = putItem(data, "id", target_value, request.json)
if request.method == "GET":
pass
# 업데이트된 데이터를 JSON 파일에 다시 저장5
with open(rules_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
if(request.method!="GET"):
file_changed_request("rules_file_changed")
return jsonify(data)
@app.route('/local/api/rooms', methods=["POST","DELETE", "PUT", "GET"])
def rooms():
try:
with open(rooms_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
except FileNotFoundError:
data = [] # 파일이 없으면 빈 리스트로 초기화
if request.method == "POST":
new_data = request.json
new_data.setdefault('activate', True)
data.append(new_data)
if request.method == "DELETE":
target_value = request.json['id']
data = deleteItem(data, "id", target_value)
if request.method == "PUT":
target_value = request.json['id']
data = putItem(data, "id", target_value, request.json)
if request.method == "GET":
pass
# 업데이트된 데이터를 JSON 파일에 다시 저장5
with open(rooms_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
schedule_config(one_time)
return jsonify(data)
@app.route('/local/api/notifications', methods=["POST","DELETE", "PUT", "GET"])
def notifications():
try:
with open(notifications_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
except FileNotFoundError:
data = [] # 파일이 없으면 빈 리스트로 초기화
if request.method == "POST":
new_data = request.json
data.append(new_data)
if request.method == "DELETE":
target_value = request.json['id']
data = deleteItem(data, "id", target_value)
if request.method == "PUT":
target_value = request.json['id']
data = putItem(data, "id", target_value, request.json)
if request.method == "GET":
pass
# 업데이트된 데이터를 JSON 파일에 다시 저장5
with open(notifications_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
if(request.method!="GET"):
res= file_changed_request("notifications_file_changed")
print(res.content)
return jsonify(data)
@app.route('/local/api/matterhub/id', methods=["GET"])
def matterhub_id():
matterhub_id = os.environ.get('matterhub_id', '').strip('"')
return jsonify({"matterhub_id": matterhub_id})
config()
one_time = one_time_schedule()
schedule_config(one_time)
p = threading.Thread(target=periodic_scheduler)
p.start()
o = threading.Thread(target=one_time_scheduler, args=[one_time])
o.start()
if __name__ == '__main__':
# 운영 환경(systemd)에서는 debug/reloader 비활성화가 기본이다.
# 필요할 때만 WM_DEBUG=1 로 켜서 사용.
_debug = os.environ.get("WM_DEBUG", "0").strip().lower() in ("1", "true", "yes", "y")
app.run('0.0.0.0', debug=_debug, use_reloader=_debug, port=8100)