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

126 lines
3.9 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
// stationPallet 模拟工位托盘位是否有件(PLC↔MES 对账用):key=工位号,默认全部无件;
// 联调/自测时可用 SetStationPallet 置位,模拟“PLC 说有位、MES 说无工件”的 mismatch 场景。
stationPallet map[int]bool
// bits 模拟 48 位点位信号(§4.1):[0..11]G1..G12 [12..26]C1..C15 [27..46]DOCK01..DOCK20 [47]JBT21
bits []bool
// mw 模拟 MW 寄存器当前值(§4.4 覆盖的 MW800..MW886)
mw map[int]int
2026-08-28 15:06:01 +08:00
}
func newMockClient() *mockClient {
m := &mockClient{
dockPallet: map[string]bool{},
stationPallet: map[int]bool{},
bits: make([]bool, 48),
mw: map[int]int{},
}
// 预置若干点位为“有托盘”,便于手动监控页 mock 演示 48 位状态展示(●/○ 混合)
for _, i := range []int{0, 1, 12, 13, 14, 27, 28, 29, 47} {
m.bits[i] = true
}
// 预置两条在途指令(MW 非0),便于演示 2.2 动作类信号解析(非0即解析 源→目标)
m.mw[800] = 105 // MW800 = 105 → G1 → C5(A 类)
m.mw[872] = 305 // MW872 = 305 → C3 → C5(D 类)
return m
}
// 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
}
// SetStationPallet 手动设置某工位托盘位是否有件(PLC↔MES 对账联调/自测用)
func (m *mockClient) SetStationPallet(stationNo int, has bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.stationPallet[stationNo] = 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
}
// ReadMWBlock 批量读连续 MW(mock:逐地址查模拟寄存器表,缺省 0)。
func (m *mockClient) ReadMWBlock(_ context.Context, start, count int) ([]int, error) {
if start < 0 || count <= 0 || start+count > 65536 {
return nil, errors.New("非法 MW 批量读区间")
}
m.mu.RLock()
defer m.mu.RUnlock()
out := make([]int, count)
for i := 0; i < count; i++ {
out[i] = m.mw[start+i]
}
return out, nil
}
// QueryStationPallet 读工位托盘位(mock):返回置位值,默认无件。
func (m *mockClient) QueryStationPallet(_ context.Context, stationNo int) (bool, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.stationPallet[stationNo], nil
}
// ReadBits 读 48 位点位信号(mock):返回内存态位数组。
func (m *mockClient) ReadBits(_ context.Context) ([]bool, error) {
m.mu.RLock()
defer m.mu.RUnlock()
out := make([]bool, len(m.bits))
copy(out, m.bits)
return out, nil
}
// ReadMW 读 16 位寄存器(mock):返回内存态 MW 值,缺省 0。
func (m *mockClient) ReadMW(_ context.Context, addr int) (int, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.mw[addr], nil
}
// WriteMW 写 16 位寄存器(mock):写入内存态 MW,供手动下发演示。
func (m *mockClient) WriteMW(_ context.Context, addr, value int) error {
m.mu.Lock()
defer m.mu.Unlock()
m.mw[addr] = value
return nil
}