Files
bj_power/bj_power_mes/internal/plc/mock.go
T

52 lines
1.3 KiB
Go
Raw Normal View History

2026-08-28 15:06:01 +08:00
package plc
import (
"context"
"errors"
"sync"
2026-08-28 15:06:01 +08:00
)
// mockClient 模拟西门子 PLC:下发成功即认为写入寄存器,完成信号由外部置位。
type mockClient struct {
ack bool
2026-08-28 15:06:01 +08:00
sent string
mu sync.RWMutex
// dockPallet 模拟接驳台是否有托盘:默认全部无托盘;联调时可用 SetDockPallet 置位
dockPallet map[string]bool
2026-08-28 15:06:01 +08:00
}
func newMockClient() *mockClient {
return &mockClient{dockPallet: map[string]bool{}}
}
// SetDockPallet 手动设置某接驳台是否有托盘(供联调/测试用)
func (m *mockClient) SetDockPallet(dockCode string, has bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.dockPallet[dockCode] = has
2026-08-28 15:06:01 +08:00
}
func (m *mockClient) SendProcessCode(_ context.Context, combination string) ([]int, error) {
codes := parseCombination(combination)
m.sent = combination
m.ack = false
return codes, nil
}
func (m *mockClient) QueryDone(_ context.Context) (bool, error) {
if m.sent == "" {
return false, errors.New("no process sent")
}
// 模拟:假设 PLC 已完成,30 秒后返回完成信号
return m.ack, nil
}
func (m *mockClient) AckDone(_ context.Context, ok bool) {
m.ack = ok
}
func (m *mockClient) QueryDockPallet(_ context.Context, dockCode string) (bool, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.dockPallet[dockCode], nil
2026-08-28 15:06:01 +08:00
}