переделал систему подключения агента к веб-сокету
This commit is contained in:
30
agent.py
30
agent.py
@@ -36,12 +36,14 @@ def read_vmix_dynamic_values():
|
||||
session_token = get_text("./dynamic/value1")
|
||||
match_id = get_text("./dynamic/value2")
|
||||
group_name = get_text("./dynamic/value3")
|
||||
operator_login = get_text("./dynamic/value4")
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"session_token": session_token,
|
||||
"match_id": int(match_id) if match_id and match_id.isdigit() else None,
|
||||
"group_name": group_name,
|
||||
"operator_login": operator_login,
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
@@ -50,6 +52,7 @@ def read_vmix_dynamic_values():
|
||||
"session_token": None,
|
||||
"match_id": None,
|
||||
"group_name": None,
|
||||
"operator_login": None,
|
||||
}
|
||||
|
||||
def execute_vmix_command(path: str):
|
||||
@@ -82,7 +85,7 @@ async def wait_for_session():
|
||||
print("[agent] waiting for vMix session...", data)
|
||||
await asyncio.sleep(POLL_INTERVAL)
|
||||
|
||||
def build_ws_url(session_token: str, match_id: int | None, group_name: str | None):
|
||||
def build_ws_url(session_token: str, match_id: int | None, group_name: str | None, operator_login: str | None):
|
||||
params = {
|
||||
"client_id": CLIENT_ID,
|
||||
"session_token": session_token,
|
||||
@@ -91,6 +94,8 @@ def build_ws_url(session_token: str, match_id: int | None, group_name: str | Non
|
||||
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):
|
||||
@@ -100,6 +105,9 @@ async def ping_loop(ws):
|
||||
|
||||
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()
|
||||
@@ -107,12 +115,13 @@ async def run_agent():
|
||||
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
|
||||
|
||||
ws_url = build_ws_url(session_token, match_id, group_name)
|
||||
ws_url = build_ws_url(session_token, match_id, group_name, operator_login)
|
||||
|
||||
try:
|
||||
async with websockets.connect(ws_url, ping_interval=None, max_size=2**20) as ws:
|
||||
@@ -124,6 +133,7 @@ async def run_agent():
|
||||
"session_token": session_token,
|
||||
"match_id": match_id,
|
||||
"group_name": group_name,
|
||||
"operator_name": operator_login,
|
||||
}))
|
||||
|
||||
pinger = asyncio.create_task(ping_loop(ws))
|
||||
@@ -132,9 +142,17 @@ async def run_agent():
|
||||
while True:
|
||||
# следим, не сменился ли проект/session в vMix
|
||||
latest = read_vmix_dynamic_values()
|
||||
if latest["ok"] and latest["session_token"] and latest["session_token"] != current_session:
|
||||
print("[agent] session changed in vMix, reconnecting")
|
||||
break
|
||||
if latest["ok"]:
|
||||
latest_match_id = latest.get("match_id")
|
||||
latest_group_name = latest.get("group_name")
|
||||
latest_operator_login = latest.get("operator_login")
|
||||
if (
|
||||
latest_match_id != match_id
|
||||
or latest_group_name != group_name
|
||||
or latest_operator_login != operator_login
|
||||
):
|
||||
print("[agent] routing meta changed in vMix, reconnecting")
|
||||
break
|
||||
|
||||
try:
|
||||
raw = await asyncio.wait_for(ws.recv(), timeout=3)
|
||||
@@ -150,6 +168,8 @@ async def run_agent():
|
||||
"type": "vmix_result",
|
||||
"client_id": CLIENT_ID,
|
||||
"session_token": session_token,
|
||||
"match_id": match_id,
|
||||
"operator_name": operator_login,
|
||||
"results": results,
|
||||
"sent_at": time.time(),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user