From de54c1ff402182768ccacef6648a50a3da49d436 Mon Sep 17 00:00:00 2001 From: Repinoid Date: Sun, 14 Jun 2026 14:05:00 +0400 Subject: [PATCH] =?UTF-8?q?feat:=20POST=20/api/v1/test/next=20+=20=D0=B0?= =?UTF-8?q?=D0=B2=D1=82=D0=BE-=D0=BF=D0=BE=D0=B4=D0=B1=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/routes.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/api/routes.py b/api/routes.py index 728ea27..3a02ea0 100644 --- a/api/routes.py +++ b/api/routes.py @@ -335,6 +335,42 @@ def register(app): return jsonify({"ok": True}) + @app.route("/api/v1/test/next", methods=["POST"]) + def test_next(): + """Авто-подбор параметров теста. Принимает результаты, возвращает следующий скрипт или done.""" + data = request.get_json(silent=True) or {} + results = data.get("results", []) + run = data.get("run", 0) + + # Анализ результатов + total = len(results) + if total == 0: + return jsonify({"done": True, "message": "Нет данных", "script": None}) + + ok_count = sum(1 for r in results if r.get("raw", "").startswith("41")) + err_pct = (total - ok_count) * 100 // total if total > 0 else 100 + wait_ms = int(request.args.get("wait", data.get("wait_ms", 1500))) + + max_runs = 5 + if err_pct < 20 or run >= max_runs: + return jsonify({ + "done": True, + "message": f"✅ Стабильно: {ok_count}/{total} ({err_pct}% ошибок) на wait={wait_ms}ms", + "script": None, + "final_wait_ms": wait_ms, + }) + + # Увеличиваем паузу + new_wait = wait_ms + 300 + return jsonify({ + "done": False, + "message": f"⚠️ {err_pct}% ошибок — увеличиваю паузу до {new_wait}ms", + "run": run + 1, + "wait_ms": new_wait, + "script": build_test_script(wait_ms=new_wait, pids=["010C", "0106"], repeat=6), + }) + + def _save_profile(mac: str, profile: dict): """Сохраняет профиль в БД (best-effort).""" try: