Spaces:
Sleeping
Sleeping
File size: 18,378 Bytes
2382288 |
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 |
"""
RAG κ²μ μ±λ΄ μΉ μ ν리μΌμ΄μ
- μ₯μΉ κ΄λ¦¬ API λΌμ°νΈ μ μ (API κ²½λ‘ μμ λ¨)
"""
import logging
import requests
from flask import request, jsonify
# λ‘κ±° κ°μ Έμ€κΈ°
logger = logging.getLogger(__name__)
def register_device_routes(app, login_required, DEVICE_SERVER_URL):
"""Flask μ ν리μΌμ΄μ
μ μ₯μΉ κ΄λ¦¬ κ΄λ ¨ λΌμ°νΈ λ±λ‘"""
# μ¬μ©μ μ§μ μ₯μΉ μλ² URL λ³μ
custom_device_url = None
# URL μ€μ ν¨μ
def get_device_url():
# μ¬μ©μ μ§μ URLμ΄ μμΌλ©΄ μ¬μ©, μμΌλ©΄ νκ²½λ³μ κ° μ¬μ©
return custom_device_url or DEVICE_SERVER_URL
@app.route('/api/device/connect', methods=['POST'])
@login_required
def connect_device_server():
"""μ¬μ©μ μ§μ μ₯μΉ μλ² URL μ°κ²° API"""
nonlocal custom_device_url # μμ μ€μ½νμ λ³μ μ°Έμ‘°
try:
# μμ²μμ URL κ°μ Έμ€κΈ°
request_data = request.get_json()
if not request_data or 'url' not in request_data:
logger.error("URLμ΄ μ 곡λμ§ μμμ΅λλ€.")
return jsonify({
"success": False,
"error": "URLμ΄ μ 곡λμ§ μμμ΅λλ€."
}), 400 # Bad Request
new_url = request_data['url'].strip()
if not new_url:
logger.error("URLμ΄ λΉμ΄ μμ΅λλ€.")
return jsonify({
"success": False,
"error": "URLμ΄ λΉμ΄ μμ΅λλ€."
}), 400 # Bad Request
# URL μ€μ
logger.info(f"μ¬μ©μ μ§μ μ₯μΉ μλ² URL μ€μ : {new_url}")
custom_device_url = new_url
# μ€μ λ URLλ‘ μν νμΈ μλ
try:
api_path = "/api/status"
logger.info(f"μ₯μΉ μλ² μν νμΈ μμ²: {custom_device_url}{api_path}")
response = requests.get(f"{custom_device_url}{api_path}", timeout=5)
if response.status_code == 200:
try:
data = response.json()
logger.info(f"μ¬μ©μ μ§μ μ₯μΉ μλ² μ°κ²° μ±κ³΅. μλ΅ λ°μ΄ν°: {data}")
return jsonify({
"success": True,
"message": "μ₯μΉ μλ² μ°κ²°μ μ±κ³΅νμ΅λλ€.",
"server_status": data.get("status", "μ μ")
})
except requests.exceptions.JSONDecodeError:
logger.error("μ₯μΉ μλ² μλ΅ JSON νμ± μ€ν¨")
return jsonify({
"success": False,
"error": "μ₯μΉ μλ²λ‘λΆν° μ ν¨νμ§ μμ JSON μλ΅μ λ°μμ΅λλ€."
}), 502 # Bad Gateway
else:
error_message = f"μ₯μΉ μλ² μλ΅ μ€λ₯: {response.status_code}"
try:
error_detail = response.json().get("error", response.text)
error_message += f" - {error_detail}"
except Exception:
error_message += f" - {response.text}"
logger.warning(error_message)
custom_device_url = None # μ°κ²° μ€ν¨ μ URL μ΄κΈ°ν
return jsonify({
"success": False,
"error": error_message
}), 502 # Bad Gateway
except requests.exceptions.Timeout:
logger.error(f"μ₯μΉ μλ² μ°κ²° νμμμ ({custom_device_url})")
custom_device_url = None # μ°κ²° μ€ν¨ μ URL μ΄κΈ°ν
return jsonify({
"success": False,
"error": "μ₯μΉ μλ² μ°κ²° νμμμ. μλ² μλ΅μ΄ λ무 λ립λλ€."
}), 504 # Gateway Timeout
except requests.exceptions.ConnectionError:
logger.error(f"μ₯μΉ κ΄λ¦¬ μλ² μ°κ²° μ€ν¨ ({custom_device_url})")
custom_device_url = None # μ°κ²° μ€ν¨ μ URL μ΄κΈ°ν
return jsonify({
"success": False,
"error": "μ₯μΉ κ΄λ¦¬ μλ²μ μ°κ²°ν μ μμ΅λλ€. μλ²κ° μ€ν μ€μΈμ§, URLμ΄ μ ννμ§ νμΈν΄μ£ΌμΈμ."
}), 502 # Bad Gateway
except Exception as e:
logger.error(f"μ₯μΉ μλ² μ°κ²° μ€ μμμΉ λͺ»ν μ€λ₯ λ°μ: {e}", exc_info=True)
custom_device_url = None # μ°κ²° μ€ν¨ μ URL μ΄κΈ°ν
return jsonify({
"success": False,
"error": f"μ₯μΉ μλ² μ°κ²° μ€ μ€λ₯ λ°μ: {str(e)}"
}), 500 # Internal Server Error
except Exception as e:
logger.error(f"/api/device/connect μ²λ¦¬ μ€ λ΄λΆ μλ² μ€λ₯: {e}", exc_info=True)
return jsonify({
"success": False,
"error": f"λ΄λΆ μλ² μ€λ₯ λ°μ: {str(e)}"
}), 500 # Internal Server Error
@app.route('/api/device/status', methods=['GET'])
@login_required
def device_status():
"""μ₯μΉ κ΄λ¦¬ μλ²μ μνλ₯Ό νμΈνλ API μλν¬μΈνΈ"""
try:
# requests λΌμ΄λΈλ¬λ¦¬λ ν¨μ λ΄μμ μν¬νΈν νμ μμ (μλ¨ μν¬νΈ μ¬μ©)
# json, jsonifyλ Flaskμμ μ΄λ―Έ μν¬νΈλ¨ (μλ¨ μν¬νΈ μ¬μ©)
# μ°κ²° νμμμ μ€μ
timeout = 5 # 5μ΄λ‘ νμμμ μ€μ
try:
# μ₯μΉ μλ² μν νμΈ - κ²½λ‘: /api/status
current_device_url = get_device_url()
api_path = "/api/status"
logger.info(f"μ₯μΉ μλ² μν νμΈ μμ²: {current_device_url}{api_path}")
response = requests.get(f"{current_device_url}{api_path}", timeout=timeout)
# μλ΅ μν μ½λ λ° λ΄μ© λ‘κΉ
(λλ²κΉ
κ°ν)
logger.debug(f"μ₯μΉ μλ² μλ΅ μν μ½λ: {response.status_code}")
try:
logger.debug(f"μ₯μΉ μλ² μλ΅ λ΄μ©: {response.text[:200]}...") # λ무 κΈΈλ©΄ μλΌμ λ‘κΉ
except Exception:
logger.debug("μ₯μΉ μλ² μλ΅ λ΄μ© λ‘κΉ
μ€ν¨ (ν
μ€νΈ νμ μλ?)")
if response.status_code == 200:
# μ±κ³΅ μ μλ΅ λ°μ΄ν° ꡬ쑰 νμΈ λ° λ‘κΉ
try:
data = response.json()
logger.info(f"μ₯μΉ μλ² μν νμΈ μ±κ³΅. μλ΅ λ°μ΄ν°: {data}")
return jsonify({"success": True, "status": "connected", "data": data})
except requests.exceptions.JSONDecodeError:
logger.error("μ₯μΉ μλ² μλ΅ JSON νμ± μ€ν¨")
return jsonify({
"success": False,
"error": "μ₯μΉ μλ²λ‘λΆν° μ ν¨νμ§ μμ JSON μλ΅μ λ°μμ΅λλ€."
}), 502 # Bad Gateway (μ
μ€νΈλ¦Ό μλ² μ€λ₯)
else:
# μ€ν¨ μ μ€λ₯ λ©μμ§ ν¬ν¨ λ‘κΉ
error_message = f"μ₯μΉ μλ² μλ΅ μ€λ₯: {response.status_code}"
try:
# μλ²μμ μ€λ₯ λ©μμ§λ₯Ό jsonμΌλ‘ 보λ΄λ κ²½μ° ν¬ν¨
error_detail = response.json().get("error", response.text)
error_message += f" - {error_detail}"
except Exception:
error_message += f" - {response.text}" # JSON νμ± μ€ν¨ μ μλ³Έ ν
μ€νΈ
logger.warning(error_message) # κ²½κ³ λ λ²¨λ‘ λ‘κΉ
return jsonify({
"success": False,
"error": error_message
}), 502 # Bad Gateway
except requests.exceptions.Timeout:
logger.error(f"μ₯μΉ μλ² μ°κ²° νμμμ ({DEVICE_SERVER_URL})")
return jsonify({
"success": False,
"error": "μ₯μΉ μλ² μ°κ²° νμμμ. μλ² μλ΅μ΄ λ무 λ립λλ€."
}), 504 # Gateway Timeout
except requests.exceptions.ConnectionError:
current_device_url = get_device_url()
logger.error(f"μ₯μΉ κ΄λ¦¬ μλ² μ°κ²° μ€ν¨ ({current_device_url})")
return jsonify({
"success": False,
"error": "μ₯μΉ κ΄λ¦¬ μλ²μ μ°κ²°ν μ μμ΅λλ€. μλ²κ° μ€ν μ€μΈμ§, URLμ΄ μ ννμ§ νμΈν΄μ£ΌμΈμ."
}), 502 # Bad Gateway (μ°κ²° μ€ν¨λ μ
μ€νΈλ¦Ό λ¬Έμ λ‘ κ°μ£Ό)
except Exception as e:
logger.error(f"μ₯μΉ μλ² μ°κ²° μ€ μμμΉ λͺ»ν μ€λ₯ λ°μ: {e}", exc_info=True) # μμΈ μ€ν νΈλ μ΄μ€ λ‘κΉ
return jsonify({
"success": False,
"error": f"μ₯μΉ μλ² μ°κ²° μ€ μ€λ₯ λ°μ: {str(e)}"
}), 500 # Internal Server Error (Flask μ± λ΄λΆ λλ requests λΌμ΄λΈλ¬λ¦¬ λ¬Έμ κ°λ₯μ±)
except Exception as e:
# μ΄ try-except λΈλ‘μ /api/device/status λΌμ°νΈ μ체μ λ΄λΆ μ€λ₯ μ²λ¦¬
logger.error(f"/api/device/status μ²λ¦¬ μ€ λ΄λΆ μλ² μ€λ₯: {e}", exc_info=True)
return jsonify({
"success": False,
"error": f"λ΄λΆ μλ² μ€λ₯ λ°μ: {str(e)}"
}), 500 # Internal Server Error
@app.route('/api/device/list', methods=['GET'])
@login_required
def device_list():
"""μ₯μΉ λͺ©λ‘ μ‘°ν API"""
logger.info("μ₯μΉ λͺ©λ‘ μ‘°ν μμ²")
try:
current_device_url = get_device_url()
api_path = "/api/devices"
logger.info(f"μ₯μΉ λͺ©λ‘ μ‘°ν μμ²: {current_device_url}{api_path}")
response = requests.get(f"{current_device_url}{api_path}", timeout=5)
logger.debug(f"μ₯μΉ λͺ©λ‘ μλ΅ μν μ½λ: {response.status_code}")
if response.status_code == 200:
try:
data = response.json()
devices = data.get("devices", [])
logger.info(f"μ₯μΉ λͺ©λ‘ μ‘°ν μ±κ³΅: {len(devices)}κ° μ₯μΉ")
return jsonify({
"success": True,
"devices": devices
})
except requests.exceptions.JSONDecodeError:
logger.error("μ₯μΉ λͺ©λ‘ μλ΅ JSON νμ± μ€ν¨")
return jsonify({"success": False, "error": "μ₯μΉ μλ²λ‘λΆν° μ ν¨νμ§ μμ JSON μλ΅"}), 502
else:
error_message = f"μ₯μΉ λͺ©λ‘ μ‘°ν μ€ν¨: {response.status_code}"
try: error_message += f" - {response.json().get('error', response.text)}"
except Exception: error_message += f" - {response.text}"
logger.warning(error_message)
return jsonify({
"success": False,
"error": error_message
}), 502
except requests.exceptions.Timeout:
logger.error("μ₯μΉ λͺ©λ‘ μ‘°ν μκ° μ΄κ³Ό")
return jsonify({
"success": False,
"error": "μ₯μΉ λͺ©λ‘ μ‘°ν μκ°μ΄ μ΄κ³Όλμμ΅λλ€."
}), 504
except requests.exceptions.ConnectionError:
logger.error("μ₯μΉ κ΄λ¦¬ μλ² μ°κ²° μ€ν¨")
return jsonify({
"success": False,
"error": "μ₯μΉ κ΄λ¦¬ μλ²μ μ°κ²°ν μ μμ΅λλ€. μλ²κ° μ€ν μ€μΈμ§ νμΈν΄μ£ΌμΈμ."
}), 503 # Service Unavailable
except Exception as e:
logger.error(f"μ₯μΉ λͺ©λ‘ μ‘°ν μ€ μ€λ₯ λ°μ: {e}", exc_info=True)
return jsonify({
"success": False,
"error": f"μ₯μΉ λͺ©λ‘ μ‘°ν μ€ μ€λ₯ λ°μ: {str(e)}"
}), 500
@app.route('/api/device/programs', methods=['GET'])
@login_required
def device_programs():
"""μ€ν κ°λ₯ν νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν API"""
logger.info("νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μμ²")
try:
current_device_url = get_device_url()
api_path = "/api/programs"
logger.info(f"νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μμ²: {current_device_url}{api_path}")
response = requests.get(f"{current_device_url}{api_path}", timeout=5)
logger.debug(f"νλ‘κ·Έλ¨ λͺ©λ‘ μλ΅ μν μ½λ: {response.status_code}")
if response.status_code == 200:
try:
data = response.json()
programs = data.get("programs", [])
logger.info(f"νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μ±κ³΅: {len(programs)}κ° νλ‘κ·Έλ¨")
return jsonify({
"success": True,
"programs": programs
})
except requests.exceptions.JSONDecodeError:
logger.error("νλ‘κ·Έλ¨ λͺ©λ‘ μλ΅ JSON νμ± μ€ν¨")
return jsonify({"success": False, "error": "μ₯μΉ μλ²λ‘λΆν° μ ν¨νμ§ μμ JSON μλ΅"}), 502
else:
error_message = f"νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μ€ν¨: {response.status_code}"
try: error_message += f" - {response.json().get('error', response.text)}"
except Exception: error_message += f" - {response.text}"
logger.warning(error_message)
return jsonify({
"success": False,
"error": error_message
}), 502
except requests.exceptions.Timeout:
logger.error("νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μκ° μ΄κ³Ό")
return jsonify({
"success": False,
"error": "νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μκ°μ΄ μ΄κ³Όλμμ΅λλ€."
}), 504
except requests.exceptions.ConnectionError:
logger.error("μ₯μΉ κ΄λ¦¬ μλ² μ°κ²° μ€ν¨")
return jsonify({
"success": False,
"error": "μ₯μΉ κ΄λ¦¬ μλ²μ μ°κ²°ν μ μμ΅λλ€. μλ²κ° μ€ν μ€μΈμ§ νμΈν΄μ£ΌμΈμ."
}), 503
except Exception as e:
logger.error(f"νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μ€ μ€λ₯ λ°μ: {e}", exc_info=True)
return jsonify({
"success": False,
"error": f"νλ‘κ·Έλ¨ λͺ©λ‘ μ‘°ν μ€ μ€λ₯ λ°μ: {str(e)}"
}), 500
@app.route('/api/device/programs/<program_id>/execute', methods=['POST'])
@login_required
def execute_program(program_id):
"""νλ‘κ·Έλ¨ μ€ν API"""
logger.info(f"νλ‘κ·Έλ¨ μ€ν μμ²: {program_id}")
try:
current_device_url = get_device_url()
api_path = f"/api/programs/{program_id}/execute"
logger.info(f"νλ‘κ·Έλ¨ μ€ν μμ²: {current_device_url}{api_path}")
response = requests.post(
f"{current_device_url}{api_path}",
json={}, # νμμ μ¬κΈ°μ νλΌλ―Έν° μΆκ° κ°λ₯
timeout=10 # νλ‘κ·Έλ¨ μ€νμλ λ κΈ΄ μκ° λΆμ¬
)
logger.debug(f"νλ‘κ·Έλ¨ μ€ν μλ΅ μν μ½λ: {response.status_code}")
if response.status_code == 200:
try:
data = response.json()
success = data.get("success", False)
message = data.get("message", "")
logger.info(f"νλ‘κ·Έλ¨ μ€ν μλ΅: {success}, {message}")
return jsonify(data) # μλ² μλ΅ κ·Έλλ‘ λ°ν
except requests.exceptions.JSONDecodeError:
logger.error("νλ‘κ·Έλ¨ μ€ν μλ΅ JSON νμ± μ€ν¨")
# μ±κ³΅(200)νμ§λ§ μλ΅μ΄ λΉμ μμ μΈ κ²½μ°
return jsonify({
"success": False, # νμ± μ€ν¨νμΌλ―λ‘ Falseλ‘ κ°μ£Ό
"error": "νλ‘κ·Έλ¨ μ€ν μλ²λ‘λΆν° μ ν¨νμ§ μμ JSON μλ΅"
}), 502
else:
error_message = f"νλ‘κ·Έλ¨ μ€ν μμ² μ€ν¨: {response.status_code}"
try: error_message += f" - {response.json().get('error', response.text)}"
except Exception: error_message += f" - {response.text}"
logger.warning(error_message)
return jsonify({
"success": False,
"error": error_message
}), 502 # λλ response.status_code λ₯Ό κ·Έλλ‘ λ°ννλ κ²λ κ³ λ €
except requests.exceptions.Timeout:
logger.error("νλ‘κ·Έλ¨ μ€ν μμ² μκ° μ΄κ³Ό")
return jsonify({
"success": False,
"error": "νλ‘κ·Έλ¨ μ€ν μμ² μκ°μ΄ μ΄κ³Όλμμ΅λλ€."
}), 504
except requests.exceptions.ConnectionError:
logger.error("μ₯μΉ κ΄λ¦¬ μλ² μ°κ²° μ€ν¨")
return jsonify({
"success": False,
"error": "μ₯μΉ κ΄λ¦¬ μλ²μ μ°κ²°ν μ μμ΅λλ€. μλ²κ° μ€ν μ€μΈμ§ νμΈν΄μ£ΌμΈμ."
}), 503
except Exception as e:
logger.error(f"νλ‘κ·Έλ¨ μ€ν μ€ μ€λ₯ λ°μ: {e}", exc_info=True)
return jsonify({
"success": False,
"error": f"νλ‘κ·Έλ¨ μ€ν μ€ μ€λ₯ λ°μ: {str(e)}"
}), 500 |