119 lines
4.1 KiB
Go
119 lines
4.1 KiB
Go
// logging.go PLC 全量交互日志装饰器(2026-11-16 联调)。
|
|
// 包装 Client 接口全部方法:调用参数、返回值、错误统一落 logx(前缀 [PLC])。
|
|
// 在 NewManager 处包一层,mock / 真实 S7(tcp.go) / tcp 自定义协议全部覆盖;
|
|
// tcp.go 为保护文件不做改动,日志在此层完成。
|
|
package plc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"bj_power_mes/internal/netutil"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
// commTag 本机 IP 前缀(进程启动解析一次),断网排查时可一眼看出日志来自哪台机器。
|
|
var commTag = netutil.Tag()
|
|
|
|
// logClient 日志装饰器:inner 为真实实现(s7Real / tcpClient / mock)。
|
|
type logClient struct{ inner Client }
|
|
|
|
func newLogClient(inner Client) *logClient { return &logClient{inner: inner} }
|
|
|
|
func (l *logClient) SendProcessCode(ctx context.Context, combination string) ([]int, error) {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" --> SendProcessCode combination=%s", combination)
|
|
codes, err := l.inner.SendProcessCode(ctx, combination)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- SendProcessCode err=%v", err)
|
|
} else {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" <-- SendProcessCode codes=%v", codes)
|
|
}
|
|
return codes, err
|
|
}
|
|
|
|
func (l *logClient) QueryDone(ctx context.Context) (bool, error) {
|
|
done, err := l.inner.QueryDone(ctx)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- QueryDone err=%v", err)
|
|
} else {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" <-- QueryDone done=%v", done)
|
|
}
|
|
return done, err
|
|
}
|
|
|
|
func (l *logClient) AckDone(ctx context.Context, ok bool) {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" AckDone ok=%v", ok)
|
|
l.inner.AckDone(ctx, ok)
|
|
}
|
|
|
|
func (l *logClient) QueryDockPallet(ctx context.Context, dockCode string) (bool, error) {
|
|
has, err := l.inner.QueryDockPallet(ctx, dockCode)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- QueryDockPallet dock=%s err=%v", dockCode, err)
|
|
} else {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" <-- QueryDockPallet dock=%s hasPallet=%v", dockCode, has)
|
|
}
|
|
return has, err
|
|
}
|
|
|
|
func (l *logClient) QueryStationPallet(ctx context.Context, stationNo int) (bool, error) {
|
|
has, err := l.inner.QueryStationPallet(ctx, stationNo)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- QueryStationPallet station=%d err=%v", stationNo, err)
|
|
} else {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" <-- QueryStationPallet station=%d hasPallet=%v", stationNo, has)
|
|
}
|
|
return has, err
|
|
}
|
|
|
|
func (l *logClient) ReadBits(ctx context.Context) ([]bool, error) {
|
|
bits, err := l.inner.ReadBits(ctx)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- ReadBits err=%v", err)
|
|
} else {
|
|
// 48 位压成 0/1 字符串,一行可读
|
|
s := make([]byte, len(bits))
|
|
for i, b := range bits {
|
|
if b {
|
|
s[i] = '1'
|
|
} else {
|
|
s[i] = '0'
|
|
}
|
|
}
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" <-- ReadBits bits=%s", string(s))
|
|
}
|
|
return bits, err
|
|
}
|
|
|
|
func (l *logClient) ReadMW(ctx context.Context, addr int) (int, error) {
|
|
v, err := l.inner.ReadMW(ctx, addr)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- ReadMW MW%d err=%v", addr, err)
|
|
} else {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" <-- ReadMW MW%d=%d", addr, v)
|
|
}
|
|
return v, err
|
|
}
|
|
|
|
func (l *logClient) ReadMWBlock(ctx context.Context, start, count int) ([]int, error) {
|
|
vals, err := l.inner.ReadMWBlock(ctx, start, count)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- ReadMWBlock MW%d..MW%d err=%v", start, start+count-1, err)
|
|
} else {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" <-- ReadMWBlock MW%d..MW%d vals=%v", start, start+count-1, vals)
|
|
}
|
|
return vals, err
|
|
}
|
|
|
|
func (l *logClient) WriteMW(ctx context.Context, addr, value int) error {
|
|
logx.WithContext(ctx).Infof("[PLC]"+commTag+" --> WriteMW MW%d=%d", addr, value)
|
|
err := l.inner.WriteMW(ctx, addr, value)
|
|
if err != nil {
|
|
logx.WithContext(ctx).Errorf("[PLC]"+commTag+" <-- WriteMW MW%d=%d err=%v", addr, value, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// String 满足调试打印。
|
|
func (l *logClient) String() string { return fmt.Sprintf("logClient(%T)", l.inner) }
|