feat: AGV状态实时推送与受控异常处置 + rcs-dps状态接口独立配置与日志落盘修复

This commit is contained in:
SunYF
2026-11-16 19:42:22 +08:00
parent d92ca803d5
commit a998a0d0f7
18 changed files with 861 additions and 119 deletions
@@ -305,6 +305,31 @@ func AgvChainEndHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
}
// AgvChainForceEndHandler POST /agv/chain/force-end 强制结束安全阀:
// 人工确认现场实物已处置后,仅清系统死锁状态(段置终态+释放点位锁+留痕),
// 不调用海康任何接口、不下发任何物理动作。风险由前端确认弹窗告知。
func AgvChainForceEndHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
ChainNo string `json:"chainNo"`
Reason string `json:"reason"` // 现场实际处置情况(受控入口必选),仅用于留痕
}
if err := httpx.ParseJSON(r, &req); err != nil || req.ChainNo == "" {
httpx.BadRequest(w, "缺少任务链号(chainNo)")
return
}
operator := r.Header.Get("X-Username")
if operator == "" {
operator = "dispatcher"
}
if err := logic.New(svcCtx).AgvChainForceEnd(r.Context(), req.ChainNo, operator, req.Reason); err != nil {
httpx.Fail(w, 3315, err.Error())
return
}
httpx.OkMessage(w, "已强制结束,系统状态已解除;请确认现场实物已处理", nil)
}
}
// AgvChainListHandler GET /agv/chain/list 手动链列表(任务进行中面板:链号/锁定车/节点/进行中)。
func AgvChainListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -319,9 +344,11 @@ func AgvChainListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
// AgvPointLockHandler POST /agv/points/:id/lock —— 点位锁定管理(派车锁 2026-11-06 拍板):
// body {locked bool, reason string, clearForcePick bool}
// locked=true → 人工锁定(reason 必填,locked_by=manual,仅人工解锁);
// locked=false → 人工解锁(含 JBT21 手动解锁、系统锁人工释放);
// clearForcePick=true → 清除强提风险标记(人工确认现场安全后)。
//
// locked=true → 人工锁定(reason 必填,locked_by=manual,仅人工解锁);
// locked=false → 人工解锁(含 JBT21 手动解锁、系统锁人工释放);
// clearForcePick=true → 清除强提风险标记(人工确认现场安全后)。
//
// 全部操作 event_log 留痕。
func AgvPointLockHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -0,0 +1,44 @@
package production
import (
"net/http"
"strings"
"sync"
"time"
"bj_power_mes/common/httpx"
tokenx "bj_power_mes/common/token"
"bj_power_mes/internal/svc"
"bj_power_mes/internal/ws"
)
var (
agvHubOnce sync.Once
agvHub *ws.AgvHub
)
func globalAgvHub(svcCtx *svc.ServiceContext) *ws.AgvHub {
agvHubOnce.Do(func() {
agvHub = ws.NewAgvHub(svcCtx)
})
return agvHub
}
// AgvWsHandler GET /api/v1/agv/ws?token=<JWT> 人工派车状态推送。
// 浏览器 WebSocket 无法带 Authorization 头,token 走 query;校验复用 tokenx.Parse(签名+过期),
// 通过后交由 AgvHub 升级管理。状态来源:AgvHub 定时拉海康 RCS 写回本地库后对比快照变化即推。
func AgvWsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tok := strings.TrimSpace(r.URL.Query().Get("token"))
if tok == "" {
httpx.Unauthorized(w, "缺少登录凭证,请重新登录")
return
}
claims, err := tokenx.Parse(svcCtx.Config.Auth.AccessSecret, tok)
if err != nil || claims == nil || claims.ExpiresAt == nil || time.Until(claims.ExpiresAt.Time) <= 0 {
httpx.Unauthorized(w, "登录凭证无效或已过期,请重新登录")
return
}
globalAgvHub(svcCtx).Handle(w, r)
}
}
@@ -449,6 +449,7 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
// 手动多段链(2026-11-06 拍板):逐节点人工推进,全程锁同一台车
{Method: http.MethodPost, Path: "/agv/chain/next", Handler: production.AgvChainNextHandler(serverCtx)},
{Method: http.MethodPost, Path: "/agv/chain/end", Handler: production.AgvChainEndHandler(serverCtx)},
{Method: http.MethodPost, Path: "/agv/chain/force-end", Handler: production.AgvChainForceEndHandler(serverCtx)},
{Method: http.MethodGet, Path: "/agv/chain/list", Handler: production.AgvChainListHandler(serverCtx)},
{Method: http.MethodGet, Path: "/agv/mode", Handler: production.AgvModeHandler(serverCtx)},
// AGV 点位管理(独立点位表:硬件映射编码/接驳台绑定不可改,仅名称/启用可改)
@@ -652,6 +653,12 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
Method: http.MethodGet, Path: "/api/v1/todos/ws", Handler: production.TodoWsHandler(serverCtx),
}, rest.WithTimeout(3600_000_000_000))
// ---------- 人工派车状态 WebSocket:长连接推送 AGV 到位/失败,免手动刷新 ----------
// token 走 query 由 AgvWsHandler 自校验(同 TodoWs),不挂 WithJwt;放宽超时避免 5s 断连。
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/v1/agv/ws", Handler: production.AgvWsHandler(serverCtx),
}, rest.WithTimeout(3600_000_000_000))
// ---------- 海康 RCS-Lite SPI 主动回调(文档 2.2.1 任务执行通知 agvCallback) ----------
// 注意:RCS 回调不携带我方鉴权头;生产环境必须以防火墙 / IP 白名单隔离该端点(待与 RCS 方确认鉴权方式)。
// RCS-Lite 后台需将上层回调地址配置为本端点(路径遵循文档 /xxx/agv/agvCallbackService/agvCallback)。