Files
bj_power/bj_power_mes/internal/plc/mock.go
T
SunYF b0c048d9aa feat: 新增收集式校验错误弹窗及工位报警语音播报
新增 ErrorListDialog 组件用于统一展示结构化校验错误信息,实现报错一次收全;新增工位报警语音播报功能,通过浏览器 SpeechSynthesis 播放本工位相关报警;调整物料类别及序列号字段文案,移除「其他」类别;新增工单实际开始字段及采集方式配置;优化 AGV 及 PlcSend 页签刷新逻辑;新增看板物料追溯及工位报警接口;删除 PLC 强制写 M 位及相关测试代码;同步数据库字段注释及补丁。
2026-10-20 16:36:26 +08:00

112 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package plc
import (
"context"
"errors"
"sync"
)
// mockClient 模拟西门子 PLC:下发成功即认为写入寄存器,完成信号由外部置位。
type mockClient struct {
ack bool
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
}
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
}
// SetStationPallet 手动设置某工位托盘位是否有件(PLC↔MES 对账联调/自测用)
func (m *mockClient) SetStationPallet(stationNo int, has bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.stationPallet[stationNo] = has
}
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
}
// 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
}