Files
bj_power/bj_power_mes/internal/plc/plc.go
T
SunYF 8e6048c64b feat: 实现 PLC↔MES 对账功能(#8),包含终端触发与系统触发逻辑
新增功能:
1. 新增 PLC 客户端接口 QueryStationPallet,支持 mock 与真实 PLC 寄存器读取
2. 新增对账检测逻辑,覆盖 11 个工位(G1~G10 + C15)
3. 事件驱动触发:终端 T2/T3/T5 触发单工位检测,T4 报工命中则阻断;T6/T7 系统触发全量检测并落预警中心
4. 新增路由、handler、logic、测试用例及帮助文档,完成对账全链路实现
2026-10-13 15:51:45 +08:00

92 lines
2.9 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"
"strconv"
"strings"
"time"
"bj_power_mes/internal/config"
)
// Client PLC 对接抽象。
// 生产对接西门子 S7:写入工序码寄存器,读完成信号位;未收到完成信号不下发下一条。
// mock 模式下直接模拟:下发后置发送状态,可主动标记完成。
type Client interface {
// SendProcessCode 下发工位组合(如 "1,3,5"),返回已下发工位码
SendProcessCode(ctx context.Context, processCombination string) ([]int, error)
// QueryDone 查询最近一次下发是否收到完成信号,完成则返回 true
QueryDone(ctx context.Context) (bool, error)
// AckDone 标记完成(mock 使用,也可由 PLC 信号轮询写入)
AckDone(ctx context.Context, ok bool)
// QueryDockPallet 查询某接驳台(DOCK01..21)当前是否有托盘(读 PLC 托盘传感器信号)。
// 用于 AGV 到达目的地前确认接驳台空闲。暂未接真实 PLC 时 mock 返回 false(默认无托盘)。
QueryDockPallet(ctx context.Context, dockCode string) (bool, error)
// QueryStationPallet 查询某工位(stationNo)托盘位是否有件(读 PLC 工位托盘传感器信号)。
// 用于 PLC↔MES 对账(#8):PLC 说有位、MES 说无工件 ⇒ mismatch。
// 暂未接真实 PLC 时 mock 按置位返回(默认无件);真实 S7 对接待 PLC 点位表
// (G1~G10→M895.0~M896.1、C15→M898.0)后由 tcp 实现补读寄存器逻辑。
QueryStationPallet(ctx context.Context, stationNo int) (bool, error)
}
// Manager 管理 PLC 客户端实例
type Manager struct {
client Client
mock bool
}
func NewManager(c config.PlcConf, mock bool) (*Manager, error) {
m := &Manager{mock: mock}
if mock {
m.client = newMockClient()
} else {
m.client = newTcpClient(c)
}
return m, nil
}
func (m *Manager) Get() Client { return m.client }
func (m *Manager) IsMock() bool { return m.mock }
// SetMockDockPallet 仅在 mock 模式下生效:手动置位某接驳台是否有托盘(联调用)。
func (m *Manager) SetMockDockPallet(dockCode string, has bool) {
if !m.mock {
return
}
if mc, ok := m.client.(*mockClient); ok {
mc.SetDockPallet(dockCode, has)
}
}
// SetMockStationPallet 仅在 mock 模式下生效:手动置位某工位托盘位是否有件(PLC↔MES 对账联调用)。
func (m *Manager) SetMockStationPallet(stationNo int, has bool) {
if !m.mock {
return
}
if mc, ok := m.client.(*mockClient); ok {
mc.SetStationPallet(stationNo, has)
}
}
// parseCombination 解析工位组合 "1,3,5" / "10,11,12" -> [1,3,5] / [10,11,12]
func parseCombination(s string) []int {
var out []int
for _, p := range strings.Split(s, ",") {
p = strings.TrimSpace(p)
if p == "" {
continue
}
n, err := strconv.Atoi(p)
if err != nil || n < 1 {
continue
}
out = append(out, n)
}
return out
}
// Sleep 简化轮询间隔(供 logic 使用)
func Sleep(d time.Duration) {
time.Sleep(d)
}