【MES】 - AGV点位/线路点位/AGV任务/工单 ent schema 重构(全量重生成 ent 代码) - 新增派车锁 agv_point_lock.go:目标点位占用即拒车,JBT21 库房接驳台卸货后保持锁定待库管手动解锁 - 新增进程内 TTL 缓存 cache.go,由 Redis 看板缓存迁移至内存(singleflight 防击穿,去除外部依赖) - AGV 三段链安全机制、车辆状态/接驳台左右互换、补料损耗闭环完善 - 来料检/工序卡/工单/质检/日排产/AGV 前端与帮助文档更新 - 新增 migrations/、sql/ 目录 【WMS】 - 出库、Excel 导出、物料、路由处理增强 - 配置与 DB 初始化调整;基础数据/成品入库/入库前端调整 【工位终端】 - 移除离线同步队列(syncer 包、sync.go、SyncQueueDialog.vue),改为实时直连上报 MES(断网即失败报错),以本人身份操作 - 本地 SQLite 仅保留操作日志 + 内置账号 + 登录态;保留空闲自动登出 【文档】 - 删除 AGV海康litev1.5.md;案例.md 更新
167 lines
7.0 KiB
Go
167 lines
7.0 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
|
||
"bj_power_mes/ent"
|
||
"bj_power_mes/ent/agvvehiclestate"
|
||
"bj_power_mes/ent/dock"
|
||
)
|
||
|
||
// ============ AGV 载货状态台账(2026-11-04 防撞校验补做) ============
|
||
// 背景:海康 RCS-Lite 协议无直接「载货/有无货」布尔字段(queryAgvStatus 仅 podCode 背货架编号可间接参考,
|
||
// 见 internal/agv/client.go),故本系统按车辆编号自维护载货台账 agv_vehicle_state:
|
||
// - 取货确认(ConfirmLoadAndGo)→ has_pallet=true(source=AUTO_LOAD)
|
||
// - 卸货确认(ConfirmUnloadRelease,含传感器自动放行路径)→ has_pallet=false(source=AUTO_UNLOAD)
|
||
// - RCS 回调/任务查询回写的 agv_code 绑定具体车辆;RCS 未分车时记为 DEFAULT(未知车辆)
|
||
// - MES「AGV配送 → 车辆状态」页提供人工维护入口(B2),留痕操作人与时间
|
||
// 消费方:dispatchAgvSegment 派车前防撞硬拒(B3)——AGV 载货=true 且 目标接驳台有托盘(台账/PLC 传感器)→ 拒派。
|
||
|
||
// agvUnknownCode RCS 未分车(agv_code 为空)时的兜底台账键。
|
||
const agvUnknownCode = "DEFAULT"
|
||
|
||
// AgvVehicleStateView 载货状态视图(camelCase,与前端契约一致)。
|
||
type AgvVehicleStateView struct {
|
||
ID int `json:"id"`
|
||
AgvCode string `json:"agvCode"`
|
||
HasPallet bool `json:"hasPallet"`
|
||
Source string `json:"source"`
|
||
UpdatedBy string `json:"updatedBy"`
|
||
UpdatedAt int64 `json:"updatedAt"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
|
||
// setAgvLoadedState 写载货台账(upsert,按 agv_code)。agvCode 为空 → DEFAULT(未知车辆)。
|
||
// best-effort:写失败仅打日志,不阻断装卸确认主链路(台账仅用于派车防撞,可人工修正)。
|
||
func (s *Service) setAgvLoadedState(ctx context.Context, agvCode string, hasPallet bool, source, operator string) {
|
||
code := strings.TrimSpace(agvCode)
|
||
if code == "" {
|
||
code = agvUnknownCode
|
||
}
|
||
now := time.Now().Unix()
|
||
exist, err := s.ctx.EntClient.AgvVehicleState.Query().
|
||
Where(agvvehiclestate.AgvCode(code)).Only(ctx)
|
||
if err != nil || exist == nil {
|
||
if _, cerr := s.ctx.EntClient.AgvVehicleState.Create().
|
||
SetAgvCode(code).SetHasPallet(hasPallet).
|
||
SetSource(source).SetUpdatedBy(operator).SetUpdatedAt(now).
|
||
Save(ctx); cerr != nil {
|
||
fmt.Println("AGV 载货台账写入失败:", cerr)
|
||
}
|
||
return
|
||
}
|
||
if uerr := s.ctx.EntClient.AgvVehicleState.UpdateOneID(exist.ID).
|
||
SetHasPallet(hasPallet).SetSource(source).
|
||
SetUpdatedBy(operator).SetUpdatedAt(now).Exec(ctx); uerr != nil {
|
||
fmt.Println("AGV 载货台账更新失败:", uerr)
|
||
}
|
||
}
|
||
|
||
// ListAgvVehicleStates 列出载货台账(含 DEFAULT 未知车辆行),供 MES 车辆状态页维护。
|
||
func (s *Service) ListAgvVehicleStates(ctx context.Context) ([]AgvVehicleStateView, error) {
|
||
rows, err := s.ctx.EntClient.AgvVehicleState.Query().
|
||
Order(ent.Asc(agvvehiclestate.FieldAgvCode)).All(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
out := make([]AgvVehicleStateView, 0, len(rows))
|
||
for _, r := range rows {
|
||
out = append(out, AgvVehicleStateView{
|
||
ID: r.ID, AgvCode: r.AgvCode, HasPallet: r.HasPallet,
|
||
Source: r.Source, UpdatedBy: r.UpdatedBy, UpdatedAt: r.UpdatedAt, Remark: r.Remark,
|
||
})
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// SetAgvVehicleStateManual 人工维护载货状态(B2):手动改「有托盘/空车」,留痕操作人/时间/来源。
|
||
func (s *Service) SetAgvVehicleStateManual(ctx context.Context, agvCode string, hasPallet bool, operator, remark string) error {
|
||
code := strings.TrimSpace(agvCode)
|
||
if code == "" {
|
||
return errors.New("车辆编号(agvCode)必填")
|
||
}
|
||
if len(code) > 32 {
|
||
return errors.New("车辆编号过长(≤32 字符)")
|
||
}
|
||
if operator == "" {
|
||
operator = "manual"
|
||
}
|
||
now := time.Now().Unix()
|
||
exist, err := s.ctx.EntClient.AgvVehicleState.Query().
|
||
Where(agvvehiclestate.AgvCode(code)).Only(ctx)
|
||
if err != nil || exist == nil {
|
||
if _, cerr := s.ctx.EntClient.AgvVehicleState.Create().
|
||
SetAgvCode(code).SetHasPallet(hasPallet).
|
||
SetSource("MANUAL").SetUpdatedBy(operator).SetUpdatedAt(now).
|
||
SetRemark(strings.TrimSpace(remark)).Save(ctx); cerr != nil {
|
||
return cerr
|
||
}
|
||
} else if uerr := s.ctx.EntClient.AgvVehicleState.UpdateOneID(exist.ID).
|
||
SetHasPallet(hasPallet).SetSource("MANUAL").
|
||
SetUpdatedBy(operator).SetUpdatedAt(now).
|
||
SetRemark(strings.TrimSpace(remark)).Exec(ctx); uerr != nil {
|
||
return uerr
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "agv.vehicle_state", operator, "", "agv_vehicle_state", code,
|
||
"人工修改 AGV 载货状态:"+boolCn2(hasPallet), map[string]any{"hasPallet": hasPallet, "remark": remark})
|
||
return nil
|
||
}
|
||
|
||
// anyAgvLoaded 是否存在载货=true 的车辆。
|
||
func (s *Service) anyAgvLoaded(ctx context.Context) bool {
|
||
n, err := s.ctx.EntClient.AgvVehicleState.Query().
|
||
Where(agvvehiclestate.HasPallet(true)).Count(ctx)
|
||
return err == nil && n > 0
|
||
}
|
||
|
||
// guardLoadedAgvVsTarget 派车前防撞硬拒(B3):AGV 载货=true 且 目标接驳台有托盘
|
||
//(dock 台账或 PLC 传感器任一判定有托盘)→ 拒绝派车。
|
||
// 仅对「目标为接驳台(dock)」的段生效;纯点位(充电位等)直接放行。
|
||
func (s *Service) guardLoadedAgvVsTarget(ctx context.Context, target string) error {
|
||
if target == "" || target == agvChargePoint {
|
||
return nil
|
||
}
|
||
if !s.anyAgvLoaded(ctx) {
|
||
// 2026-11-06 JBT21 危险场景加固:载货台账双源校验——台账全空时强制回查 RCS 车载货架
|
||
//(podCode 非空且非"0"=车上背着货);台账与 RCS 不一致以 RCS 为准:回写台账并硬拒。
|
||
// mock 模式(未接真实 RCS)返回的是示例车,跳过双源校验。
|
||
if s.ctx.Agv != nil && !s.ctx.Agv.Disable() {
|
||
if vs, verr := s.ctx.Agv.QueryAgvStatus(ctx); verr == nil {
|
||
for _, v := range vs {
|
||
if v.PodCode != "" && v.PodCode != "0" {
|
||
s.setAgvLoadedState(ctx, v.RobotCode, true, "RCS_SYNC", "system")
|
||
return errors.New("载货台账与 RCS 不一致(RCS 上报 AGV " + v.RobotCode + " 载货 " + v.PodCode + "),已按 RCS 为准回写台账并拒绝派车;请先人工核实并卸货")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
d, err := s.ctx.EntClient.Dock.Query().Where(dock.DockCodeEQ(target)).First(ctx)
|
||
if err != nil || d == nil {
|
||
return nil // 非接驳台点位(纯点位),不适用本防撞
|
||
}
|
||
if d.HasPallet {
|
||
return errors.New("AGV载有托盘,不能前往已有托盘的接驳台(" + target + " 台账有托盘;请先人工修正载货状态或清空接驳台)")
|
||
}
|
||
// PLC 传感器兜底判定(台账无托盘但传感器检到 → 同样硬拒;PLC 不可达不阻断,走台账)
|
||
if s.ctx.PLC != nil && !s.ctx.PLC.IsMock() {
|
||
if has, perr := s.ctx.PLC.Get().QueryDockPallet(ctx, target); perr == nil && has {
|
||
return errors.New("AGV载有托盘,不能前往已有托盘的接驳台(" + target + " 传感器检到托盘;请先人工修正载货状态或清空接驳台)")
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// boolCn2 bool 转中文(有托盘/空车)。
|
||
func boolCn2(b bool) string {
|
||
if b {
|
||
return "有托盘"
|
||
}
|
||
return "空车"
|
||
}
|