feat: 重构 AGV 对接与在制品查询,新增 PLC 手动监控功能

This commit is contained in:
SunYF
2026-10-17 17:49:47 +08:00
parent f65a52d9e1
commit f743cf21fb
96 changed files with 6130 additions and 1781 deletions
+10 -2
View File
@@ -7,6 +7,8 @@ import (
"strings"
"time"
"github.com/zeromicro/go-zero/core/logx"
"bj_power_workstation/internal/auth"
"bj_power_workstation/internal/mes"
"bj_power_workstation/internal/svc"
@@ -57,8 +59,11 @@ func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
fail(w, http.StatusUnauthorized, bizErr.Message)
return
}
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
return
// MES 不可达(连接拒绝/超时/DNS 失败等):技术细节记日志,面向终端只给业务化提示,不外泄 dial tcp
logx.Errorf("登录代理 MES /api/internal/station/login 不可达:user=%s stationNo=%d err=%v",
strings.TrimSpace(req.Username), req.StationNo, err)
fail(w, http.StatusBadGateway, "MES 服务不可达,请检查网络或联系管理员")
return
}
// 方案 B:落盘工位登录态 JWT,作为后续报工/拧紧的可信身份锚点
var loginResp struct {
@@ -100,9 +105,11 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
_ = parseJSON(r, &req)
// 退出前先统计「当前登录人」尚未成功上报的本地数据条数,供前端提醒本人处理
pendingQueue, pendingTorque := int64(0), int64(0)
deadQueue := int64(0)
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
pendingQueue, _ = ctx.Store.PendingQueueCountForOperator(ss.UserID)
pendingTorque, _ = ctx.Store.PendingTorqueCountForOperator(ss.UserID)
deadQueue, _ = ctx.Store.DeadQueueCountForOperator(ss.UserID)
}
username := strings.TrimSpace(req.Username)
if username != "" {
@@ -121,6 +128,7 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
"pendingQueue": pendingQueue,
"pendingTorque": pendingTorque,
"pendingTotal": pendingQueue + pendingTorque,
"deadTotal": deadQueue,
})
}
}
@@ -19,9 +19,9 @@ import (
func taskCurrentHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stationNo := atoi(strings.TrimSpace(r.URL.Query().Get("stationNo")), 0)
// 0=上线位为合法虚拟工位,仅负数视为未配置
if stationNo < 0 {
fail(w, http.StatusBadRequest, "缺少 stationNo 参数")
// 工位号须为正整数(原 0/13 虚拟位已废弃,业务定稿·一.10)
if stationNo <= 0 {
fail(w, http.StatusBadRequest, "缺少有效的 stationNo 参数")
return
}
@@ -51,8 +51,9 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
{Method: http.MethodPost, Path: "/api/report/process-done", Handler: processDoneHandler(ctx)},
{Method: http.MethodPost, Path: "/api/report/temp-store", Handler: tempStoreHandler(ctx)},
{Method: http.MethodPost, Path: "/api/station/logout", Handler: logoutHandler(ctx)},
{Method: http.MethodPost, Path: "/api/sync/flush", Handler: syncFlushHandler(ctx)},
{Method: http.MethodGet, Path: "/api/sync/stats", Handler: syncStatsHandler(ctx)},
{Method: http.MethodPost, Path: "/api/sync/flush", Handler: syncFlushHandler(ctx)},
{Method: http.MethodGet, Path: "/api/sync/stats", Handler: syncStatsHandler(ctx)},
{Method: http.MethodPost, Path: "/api/sync/retry-queue", Handler: syncRetryQueueHandler(ctx)},
// 产线点位 / 叫料(代理 MES 内部 API)
{Method: http.MethodGet, Path: "/api/line/points", Handler: linePointsHandler(ctx)},
+74 -12
View File
@@ -1,6 +1,7 @@
package handler
import (
"encoding/json"
"net/http"
"bj_power_workstation/internal/svc"
@@ -19,21 +20,82 @@ func syncFlushHandler(ctx *svc.ServiceContext) http.HandlerFunc {
// syncStatsHandler GET /api/sync/stats 返回「当前登录人」尚未成功上报的本地数据(失败列表),
// 供前端在「同一人重新登录」时仅展示失败信息(不自动打开页面)。他人数据不被返回、不可见。
// queueDead=死信(MES 业务拒绝、待人工重传处理),与 queue(待自动重传)分开回报。
func syncStatsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if ss, err := ctx.Store.GetStationSession(); err != nil || ss == nil {
ok(w, map[string]any{"pendingTorque": 0, "pendingQueue": 0, "pendingTotal": 0,
"deadTotal": 0, "queue": []any{}, "queueDead": []any{}, "torque": []any{}})
return
} else {
queue, _ := ctx.Store.PendingQueueItemsForOperator(ss.UserID, 200)
queueDead, _ := ctx.Store.DeadQueueItemsForOperator(ss.UserID, 200)
torque, _ := ctx.Store.PendingTorqueResultsForOperator(ss.UserID, 200)
ok(w, map[string]any{
"pendingQueue": len(queue),
"pendingTorque": len(torque),
"pendingTotal": len(queue) + len(torque),
"queue": queue,
"queueDead": queueDead,
"deadTotal": len(queueDead),
"torque": torque,
})
}
}
}
// syncRetryQueueHandler POST /api/sync/retry-queue 人工重传处理:
// 操作工查看失败原因、重选空闲点位后再次提交。body {id, payload},payload 为修正后的完整 JSON body。
// 处理:校验归属当前登录人(数据按操作人隔离)→ 写入修正 payload 并重置为待重传 → 立即按当前登录人身份发一轮 →
// 回报本轮结果(synced=已发送 / rejected=再次被拒(含原因) / retrying=网络失败,下轮继续)。
func syncRetryQueueHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ss, err := ctx.Store.GetStationSession()
if err != nil || ss == nil {
ok(w, map[string]any{"pendingTorque": 0, "pendingQueue": 0, "pendingTotal": 0, "queue": []any{}, "torque": []any{}})
if err != nil || ss == nil || ss.Token == "" {
fail(w, http.StatusUnauthorized, "登录已失效,请重新登录后再重传")
return
}
queue, _ := ctx.Store.PendingQueueItemsForOperator(ss.UserID, 200)
torque, _ := ctx.Store.PendingTorqueResultsForOperator(ss.UserID, 200)
ok(w, map[string]any{
"pendingQueue": len(queue),
"pendingTorque": len(torque),
"pendingTotal": len(queue) + len(torque),
"queue": queue,
"torque": torque,
})
var req struct {
ID int64 `json:"id"`
Payload json.RawMessage `json:"payload"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.ID <= 0 {
fail(w, http.StatusBadRequest, "请求体解析失败")
return
}
if len(req.Payload) == 0 {
fail(w, http.StatusBadRequest, "缺少上报内容(payload)")
return
}
// 归属校验:只能重传自己的数据(他人数据不被代发、也不可见)
item, err := ctx.Store.QueueItemByID(req.ID)
if err != nil {
fail(w, http.StatusNotFound, "记录不存在或已处理")
return
}
if item.OperatorID != ss.UserID {
fail(w, http.StatusForbidden, "只能重传本人录入的数据")
return
}
// 写入修正 payload,重置为待重传(同步器按当前登录人身份发送)
if err := ctx.Store.ResetQueueForRetry(req.ID, string(req.Payload)); err != nil {
fail(w, http.StatusInternalServerError, "重置重传状态失败")
return
}
ctx.Syncer.RunOnce(r.Context())
// 回报本轮结果
after, err := ctx.Store.QueueItemByID(req.ID)
if err != nil {
ok(w, map[string]any{"status": "retrying", "message": "已提交重传,结果下轮同步后可见"})
return
}
switch {
case after.Synced == 1:
ok(w, map[string]any{"status": "synced", "message": "重传成功,已上报 MES"})
case after.Synced == 2:
ok(w, map[string]any{"status": "rejected", "message": after.LastError, "reason": after.LastError})
default:
ok(w, map[string]any{"status": "retrying", "message": "MES 暂不可达,已留在队列等待下轮自动重传"})
}
}
}
}
@@ -21,14 +21,18 @@ func passJSON(w http.ResponseWriter, body []byte) {
_, _ = w.Write(body)
}
// wipHandler GET /api/wip?stationNo= —— 代理 MES /api/internal/workpieces/in-process。
// 在制品默认展示(5.2):停在本工位/上线位(0)/下线位(13) 的未完工工件列表。
// wipHandler GET /api/wip?stationNo=(>0=停在该工位)/ ?scope=(pending=待流转 / end-pending=末端待取件)
// —— 代理 MES /api/internal/workpieces/in-process。
// 在制品默认展示(5.2):停在本工位的未完工工件列表;上下线面板按 scope 拉待流转/末端待取件。
func wipHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params := url.Values{}
if sn := strings.TrimSpace(r.URL.Query().Get("stationNo")); sn != "" {
params.Set("stationNo", sn)
}
if sc := strings.TrimSpace(r.URL.Query().Get("scope")); sc != "" {
params.Set("scope", sc)
}
body, _, err := ctx.Mes.Get("/api/internal/workpieces/in-process", params)
if err != nil {
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))