Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 95 additions & 72 deletions server/apis/extenal.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Dict
from typing import Optional

import yaml
from pydantic import BaseModel
from pyee.asyncio import AsyncIOEventEmitter
from robosdk.common.config import Config
Expand Down Expand Up @@ -341,90 +342,112 @@ async def on_app_create(self,

async def on_app_deploy(self, app: AppModel, robot: RobotModel, **kwargs):
self.logger.debug(f"CloudRTC app {app} deploy")
if robot.robot_id in self._app_env and app.app_type == "sparkrtc":
if not (app.run_env and isinstance(app.run_env, dict)):
app.run_env = {}

mode = app.run_env.get("mode", "p2p") # p2p / mutil
token: str = gen_token()
rtc: RTCModel = self._app_env[robot.robot_id]["rtc"]
expire_time = ""
if mode == "mutil":
agent_num = int(app.run_env.get("agent_num", 1))
stream = {}
for i in range(agent_num):
client_id = app.run_env.get(f"stream_{i}_id", "")
client_name = (app.run_env.get(f"stream_{i}_name", "")
or f"{robot.robot_name}_{i}")

if not client_id:
ros_topic = app.run_env.get(
f"stream_{i}_ros_topic", "/camera/color/image_raw"
)
client_id = genearteMD5(ros_topic)
if client_id in rtc.clients:
client_id = f"{client_id}_{token}"
client_name = f"{client_name}_{token}"
client = self._add_single_client(
robot=robot,
client_id=client_id,
client_name=client_name,
)
stream[client_id] = {
"stream_id": client_id,
"stream_name": client_name,
"signature": client.signature,
"rtc_ctime": client.expire_time
}
expire_time = client.expire_time
run_env = {
"device_id": rtc.room_id,
"rtc_app_id": rtc.app_id,
"stream": stream,
"rtc_app_key": rtc.app_key,
"rtc_domain": rtc.domain,
"rtc_ctime": expire_time
}
else:
if "stream_id" in app.run_env:
client_id = app.run_env["stream_id"]
else:
client_id = kwargs.get("client_id", "")
if not client_id:
client_id = genearteMD5(
app.run_env.get(
"ros_image_topic", "/camera/color/image_raw"
)
)
if not (robot.robot_id in self._app_env and app.app_type == "sparkrtc"):
return
if not (app.run_env and isinstance(app.run_env, dict)):
app.run_env = {}

if "stream_name" in app.run_env:
client_name = app.run_env["stream_name"]
else:
client_name = (
kwargs.get("client_name", "") or robot.robot_name
)
mode = app.run_env.get("mode", "p2p") # p2p / mutil
token: str = gen_token()
rtc: RTCModel = self._app_env[robot.robot_id]["rtc"]
expire_time = ""
if mode == "mutil":
agent_num = int(app.run_env.get("agent_num", 1))
stream = []
for i in range(agent_num):
client_id = app.run_env.get(f"stream_{i}_id", "")
client_name = (app.run_env.get(f"stream_{i}_name", "")
or f"{robot.robot_name}_{i}")
ros_topic = app.run_env.get(
f"stream_{i}_ros_topic", "/camera/color/image_raw"
)
if not client_id:
client_id = genearteMD5(ros_topic)
if client_id in rtc.clients:
client_id = f"{client_id}_{token}"
client_name = f"{client_name}_{token}"

client = self._add_single_client(
robot=robot,
client_id=client_id,
client_name=client_name,
)
run_env = {
"device_id": rtc.room_id,
"rtc_app_id": rtc.app_id,
"stream_id": client_id,
"stream_name": client_name,
"signature": client.signature,
"rtc_app_key": rtc.app_key,
"rtc_domain": rtc.domain,
"rtc_ctime": client.expire_time
stream.append(
{
"stream_id": client_id,
"stream_name": client_name,
"ros_image_topic": ros_topic,
"signature": client.signature,
"rtc_ctime": client.expire_time,
"client_type": "robot"
}
)
Comment thread
JoeyHwong-gk marked this conversation as resolved.
expire_time = client.expire_time
run_env = {
"device_id": rtc.room_id,
"app_id": rtc.app_id,
"app_key": rtc.app_key,
"domain": rtc.domain,
"ctime": expire_time,
"streams": stream,
}
if not app.volumes:
app.volumes = []
app.volumes = list(
filter(lambda x: x["name"] != "sparkrtconf", app.volumes)
)
app.volumes.append(
{
"name": "sparkrtconf",
"type": "configMap",
"source": "sparkrtc-config",
"destination": "/home/cameraagent/share/robocamera_agent/config", # noqa
"data": {
"config.yaml": yaml.dump(run_env)
}
}
)

else:
if "stream_id" in app.run_env:
client_id = app.run_env["stream_id"]
else:
client_id = kwargs.get("client_id", "")
if not client_id:
client_id = genearteMD5(
app.run_env.get(
"ros_image_topic", "/camera/color/image_raw"
)
)

if "stream_name" in app.run_env:
client_name = app.run_env["stream_name"]
else:
client_name = (
kwargs.get("client_name", "") or robot.robot_name
)
if client_id in rtc.clients:
client_id = f"{client_id}_{token}"
client_name = f"{client_name}_{token}"

client = self._add_single_client(
robot=robot,
client_id=client_id,
client_name=client_name,
)
run_env = {
"device_id": rtc.room_id,
"rtc_app_id": rtc.app_id,
"stream_id": client_id,
"stream_name": client_name,
"signature": client.signature,
"rtc_app_key": rtc.app_key,
"rtc_domain": rtc.domain,
"rtc_ctime": client.expire_time
}
app.run_env.update(run_env)

def _add_single_client(self, robot: RobotModel, client_id: str, client_name: str):
def _add_single_client(self, robot: RobotModel, client_id: str,
client_name: str):
rtc: RTCModel = self._app_env[robot.robot_id]["rtc"]
rooms: RoomManage = self._app_env[robot.robot_id]["rooms"]
room: RoomModel = self._app_env[robot.robot_id]["room"]
Expand Down
3 changes: 3 additions & 0 deletions server/apis/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ def initial(self, **kwargs):
on_startup=[self._on_startup],
on_shutdown=[self._on_shutdown],
routes=rounters,
openapi_url=None,
docs_url=None,
redoc_url=None
)

self.app.add_middleware(
Expand Down