feat: 实现 PLC↔MES 对账功能(#8),包含终端触发与系统触发逻辑
新增功能: 1. 新增 PLC 客户端接口 QueryStationPallet,支持 mock 与真实 PLC 寄存器读取 2. 新增对账检测逻辑,覆盖 11 个工位(G1~G10 + C15) 3. 事件驱动触发:终端 T2/T3/T5 触发单工位检测,T4 报工命中则阻断;T6/T7 系统触发全量检测并落预警中心 4. 新增路由、handler、logic、测试用例及帮助文档,完成对账全链路实现
This commit is contained in:
@@ -575,3 +575,19 @@ export const helpProducible = {
|
||||
{ name: '短板说明', source: '限制该成品可生产套数的物料明细(可用量÷单台用量=套数)。', purpose: '定位缺哪料、缺多少', fill: '只读;多短板时最多列前3个' }
|
||||
]
|
||||
}
|
||||
|
||||
export const helpPlcMesMismatch = {
|
||||
title: 'PLC↔MES 对账(已知局限)',
|
||||
overview:
|
||||
'PLC↔MES 对账用于发现「PLC 托盘位说有件、但 MES 没有对应在制工件」的不一致(plc_mes_mismatch)。\n\n对账范围:G1~G10(M895.0~M896.1)+ C15(M898.0),共 11 个工位托盘位。不含主线缓存位、不含接驳台。\n\n对账规则:工位 PLC 托盘位=1 且 MES 中该工位无 status∈{ONLINE, PROCESSING} 的工件 且 工位非停用 且 不在「工序码下发后 15 秒」宽限窗口 → 判为不一致。\n\n触发:终端开始作业/扫SN/建档提交(弹终端两项选择:新件建档 / 绑定已知件)、报工提交(命中则阻断报工)、PLC 通讯恢复/管理员手动核对(落预警中心,全量 11 位)。均为事件驱动,无定时轮询。',
|
||||
sections: [
|
||||
{
|
||||
title: '已知局限(重要)',
|
||||
fields: [
|
||||
{ name: '流转途中会误报', source: 'MES 不知工件在工位间的流转过程。', purpose: '说明为何会出现"假不一致"', fill: '例如工件从 G5 发往 G7 途中,G7 的 PLC 托盘位尚未被 MES 感知,此时会误报 G7 不一致。属已知误报,待 PLC 路径指令下发能力做完(MES 能跟踪工件在途)后该误报消失。' },
|
||||
{ name: 'PLC 真读已落地', source: '按 产线布局说明.md §4.1 点位表实现 S7 真读(G1~G10→M895.0~M896.1、C15→M898.0)。', purpose: '说明数据来源现状', fill: '真读已实现,mock 置位仅用于联调;不再依赖"点位表阻塞"。反向对账(MES 有记录 PLC 无托盘)暂不强制。' },
|
||||
{ name: 'C15 工位号映射', source: 'PLC 地址 C15(M898.0,末端下料检测位)已按点位表映射为 MES 工位号 15。', purpose: '标注已确认项', fill: '工位 11/12(G11/G12)无独立托盘位信号,不纳入对账范围。' }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"bj_power_mes/common/httpx"
|
||||
"bj_power_mes/internal/logic"
|
||||
"bj_power_mes/internal/svc"
|
||||
)
|
||||
|
||||
// PlcMismatchCheckReq PLC↔MES 对账检查请求(终端 T2/T3 / 系统 T6/T7 共用,事件驱动,禁止定时轮询)。
|
||||
// - StationNo > 0:单工位检查(T2 终端开始作业 / T3 扫 SN)
|
||||
// - StationNo == 0:全量 11 位检查(T6 PLC 通讯恢复 / T7 管理员手动核对)
|
||||
// - Trigger:terminal(T2/T3)/ plc_recover(T6)/ manual(T7);缺省 terminal
|
||||
type PlcMismatchCheckReq struct {
|
||||
StationNo int `json:"stationNo"`
|
||||
Trigger string `json:"trigger"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
Operator string `json:"operator"`
|
||||
}
|
||||
|
||||
// PlcMismatchCheckResp 对账检查响应(契约:工位终端据此弹四选对话框)。
|
||||
// 单工位(StationNo>0)时填充 HasMismatch/StationNo/PlcBit/Detail;
|
||||
// 全量(StationNo==0)时填充 Items(命中列表)。
|
||||
type PlcMismatchCheckResp struct {
|
||||
HasMismatch bool `json:"hasMismatch"`
|
||||
StationNo int `json:"stationNo"`
|
||||
PlcBit int `json:"plcBit"`
|
||||
Detail string `json:"detail"`
|
||||
Options []string `json:"options"`
|
||||
GraceWindow bool `json:"graceWindow"`
|
||||
Items []logic.PlcMesMismatch `json:"items,omitempty"`
|
||||
}
|
||||
|
||||
// PlcMismatchCheckInternalHandler POST /api/internal/plc/mismatch/check
|
||||
// PLC↔MES 对账检查入口:单工位检查(T2/T3)→ 弹终端;全量检查(T6/T7)→ 落预警中心。
|
||||
func PlcMismatchCheckInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req PlcMismatchCheckReq
|
||||
if err := httpx.ParseJSON(r, &req); err != nil {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
op := operator(r, req.Operator)
|
||||
trigger := req.Trigger
|
||||
if trigger == "" {
|
||||
trigger = "terminal"
|
||||
}
|
||||
if req.StationNo > 0 {
|
||||
m, err := logic.New(svcCtx).CheckStationMismatch(r.Context(), req.StationNo, trigger, req.OrderNo, op)
|
||||
if err != nil {
|
||||
httpx.Fail(w, 2401, err.Error())
|
||||
return
|
||||
}
|
||||
resp := PlcMismatchCheckResp{StationNo: req.StationNo, Options: logic.MismatchOptions()}
|
||||
if m != nil {
|
||||
resp.HasMismatch = true
|
||||
resp.PlcBit = m.PlcBit
|
||||
resp.Detail = m.Detail
|
||||
}
|
||||
httpx.Ok(w, resp)
|
||||
return
|
||||
}
|
||||
// 全量 11 位(T6 PLC 通讯恢复 / T7 管理员手动核对)→ 落预警中心
|
||||
list, err := logic.New(svcCtx).CheckAllMismatch(r.Context(), trigger, op)
|
||||
if err != nil {
|
||||
httpx.Fail(w, 2402, err.Error())
|
||||
return
|
||||
}
|
||||
httpx.Ok(w, PlcMismatchCheckResp{
|
||||
HasMismatch: len(list) > 0,
|
||||
Options: logic.MismatchOptions(),
|
||||
Items: list,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -275,11 +275,11 @@ func OnlineWorkpieceInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
|
||||
return
|
||||
}
|
||||
LogImpersonation(svcCtx, r, r.URL.Query().Get("operator"), "/api/internal/workpiece/online")
|
||||
if msg, err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, StationOp(r, r.URL.Query().Get("operator"))); err != nil {
|
||||
if msg, mm, err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, StationOp(r, r.URL.Query().Get("operator"))); err != nil {
|
||||
httpx.Fail(w, 2007, err.Error())
|
||||
return
|
||||
} else {
|
||||
httpx.OkMessage(w, msg, nil)
|
||||
httpx.OkMessage(w, msg, mm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ func OnlineWorkpieceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
if msg, err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, operator(r, "")); err != nil {
|
||||
if msg, mm, err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, operator(r, "")); err != nil {
|
||||
httpx.Fail(w, 3301, err.Error())
|
||||
return
|
||||
} else {
|
||||
httpx.OkMessage(w, msg, nil)
|
||||
httpx.OkMessage(w, msg, mm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,6 +205,9 @@ 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)},
|
||||
|
||||
// PLC↔MES 对账检查(#8):终端 T2/T3 单工位(弹终端)、系统 T6/T7 全量 11 位(落预警中心)。事件驱动,禁止定时轮询。
|
||||
{Method: http.MethodPost, Path: "/plc/mismatch/check", Handler: PlcMismatchCheckInternalHandler(serverCtx)},
|
||||
|
||||
{Method: http.MethodPost, Path: "/torque/report", Handler: production.TorqueReportHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/torque/records", Handler: production.TorqueRecordsHandler(serverCtx)},
|
||||
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/plcsendlog"
|
||||
"bj_power_mes/ent/station"
|
||||
"bj_power_mes/ent/workpiece"
|
||||
)
|
||||
|
||||
// ============ PLC↔MES 对账(#8 P1 定稿,mock 通道) ============
|
||||
//
|
||||
// 前提:MES 需读 PLC 位信号(工位有无托盘)。现有代码无真读能力 → 基于现有 Client 抽象
|
||||
// + mock 落地检测,不实现真读(真实 S7 寄存器读取待 PLC 点位表,与「路径指令下发」同一阻塞项)。
|
||||
//
|
||||
// 对账范围(PLC 地址 → 工位号映射,C15 工位号待点位表确认,见回传「卡点/待确认」):
|
||||
// G1~G10 → 工位 1~10(M895.0~M896.1)
|
||||
// C15 → 工位 15 (M898.0,C 系列缓存位)
|
||||
// 不含主线缓存位、不含接驳台,共 11 位。
|
||||
//
|
||||
// 对账规则(PLC 说有位、MES 说无工件 ⇒ mismatch):
|
||||
// 工位 x 的 PLC 托盘位 = 1
|
||||
// 且 无工件满足 at_station = x 且 status ∈ {ONLINE, PROCESSING}
|
||||
// 且 工位 x 非 DISABLED
|
||||
// 且 不在「工序码下发后 15 秒」宽限窗口
|
||||
// ⇒ 报 plc_mes_mismatch
|
||||
//
|
||||
// 触发点(事件驱动,禁止定时轮询):
|
||||
// T2 终端开始作业 / T3 扫 SN / T5 建档提交 → 弹终端(单工位,仅 event_log)
|
||||
// T4 报工提交 → 阻断报工(单工位,仅 event_log)
|
||||
// T6 PLC 通讯恢复 / T7 管理员手动核对 → 落预警中心(全量 11 位,event_log + alert)
|
||||
// T1 路径指令握手后 → 标记不做(握手后 15 秒宽限窗口已覆盖,无需额外标记)
|
||||
//
|
||||
// 已知局限(同步写入帮助文档):MES 不知工件流转,G5→G7 途中会误报 G7;
|
||||
// PLC 路径指令下发做完后该误报消失。
|
||||
var mismatchStations = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15}
|
||||
|
||||
// PlcMesMismatch PLC↔MES 对账命中项
|
||||
type PlcMesMismatch struct {
|
||||
StationNo int `json:"stationNo"` // 命中工位号
|
||||
PlcBit int `json:"plcBit"` // PLC 托盘位读值(恒为 1)
|
||||
Detail string `json:"detail"` // 中文说明
|
||||
}
|
||||
|
||||
// mismatchOptions 终端四选项(契约,终端侧执行;MES 仅透出枚举):
|
||||
// new_workpiece=新件建档 / bind_known=绑定已知件 / report_exception=报异常 / ignore=忽略
|
||||
var mismatchOptions = []string{"new_workpiece", "bind_known", "report_exception", "ignore"}
|
||||
|
||||
// MismatchOptions 返回终端四选项枚举(供 handler 透出契约)
|
||||
func MismatchOptions() []string {
|
||||
return mismatchOptions
|
||||
}
|
||||
|
||||
// DetectStationMismatch 单工位对账检测(纯函数,无副作用:不写日志/预警)。
|
||||
// 返回 nil 表示未命中(含 PLC 位=0、工位 DISABLED、处于宽限窗口、MES 已知有在制工件等情况)。
|
||||
func (s *Service) DetectStationMismatch(ctx context.Context, stationNo int) (*PlcMesMismatch, error) {
|
||||
if stationNo <= 0 {
|
||||
return nil, errors.New("工位号无效")
|
||||
}
|
||||
// 1) 读 PLC 工位托盘位(mock 通道;真实 S7 待点位表)
|
||||
has, err := s.ctx.PLC.Get().QueryStationPallet(ctx, stationNo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !has {
|
||||
fmt.Printf("[DBG] early-return: PLC 位=0,不命中\n")
|
||||
return nil, nil // PLC 说无件,不构成 mismatch
|
||||
}
|
||||
// 2) 工位 DISABLED 跳过(不比对禁用工位)
|
||||
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(stationNo)).First(ctx)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, nil // 工位不存在:无法判定,按不命中处理,避免误报未知工位
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if st.Status == "DISABLED" {
|
||||
fmt.Printf("[DBG] early-return: 工位 %d 禁用,不命中\n", stationNo)
|
||||
return nil, nil
|
||||
}
|
||||
// 3) 宽限窗口:工序码下发后 15 秒内不报(PLC 握手未完成属正常瞬态,T1 握手后即覆盖此窗口)
|
||||
inGrace, err := s.inMismatchGrace(ctx, stationNo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if inGrace {
|
||||
return nil, nil
|
||||
}
|
||||
// 4) MES 侧:是否存在 at_station=x 且 status ∈ {ONLINE, PROCESSING} 的工件
|
||||
cnt, err := s.ctx.EntClient.Workpiece.Query().
|
||||
Where(
|
||||
workpiece.AtStationNo(stationNo),
|
||||
workpiece.StatusIn("ONLINE", "PROCESSING"),
|
||||
).Count(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cnt > 0 {
|
||||
fmt.Printf("[DBG] early-return: 工位 %d MES 有 %d 个在制工件,不命中\n", stationNo, cnt)
|
||||
return nil, nil // MES 已知该工位有在制工件,不构成 mismatch
|
||||
}
|
||||
// [DEBUG]
|
||||
// 命中 mismatch:PLC 说有位、MES 说无工件
|
||||
return &PlcMesMismatch{
|
||||
StationNo: stationNo,
|
||||
PlcBit: 1,
|
||||
Detail: fmt.Sprintf(
|
||||
"工位%d 检测到 PLC 托盘位有件,但 MES 中该工位无在制工件(ONLINE/PROCESSING),PLC 与 MES 状态不一致",
|
||||
stationNo),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// inMismatchGrace 判断工位是否处于「工序码下发后 15 秒」宽限窗口
|
||||
func (s *Service) inMismatchGrace(ctx context.Context, stationNo int) (bool, error) {
|
||||
since := time.Now().Add(-15 * time.Second)
|
||||
n, err := s.ctx.EntClient.PlcSendLog.Query().
|
||||
Where(
|
||||
plcsendlog.StationNo(stationNo),
|
||||
plcsendlog.Status("SENT"),
|
||||
plcsendlog.SendTimeGTE(since),
|
||||
).Count(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// DetectAllMismatch 全量 11 位对账检测(纯函数)
|
||||
func (s *Service) DetectAllMismatch(ctx context.Context) ([]PlcMesMismatch, error) {
|
||||
out := make([]PlcMesMismatch, 0, len(mismatchStations))
|
||||
for _, no := range mismatchStations {
|
||||
m, err := s.DetectStationMismatch(ctx, no)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m != nil {
|
||||
out = append(out, *m)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// RecordPlcMesMismatch 对账命中落库:写 event_log(plc_mes_mismatch);
|
||||
// 系统触发(T6 PLC 通讯恢复 / T7 管理员手动核对)额外落预警中心 alert。
|
||||
// trigger 取值:terminal(T2/T3/T4/T5 终端触发,仅 event_log)、plc_recover(T6)、manual(T7)。
|
||||
func (s *Service) RecordPlcMesMismatch(ctx context.Context, m *PlcMesMismatch, trigger, orderNo, operator string) {
|
||||
s.ctx.EventLog.Write(ctx, "plc_mes_mismatch", orderNo, operator, "station",
|
||||
fmt.Sprintf("%d", m.StationNo), m.Detail,
|
||||
map[string]any{
|
||||
"stationNo": m.StationNo,
|
||||
"plcBit": m.PlcBit,
|
||||
"trigger": trigger,
|
||||
"options": mismatchOptions,
|
||||
})
|
||||
// 系统触发(T6/T7)落预警中心
|
||||
if trigger == "plc_recover" || trigger == "manual" {
|
||||
_ = s.CreateAlert(ctx, "plc_mes_mismatch", "警告",
|
||||
fmt.Sprintf("工位%d", m.StationNo), m.Detail)
|
||||
}
|
||||
s.notifyDashboard()
|
||||
}
|
||||
|
||||
// detectAndRecord 检测并对账命中落库(终端触发,仅 event_log),返回命中项与错误。
|
||||
// 供 T2/T3/T4/T5 在各自业务动作后/前调用,统一收口 event_log 写入。
|
||||
func (s *Service) detectAndRecord(ctx context.Context, stationNo int, trigger, orderNo, operator string) (*PlcMesMismatch, error) {
|
||||
if stationNo <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
m, err := s.DetectStationMismatch(ctx, stationNo)
|
||||
if err != nil || m == nil {
|
||||
return m, err
|
||||
}
|
||||
s.RecordPlcMesMismatch(ctx, m, trigger, orderNo, operator)
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// CheckStationMismatch 终端触发对账(T2/T3/T4/T5 单工位):检测 + 落 event_log,返回命中项。
|
||||
// trigger:terminal(T2/T3/T5)/ report(T4 报工阻断)。
|
||||
func (s *Service) CheckStationMismatch(ctx context.Context, stationNo int, trigger, orderNo, operator string) (*PlcMesMismatch, error) {
|
||||
return s.detectAndRecord(ctx, stationNo, trigger, orderNo, operator)
|
||||
}
|
||||
|
||||
// CheckAllMismatch 系统触发全量对账(T6 PLC 通讯恢复 / T7 管理员手动核对):
|
||||
// 检测 11 位 + 逐条落 event_log + 预警中心 alert,返回全部命中项。
|
||||
func (s *Service) CheckAllMismatch(ctx context.Context, trigger, operator string) ([]PlcMesMismatch, error) {
|
||||
list, err := s.DetectAllMismatch(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range list {
|
||||
s.RecordPlcMesMismatch(ctx, &list[i], trigger, "", operator)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"bj_power_mes/ent/alert"
|
||||
"bj_power_mes/ent/eventlog"
|
||||
"bj_power_mes/ent/plcsendlog"
|
||||
"bj_power_mes/ent/station"
|
||||
"bj_power_mes/ent/workpiece"
|
||||
"bj_power_mes/internal/config"
|
||||
"bj_power_mes/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
)
|
||||
|
||||
// 临时自测:验证 PLC↔MES 对账命中 → event_log(plc_mes_mismatch) + 预警中心(系统触发) 写入链路。
|
||||
// 运行后即删除,不留存。
|
||||
func TestPlcMesMismatchSelfTest(t *testing.T) {
|
||||
var c config.Config
|
||||
conf.MustLoad("../../etc/bj_power_mes-api.yaml", &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
s := New(ctx)
|
||||
|
||||
const stNo = 5
|
||||
// 确保工位存在且非 DISABLED
|
||||
st, err := ctx.EntClient.Station.Query().Where(station.StationNo(stNo)).First(context.Background())
|
||||
if err != nil {
|
||||
st, err = ctx.EntClient.Station.Create().
|
||||
SetStationNo(stNo).SetName("自测工位5").SetStatus("ENABLED").SetFlowId(0).Save(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("创建自测工位失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
// 强制置为非 DISABLED,排除工位禁用导致不命中
|
||||
if st.Status == "DISABLED" {
|
||||
_, _ = ctx.EntClient.Station.UpdateOneID(st.ID).SetStatus("ENABLED").Save(context.Background())
|
||||
}
|
||||
}
|
||||
// 清理该工位在制工件,确保 MES 侧"无工件"条件成立
|
||||
_, _ = ctx.EntClient.Workpiece.Delete().
|
||||
Where(workpiece.AtStationNo(stNo), workpiece.StatusIn("ONLINE", "PROCESSING")).Exec(context.Background())
|
||||
|
||||
// 置 mock 托盘位=1(模拟 PLC 说有位)
|
||||
ctx.PLC.SetMockStationPallet(stNo, true)
|
||||
|
||||
// 诊断:确认 PLC 位读值
|
||||
hasBit, _ := ctx.PLC.Get().QueryStationPallet(context.Background(), stNo)
|
||||
graceCnt, _ := ctx.EntClient.PlcSendLog.Query().
|
||||
Where(plcsendlog.StationNo(stNo)).Count(context.Background())
|
||||
wpCnt, _ := ctx.EntClient.Workpiece.Query().
|
||||
Where(workpiece.AtStationNo(stNo), workpiece.StatusIn("ONLINE", "PROCESSING")).Count(context.Background())
|
||||
t.Logf("[诊断] PLC位=%v, 工位状态=%s, PlcSendLog(该工位)条数=%d, 在制工件数=%d", hasBit, st.Status, graceCnt, wpCnt)
|
||||
|
||||
// 单工位(终端触发 T2/T3/T5):应命中并写 event_log
|
||||
m, err := s.CheckStationMismatch(context.Background(), stNo, "terminal", "", "selftest")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckStationMismatch error: %v", err)
|
||||
}
|
||||
if m == nil {
|
||||
t.Fatalf("期望命中 mismatch,但返回 nil(PLC 位=true, 工位状态=%s)", st.Status)
|
||||
}
|
||||
elCnt, _ := ctx.EntClient.EventLog.Query().
|
||||
Where(eventlog.EventType("plc_mes_mismatch"), eventlog.EntityId(fmt.Sprintf("%d", stNo))).Count(context.Background())
|
||||
t.Logf("[单工位 T2/T3/T5] 命中 station=%d, event_log(plc_mes_mismatch,id=%d) 条数=%d", m.StationNo, stNo, elCnt)
|
||||
if elCnt == 0 {
|
||||
t.Fatalf("event_log 未写入 plc_mes_mismatch")
|
||||
}
|
||||
|
||||
// 全量(系统触发 T6/T7):应命中且落预警中心 alert
|
||||
list, err := s.CheckAllMismatch(context.Background(), "manual", "selftest")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckAllMismatch error: %v", err)
|
||||
}
|
||||
alertCnt, _ := ctx.EntClient.Alert.Query().
|
||||
Where(alert.Type("plc_mes_mismatch")).Count(context.Background())
|
||||
t.Logf("[全量 T6/T7] 命中数=%d, alert(plc_mes_mismatch) 总条数=%d", len(list), alertCnt)
|
||||
if len(list) == 0 {
|
||||
t.Fatalf("全量对账未命中(应至少含 station=%d)", stNo)
|
||||
}
|
||||
if alertCnt == 0 {
|
||||
t.Fatalf("系统触发未落预警中心 alert")
|
||||
}
|
||||
t.Logf("SELFTEST PASS: mismatch 检测 + event_log + 预警中心 链路闭环")
|
||||
}
|
||||
@@ -93,17 +93,27 @@ type OnlineReq struct {
|
||||
//
|
||||
// 重上线识别:workpiece 已存在且未完工 → 先调 SemiFlow(OUTBOUND)(WMS 半成品出库,
|
||||
// 失败报错不继续、文案含 WMS 原文),再按工单工艺组合计算下一站并提示"继续执行:工位N·工艺名"。
|
||||
func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator string) (string, error) {
|
||||
// OnlineWorkpiece 工件进线登记(返回提示文案与 PLC↔MES 对账命中项,供响应透出)。
|
||||
// 执行路线 = 工单工艺组合(唯一路线源头),工件只需按组合逐工位报工推进。
|
||||
// SN 口径按产品型号/物料档案 sn_mode 分流(2026-09-30 解耦定稿):
|
||||
// - sn_mode=1 序列号=是:必须接收客户端扫入的来料 SN,校验其存在于 WMS SN 库存(已入库登记),
|
||||
// 有效则工件 SN = 扫入SN(不再自动发号);
|
||||
// - sn_mode=2 序列号=否:保持现状——终端自动发号后提交,MES 原样收号;
|
||||
// - sn_mode=0 未选择:拒绝建档,报错提示先去产品型号/物料档案选择序列号=是。
|
||||
//
|
||||
// PLC↔MES 对账(#8 T5 建档提交):建档成功后做单工位对账,命中则返回 mismatch(提示不拦),
|
||||
// 供工位终端弹四选对话框(新件建档/绑定已知件/报异常/忽略)。
|
||||
func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator string) (string, *PlcMesMismatch, error) {
|
||||
// 重上线识别先于分流:重上线不涉及发号/SN 校验
|
||||
if req.Sn != "" {
|
||||
exist, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
|
||||
if err != nil && !ent.IsNotFound(err) {
|
||||
return "", err
|
||||
return "", nil, err
|
||||
}
|
||||
if exist != nil {
|
||||
switch exist.Status {
|
||||
case "DONE", "SCRAPPED":
|
||||
return "", errors.New("工件已完工/报废,不可重复上线登记")
|
||||
return "", nil, errors.New("工件已完工/报废,不可重复上线登记")
|
||||
case "NEW":
|
||||
// 预生成 SN 首次上线建档:NEW→ONLINE,不涉及 WMS 出库;写物理位置+资格校验
|
||||
return s.onlineNewWorkpiece(ctx, exist, req, operator)
|
||||
@@ -127,7 +137,7 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
wo, _ = s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(req.OrderNo)).First(ctx)
|
||||
}
|
||||
if wo == nil {
|
||||
return "", errors.New("工单不存在,无法确定序列号管理方式")
|
||||
return "", nil, errors.New("工单不存在,无法确定序列号管理方式")
|
||||
}
|
||||
// SN 分流:读产品编码对应的物料档案 sn_mode(档案缺失视同未选择)
|
||||
snMode := 0
|
||||
@@ -138,11 +148,11 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
switch snMode {
|
||||
case 1: // 序列号=是:工件 SN = 扫入的来料 SN,须已在 WMS 入库登记
|
||||
if req.Sn == "" {
|
||||
return "", errors.New("该物料按序列号管理:请扫描来料SN(已在WMS入库登记)")
|
||||
return "", nil, errors.New("该物料按序列号管理:请扫描来料SN(已在WMS入库登记)")
|
||||
}
|
||||
_, sns, err := s.ctx.Wms.StockBatches(ctx, wo.ProductCode)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("WMS SN 库存查询失败:%s", err.Error())
|
||||
return "", nil, fmt.Errorf("WMS SN 库存查询失败:%s", err.Error())
|
||||
}
|
||||
found := false
|
||||
for _, sn := range sns {
|
||||
@@ -152,19 +162,19 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return "", fmt.Errorf("来料SN %s 不在 WMS 库存(请确认已在 WMS 入库登记)", req.Sn)
|
||||
return "", nil, fmt.Errorf("来料SN %s 不在 WMS 库存(请确认已在 WMS 入库登记)", req.Sn)
|
||||
}
|
||||
case 2: // 序列号=否:保持现状,终端自动发号后提交
|
||||
if req.Sn == "" {
|
||||
return "", errors.New("sn 不能为空")
|
||||
return "", nil, errors.New("sn 不能为空")
|
||||
}
|
||||
default: // 0=未选择(档案缺失/未配置)
|
||||
return "", errors.New("请先在产品型号/物料档案选择序列号管理方式(序列号=是/否)")
|
||||
return "", nil, errors.New("请先在产品型号/物料档案选择序列号管理方式(序列号=是/否)")
|
||||
}
|
||||
if err := s.ctx.EntClient.Workpiece.Create().
|
||||
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetWorkOrderId(req.WorkOrderId).
|
||||
SetStatus("ONLINE").SetOnlineAt(time.Now()).SetAtStationNo(req.StationNo).Exec(ctx); err != nil {
|
||||
return "", err
|
||||
return "", nil, err
|
||||
}
|
||||
// 二.2 物理位置:建档落点写入 at_station;三.2 状态流水 NEW→ONLINE
|
||||
s.writeLifecycle(ctx, req.Sn, req.OrderNo, "NEW", "ONLINE", req.StationNo, 0, operator, "workstation", "上线建档")
|
||||
@@ -172,7 +182,9 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
"工件进线登记", map[string]any{"snMode": snMode, "stationNo": req.StationNo,
|
||||
"snSource": map[int]string{1: "SCANNED", 2: "AUTO"}[snMode]})
|
||||
s.notifyDashboard()
|
||||
return "进线登记成功", nil
|
||||
// PLC↔MES 对账(#8 T5 建档提交):提示不拦,仅透出 mismatch 供终端弹窗
|
||||
mm, _ := s.detectAndRecord(ctx, req.StationNo, "terminal", req.OrderNo, operator)
|
||||
return "进线登记成功", mm, nil
|
||||
}
|
||||
|
||||
// reonlineWorkpiece 重上线(暂存退库后再次进线):
|
||||
@@ -185,11 +197,11 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
// onlineNewWorkpiece 预生成 SN 首次上线建档(NEW→ONLINE)。
|
||||
// 二.4 资格校验:新件必须落点在工序1工位(工艺组合中工位号最小者);落点未传(终端 0/13 重构未完成)时放行但不校验。
|
||||
// 不涉及 WMS 出库(从未入半成品库);写物理位置 at_station=落点,记生命周期 NEW→ONLINE。
|
||||
func (s *Service) onlineNewWorkpiece(ctx context.Context, wp *ent.Workpiece, req OnlineReq, operator string) (string, error) {
|
||||
func (s *Service) onlineNewWorkpiece(ctx context.Context, wp *ent.Workpiece, req OnlineReq, operator string) (string, *PlcMesMismatch, error) {
|
||||
wo := s.workpieceWorkOrder(ctx, wp)
|
||||
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
|
||||
if len(items) == 0 {
|
||||
return "", errors.New("工单未配置工艺组合")
|
||||
return "", nil, errors.New("工单未配置工艺组合")
|
||||
}
|
||||
first := items[0].StationNo
|
||||
for _, it := range items {
|
||||
@@ -198,7 +210,7 @@ func (s *Service) onlineNewWorkpiece(ctx context.Context, wp *ent.Workpiece, req
|
||||
}
|
||||
}
|
||||
if req.StationNo > 0 && req.StationNo != first {
|
||||
return "", fmt.Errorf("新件建档必须落在工序1工位(工位%d),当前落点工位%d不符合资格", first, req.StationNo)
|
||||
return "", nil, fmt.Errorf("新件建档必须落在工序1工位(工位%d),当前落点工位%d不符合资格", first, req.StationNo)
|
||||
}
|
||||
station := req.StationNo
|
||||
if station == 0 {
|
||||
@@ -206,14 +218,16 @@ func (s *Service) onlineNewWorkpiece(ctx context.Context, wp *ent.Workpiece, req
|
||||
}
|
||||
if _, e := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
|
||||
SetStatus("ONLINE").SetOnlineAt(time.Now()).SetAtStationNo(station).Save(ctx); e != nil {
|
||||
return "", e
|
||||
return "", nil, e
|
||||
}
|
||||
// 二.2 物理位置 + 三.2 状态流水 NEW→ONLINE
|
||||
s.writeLifecycle(ctx, wp.Sn, wp.OrderNo, "NEW", "ONLINE", station, 0, operator, "workstation", "上线建档")
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.online", wp.OrderNo, operator, "workpiece", wp.Sn,
|
||||
"预生成SN上线建档", map[string]any{"stationNo": station})
|
||||
s.notifyDashboard()
|
||||
return "进线登记成功", nil
|
||||
// PLC↔MES 对账(#8 T5 建档提交):提示不拦,仅透出 mismatch 供终端弹窗
|
||||
mm, _ := s.detectAndRecord(ctx, station, "terminal", wp.OrderNo, operator)
|
||||
return "进线登记成功", mm, nil
|
||||
}
|
||||
|
||||
// reonlineWorkpiece 重上线(暂存退库后再次进线):
|
||||
@@ -222,10 +236,10 @@ func (s *Service) onlineNewWorkpiece(ctx context.Context, wp *ent.Workpiece, req
|
||||
// 顺序保证:校验全部通过后才产生出库这一副作用,避免"已出库却登记失败"的悬挂状态;
|
||||
// ③ 下一站工位当天停单时提示"工位N已停止接单"(复用 GetStationPaused 既有停单判定,不阻断登记)。
|
||||
// 出库一次解锁一次(一.5):置回 ONLINE 并写物理位置 at_station=下一站;记生命周期 SEMI_OUT。
|
||||
func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, req OnlineReq, operator string) (string, error) {
|
||||
func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, req OnlineReq, operator string) (string, *PlcMesMismatch, error) {
|
||||
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, s.workpieceWorkOrder(ctx, wp))
|
||||
if len(items) == 0 {
|
||||
return "", errors.New("工单未配置工艺组合")
|
||||
return "", nil, errors.New("工单未配置工艺组合")
|
||||
}
|
||||
next, nextFlow := 0, 0
|
||||
for _, it := range items {
|
||||
@@ -234,19 +248,19 @@ func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, req
|
||||
}
|
||||
}
|
||||
if next == 0 {
|
||||
return "", fmt.Errorf("工件已报工至工位%d,工艺组合中无下一站", wp.CurrentStationNo)
|
||||
return "", nil, fmt.Errorf("工件已报工至工位%d,工艺组合中无下一站", wp.CurrentStationNo)
|
||||
}
|
||||
// 二.4 资格校验:半成品(SEMI_STORED)重上线落点须为后续工序工位(=下一站);未传落点时按下一站兜底
|
||||
if req.StationNo > 0 && req.StationNo != next {
|
||||
return "", fmt.Errorf("半成品重上线须落在后续工序工位(工位%d),当前落点工位%d不符合资格", next, req.StationNo)
|
||||
return "", nil, fmt.Errorf("半成品重上线须落在后续工序工位(工位%d),当前落点工位%d不符合资格", next, req.StationNo)
|
||||
}
|
||||
if err := s.SemiFlow(ctx, SemiReq{Sn: wp.Sn, OrderNo: wp.OrderNo, Action: "OUTBOUND"}, operator); err != nil {
|
||||
return "", fmt.Errorf("重上线失败:%s", err.Error())
|
||||
return "", nil, fmt.Errorf("重上线失败:%s", err.Error())
|
||||
}
|
||||
// 出库一次解锁一次(问题.md 一.5):置回 ONLINE,写物理位置=下一站,等待在所选工位重新上线建档。
|
||||
if _, e := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
|
||||
SetStatus("ONLINE").SetAtStationNo(next).Save(ctx); e != nil {
|
||||
return "", fmt.Errorf("重上线解锁状态失败:%s", e.Error())
|
||||
return "", nil, fmt.Errorf("重上线解锁状态失败:%s", e.Error())
|
||||
}
|
||||
// 二.2 物理位置 + 三.2 状态流水 SEMI_STORED→ONLINE(出库 SEMI_OUT)
|
||||
s.writeLifecycle(ctx, wp.Sn, wp.OrderNo, "SEMI_STORED", "ONLINE", next, 0, operator, "workstation", "半成品出库重上线")
|
||||
@@ -257,7 +271,9 @@ func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, req
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.reonline", wp.OrderNo, operator, "workpiece", wp.Sn,
|
||||
"工件重上线(暂存退库后出库继续)", map[string]any{"nextStationNo": next, "nextFlowId": nextFlow})
|
||||
s.notifyDashboard()
|
||||
return msg, nil
|
||||
// PLC↔MES 对账(#8 T5 建档提交):提示不拦,仅透出 mismatch 供终端弹窗
|
||||
mm, _ := s.detectAndRecord(ctx, next, "terminal", wp.OrderNo, operator)
|
||||
return msg, mm, nil
|
||||
}
|
||||
|
||||
// writeLifecycle 写工件生命周期状态流水(三.2 / P2 定稿:所有 status 变更点同事务写)。
|
||||
@@ -348,6 +364,11 @@ func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, opera
|
||||
if req.Sn == "" || req.StationNo <= 0 {
|
||||
return errors.New("sn 与 stationNo 必填")
|
||||
}
|
||||
// PLC↔MES 对账(#8 T4 报工提交):命中 mismatch 则阻断报工,提示先处理该工位托盘位异常
|
||||
// (新件建档 / 绑定已知件 / 报异常 / 忽略),避免在无 MES 在制工件状态下误报工。
|
||||
if m, _ := s.detectAndRecord(ctx, req.StationNo, "report", "", operator); m != nil {
|
||||
return fmt.Errorf("PLC与MES对账不一致:%s。请先处理该工位托盘位异常(新件建档 / 绑定已知件 / 报异常 / 忽略)后再报工", m.Detail)
|
||||
}
|
||||
wp, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
|
||||
if err != nil {
|
||||
return errors.New("工件未进线登记")
|
||||
|
||||
@@ -13,10 +13,13 @@ type mockClient struct {
|
||||
mu sync.RWMutex
|
||||
// dockPallet 模拟接驳台是否有托盘:默认全部无托盘;联调时可用 SetDockPallet 置位
|
||||
dockPallet map[string]bool
|
||||
// stationPallet 模拟工位托盘位是否有件(PLC↔MES 对账用):key=工位号,默认全部无件;
|
||||
// 联调/自测时可用 SetStationPallet 置位,模拟“PLC 说有位、MES 说无工件”的 mismatch 场景。
|
||||
stationPallet map[int]bool
|
||||
}
|
||||
|
||||
func newMockClient() *mockClient {
|
||||
return &mockClient{dockPallet: map[string]bool{}}
|
||||
return &mockClient{dockPallet: map[string]bool{}, stationPallet: map[int]bool{}}
|
||||
}
|
||||
|
||||
// SetDockPallet 手动设置某接驳台是否有托盘(供联调/测试用)
|
||||
@@ -26,6 +29,13 @@ func (m *mockClient) SetDockPallet(dockCode string, has bool) {
|
||||
m.dockPallet[dockCode] = has
|
||||
}
|
||||
|
||||
// SetStationPallet 手动设置某工位托盘位是否有件(PLC↔MES 对账联调/自测用)
|
||||
func (m *mockClient) SetStationPallet(stationNo int, has bool) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.stationPallet[stationNo] = has
|
||||
}
|
||||
|
||||
func (m *mockClient) SendProcessCode(_ context.Context, combination string) ([]int, error) {
|
||||
codes := parseCombination(combination)
|
||||
m.sent = combination
|
||||
@@ -49,4 +59,11 @@ func (m *mockClient) QueryDockPallet(_ context.Context, dockCode string) (bool,
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return m.dockPallet[dockCode], nil
|
||||
}
|
||||
|
||||
// QueryStationPallet 读工位托盘位(mock):返回置位值,默认无件。
|
||||
func (m *mockClient) QueryStationPallet(_ context.Context, stationNo int) (bool, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return m.stationPallet[stationNo], nil
|
||||
}
|
||||
@@ -22,6 +22,11 @@ type Client interface {
|
||||
// QueryDockPallet 查询某接驳台(DOCK01..21)当前是否有托盘(读 PLC 托盘传感器信号)。
|
||||
// 用于 AGV 到达目的地前确认接驳台空闲。暂未接真实 PLC 时 mock 返回 false(默认无托盘)。
|
||||
QueryDockPallet(ctx context.Context, dockCode string) (bool, error)
|
||||
// QueryStationPallet 查询某工位(stationNo)托盘位是否有件(读 PLC 工位托盘传感器信号)。
|
||||
// 用于 PLC↔MES 对账(#8):PLC 说有位、MES 说无工件 ⇒ mismatch。
|
||||
// 暂未接真实 PLC 时 mock 按置位返回(默认无件);真实 S7 对接待 PLC 点位表
|
||||
// (G1~G10→M895.0~M896.1、C15→M898.0)后由 tcp 实现补读寄存器逻辑。
|
||||
QueryStationPallet(ctx context.Context, stationNo int) (bool, error)
|
||||
}
|
||||
|
||||
// Manager 管理 PLC 客户端实例
|
||||
@@ -54,6 +59,16 @@ func (m *Manager) SetMockDockPallet(dockCode string, has bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// SetMockStationPallet 仅在 mock 模式下生效:手动置位某工位托盘位是否有件(PLC↔MES 对账联调用)。
|
||||
func (m *Manager) SetMockStationPallet(stationNo int, has bool) {
|
||||
if !m.mock {
|
||||
return
|
||||
}
|
||||
if mc, ok := m.client.(*mockClient); ok {
|
||||
mc.SetStationPallet(stationNo, has)
|
||||
}
|
||||
}
|
||||
|
||||
// parseCombination 解析工位组合 "1,3,5" / "10,11,12" -> [1,3,5] / [10,11,12]
|
||||
func parseCombination(s string) []int {
|
||||
var out []int
|
||||
|
||||
@@ -32,4 +32,11 @@ func (t *tcpClient) AckDone(_ context.Context, ok bool) {
|
||||
// 信号定义未确认前返回 false(默认无托盘),待现场 PLC 点位表给到后在此处补读寄存器逻辑。
|
||||
func (t *tcpClient) QueryDockPallet(_ context.Context, _ string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// QueryStationPallet 读工位托盘位(PLC↔MES 对账)。真实对接:按工位号映射 PLC 寄存器
|
||||
// (G1~G10→M895.0~M896.1、C15→M898.0)。信号定义未确认前返回 false(默认无件),
|
||||
// 待现场 PLC 点位表给到后在此处补读寄存器逻辑(与 QueryDockPallet 同为占位实现)。
|
||||
func (t *tcpClient) QueryStationPallet(_ context.Context, _ int) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user