初始化2
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package plc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// mockClient 模拟西门子 PLC:下发成功即认为写入寄存器,完成信号由外部置位。
|
||||
type mockClient struct {
|
||||
ack bool
|
||||
sent string
|
||||
}
|
||||
|
||||
func newMockClient() *mockClient {
|
||||
return &mockClient{}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package plc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/internal/config"
|
||||
)
|
||||
|
||||
// Client PLC 对接抽象。
|
||||
// 生产对接西门子 S7:写入工序码寄存器,读完成信号位;未收到完成信号不下发下一条。
|
||||
// mock 模式下直接模拟:下发后置发送状态,可主动标记完成。
|
||||
type Client interface {
|
||||
// SendProcessCode 下发工序组合(如 "135"),返回已下发工序码
|
||||
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)
|
||||
}
|
||||
|
||||
// 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 }
|
||||
|
||||
// parseCombination 解析 "135" -> [1,3,5]
|
||||
func parseCombination(s string) []int {
|
||||
var out []int
|
||||
for _, ch := range strings.TrimSpace(s) {
|
||||
if ch >= '1' && ch <= '9' {
|
||||
out = append(out, int(ch-'0'))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Sleep 简化轮询间隔(供 logic 使用)
|
||||
func Sleep(d time.Duration) {
|
||||
time.Sleep(d)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package plc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/config"
|
||||
)
|
||||
|
||||
// tcpClient 占位:对接西门子 S7 的实现签名。
|
||||
// 本项目以 mock 为主,真实 S7 对接预留(寄存器写工序码、读完成信号位)。
|
||||
type tcpClient struct {
|
||||
c config.PlcConf
|
||||
}
|
||||
|
||||
func newTcpClient(c config.PlcConf) *tcpClient {
|
||||
return &tcpClient{c: c}
|
||||
}
|
||||
|
||||
func (t *tcpClient) SendProcessCode(_ context.Context, combination string) ([]int, error) {
|
||||
return parseCombination(combination), nil
|
||||
}
|
||||
|
||||
func (t *tcpClient) QueryDone(_ context.Context) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (t *tcpClient) AckDone(_ context.Context, ok bool) {
|
||||
_ = ok
|
||||
}
|
||||
Reference in New Issue
Block a user