feat: 重构 AGV 对接与在制品查询,新增 PLC 手动监控功能
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// Package mesclient MES 内部接口客户端(备料单拉取/状态回写,AGV 配送用)。
|
||||
// Package mesclient MES 内部接口客户端(备料单拉取 / 状态回写 / 接驳台与工单主数据只读)。
|
||||
// AGV 配送真源已在 MES,2026-10-17 移除 WMS 侧 AGV 代理方法与 /api/agv/* 路由。
|
||||
package mesclient
|
||||
|
||||
import (
|
||||
@@ -326,145 +327,3 @@ func (e *RespError) Error() string {
|
||||
return "mes call failed with status " + fmt.Sprintf("%d", e.Status)
|
||||
}
|
||||
|
||||
// ============ AGV 调度代理(2026-10-14 AGV 唯一真源迁 MES) ============
|
||||
|
||||
// AgvSubmitRow MES 下发 AGV 行(字段与 MES AgvSubmitRequest 对齐)
|
||||
type AgvSubmitRow struct {
|
||||
RequestNo string `json:"requestNo"`
|
||||
MaterialCode string `json:"materialCode"`
|
||||
MaterialName string `json:"materialName"`
|
||||
Unit string `json:"unit"`
|
||||
Qty float64 `json:"qty"`
|
||||
TargetDock string `json:"targetDock"`
|
||||
}
|
||||
|
||||
// postInternalJSON 通用内部 POST:带 X-API-TOKEN;解析 MES 业务错误(HTTP 200 + body{code≠0} 必须判读)。
|
||||
// 与 postInternal 区别:正确读取 message 字段(MES 统一返回 {code,message,data})。
|
||||
func (c *Client) postInternalJSON(ctx context.Context, path string, body []byte, out any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-API-TOKEN", c.token)
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 300 {
|
||||
return &RespError{Status: resp.StatusCode}
|
||||
}
|
||||
var env struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||||
return err
|
||||
}
|
||||
if env.Code != 0 {
|
||||
msg := env.Message
|
||||
if msg == "" {
|
||||
msg = "MES 返回失败"
|
||||
}
|
||||
return fmt.Errorf("%s", msg)
|
||||
}
|
||||
if out != nil && len(env.Data) > 0 {
|
||||
return json.Unmarshal(env.Data, out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getInternalJSON 通用内部 GET:带 X-API-TOKEN;解析 MES 业务错误。
|
||||
func (c *Client) getInternalJSON(ctx context.Context, path string, out any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("X-API-TOKEN", c.token)
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 300 {
|
||||
return &RespError{Status: resp.StatusCode}
|
||||
}
|
||||
var env struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||||
return err
|
||||
}
|
||||
if env.Code != 0 {
|
||||
msg := env.Message
|
||||
if msg == "" {
|
||||
msg = "MES 返回失败"
|
||||
}
|
||||
return fmt.Errorf("%s", msg)
|
||||
}
|
||||
if out != nil && len(env.Data) > 0 {
|
||||
return json.Unmarshal(env.Data, out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SubmitAgv 仓管下发 AGV:代理 MES /api/internal/agv/submit(MES 为 AGV 唯一真源,调度海康 RCS)
|
||||
func (c *Client) SubmitAgv(ctx context.Context, rows []AgvSubmitRow, sourceDock, operator string) (int, []string, error) {
|
||||
b, _ := json.Marshal(map[string]any{"rows": rows, "sourceDock": sourceDock, "operator": operator})
|
||||
var data struct {
|
||||
Created int `json:"created"`
|
||||
Failed []string `json:"failed"`
|
||||
}
|
||||
if err := c.postInternalJSON(ctx, "/api/internal/agv/submit", b, &data); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
return data.Created, data.Failed, nil
|
||||
}
|
||||
|
||||
// ListAgvTasks 查询 AGV 任务(代理 MES,分页契约透传 {list,total,page,pageSize})
|
||||
func (c *Client) ListAgvTasks(ctx context.Context, status, targetDock string, page, size int) (map[string]any, error) {
|
||||
q := url.Values{}
|
||||
if status != "" {
|
||||
q.Set("status", status)
|
||||
}
|
||||
if targetDock != "" {
|
||||
q.Set("targetDock", targetDock)
|
||||
}
|
||||
q.Set("page", strconv.Itoa(page))
|
||||
q.Set("pageSize", strconv.Itoa(size))
|
||||
var data map[string]any
|
||||
if err := c.getInternalJSON(ctx, "/api/internal/agv/tasks?"+q.Encode(), &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// RefreshAgvTasks 轮询海康 RCS 刷新未终态任务(代理 MES)
|
||||
func (c *Client) RefreshAgvTasks(ctx context.Context) (int, error) {
|
||||
var data struct {
|
||||
Changed int `json:"changed"`
|
||||
}
|
||||
if err := c.postInternalJSON(ctx, "/api/internal/agv/refresh", []byte("{}"), &data); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return data.Changed, nil
|
||||
}
|
||||
|
||||
// CancelAgvTask 取消 AGV 任务(代理 MES)
|
||||
func (c *Client) CancelAgvTask(ctx context.Context, taskNo, reason, operator string) error {
|
||||
b, _ := json.Marshal(map[string]any{"taskNo": taskNo, "reason": reason, "operator": operator})
|
||||
return c.postInternalJSON(ctx, "/api/internal/agv/cancel", b, nil)
|
||||
}
|
||||
|
||||
// AgvMode 返回 AGV 对接模式(代理 MES)
|
||||
func (c *Client) AgvMode(ctx context.Context) (map[string]any, error) {
|
||||
var data map[string]any
|
||||
if err := c.getInternalJSON(ctx, "/api/internal/agv/mode", &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user