тест 2
This commit is contained in:
43
agent.py
43
agent.py
@@ -18,6 +18,7 @@ POLL_INTERVAL = 3
|
|||||||
REQUEST_TIMEOUT = 3
|
REQUEST_TIMEOUT = 3
|
||||||
RECONNECT_DELAY = 5
|
RECONNECT_DELAY = 5
|
||||||
|
|
||||||
|
|
||||||
def read_vmix_dynamic_values():
|
def read_vmix_dynamic_values():
|
||||||
try:
|
try:
|
||||||
resp = requests.get(VMIX_API, timeout=REQUEST_TIMEOUT)
|
resp = requests.get(VMIX_API, timeout=REQUEST_TIMEOUT)
|
||||||
@@ -55,6 +56,7 @@ def read_vmix_dynamic_values():
|
|||||||
"operator_login": None,
|
"operator_login": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def execute_vmix_command(path: str):
|
def execute_vmix_command(path: str):
|
||||||
path = str(path or "").strip()
|
path = str(path or "").strip()
|
||||||
if not path.startswith("/api/"):
|
if not path.startswith("/api/"):
|
||||||
@@ -76,52 +78,55 @@ def execute_vmix_command(path: str):
|
|||||||
"error": str(e),
|
"error": str(e),
|
||||||
}
|
}
|
||||||
|
|
||||||
async def wait_for_session():
|
|
||||||
|
async def wait_for_routing():
|
||||||
while True:
|
while True:
|
||||||
data = read_vmix_dynamic_values()
|
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
|
return data
|
||||||
|
|
||||||
print("[agent] waiting for vMix session...", data)
|
print("[agent] waiting for match_id + operator_login...", data)
|
||||||
await asyncio.sleep(POLL_INTERVAL)
|
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 = {
|
params = {
|
||||||
"client_id": CLIENT_ID,
|
"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:
|
if group_name:
|
||||||
params["group_name"] = group_name
|
params["group_name"] = group_name
|
||||||
if operator_login:
|
|
||||||
params["operator_name"] = operator_login
|
|
||||||
return f"{WS_BASE}?{urlencode(params)}"
|
return f"{WS_BASE}?{urlencode(params)}"
|
||||||
|
|
||||||
|
|
||||||
async def ping_loop(ws):
|
async def ping_loop(ws):
|
||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(15)
|
await asyncio.sleep(15)
|
||||||
await ws.send(json.dumps({"type": "ping"}))
|
await ws.send(json.dumps({"type": "ping"}))
|
||||||
|
|
||||||
|
|
||||||
async def run_agent():
|
async def run_agent():
|
||||||
current_session = None
|
|
||||||
current_match_id = None
|
current_match_id = None
|
||||||
current_group_name = None
|
current_group_name = None
|
||||||
current_operator_login = None
|
current_operator_login = None
|
||||||
|
|
||||||
while True:
|
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"]
|
match_id = vmix_data["match_id"]
|
||||||
group_name = vmix_data["group_name"]
|
group_name = vmix_data["group_name"]
|
||||||
operator_login = vmix_data["operator_login"]
|
operator_login = vmix_data["operator_login"]
|
||||||
|
|
||||||
if 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] found session: {session_token}")
|
print(
|
||||||
current_session = session_token
|
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:
|
try:
|
||||||
async with websockets.connect(ws_url, ping_interval=None, max_size=2**20) as ws:
|
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({
|
await ws.send(json.dumps({
|
||||||
"type": "register",
|
"type": "register",
|
||||||
"client_id": CLIENT_ID,
|
"client_id": CLIENT_ID,
|
||||||
"session_token": session_token,
|
|
||||||
"match_id": match_id,
|
"match_id": match_id,
|
||||||
"group_name": group_name,
|
"group_name": group_name,
|
||||||
"operator_name": operator_login,
|
"operator_name": operator_login,
|
||||||
@@ -140,7 +144,6 @@ async def run_agent():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
# следим, не сменился ли проект/session в vMix
|
|
||||||
latest = read_vmix_dynamic_values()
|
latest = read_vmix_dynamic_values()
|
||||||
if latest["ok"]:
|
if latest["ok"]:
|
||||||
latest_match_id = latest.get("match_id")
|
latest_match_id = latest.get("match_id")
|
||||||
@@ -167,7 +170,6 @@ async def run_agent():
|
|||||||
await ws.send(json.dumps({
|
await ws.send(json.dumps({
|
||||||
"type": "vmix_result",
|
"type": "vmix_result",
|
||||||
"client_id": CLIENT_ID,
|
"client_id": CLIENT_ID,
|
||||||
"session_token": session_token,
|
|
||||||
"match_id": match_id,
|
"match_id": match_id,
|
||||||
"operator_name": operator_login,
|
"operator_name": operator_login,
|
||||||
"results": results,
|
"results": results,
|
||||||
@@ -181,5 +183,6 @@ async def run_agent():
|
|||||||
|
|
||||||
await asyncio.sleep(RECONNECT_DELAY)
|
await asyncio.sleep(RECONNECT_DELAY)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(run_agent())
|
asyncio.run(run_agent())
|
||||||
|
|||||||
7
app.py
7
app.py
@@ -229,13 +229,12 @@ class VmixConnectionManager:
|
|||||||
except Exception:
|
except Exception:
|
||||||
await self.disconnect(client_id)
|
await self.disconnect(client_id)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def send_to_match(
|
async def send_to_match(
|
||||||
self,
|
self,
|
||||||
match_id: int,
|
match_id: int,
|
||||||
message: dict,
|
message: dict,
|
||||||
target_group: str | None = None,
|
target_group: str | None = None,
|
||||||
session_token: str | None = None,
|
|
||||||
target_login: str | None = None,
|
target_login: str | None = None,
|
||||||
):
|
):
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
@@ -249,8 +248,6 @@ class VmixConnectionManager:
|
|||||||
continue
|
continue
|
||||||
if target_login and conn.get("operator_name") != target_login:
|
if target_login and conn.get("operator_name") != target_login:
|
||||||
continue
|
continue
|
||||||
if session_token and conn.get("session_token") != session_token:
|
|
||||||
continue
|
|
||||||
matched.append(conn["client_id"])
|
matched.append(conn["client_id"])
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
@@ -1625,7 +1622,6 @@ async def publish_vmix_command(payload: PublishVmixCommandPayload, request: Requ
|
|||||||
message = {
|
message = {
|
||||||
"type": "vmix_command",
|
"type": "vmix_command",
|
||||||
"match_id": payload.match_id,
|
"match_id": payload.match_id,
|
||||||
"session_token": payload.session_token,
|
|
||||||
"commands": commands,
|
"commands": commands,
|
||||||
"target_login": payload.target_login,
|
"target_login": payload.target_login,
|
||||||
"meta": payload.meta or {},
|
"meta": payload.meta or {},
|
||||||
@@ -1640,7 +1636,6 @@ async def publish_vmix_command(payload: PublishVmixCommandPayload, request: Requ
|
|||||||
match_id=payload.match_id,
|
match_id=payload.match_id,
|
||||||
message=message,
|
message=message,
|
||||||
target_group=payload.target_group,
|
target_group=payload.target_group,
|
||||||
session_token=payload.session_token,
|
|
||||||
target_login=payload.target_login,
|
target_login=payload.target_login,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user