diff --git a/API_Device_RemoteMgmt_InterfaceTable.md b/API_Device_RemoteMgmt_InterfaceTable.md index 81624fb..ab04f83 100644 --- a/API_Device_RemoteMgmt_InterfaceTable.md +++ b/API_Device_RemoteMgmt_InterfaceTable.md @@ -241,6 +241,8 @@ Response 200:`{"ok":true}` > 说明:该能力用于“启动/重启/关闭主程序(media-server)并选择加载哪个配置文件”。 > > agent 启动 media-server 时会显式传入:`--config `,因此不依赖 media-server 内部默认配置。 +> +> 路径说明:`agent.media_server_process` 下的 `exec_path/work_dir/configs_dir/pid_file` 支持绝对或相对路径;若为相对路径,则以 agent 启动参数 `--config ` 的**配置文件所在目录**为基准解析。 ### 6.1 `POST /v1/media-server/start` 用途:启动本机 media-server(若已运行则幂等返回 ok;若已运行但 config 不同则 409)。 @@ -252,6 +254,8 @@ Body(可选,JSON): {"config":"cam1"} ``` +说明:若请求 body 非空,则必须带 `Content-Type: application/json`;若 body 为空,可不带该 header。 + `config` 解析规则: - 为空/缺省:使用 `agent.config_path` - 非空:只允许文件名/配置名(禁止包含 `/`、`\\`、`..`);若不带扩展名自动补 `.json`;最终从 `agent.media_server_process.configs_dir` 下解析为 `/.json` @@ -277,6 +281,8 @@ Body(可选,JSON): {"config":"cam1"} ``` +说明:若请求 body 非空,则必须带 `Content-Type: application/json`;若 body 为空,可不带该 header。 + Response 200: ```json {"ok":true,"running":true,"pid":1234,"config_path":"/home/orangepi/Desktop/OrangePi3588Media/configs/cam1.json"} diff --git a/agent/agent_cam1.config.json b/agent/agent_cam1.config.json index 9ce3fab..38ecc95 100644 --- a/agent/agent_cam1.config.json +++ b/agent/agent_cam1.config.json @@ -16,10 +16,10 @@ "config_path": "./test_cam1_strict_minio_alarm_rtsp_server.json", "media_server_process": { "enable": true, - "exec_path": "/home/orangepi/Desktop/OrangePi3588Media/build/media-server", - "work_dir": "/home/orangepi/Desktop/OrangePi3588Media", - "configs_dir": "/home/orangepi/Desktop/OrangePi3588Media/configs", - "pid_file": "/var/run/rk3588sys-media-server.pid", + "exec_path": "../build/media-server", + "work_dir": "..", + "configs_dir": "../configs", + "pid_file": "./rk3588sys-media-server.pid", "graceful_timeout_ms": 5000 }, "media_server_base_url": "http://127.0.0.1:9000", diff --git a/agent/cmd/rk3588-agent/main.go b/agent/cmd/rk3588-agent/main.go index c6c2c86..15e90d6 100644 --- a/agent/cmd/rk3588-agent/main.go +++ b/agent/cmd/rk3588-agent/main.go @@ -9,6 +9,7 @@ import ( "net/url" "os" "os/signal" + "path/filepath" "strconv" "strings" "sync" @@ -65,7 +66,12 @@ func main() { } store := modelstore.New(cfg.Agent.ModelsDir, cfg.Agent.MaxUploadMB) - h := httpapi.New(cfg.Agent, ms, store, deviceID, agentPort, mediaPort, Version, GitSHA) + baseDir := filepath.Dir(*cfgPath) + if abs, err := filepath.Abs(*cfgPath); err == nil { + baseDir = filepath.Dir(abs) + } + + h := httpapi.New(cfg.Agent, baseDir, ms, store, deviceID, agentPort, mediaPort, Version, GitSHA) httpSrv := &http.Server{ Addr: cfg.Agent.Listen, diff --git a/agent/internal/httpapi/server.go b/agent/internal/httpapi/server.go index 7911726..273efdf 100644 --- a/agent/internal/httpapi/server.go +++ b/agent/internal/httpapi/server.go @@ -49,10 +49,10 @@ type InfoResponse struct { UptimeSec int64 `json:"uptime_sec"` } -func New(agentCfg config.AgentConfig, ms *mediaserver.Client, store *modelstore.Store, deviceID string, agentPort int, mediaPort int, version, gitSHA string) http.Handler { +func New(agentCfg config.AgentConfig, baseDir string, ms *mediaserver.Client, store *modelstore.Store, deviceID string, agentPort int, mediaPort int, version, gitSHA string) http.Handler { var pc *procctl.Controller if agentCfg.MediaServerProcess.Enable { - pc = procctl.New(agentCfg) + pc = procctl.New(agentCfg, baseDir) } s := &Server{ agentCfg: agentCfg, diff --git a/agent/internal/procctl/procctl.go b/agent/internal/procctl/procctl.go index 8b1e7af..5a337c6 100644 --- a/agent/internal/procctl/procctl.go +++ b/agent/internal/procctl/procctl.go @@ -37,11 +37,23 @@ type Controller struct { defCfg string } -func New(agentCfg config.AgentConfig) *Controller { - return &Controller{ - proc: agentCfg.MediaServerProcess, - defCfg: agentCfg.ConfigPath, +func New(agentCfg config.AgentConfig, baseDir string) *Controller { + p := agentCfg.MediaServerProcess + if baseDir != "" { + if p.ExecPath != "" && !filepath.IsAbs(p.ExecPath) { + p.ExecPath = filepath.Join(baseDir, p.ExecPath) + } + if p.WorkDir != "" && !filepath.IsAbs(p.WorkDir) { + p.WorkDir = filepath.Join(baseDir, p.WorkDir) + } + if p.ConfigsDir != "" && !filepath.IsAbs(p.ConfigsDir) { + p.ConfigsDir = filepath.Join(baseDir, p.ConfigsDir) + } + if p.PidFile != "" && !filepath.IsAbs(p.PidFile) { + p.PidFile = filepath.Join(baseDir, p.PidFile) + } } + return &Controller{proc: p, defCfg: agentCfg.ConfigPath} } func (c *Controller) Enabled() bool { return c != nil && c.proc.Enable } @@ -76,6 +88,7 @@ func (c *Controller) Start(configName string) (Status, error) { b, _ := json.Marshal(pf2) b = append(b, '\n') if err := files.WriteFileAtomic(c.proc.PidFile, b, 0o644); err != nil { + _ = stopProcess(pid, 1*time.Second) return Status{}, fmt.Errorf("write pid file: %w", err) } return Status{Running: true, Pid: pid, ConfigPath: resolved}, nil diff --git a/agent/rk3588-agent b/agent/rk3588-agent index af5487d..623b697 100644 Binary files a/agent/rk3588-agent and b/agent/rk3588-agent differ