2026-08-28 15:06:01 +08:00
|
|
|
|
package mes
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
2026-10-06 19:36:22 +08:00
|
|
|
|
"errors"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/url"
|
|
|
|
|
|
"strings"
|
2026-10-06 19:36:22 +08:00
|
|
|
|
"sync"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"time"
|
2026-11-09 18:58:47 +08:00
|
|
|
|
|
2026-11-16 16:42:44 +08:00
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
|
2026-11-16 19:42:22 +08:00
|
|
|
|
"bj_power_workstation/internal/netutil"
|
2026-11-09 18:58:47 +08:00
|
|
|
|
"bj_power_workstation/internal/tightening"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-11-16 19:42:22 +08:00
|
|
|
|
// commTag 本机 IP 前缀(进程启动解析一次),断网排查时可一眼看出日志来自哪台终端机。
|
|
|
|
|
|
var commTag = netutil.Tag()
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// ErrAuth 认证失效(401 / 登录态过期):可恢复错误。
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 调用方据此提示重新登录(2026-11-05 拍板:断网/认证失败直接报错,无离线重试)。
|
2026-10-06 19:36:22 +08:00
|
|
|
|
var ErrAuth = errors.New("station auth expired or invalid")
|
|
|
|
|
|
|
2026-10-09 13:46:33 +08:00
|
|
|
|
// BizError MES 业务拒绝(HTTP 2xx + code≠0):请求已送达且被明确拒绝,数据本身不合法,
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 调用方应把拒绝原因直接回前端。
|
2026-10-09 13:46:33 +08:00
|
|
|
|
type BizError struct {
|
|
|
|
|
|
Body []byte // MES 原始响应体
|
|
|
|
|
|
Code int // 业务码
|
|
|
|
|
|
Message string // 业务消息
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (e *BizError) Error() string {
|
|
|
|
|
|
return fmt.Sprintf("MES 业务拒绝(code=%d): %s", e.Code, truncate(e.Message, 200))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// Client MES 内部 API HTTP 客户端。
|
|
|
|
|
|
// 自动携带请求头:X-API-TOKEN(项目间 token)、Content-Type: application/json。
|
|
|
|
|
|
// 任何非 2xx 状态返回包含响应体的错误信息。
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// 多会话版(2026-11-11):Bearer 不再是单一全局登录态,Get/Post 支持可选 bearer 参数——
|
|
|
|
|
|
// 终端 handler 把「当前请求的 JWT」透传给 MES(token 即会话身份),多个会话互不串身份;
|
|
|
|
|
|
// 不传 bearer 的调用(登录/内部通知等)只带 X-API-TOKEN。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
type Client struct {
|
2026-10-06 19:36:22 +08:00
|
|
|
|
baseURL string
|
|
|
|
|
|
token string // X-API-TOKEN 共享内部 token(项目间)
|
|
|
|
|
|
httpClient *http.Client
|
2026-10-20 16:36:26 +08:00
|
|
|
|
streamClient *http.Client // SSE 长连接专用(无总超时)
|
2026-10-06 19:36:22 +08:00
|
|
|
|
|
|
|
|
|
|
mu sync.Mutex
|
2026-11-11 14:36:16 +08:00
|
|
|
|
Touch func(bearer string) // 每次以有效登录态(bearer 非空)发起的调用触发,刷新该会话空闲计时(核实4)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func New(baseURL, token string) *Client {
|
|
|
|
|
|
return &Client{
|
2026-10-20 16:36:26 +08:00
|
|
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
|
|
|
|
token: token,
|
|
|
|
|
|
httpClient: &http.Client{Timeout: 10 * time.Second},
|
|
|
|
|
|
streamClient: &http.Client{}, // SSE 长连接专用:无总超时,靠服务端心跳+断线重连兜底
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// Get 发送 GET 请求,query 为空则忽略;bearer 可选(当前请求的工位登录 JWT)。
|
|
|
|
|
|
func (c *Client) Get(path string, query url.Values, bearer ...string) ([]byte, http.Header, error) {
|
2026-08-28 15:06:01 +08:00
|
|
|
|
u := c.baseURL + path
|
|
|
|
|
|
if len(query) > 0 {
|
|
|
|
|
|
u += "?" + query.Encode()
|
|
|
|
|
|
}
|
2026-11-11 14:36:16 +08:00
|
|
|
|
return c.do(http.MethodGet, u, nil, firstBearer(bearer))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// Post 发送 POST 请求(实时路径),body 序列化为 JSON;bearer 可选(当前请求的工位登录 JWT)。
|
|
|
|
|
|
func (c *Client) Post(path string, body any, bearer ...string) ([]byte, http.Header, error) {
|
2026-08-28 15:06:01 +08:00
|
|
|
|
var reader io.Reader
|
|
|
|
|
|
if body != nil {
|
|
|
|
|
|
b, err := json.Marshal(body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, fmt.Errorf("序列化请求体失败: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
reader = bytes.NewReader(b)
|
|
|
|
|
|
}
|
2026-11-11 14:36:16 +08:00
|
|
|
|
return c.do(http.MethodPost, c.baseURL+path, reader, firstBearer(bearer))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func firstBearer(v []string) string {
|
|
|
|
|
|
for _, s := range v {
|
|
|
|
|
|
if s != "" {
|
|
|
|
|
|
return s
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return ""
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-09 18:58:47 +08:00
|
|
|
|
// ReportTighteningConfig 向 MES 上报本终端 yaml 的拧紧枪配置快照,仅供「拧紧枪配置」页展示。
|
|
|
|
|
|
// 2026-11-09 拍板:三把枪通道号仅展示,不参与任何拧紧数据区分 / 校验 / 拦截;不下发控制指令、
|
|
|
|
|
|
// 不改变本终端控制器连接与采集链路。启动与登录各调用一次(best-effort),MES 侧该工位已有
|
|
|
|
|
|
// 展示记录时自动跳过,绝不覆盖页面人工编辑。
|
|
|
|
|
|
func (c *Client) ReportTighteningConfig(cfg tightening.TighteningConfig, stationNo int) error {
|
|
|
|
|
|
if stationNo <= 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
payload := map[string]any{
|
|
|
|
|
|
"stationNo": stationNo,
|
|
|
|
|
|
"enable": cfg.Enable,
|
|
|
|
|
|
"controllerAddr": cfg.ControllerAddr,
|
|
|
|
|
|
"cellId": cfg.CellID,
|
|
|
|
|
|
"channelId": cfg.ChannelID,
|
|
|
|
|
|
"stationId": cfg.StationID,
|
|
|
|
|
|
"operatorId": cfg.OperatorID,
|
|
|
|
|
|
"heartbeatSec": cfg.HeartbeatSec,
|
|
|
|
|
|
}
|
|
|
|
|
|
_, _, err := c.Post("/api/internal/torque/config/report", payload)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// GetStream 发起 SSE 长连接(工位报警事件流,2026-10-20):仅 X-API-TOKEN 鉴权,
|
|
|
|
|
|
// 不携带/不刷新工位登录态(长连接期间登录态可能过期,SSE 鉴权只认项目间 token)。
|
|
|
|
|
|
// 使用独立的无超时 http.Client(默认 httpClient 的 10s 总超时会掐断长连接);
|
|
|
|
|
|
// 调用方负责关闭返回的 ReadCloser,断线重连由调用方(alertstream.Manager)负责。
|
|
|
|
|
|
func (c *Client) GetStream(path string, query url.Values) (io.ReadCloser, error) {
|
|
|
|
|
|
u := c.baseURL + path
|
|
|
|
|
|
if len(query) > 0 {
|
|
|
|
|
|
u += "?" + query.Encode()
|
|
|
|
|
|
}
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u, nil)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
req.Header.Set("X-API-TOKEN", c.token)
|
|
|
|
|
|
req.Header.Set("Accept", "text/event-stream")
|
|
|
|
|
|
resp, err := c.streamClient.Do(req)
|
|
|
|
|
|
if err != nil {
|
2026-11-16 19:42:22 +08:00
|
|
|
|
logx.Errorf("[终端→MES]"+commTag+" <-- SSE connect error %s err=%v", u, err)
|
2026-10-20 16:36:26 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
resp.Body.Close()
|
2026-11-16 19:42:22 +08:00
|
|
|
|
logx.Errorf("[终端→MES]"+commTag+" <-- SSE HTTP %d %s body=%s", resp.StatusCode, u, truncate(string(body), 200))
|
2026-10-20 16:36:26 +08:00
|
|
|
|
return nil, fmt.Errorf("MES %s 返回 %d:%s", path, resp.StatusCode, truncate(string(body), 200))
|
|
|
|
|
|
}
|
2026-11-16 19:42:22 +08:00
|
|
|
|
logx.Infof("[终端→MES]"+commTag+" <-- SSE connected %s", u)
|
2026-10-20 16:36:26 +08:00
|
|
|
|
return resp.Body, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// do 执行请求:Bearer 使用调用方传入的请求级 JWT(多会话透传),空则不携带。
|
|
|
|
|
|
// 仅在携带有效登录态时刷新对应会话的空闲计时(核实4)。
|
|
|
|
|
|
func (c *Client) do(method, u string, body io.Reader, bearer string) ([]byte, http.Header, error) {
|
2026-08-28 15:06:01 +08:00
|
|
|
|
req, err := http.NewRequest(method, u, body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
req.Header.Set("X-API-TOKEN", c.token)
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json;charset=utf-8")
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// 方案 B(多会话):透传当前请求的工位登录态 JWT,使 MES 能从 claims 取出可信操作人/工位号
|
2026-10-06 19:36:22 +08:00
|
|
|
|
if bearer != "" {
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+bearer)
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// 核实4:携带当前登录态的调用视为一次人工活动,刷新该会话的空闲计时
|
2026-10-06 19:36:22 +08:00
|
|
|
|
if c.Touch != nil {
|
2026-11-11 14:36:16 +08:00
|
|
|
|
c.Touch(bearer)
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-16 16:42:44 +08:00
|
|
|
|
// 2026-11-16 联调:请求行日志(body 截断 1000;method/u 齐全可定位每一次跨系统调用)
|
|
|
|
|
|
var reqBodyForLog []byte
|
|
|
|
|
|
if body != nil && method == http.MethodPost {
|
|
|
|
|
|
reqBodyForLog, _ = io.ReadAll(body)
|
|
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(reqBodyForLog))
|
|
|
|
|
|
}
|
2026-11-16 19:42:22 +08:00
|
|
|
|
logx.Infof("[终端→MES]"+commTag+" --> %s %s body=%s", method, u, truncate(string(reqBodyForLog), 1000))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
|
|
if err != nil {
|
2026-11-16 16:42:44 +08:00
|
|
|
|
// 2026-11-16 联调:终端→MES 全量交互落日志(传输错误单独记录,grep "[终端→MES]")
|
2026-11-16 19:42:22 +08:00
|
|
|
|
logx.WithContext(req.Context()).Errorf("[终端→MES]"+commTag+" <-- transport error %s %s err=%v", method, u, err)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, resp.Header, err
|
|
|
|
|
|
}
|
2026-11-16 16:42:44 +08:00
|
|
|
|
// 请求/响应全量日志(请求体在下面 marshal 后记录;这里记录响应)
|
2026-11-16 19:42:22 +08:00
|
|
|
|
logx.WithContext(req.Context()).Infof("[终端→MES]"+commTag+" <-- HTTP %d %s body=%s", resp.StatusCode, u, truncate(string(respBody), 1000))
|
2026-10-06 19:36:22 +08:00
|
|
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 401 = 工位登录态过期/无效:调用方据此提示重新登录。
|
2026-10-06 19:36:22 +08:00
|
|
|
|
return respBody, resp.Header, fmt.Errorf("MES %s %s: %w", method, u, ErrAuth)
|
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
|
|
|
|
return respBody, resp.Header, fmt.Errorf("MES %s %s 返回 %d:%s",
|
|
|
|
|
|
method, u, resp.StatusCode, truncate(string(respBody), 200))
|
|
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// W2(安全审计):MES 内部接口常以 HTTP 200 + {code≠0} 返回业务失败(如数据校验不通过、
|
|
|
|
|
|
// 重复提交被拒)。仅看 HTTP 状态会误判成功,导致离线队列数据被标记 synced=1 永久丢失。
|
2026-10-09 13:46:33 +08:00
|
|
|
|
// 此处解析业务码,code≠0 一律视为失败;但与网络失败不同——业务拒绝返回 BizError
|
|
|
|
|
|
//(调用方直接回前端/死信,不进离线队列无限重传),网络失败返回普通 error(可重试,入离线队列)。
|
2026-10-06 19:36:22 +08:00
|
|
|
|
var br struct {
|
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := json.Unmarshal(respBody, &br); err == nil && br.Code != 0 {
|
2026-10-09 13:46:33 +08:00
|
|
|
|
return respBody, resp.Header, &BizError{Body: respBody, Code: br.Code, Message: br.Message}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
return respBody, resp.Header, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func truncate(s string, n int) string {
|
|
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
if len(s) > n {
|
|
|
|
|
|
return s[:n] + "..."
|
|
|
|
|
|
}
|
|
|
|
|
|
return s
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|