feat: AGV状态实时推送与受控异常处置 + rcs-dps状态接口独立配置与日志落盘修复
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
// Package agv 海康 AGV 调度系统(RCS-Lite V1.5) 对接客户端(2026-10-17 由 RCS-2000 改为 RCS-Lite 1.5)。
|
||||
// 能力:生成搬运任务(genAgvSchedulingTask)、查询任务状态(queryTaskStatus)、取消任务(cancelTask)。
|
||||
// 协议详见《AGV海康litev1.5.md》:无签名;所有参数统一字符串;成功 code=="0"。
|
||||
// 完整请求路径 = BaseURL(已含 /rCMS/services/rest/hikRpcService) + 业务路径。
|
||||
// 完整请求路径 = BaseURL(已含 /rcms/services/rest/hikRpcService,小写) + 业务路径;
|
||||
// 车辆状态查询例外:走 AgvStatusURL(rcms-dps/rest 独立服务) + /queryAgvStatus。
|
||||
// 未配置真实 RCS(BaseURL 为空) 或 EnableMock=true 时走 Mock 客户端。
|
||||
package agv
|
||||
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -19,8 +21,12 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
"bj_power_mes/internal/config"
|
||||
"bj_power_mes/internal/netutil"
|
||||
)
|
||||
|
||||
// commTag 本机 IP 前缀(进程启动解析一次),断网排查时可一眼看出日志来自哪台机器。
|
||||
var commTag = netutil.Tag()
|
||||
|
||||
// Client AGV 调度客户端抽象
|
||||
type Client interface {
|
||||
// SubmitTask 下发搬运任务 fromDock -> toDock。taskCode 为我方任务单号(透传给 RCS 便于回查)。
|
||||
@@ -65,34 +71,91 @@ func New(cfg config.AgvConf) Client {
|
||||
// ---------- 请求与响应(真实海康 RCS-Lite) ----------
|
||||
|
||||
type realClient struct {
|
||||
cfg config.AgvConf
|
||||
hc *http.Client
|
||||
carrier string
|
||||
cfg config.AgvConf
|
||||
hc *http.Client
|
||||
carrier string
|
||||
statusBase string // 车辆状态(rcms-dps/rest)独立服务地址
|
||||
}
|
||||
|
||||
func newRealClient(cfg config.AgvConf) *realClient {
|
||||
return &realClient{
|
||||
cfg: cfg,
|
||||
hc: &http.Client{Timeout: 15 * time.Second},
|
||||
carrier: cfg.CarrierCode,
|
||||
cfg: cfg,
|
||||
hc: &http.Client{Timeout: 15 * time.Second},
|
||||
carrier: cfg.CarrierCode,
|
||||
statusBase: resolveStatusBase(cfg),
|
||||
}
|
||||
}
|
||||
|
||||
// url 拼接 RCS-Lite 基础地址 + 业务路径(BaseURL 已含 /rCMS/services/rest/hikRpcService)。
|
||||
// resolveStatusBase 车辆状态(rcms-dps/rest)地址:优先显式配置;留空则从 BaseURL 取 scheme://host 推导。
|
||||
// 推导用 net/url 解析(非正则),如 http://IP:8182/rcms/services/... → http://IP:8182/rcms-dps/rest。
|
||||
func resolveStatusBase(cfg config.AgvConf) string {
|
||||
if s := strings.TrimSpace(cfg.AgvStatusURL); s != "" {
|
||||
return strings.TrimRight(s, "/")
|
||||
}
|
||||
u, err := url.Parse(cfg.BaseURL)
|
||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
||||
return ""
|
||||
}
|
||||
return u.Scheme + "://" + u.Host + "/rcms-dps/rest"
|
||||
}
|
||||
|
||||
// url 拼接 RCS-Lite 任务接口地址 + 业务路径(BaseURL 已含 /rcms/services/rest/hikRpcService,小写)。
|
||||
func (c *realClient) url(path string) string {
|
||||
return strings.TrimRight(c.cfg.BaseURL, "/") + path
|
||||
}
|
||||
|
||||
// statusURL 拼接车辆状态地址 + 业务路径(rcms-dps/rest 独立服务)。
|
||||
func (c *realClient) statusURL(path string) string {
|
||||
return strings.TrimRight(c.statusBase, "/") + path
|
||||
}
|
||||
|
||||
// flexString 兼容 RCS 字段「有时返回 JSON 字符串、有时返回数字」的情况。
|
||||
// 协议文档写"参数统一字符串",但实测响应 code 返回数字 1006/-1、status 也可能是数字,
|
||||
// 用 string 直接反序列化会报 cannot unmarshal number into Go struct field ... of type string。
|
||||
// 本类型把字符串/数字/布尔/null 统一收成字符串,下游按字符串比较即可。不使用正则。
|
||||
type flexString string
|
||||
|
||||
func (f *flexString) UnmarshalJSON(b []byte) error {
|
||||
if len(b) == 0 {
|
||||
*f = ""
|
||||
return nil
|
||||
}
|
||||
if b[0] == '"' { // JSON 字符串
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
*f = flexString(s)
|
||||
return nil
|
||||
}
|
||||
var v any // 数字/布尔/null 按字面量收
|
||||
if err := json.Unmarshal(b, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
if v == nil {
|
||||
*f = ""
|
||||
return nil
|
||||
}
|
||||
*f = flexString(fmt.Sprint(v))
|
||||
return nil
|
||||
}
|
||||
|
||||
// envelope RCS-Lite 统一响应信封(§1.4):code 必填、message 必填、reqCode 回传、data 选填。
|
||||
type envelope struct {
|
||||
Code string `json:"code"`
|
||||
Code flexString `json:"code"`
|
||||
Message string `json:"message"`
|
||||
ReqCode string `json:"reqCode"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
// call POST 到 RCS-Lite 并解析信封。成功判定 code=="0";out 非空时把 data 反序列化进去。
|
||||
// call POST 到 RCS-Lite 任务接口(BaseURL)并解析信封。
|
||||
func (c *realClient) call(ctx context.Context, path string, payload map[string]any, out any) error {
|
||||
return c.callAt(ctx, c.url(path), payload, out)
|
||||
}
|
||||
|
||||
// callAt POST 到完整 URL 并解析信封。成功判定 code=="0";out 非空时把 data 反序列化进去。
|
||||
// 车辆状态(rcms-dps)等独立服务接口用本方法,避免误挂到任务接口 BaseURL 下。
|
||||
func (c *realClient) callAt(ctx context.Context, reqURL string, payload map[string]any, out any) error {
|
||||
if payload == nil {
|
||||
payload = map[string]any{}
|
||||
}
|
||||
@@ -107,29 +170,28 @@ func (c *realClient) call(ctx context.Context, path string, payload map[string]a
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reqURL := c.url(path)
|
||||
// 2026-11-16 联调:AGV 全量 HTTP 交互落日志(请求 URL/报文 + 响应状态/响应体),grep "[AGV]" 可见。
|
||||
logx.WithContext(ctx).Infof("[AGV] --> POST %s payload=%s", reqURL, string(body))
|
||||
logx.WithContext(ctx).Infof("[AGV]"+commTag+" --> POST %s payload=%s", reqURL, string(body))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
logx.WithContext(ctx).Errorf("[AGV] <-- build request error url=%s err=%v", reqURL, err)
|
||||
logx.WithContext(ctx).Errorf("[AGV]"+commTag+" <-- build request error url=%s err=%v", reqURL, err)
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
||||
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
logx.WithContext(ctx).Errorf("[AGV] <-- transport error url=%s err=%v", reqURL, err)
|
||||
logx.WithContext(ctx).Errorf("[AGV]"+commTag+" <-- transport error url=%s err=%v", reqURL, err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.WithContext(ctx).Errorf("[AGV] <-- read body error url=%s httpStatus=%d err=%v", reqURL, resp.StatusCode, err)
|
||||
logx.WithContext(ctx).Errorf("[AGV]"+commTag+" <-- read body error url=%s httpStatus=%d err=%v", reqURL, resp.StatusCode, err)
|
||||
return err
|
||||
}
|
||||
logx.WithContext(ctx).Infof("[AGV] <-- HTTP %d url=%s body=%s", resp.StatusCode, reqURL, string(raw))
|
||||
logx.WithContext(ctx).Infof("[AGV]"+commTag+" <-- HTTP %d url=%s body=%s", resp.StatusCode, reqURL, string(raw))
|
||||
if resp.StatusCode >= 400 {
|
||||
return fmt.Errorf("agv http %d: %s", resp.StatusCode, string(raw))
|
||||
}
|
||||
@@ -139,7 +201,7 @@ func (c *realClient) call(ctx context.Context, path string, payload map[string]a
|
||||
}
|
||||
// 成功码固定字符串 "0"(§1.5 结果码定义)
|
||||
if env.Code != "0" {
|
||||
logx.WithContext(ctx).Errorf("[AGV] <-- biz error url=%s code=%s message=%s reqCode=%v", reqURL, env.Code, env.Message, payload["reqCode"])
|
||||
logx.WithContext(ctx).Errorf("[AGV]"+commTag+" <-- biz error url=%s code=%s message=%s reqCode=%v", reqURL, env.Code, env.Message, payload["reqCode"])
|
||||
return fmt.Errorf("agv code=%s message=%s", env.Code, env.Message)
|
||||
}
|
||||
if out != nil && len(env.Data) > 0 {
|
||||
@@ -203,12 +265,12 @@ func (c *realClient) SubmitTask(ctx context.Context, taskCode, fromDock, toDock,
|
||||
return taskCode, nil
|
||||
}
|
||||
|
||||
// queryTaskData queryTaskStatus 的 data 列表项
|
||||
// queryTaskData queryTaskStatus 的 data 列表项(字段兼容字符串/数字)
|
||||
type queryTaskData struct {
|
||||
TaskCode string `json:"taskCode"`
|
||||
TaskStatus string `json:"taskStatus"`
|
||||
AgvCode string `json:"agvCode"`
|
||||
TaskTyp string `json:"taskTyp"`
|
||||
TaskCode flexString `json:"taskCode"`
|
||||
TaskStatus flexString `json:"taskStatus"`
|
||||
AgvCode flexString `json:"agvCode"`
|
||||
TaskTyp flexString `json:"taskTyp"`
|
||||
}
|
||||
|
||||
// QueryTask 实现 Client:查询任务状态(queryTaskStatus)。
|
||||
@@ -225,8 +287,8 @@ func (c *realClient) QueryTask(ctx context.Context, taskCode string) (string, st
|
||||
return "", "", err
|
||||
}
|
||||
for _, d := range out.Data {
|
||||
if d.TaskCode == taskCode && d.TaskStatus != "" {
|
||||
return d.TaskStatus, d.AgvCode, nil
|
||||
if string(d.TaskCode) == taskCode && d.TaskStatus != "" {
|
||||
return string(d.TaskStatus), string(d.AgvCode), nil
|
||||
}
|
||||
}
|
||||
return "1", "", nil
|
||||
@@ -245,18 +307,20 @@ func (c *realClient) CancelTask(ctx context.Context, taskCode, reason string) er
|
||||
return c.call(ctx, "/cancelTask", payload, nil)
|
||||
}
|
||||
|
||||
// queryAgvStatusData 3.1.7 单条车辆状态 data 项
|
||||
// queryAgvStatusData 3.1.7 单条车辆状态 data 项(字段兼容字符串/数字)
|
||||
type queryAgvStatusData struct {
|
||||
RobotCode string `json:"robotCode"`
|
||||
Battery string `json:"battery"`
|
||||
Status string `json:"status"`
|
||||
PodCode string `json:"podCode"`
|
||||
PosX string `json:"posX"`
|
||||
PosY string `json:"posY"`
|
||||
Stop string `json:"stop"`
|
||||
RobotCode flexString `json:"robotCode"`
|
||||
Battery flexString `json:"battery"`
|
||||
Status flexString `json:"status"`
|
||||
PodCode flexString `json:"podCode"`
|
||||
PosX flexString `json:"posX"`
|
||||
PosY flexString `json:"posY"`
|
||||
Stop flexString `json:"stop"`
|
||||
}
|
||||
|
||||
// QueryAgvStatus 实现 Client:3.1.7 只读获取全部车辆状态。
|
||||
// 走 rcs-dps/rest 独立服务(§3.1.7 调用路径 http://IP:8182/rcms-dps/rest/queryAgvStatus),
|
||||
// 不是 hikRpcService 任务接口——此前挂到任务 BaseURL 下导致 404。
|
||||
// mapShortName 取配置 MapCode(多地图场景必填;单地图可空,RCS 侧默认)。
|
||||
func (c *realClient) QueryAgvStatus(ctx context.Context) ([]AgvVehicle, error) {
|
||||
payload := map[string]any{
|
||||
@@ -265,19 +329,19 @@ func (c *realClient) QueryAgvStatus(ctx context.Context) ([]AgvVehicle, error) {
|
||||
var out struct {
|
||||
Data []queryAgvStatusData `json:"data"`
|
||||
}
|
||||
if err := c.call(ctx, "/queryAgvStatus", payload, &out); err != nil {
|
||||
if err := c.callAt(ctx, c.statusURL("/queryAgvStatus"), payload, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vs := make([]AgvVehicle, 0, len(out.Data))
|
||||
for _, d := range out.Data {
|
||||
vs = append(vs, AgvVehicle{
|
||||
RobotCode: d.RobotCode,
|
||||
Battery: atof(d.Battery),
|
||||
Status: d.Status,
|
||||
PodCode: d.PodCode,
|
||||
PosX: atof(d.PosX),
|
||||
PosY: atof(d.PosY),
|
||||
Stop: d.Stop == "1",
|
||||
RobotCode: string(d.RobotCode),
|
||||
Battery: atof(string(d.Battery)),
|
||||
Status: string(d.Status),
|
||||
PodCode: string(d.PodCode),
|
||||
PosX: atof(string(d.PosX)),
|
||||
PosY: atof(string(d.PosY)),
|
||||
Stop: d.Stop == "1" || d.Stop == "true",
|
||||
})
|
||||
}
|
||||
return vs, nil
|
||||
@@ -300,24 +364,24 @@ func newMockClient() *mockClient { return &mockClient{} }
|
||||
|
||||
func (m *mockClient) SubmitTask(ctx context.Context, taskCode, fromDock, toDock, carrierCode, templateType, agvCode string) (string, error) {
|
||||
// 模拟立即成功下发,返回我方 taskCode(与真实客户端一致,HikTaskCode=taskNo)
|
||||
logx.WithContext(ctx).Infof("[AGV][MOCK] SubmitTask taskCode=%s %s->%s template=%s agvCode=%s(模拟下发成功,未发真实 HTTP)", taskCode, fromDock, toDock, templateType, agvCode)
|
||||
logx.WithContext(ctx).Infof("[AGV]"+commTag+"[MOCK] SubmitTask taskCode=%s %s->%s template=%s agvCode=%s(模拟下发成功,未发真实 HTTP)", taskCode, fromDock, toDock, templateType, agvCode)
|
||||
return taskCode, nil
|
||||
}
|
||||
|
||||
func (m *mockClient) QueryTask(ctx context.Context, taskCode string) (string, string, error) {
|
||||
// 模拟:MOCK 任务一律视为已结束,便于离线联调观察"待搬运→已到位";模拟模式无真实分车,agvCode 为空
|
||||
logx.WithContext(ctx).Infof("[AGV][MOCK] QueryTask taskCode=%s(模拟返回已结束)", taskCode)
|
||||
logx.WithContext(ctx).Infof("[AGV]"+commTag+"[MOCK] QueryTask taskCode=%s(模拟返回已结束)", taskCode)
|
||||
return "9", "", nil
|
||||
}
|
||||
|
||||
func (m *mockClient) CancelTask(ctx context.Context, taskCode, reason string) error {
|
||||
logx.WithContext(ctx).Infof("[AGV][MOCK] CancelTask taskCode=%s reason=%s(模拟取消成功)", taskCode, reason)
|
||||
logx.WithContext(ctx).Infof("[AGV]"+commTag+"[MOCK] CancelTask taskCode=%s reason=%s(模拟取消成功)", taskCode, reason)
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryAgvStatus Mock:返回两台示例车(一台空闲、一台执行中),便于联调展示。
|
||||
func (m *mockClient) QueryAgvStatus(ctx context.Context) ([]AgvVehicle, error) {
|
||||
logx.WithContext(ctx).Infof("[AGV][MOCK] QueryAgvStatus(模拟返回样例车 9001/9002)")
|
||||
logx.WithContext(ctx).Infof("[AGV]" + commTag + "[MOCK] QueryAgvStatus(模拟返回样例车 9001/9002)")
|
||||
return []AgvVehicle{
|
||||
{RobotCode: "9001", Battery: 99, Status: "4", PodCode: "0", PosX: 20000, PosY: 20000, Stop: false},
|
||||
{RobotCode: "9002", Battery: 78, Status: "2", PodCode: "100001", PosX: 35000, PosY: 12000, Stop: false},
|
||||
|
||||
Reference in New Issue
Block a user