AGV 调度: - 任务重构为 GO_LOAD/TRANSPORT/EMPTY_RETURN/FREE 三段链,到位即停(ARRIVED)需人工确认装卸 - 自由调度单点步进,每段强制弹"请确保路径畅通无杂物";托盘回收独立工作台 - 接驳台 PLC 传感器检测到托盘即自动卸货放行,传感器故障由人工按钮兜底(无轮询) - 车辆状态只读展示(海康3.1.7 queryAgvStatus),调度仍由 RCS 负责 物料闭环: - 补料管理(SHORTAGE):工位申请 + 主管审批调增叫料池额度,与借料/差异补发三轨并行 - 损耗台账(loss_ledger):退料实际 < 理论必填原因、后审核,工单不记损耗 - 退料强制指定左/右接驳台,WMS 库管可代工位退料 下班闭环: - 工位终端自检接驳台剩余物料后下班;库管收货 + 托盘拉走后事件驱动自动结班 - 退出登录未下班仅作安全提示(不强制拦截、不新增责任审批) 接驳台左右按操作员朝向建模(dock.side 字段),废除"奇左偶右"隐式约定; 看板标签 / seed / 迁移补丁 / 布局文档统一为中文左/右。 产线布局说明:C1 前线头 AGV 点、C15 后线尾 AGV 点、外部 O1/O2/O3 散点, 清理修订痕迹为最终态描述。
316 lines
12 KiB
Go
316 lines
12 KiB
Go
package handler
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"net/http"
|
||
"net/url"
|
||
|
||
"bj_power_workstation/internal/mes"
|
||
"bj_power_workstation/internal/svc"
|
||
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
// mesRespCode 解析 MES 统一响应 {code,message}。
|
||
// MES 业务失败约定为 HTTP 200 + code!=0(httpx.Fail),HTTP 层无法区分,必须显式解析业务码。
|
||
// code==0 返回空串;code!=0 返回 message(供调用方直接透传给前端)。
|
||
func mesRespCode(respBody []byte) string {
|
||
if len(respBody) == 0 {
|
||
return ""
|
||
}
|
||
var mr struct {
|
||
Code int `json:"code"`
|
||
Message string `json:"message"`
|
||
}
|
||
if err := json.Unmarshal(respBody, &mr); err != nil {
|
||
return ""
|
||
}
|
||
if mr.Code == 0 {
|
||
return ""
|
||
}
|
||
if mr.Message != "" {
|
||
return mr.Message
|
||
}
|
||
return fmt.Sprintf("MES 拒绝本次操作(业务码 %d)", mr.Code)
|
||
}
|
||
|
||
// processDoneHandler POST /api/report/process-done
|
||
// {sn, orderNo, flowId, stationNo, steps:[{stepId,name,value,text}], signs:[{stepId,signRole,signer,remark}], binds:[{materialCode,bindValue}], operator}
|
||
// 流程:先冲刷一轮积压 → 实时报 MES /api/internal/station/report;
|
||
// MES 不可达则写入 report_queue(kind=process_done) 并返回 data.queued=true(离线缓存稍后自动重传);
|
||
// MES 业务拒绝(HTTP200+code!=0,如装配物料未绑齐/电气件SN重复)直接失败回前端,绝不入离线队。
|
||
// binds 为本次工艺的装机绑定(结构件批次/电气件SN),MES 端落库 + 齐套强校验;离线重传时原样携带。
|
||
// signs 为 needOperatorSign 步骤的操作工签字(需求B 双签),原样转发 MES 强校验落库;离线重传时随 payload 保留。
|
||
func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
var req struct {
|
||
Sn string `json:"sn"`
|
||
OrderNo string `json:"orderNo"`
|
||
FlowId int `json:"flowId"`
|
||
StationNo int `json:"stationNo"`
|
||
Steps []struct {
|
||
StepId int `json:"stepId"`
|
||
Name string `json:"name"`
|
||
Value float64 `json:"value"`
|
||
Text string `json:"text"`
|
||
// Checked:needCheck 步骤的「检测确认」结果,原样转发给 MES 做强校验(勿删,否则服务端拿不到确认)
|
||
Checked bool `json:"checked"`
|
||
} `json:"steps"`
|
||
Binds []struct {
|
||
MaterialCode string `json:"materialCode"`
|
||
BindValue string `json:"bindValue"`
|
||
} `json:"binds"`
|
||
// 签字确认(需求B 双签):needOperatorSign 步骤的操作工签字,原样转发 MES 强校验落库
|
||
Signs []struct {
|
||
StepId int `json:"stepId"`
|
||
SignRole string `json:"signRole"`
|
||
Signer string `json:"signer"`
|
||
Remark string `json:"remark"`
|
||
} `json:"signs"`
|
||
Operator string `json:"operator"`
|
||
// 作业时长采集(P0-3):上料时刻/报工时刻(ISO8601),原样转发 MES 算 durationSec;离线重传时同 payload 保留
|
||
StartedAt string `json:"startedAt"`
|
||
EndedAt string `json:"endedAt"`
|
||
}
|
||
if err := parseJSON(r, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "参数错误")
|
||
return
|
||
}
|
||
if req.Sn == "" || req.FlowId <= 0 || req.StationNo <= 0 {
|
||
fail(w, http.StatusBadRequest, "sn/flowId/stationNo 必填")
|
||
return
|
||
}
|
||
|
||
// 方案 B:操作人取自工位登录态可信身份,忽略浏览器请求体里的 operator(防冒名);
|
||
// operator_id 用于离线队列按操作人隔离发送(仅当前登录人自己的数据被发送)
|
||
op := req.Operator
|
||
opID := int64(0)
|
||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||
if ss.Name != "" {
|
||
op = ss.Name
|
||
}
|
||
opID = ss.UserID
|
||
}
|
||
|
||
// 先尽力冲刷历史积压,失败不影响本次上报
|
||
ctx.Syncer.RunOnce(r.Context())
|
||
|
||
body := map[string]any{
|
||
"sn": req.Sn,
|
||
"orderNo": req.OrderNo,
|
||
"flowId": req.FlowId,
|
||
"stationNo": req.StationNo,
|
||
"operator": op,
|
||
}
|
||
if len(req.Binds) > 0 {
|
||
body["binds"] = req.Binds
|
||
}
|
||
if len(req.Signs) > 0 {
|
||
body["signs"] = req.Signs
|
||
}
|
||
if req.StartedAt != "" {
|
||
body["startedAt"] = req.StartedAt
|
||
}
|
||
if req.EndedAt != "" {
|
||
body["endedAt"] = req.EndedAt
|
||
}
|
||
payloadBytes, _ := json.Marshal(body)
|
||
|
||
respBody, _, err := ctx.Mes.Post("/api/internal/station/report", json.RawMessage(payloadBytes))
|
||
if err != nil {
|
||
// MES 业务拒绝(HTTP200+code≠0,如装配物料未绑齐/需签字确认/不可倒序报工):
|
||
// 数据本身不合法,原样重传不可能成功——直接失败回前端,绝不入离线队。
|
||
var bizErr *mes.BizError
|
||
if errors.As(err, &bizErr) {
|
||
msg := mesRespCode(bizErr.Body)
|
||
if msg == "" {
|
||
msg = bizErr.Error()
|
||
}
|
||
// 拒绝痕:记「被拒」而非「完成上报」,避免事件日志误导(成功才记完成痕)
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "process_done_rejected",
|
||
"报工被MES拒绝 sn="+req.Sn+" stationNo="+itoa(req.StationNo)+":"+msg)
|
||
logx.Errorf("工序完成上报被 MES 拒绝(不入队): %s", msg)
|
||
fail(w, http.StatusConflict, msg)
|
||
return
|
||
}
|
||
if qerr := ctx.Store.InsertQueueItem("process_done", string(payloadBytes), opID, op); qerr != nil {
|
||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||
return
|
||
}
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "process_done_queued",
|
||
"报工离线排队 sn="+req.Sn+" stationNo="+itoa(req.StationNo))
|
||
logx.Errorf("工序完成上报失败已离线排队: %v", err)
|
||
ok(w, map[string]any{"queued": true})
|
||
return
|
||
}
|
||
// MES 业务拒绝兜底(理论上已由 BizError 分支处理):缺料/SN冲突等,直接失败,不入队
|
||
if msg := mesRespCode(respBody); msg != "" {
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "process_done_rejected",
|
||
"报工被MES拒绝 sn="+req.Sn+" stationNo="+itoa(req.StationNo)+":"+msg)
|
||
logx.Errorf("工序完成上报被 MES 拒绝: %s", msg)
|
||
fail(w, http.StatusConflict, msg)
|
||
return
|
||
}
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "process_done",
|
||
"工艺完成上报 sn="+req.Sn+" stationNo="+itoa(req.StationNo)+" flowId="+itoa(req.FlowId)+" binds="+itoa(len(req.Binds)))
|
||
|
||
ok(w, map[string]any{"queued": false})
|
||
}
|
||
}
|
||
|
||
// tempStoreHandler POST /api/report/temp-store
|
||
// {sn, orderNo, stationNo, flowId, operator} → 调 MES /api/internal/station/checkin(半成品入库)。
|
||
// 不可达时以同样 payload 写入 report_queue(kind=temp_store)。
|
||
func tempStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
var req struct {
|
||
Sn string `json:"sn"`
|
||
OrderNo string `json:"orderNo"`
|
||
StationNo int `json:"stationNo"`
|
||
FlowId int `json:"flowId"`
|
||
Operator string `json:"operator"`
|
||
}
|
||
if err := parseJSON(r, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "参数错误")
|
||
return
|
||
}
|
||
if req.Sn == "" {
|
||
fail(w, http.StatusBadRequest, "缺少 sn")
|
||
return
|
||
}
|
||
|
||
// 方案 B:操作人取自工位登录态可信身份,忽略浏览器请求体里的 operator(防冒名);
|
||
// operator_id 用于离线队列按操作人隔离发送(仅当前登录人自己的数据被发送)
|
||
op := req.Operator
|
||
opID := int64(0)
|
||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||
if ss.Name != "" {
|
||
op = ss.Name
|
||
}
|
||
opID = ss.UserID
|
||
}
|
||
|
||
// 先尽力冲刷历史积压,失败不影响本次上报
|
||
ctx.Syncer.RunOnce(r.Context())
|
||
|
||
body := map[string]any{
|
||
"sn": req.Sn,
|
||
"orderNo": req.OrderNo,
|
||
"stationNo": req.StationNo,
|
||
"flowId": req.FlowId,
|
||
"operator": op,
|
||
}
|
||
payloadBytes, _ := json.Marshal(body)
|
||
|
||
respBody, _, err := ctx.Mes.Post("/api/internal/station/checkin", json.RawMessage(payloadBytes))
|
||
if err != nil {
|
||
// MES 业务拒绝(HTTP200+code≠0):数据不合法重传不可能成功,直接失败回前端不入队
|
||
var bizErr *mes.BizError
|
||
if errors.As(err, &bizErr) {
|
||
msg := mesRespCode(bizErr.Body)
|
||
if msg == "" {
|
||
msg = bizErr.Error()
|
||
}
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "temp_store_rejected",
|
||
"暂存退库被MES拒绝 sn="+req.Sn+":"+msg)
|
||
logx.Errorf("暂存退库被 MES 拒绝(不入队): %s", msg)
|
||
fail(w, http.StatusConflict, msg)
|
||
return
|
||
}
|
||
if qerr := ctx.Store.InsertQueueItem("temp_store", string(payloadBytes), opID, op); qerr != nil {
|
||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||
return
|
||
}
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "temp_store_queued",
|
||
"暂存退库离线排队 sn="+req.Sn+" stationNo="+itoa(req.StationNo))
|
||
logx.Errorf("暂存退库上报失败已离线排队: %v", err)
|
||
ok(w, map[string]any{"queued": true})
|
||
return
|
||
}
|
||
// 兜底:MES 以 HTTP200+code≠0 拒绝却未被 BizError 捕获(理论不可达),按拒绝处理
|
||
if msg := mesRespCode(respBody); msg != "" {
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "temp_store_rejected",
|
||
"暂存退库被MES拒绝 sn="+req.Sn+":"+msg)
|
||
logx.Errorf("暂存退库被 MES 拒绝: %s", msg)
|
||
fail(w, http.StatusConflict, msg)
|
||
return
|
||
}
|
||
ctx.Store.AddEventLog(req.OrderNo, op, "temp_store",
|
||
"暂存/退回库房 sn="+req.Sn+" stationNo="+itoa(req.StationNo))
|
||
|
||
ok(w, map[string]any{"queued": false})
|
||
}
|
||
}
|
||
|
||
// scrapHandler POST /api/report/scrap
|
||
// {sn, reason, operator} → 调 MES /api/internal/workpiece/scrap(工件报废/人工取走,2026-11-03 补全)。
|
||
// 与「暂存退库」语义互斥:报废=永久退出产线(SCRAPPED),退库=合法暂停(SEMI_STORED)可重上线。
|
||
// 故意不做离线排队:报废是一次性状态变更,MES 端按当前状态强校验(已报废再报会被拒),
|
||
// 离线重发必然失败徒增噪声——MES 不可达时直接提示现场重试。
|
||
func scrapHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
var req struct {
|
||
Sn string `json:"sn"`
|
||
Reason string `json:"reason"`
|
||
Operator string `json:"operator"`
|
||
}
|
||
if err := parseJSON(r, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "参数错误")
|
||
return
|
||
}
|
||
if req.Sn == "" {
|
||
fail(w, http.StatusBadRequest, "缺少 sn")
|
||
return
|
||
}
|
||
if req.Reason == "" {
|
||
fail(w, http.StatusBadRequest, "报废/取走原因必填")
|
||
return
|
||
}
|
||
// 操作人取工位登录态可信身份(防冒名),与暂存退库同口径
|
||
op := req.Operator
|
||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil && ss.Name != "" {
|
||
op = ss.Name
|
||
}
|
||
body := map[string]any{"sn": req.Sn, "reason": req.Reason}
|
||
payloadBytes, _ := json.Marshal(body)
|
||
respBody, _, err := ctx.Mes.Post("/api/internal/workpiece/scrap?operator="+url.QueryEscape(op), json.RawMessage(payloadBytes))
|
||
if err != nil {
|
||
var bizErr *mes.BizError
|
||
if errors.As(err, &bizErr) {
|
||
msg := mesRespCode(bizErr.Body)
|
||
if msg == "" {
|
||
msg = bizErr.Error()
|
||
}
|
||
fail(w, http.StatusConflict, msg)
|
||
return
|
||
}
|
||
ctx.Store.AddEventLog("", op, "scrap_failed",
|
||
"报废登记失败(MES不可达) sn="+req.Sn)
|
||
fail(w, http.StatusBadGateway, "MES 不可达,报废未登记,请稍后重试(不要重复提交)")
|
||
return
|
||
}
|
||
if msg := mesRespCode(respBody); msg != "" {
|
||
fail(w, http.StatusConflict, msg)
|
||
return
|
||
}
|
||
ctx.Store.AddEventLog("", op, "workpiece_scrap",
|
||
"工件报废/人工取走 sn="+req.Sn+" 原因="+req.Reason)
|
||
ok(w, map[string]any{"ok": true})
|
||
}
|
||
}
|
||
|
||
// mesConfigHandler GET /api/mes-config 代理 MES 公开配置快照(拧紧手填开关 / 缓存策略 / AGV 超时等),
|
||
// 供工位终端前端按配置显隐或约束交互(2026-11-03 拍板:拧紧禁止手填时输入框置灰,只能由枪自动采集)。
|
||
func mesConfigHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
b, _, err := ctx.Mes.Get("/api/internal/public-config", nil)
|
||
if err != nil {
|
||
fail(w, http.StatusBadGateway, "MES 不可达,配置获取失败")
|
||
return
|
||
}
|
||
ok(w, json.RawMessage(b))
|
||
}
|
||
}
|