тест 2
This commit is contained in:
43
agent.py
43
agent.py
@@ -18,6 +18,7 @@ POLL_INTERVAL = 3
|
||||
REQUEST_TIMEOUT = 3
|
||||
RECONNECT_DELAY = 5
|
||||
|
||||
|
||||
def read_vmix_dynamic_values():
|
||||
try:
|
||||
resp = requests.get(VMIX_API, timeout=REQUEST_TIMEOUT)
|
||||
@@ -55,6 +56,7 @@ def read_vmix_dynamic_values():
|
||||
"operator_login": None,
|
||||
}
|
||||
|
||||
|
||||
def execute_vmix_command(path: str):
|
||||
path = str(path or "").strip()
|
||||
if not path.startswith("/api/"):
|
||||
@@ -76,52 +78,55 @@ def execute_vmix_command(path: str):
|
||||
"error": str(e),
|
||||
}
|
||||
|
||||
async def wait_for_session():
|
||||
|
||||
async def wait_for_routing():
|
||||
while True:
|
||||
data = read_vmix_dynamic_values()
|
||||
if data["ok"] and data["session_token"]:
|
||||
if data["ok"] and data["match_id"] is not None and data["operator_login"]:
|
||||
return data
|
||||
|
||||
print("[agent] waiting for vMix session...", data)
|
||||
print("[agent] waiting for match_id + operator_login...", data)
|
||||
await asyncio.sleep(POLL_INTERVAL)
|
||||
|
||||
def build_ws_url(session_token: str, match_id: int | None, group_name: str | None, operator_login: str | None):
|
||||
|
||||
def build_ws_url(match_id: int, group_name: str | None, operator_login: str):
|
||||
params = {
|
||||
"client_id": CLIENT_ID,
|
||||
"session_token": session_token,
|
||||
"match_id": match_id,
|
||||
"operator_name": operator_login,
|
||||
}
|
||||
if match_id is not None:
|
||||
params["match_id"] = match_id
|
||||
if group_name:
|
||||
params["group_name"] = group_name
|
||||
if operator_login:
|
||||
params["operator_name"] = operator_login
|
||||
return f"{WS_BASE}?{urlencode(params)}"
|
||||
|
||||
|
||||
async def ping_loop(ws):
|
||||
while True:
|
||||
await asyncio.sleep(15)
|
||||
await ws.send(json.dumps({"type": "ping"}))
|
||||
|
||||
|
||||
async def run_agent():
|
||||
current_session = None
|
||||
current_match_id = None
|
||||
current_group_name = None
|
||||
current_operator_login = None
|
||||
|
||||
while True:
|
||||
vmix_data = await wait_for_session()
|
||||
vmix_data = await wait_for_routing()
|
||||
|
||||
session_token = vmix_data["session_token"]
|
||||
match_id = vmix_data["match_id"]
|
||||
group_name = vmix_data["group_name"]
|
||||
operator_login = vmix_data["operator_login"]
|
||||
|
||||
if current_session != session_token:
|
||||
print(f"[agent] found session: {session_token}")
|
||||
current_session = session_token
|
||||
if current_match_id != match_id or current_operator_login != operator_login or current_group_name != group_name:
|
||||
print(
|
||||
f"[agent] routing: match_id={match_id}, operator_login={operator_login}, group_name={group_name}"
|
||||
)
|
||||
current_match_id = match_id
|
||||
current_group_name = group_name
|
||||
current_operator_login = operator_login
|
||||
|
||||
ws_url = build_ws_url(session_token, match_id, group_name, operator_login)
|
||||
ws_url = build_ws_url(match_id, group_name, operator_login)
|
||||
|
||||
try:
|
||||
async with websockets.connect(ws_url, ping_interval=None, max_size=2**20) as ws:
|
||||
@@ -130,7 +135,6 @@ async def run_agent():
|
||||
await ws.send(json.dumps({
|
||||
"type": "register",
|
||||
"client_id": CLIENT_ID,
|
||||
"session_token": session_token,
|
||||
"match_id": match_id,
|
||||
"group_name": group_name,
|
||||
"operator_name": operator_login,
|
||||
@@ -140,7 +144,6 @@ async def run_agent():
|
||||
|
||||
try:
|
||||
while True:
|
||||
# следим, не сменился ли проект/session в vMix
|
||||
latest = read_vmix_dynamic_values()
|
||||
if latest["ok"]:
|
||||
latest_match_id = latest.get("match_id")
|
||||
@@ -167,7 +170,6 @@ async def run_agent():
|
||||
await ws.send(json.dumps({
|
||||
"type": "vmix_result",
|
||||
"client_id": CLIENT_ID,
|
||||
"session_token": session_token,
|
||||
"match_id": match_id,
|
||||
"operator_name": operator_login,
|
||||
"results": results,
|
||||
@@ -181,5 +183,6 @@ async def run_agent():
|
||||
|
||||
await asyncio.sleep(RECONNECT_DELAY)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_agent())
|
||||
asyncio.run(run_agent())
|
||||
|
||||
Reference in New Issue
Block a user