feat: AGV状态实时推送与受控异常处置 + rcs-dps状态接口独立配置与日志落盘修复
This commit is contained in:
@@ -14,9 +14,13 @@ import (
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
"bj_power_workstation/internal/netutil"
|
||||
"bj_power_workstation/internal/tightening"
|
||||
)
|
||||
|
||||
// commTag 本机 IP 前缀(进程启动解析一次),断网排查时可一眼看出日志来自哪台终端机。
|
||||
var commTag = netutil.Tag()
|
||||
|
||||
// ErrAuth 认证失效(401 / 登录态过期):可恢复错误。
|
||||
// 调用方据此提示重新登录(2026-11-05 拍板:断网/认证失败直接报错,无离线重试)。
|
||||
var ErrAuth = errors.New("station auth expired or invalid")
|
||||
@@ -128,16 +132,16 @@ func (c *Client) GetStream(path string, query url.Values) (io.ReadCloser, error)
|
||||
req.Header.Set("Accept", "text/event-stream")
|
||||
resp, err := c.streamClient.Do(req)
|
||||
if err != nil {
|
||||
logx.Errorf("[终端→MES] <-- SSE connect error %s err=%v", u, err)
|
||||
logx.Errorf("[终端→MES]"+commTag+" <-- SSE connect error %s err=%v", u, err)
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
logx.Errorf("[终端→MES] <-- SSE HTTP %d %s body=%s", resp.StatusCode, u, truncate(string(body), 200))
|
||||
logx.Errorf("[终端→MES]"+commTag+" <-- SSE HTTP %d %s body=%s", resp.StatusCode, u, truncate(string(body), 200))
|
||||
return nil, fmt.Errorf("MES %s 返回 %d:%s", path, resp.StatusCode, truncate(string(body), 200))
|
||||
}
|
||||
logx.Infof("[终端→MES] <-- SSE connected %s", u)
|
||||
logx.Infof("[终端→MES]"+commTag+" <-- SSE connected %s", u)
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
@@ -164,12 +168,12 @@ func (c *Client) do(method, u string, body io.Reader, bearer string) ([]byte, ht
|
||||
reqBodyForLog, _ = io.ReadAll(body)
|
||||
req.Body = io.NopCloser(bytes.NewReader(reqBodyForLog))
|
||||
}
|
||||
logx.Infof("[终端→MES] --> %s %s body=%s", method, u, truncate(string(reqBodyForLog), 1000))
|
||||
logx.Infof("[终端→MES]"+commTag+" --> %s %s body=%s", method, u, truncate(string(reqBodyForLog), 1000))
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
// 2026-11-16 联调:终端→MES 全量交互落日志(传输错误单独记录,grep "[终端→MES]")
|
||||
logx.WithContext(req.Context()).Errorf("[终端→MES] <-- transport error %s %s err=%v", method, u, err)
|
||||
logx.WithContext(req.Context()).Errorf("[终端→MES]"+commTag+" <-- transport error %s %s err=%v", method, u, err)
|
||||
return nil, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
@@ -179,7 +183,7 @@ func (c *Client) do(method, u string, body io.Reader, bearer string) ([]byte, ht
|
||||
return nil, resp.Header, err
|
||||
}
|
||||
// 请求/响应全量日志(请求体在下面 marshal 后记录;这里记录响应)
|
||||
logx.WithContext(req.Context()).Infof("[终端→MES] <-- HTTP %d %s body=%s", resp.StatusCode, u, truncate(string(respBody), 1000))
|
||||
logx.WithContext(req.Context()).Infof("[终端→MES]"+commTag+" <-- HTTP %d %s body=%s", resp.StatusCode, u, truncate(string(respBody), 1000))
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
// 401 = 工位登录态过期/无效:调用方据此提示重新登录。
|
||||
return respBody, resp.Header, fmt.Errorf("MES %s %s: %w", method, u, ErrAuth)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Package netutil 提供本机 IP 缓存,供跨系统通信日志前缀使用(断网排查时能一眼看到是哪台机器发的日志)。
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
tagValue string
|
||||
)
|
||||
|
||||
// Tag 返回日志前缀片段,形如 [host=192.168.1.10];取不到非环回 IPv4 时返回 [host=?]。
|
||||
// 进程内只解析一次(sync.Once),不每次请求都枚举网卡;不使用正则,纯标准库。
|
||||
func Tag() string {
|
||||
once.Do(func() {
|
||||
ip := firstNonLoopbackIPv4()
|
||||
if ip == "" {
|
||||
ip = "?"
|
||||
}
|
||||
tagValue = "[host=" + ip + "]"
|
||||
})
|
||||
return tagValue
|
||||
}
|
||||
|
||||
// LocalIP 返回缓存的本机非环回 IPv4(取不到返回空串)。
|
||||
func LocalIP() string {
|
||||
Tag()
|
||||
ip := tagValue
|
||||
const p, s = "[host=", "]"
|
||||
if len(ip) > len(p)+len(s) {
|
||||
return ip[len(p) : len(ip)-len(s)]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstNonLoopbackIPv4() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, a := range addrs {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
v4 := ipnet.IP.To4()
|
||||
if v4 != nil {
|
||||
return v4.String()
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user