feat: 新增 AgvPoint 点位表及客户端,支持点位编码与接驳台绑定
This commit is contained in:
@@ -84,6 +84,28 @@ func CancelAgvTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// RetryAgvTaskHandler POST /agv/retry 重试失败/卡住的 AGV 任务(重新向 RCS 下发)。
|
||||
func RetryAgvTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
TaskNo string `json:"taskNo"`
|
||||
}
|
||||
if err := httpx.ParseJSON(r, &req); err != nil || req.TaskNo == "" {
|
||||
httpx.BadRequest(w, "缺少任务号")
|
||||
return
|
||||
}
|
||||
operator := r.Header.Get("X-Username")
|
||||
if operator == "" {
|
||||
operator = "dispatcher"
|
||||
}
|
||||
if err := logic.New(svcCtx).RetryAgvTask(r.Context(), req.TaskNo, operator); err != nil {
|
||||
httpx.Fail(w, 3310, err.Error())
|
||||
return
|
||||
}
|
||||
httpx.OkMessage(w, "已重试派车", nil)
|
||||
}
|
||||
}
|
||||
|
||||
// AgvModeHandler 返回 AGV 对接模式(mock / 真实 RCS)。
|
||||
func AgvModeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -91,6 +113,44 @@ func AgvModeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// ListAgvPointsHandler GET /agv/points?enabledOnly=1 列出 AGV 点位(启停用管理 / 派车下拉)。
|
||||
func ListAgvPointsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
enabledOnly := r.URL.Query().Get("enabledOnly") == "1"
|
||||
data, err := logic.New(svcCtx).ListAgvPoints(r.Context(), enabledOnly)
|
||||
if err != nil {
|
||||
httpx.Fail(w, 3308, err.Error())
|
||||
return
|
||||
}
|
||||
httpx.Ok(w, data)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateAgvPointHandler PUT /agv/points/:id body {name, enabled}
|
||||
// 仅允许改显示名与启用状态;硬件映射 position_code / dock_id 不可改。
|
||||
func UpdateAgvPointHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := pathID(r)
|
||||
if id <= 0 {
|
||||
httpx.BadRequest(w, "非法的点位 ID")
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
if err := httpx.ParseJSON(r, &body); err != nil {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
if err := logic.New(svcCtx).UpdateAgvPoint(r.Context(), id, body.Name, body.Enabled); err != nil {
|
||||
httpx.Fail(w, 3309, err.Error())
|
||||
return
|
||||
}
|
||||
httpx.OkMessage(w, "已保存", nil)
|
||||
}
|
||||
}
|
||||
|
||||
// DispatchAgvInternalHandler POST /api/internal/agv/dispatch
|
||||
// WMS 仓库完成备料后调用(X-API-TOKEN):MES 据叫料单触发海康 RCS 派车。
|
||||
func DispatchAgvInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package production
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -8,6 +9,8 @@ import (
|
||||
|
||||
"bj_power_mes/common/httpx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/agvpoint"
|
||||
"bj_power_mes/ent/agvtask"
|
||||
"bj_power_mes/ent/dock"
|
||||
"bj_power_mes/internal/svc"
|
||||
)
|
||||
@@ -202,6 +205,38 @@ func isAllDigits(s string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// syncAgvPointForDock 维护接驳台与 AGV 点位的一一对应(硬件级强绑定)。
|
||||
// 接驳台新增/修改时,确保存在一条 is_dock=true、dock_id=该接驳台、position_code=接驳台编码 的点位;
|
||||
// position_code 与 dock_id 是硬件映射,软件不可改,这里仅做“存在性/绑定”兜底,不暴露给前端编辑。
|
||||
func syncAgvPointForDock(ctx context.Context, client *ent.Client, d *ent.Dock, name string) {
|
||||
if d == nil {
|
||||
return
|
||||
}
|
||||
// 1) 已有按 dock_id 绑定的点位 → 编码若随接驳台编码调整则同步;不覆盖用户已改名称/启用态
|
||||
if existing, _ := client.AgvPoint.Query().Where(agvpoint.DockIDEQ(d.ID)).Only(ctx); existing != nil {
|
||||
if existing.PositionCode != d.DockCode {
|
||||
client.AgvPoint.UpdateOneID(existing.ID).SetPositionCode(d.DockCode).Exec(ctx)
|
||||
}
|
||||
return
|
||||
}
|
||||
// 2) 历史被删后重建:按 position_code 复用,重新绑定 dock_id(硬件映射兜底)
|
||||
if byCode, _ := client.AgvPoint.Query().Where(agvpoint.PositionCode(d.DockCode)).Only(ctx); byCode != nil {
|
||||
client.AgvPoint.UpdateOneID(byCode.ID).SetDockID(d.ID).SetIsDock(true).Exec(ctx)
|
||||
return
|
||||
}
|
||||
// 3) 真正新建:硬件内置强绑定(仅名称默认=接驳台名、启用默认 true 可软件改)
|
||||
ptName := name
|
||||
if ptName == "" {
|
||||
ptName = "接驳台 " + d.DockCode
|
||||
}
|
||||
client.AgvPoint.Create().
|
||||
SetPositionCode(d.DockCode).
|
||||
SetName(ptName).
|
||||
SetIsDock(true).
|
||||
SetDockID(d.ID).
|
||||
SetEnabled(true).Save(ctx)
|
||||
}
|
||||
|
||||
// SaveDockHandler POST /docks 新增或修改接驳台(id=0 为新增)。
|
||||
func SaveDockHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -238,6 +273,8 @@ func SaveDockHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
d = created
|
||||
}
|
||||
// 新增/修改接驳台 → 强制同步一条强绑定的 AGV 点位(硬件级一一对应闭环)
|
||||
syncAgvPointForDock(r.Context(), svcCtx.EntClient, d, name)
|
||||
httpx.Ok(w, dockRowOf(d))
|
||||
}
|
||||
}
|
||||
@@ -263,6 +300,11 @@ func DeleteDockHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
httpx.Fail(w, 3116, "删除接驳台失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
// 接驳台删除 → 一并清理其强绑定的 AGV 点位,避免一一对应出现悬空绑定
|
||||
if _, derr := svcCtx.EntClient.AgvPoint.Delete().Where(agvpoint.DockIDEQ(id)).Exec(r.Context()); derr != nil {
|
||||
httpx.Fail(w, 3116, "删除关联 AGV 点位失败:"+derr.Error())
|
||||
return
|
||||
}
|
||||
httpx.Ok(w, map[string]any{"id": id})
|
||||
}
|
||||
}
|
||||
@@ -282,11 +324,28 @@ func SetDockStatusHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
httpx.BadRequest(w, "非法的启用状态(仅支持:启用 / 停用)")
|
||||
return
|
||||
}
|
||||
d, err := svcCtx.EntClient.Dock.UpdateOneID(req.ID).SetStatus(req.Status).Save(r.Context())
|
||||
d, err := svcCtx.EntClient.Dock.Get(r.Context(), req.ID)
|
||||
if err != nil {
|
||||
httpx.Fail(w, 3117, "接驳台不存在:"+err.Error())
|
||||
return
|
||||
}
|
||||
// 停用前强校验:有托盘占用 / 存在在途 AGV 任务 → 拒绝,避免误停导致在途小车失联
|
||||
if req.Status == "DISABLED" {
|
||||
if d.HasPallet {
|
||||
httpx.Fail(w, 3118, "接驳台当前有托盘占用,停用前请先清空")
|
||||
return
|
||||
}
|
||||
if n, nerr := svcCtx.EntClient.AgvTask.Query().
|
||||
Where(agvtask.TargetDock(d.DockCode), agvtask.StatusIn("PENDING", "1", "2")).Count(r.Context()); nerr == nil && n > 0 {
|
||||
httpx.Fail(w, 3119, "接驳台存在在途 AGV 任务,停用前请先取消或等待完成")
|
||||
return
|
||||
}
|
||||
}
|
||||
d2, err := svcCtx.EntClient.Dock.UpdateOneID(req.ID).SetStatus(req.Status).Save(r.Context())
|
||||
if err != nil {
|
||||
httpx.Fail(w, 3117, "更新接驳台状态失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
httpx.Ok(w, dockRowOf(d))
|
||||
httpx.Ok(w, dockRowOf(d2))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,45 @@ func PlcSendLogsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// PlcSendWithdrawHandler 撤回下发(2026-11-03 断头路补全):误下发收回,仅 SENT 可撤,原因必填。
|
||||
func PlcSendWithdrawHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
ID int `json:"id"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if err := httpx.ParseJSON(r, &req); err != nil {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
rec, err := logic.New(svcCtx).WithdrawSend(r.Context(), req.ID, req.Reason, operator(r, ""))
|
||||
if err != nil {
|
||||
httpx.Fail(w, 3204, err.Error())
|
||||
return
|
||||
}
|
||||
httpx.Ok(w, rec)
|
||||
}
|
||||
}
|
||||
|
||||
// PlcSendManualDoneHandler 人工完成收口:PLC 完成信号丢失/未接入时,现场确认到位后人工放行下一条。
|
||||
func PlcSendManualDoneHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
if err := httpx.ParseJSON(r, &req); err != nil {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
rec, err := logic.New(svcCtx).ManualComplete(r.Context(), req.ID, operator(r, ""))
|
||||
if err != nil {
|
||||
httpx.Fail(w, 3205, err.Error())
|
||||
return
|
||||
}
|
||||
httpx.Ok(w, rec)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 拧紧 ----------
|
||||
|
||||
// parseTorqueTime 兼容 RFC3339 与纯日期(YYYY-MM-DD)两种查询参数;
|
||||
|
||||
@@ -272,6 +272,10 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
|
||||
|
||||
{Method: http.MethodPost, Path: "/plc/send-process", Handler: production.PlcSendProcessHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/plc/send-logs", Handler: production.PlcSendLogsHandler(serverCtx)},
|
||||
// 下发撤回(2026-11-03 断头路补全):误下发收回,仅 SENT 可撤、原因必填、留痕操作日志。
|
||||
{Method: http.MethodPost, Path: "/plc/send-withdraw", Handler: production.PlcSendWithdrawHandler(serverCtx)},
|
||||
// 人工完成收口:PLC 完成信号丢失/产线未接入时,现场确认到位后人工放行下一条。
|
||||
{Method: http.MethodPost, Path: "/plc/send-manual-done", Handler: production.PlcSendManualDoneHandler(serverCtx)},
|
||||
|
||||
// PLC 手动监控(新菜单 produce.plc.monitor):只读点位/在途 + 手动下发(含互斥校验 + 操作日志)。
|
||||
// 不另设操作权限码:本组不进 pathPermMap,仅靠前端菜单码控制可见性(任意登录用户可调)。
|
||||
@@ -301,7 +305,11 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
|
||||
{Method: http.MethodGet, Path: "/agv/tasks", Handler: production.ListAgvTasksHandler(serverCtx)},
|
||||
{Method: http.MethodPost, Path: "/agv/refresh", Handler: production.RefreshAgvTasksHandler(serverCtx)},
|
||||
{Method: http.MethodPost, Path: "/agv/cancel", Handler: production.CancelAgvTaskHandler(serverCtx)},
|
||||
{Method: http.MethodPost, Path: "/agv/retry", Handler: production.RetryAgvTaskHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/agv/mode", Handler: production.AgvModeHandler(serverCtx)},
|
||||
// AGV 点位管理(独立点位表:硬件映射编码/接驳台绑定不可改,仅名称/启用可改)
|
||||
{Method: http.MethodGet, Path: "/agv/points", Handler: production.ListAgvPointsHandler(serverCtx)},
|
||||
{Method: http.MethodPut, Path: "/agv/points/:id", Handler: production.UpdateAgvPointHandler(serverCtx)},
|
||||
|
||||
{Method: http.MethodPost, Path: "/torque/report", Handler: production.TorqueReportHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/torque/records", Handler: production.TorqueRecordsHandler(serverCtx)},
|
||||
|
||||
Reference in New Issue
Block a user