fix: rebuild MES as compilable go-zero+ent backend (renamed bj_power_mes), restore 3 workstation projects from pristine original, align naming; all Go projects go build clean
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// EventType 事件类型
|
||||
type EventType string
|
||||
|
||||
const (
|
||||
// 生产事件
|
||||
EventLoad EventType = "LOAD"
|
||||
EventUnload EventType = "UNLOAD"
|
||||
EventExchange EventType = "EXCHANGE"
|
||||
EventScan EventType = "SCAN"
|
||||
EventMark EventType = "MARK"
|
||||
EventReplenish EventType = "REPLENISH"
|
||||
EventMachineStart EventType = "MACHINE_START"
|
||||
EventMachineDone EventType = "MACHINE_DONE"
|
||||
EventInspectionResult EventType = "INSPECTION_RESULT"
|
||||
EventJobCompleted EventType = "JOB_COMPLETED"
|
||||
EventJobScrapped EventType = "JOB_SCRAPPED"
|
||||
EventJobDischarged EventType = "JOB_DISCHARGED"
|
||||
|
||||
// 工单生命周期事件
|
||||
EventOrderStarted EventType = "ORDER_STARTED"
|
||||
EventOrderPaused EventType = "ORDER_PAUSED"
|
||||
EventOrderResumed EventType = "ORDER_RESUMED"
|
||||
EventOrderCancelled EventType = "ORDER_CANCELLED"
|
||||
EventOrderCompleted EventType = "ORDER_COMPLETED"
|
||||
|
||||
// 托盘生命周期事件
|
||||
EventPalletCompleted EventType = "PALLET_COMPLETED"
|
||||
|
||||
// MOM 对接事件
|
||||
EventMomReportSuccess EventType = "MOM_REPORT_SUCCESS"
|
||||
EventMomReportFailure EventType = "MOM_REPORT_FAILURE"
|
||||
EventMomAgvRequest EventType = "MOM_AGV_ENTRY_REQUESTED"
|
||||
|
||||
EventTempSlotChanged EventType = "TEMP_SLOT_CHANGED"
|
||||
|
||||
// 报警事件
|
||||
EventAlarmRaised EventType = "ALARM_RAISED"
|
||||
EventAlarmAcked EventType = "ALARM_ACKED"
|
||||
|
||||
// 人工交互事件
|
||||
EventManualActionCreated EventType = "MANUAL_ACTION_CREATED"
|
||||
EventManualActionResolved EventType = "MANUAL_ACTION_RESOLVED"
|
||||
|
||||
// 设备事件
|
||||
EventEquipmentAvailabilityChanged EventType = "EQUIPMENT_AVAILABILITY_CHANGED"
|
||||
|
||||
// 连接状态事件
|
||||
EventConnectionStatusUpdated EventType = "CONNECTION_STATUS_UPDATED"
|
||||
|
||||
EventAll EventType = "*"
|
||||
)
|
||||
|
||||
// EntityType 实体类型
|
||||
type EntityType string
|
||||
|
||||
const (
|
||||
EntityJob EntityType = "job"
|
||||
EntityTempSlot EntityType = "temp_slot"
|
||||
)
|
||||
|
||||
// Event 事件
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
||||
Type EventType `json:"type"`
|
||||
Source string `json:"source"`
|
||||
EntityType string `json:"entityType,omitempty"`
|
||||
EntityID string `json:"entityId,omitempty"`
|
||||
EntityVersion int64 `json:"entityVersion,omitempty"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Payload map[string]any `json:"payload"`
|
||||
}
|
||||
|
||||
// EventHandler 事件处理器
|
||||
type EventHandler func(ctx context.Context, event Event) error
|
||||
|
||||
// Subscription 事件订阅
|
||||
type Subscription interface {
|
||||
Unsubscribe()
|
||||
}
|
||||
|
||||
// Bus 事件总线接口
|
||||
type Bus interface {
|
||||
Publish(ctx context.Context, event Event) error
|
||||
Subscribe(eventType EventType, handler EventHandler) Subscription
|
||||
SubscribeAll(handler EventHandler) Subscription
|
||||
Start(ctx context.Context) error
|
||||
Stop() error
|
||||
Close() error
|
||||
}
|
||||
|
||||
type handlerEntry struct {
|
||||
id uint64
|
||||
handler EventHandler
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 事件构造辅助函数
|
||||
|
||||
// NewTempSlotChangedEvent 构建暂存台槽位变更事件
|
||||
func NewTempSlotChangedEvent(slotNo, jobID int, action string) Event {
|
||||
return Event{
|
||||
ID: fmt.Sprintf("tsc-%d-%d", slotNo, time.Now().UnixNano()),
|
||||
Type: EventTempSlotChanged,
|
||||
Source: "processor",
|
||||
EntityType: string(EntityTempSlot),
|
||||
EntityID: strconv.Itoa(slotNo),
|
||||
Timestamp: time.Now(),
|
||||
Payload: map[string]any{
|
||||
"slotNo": slotNo,
|
||||
"jobId": jobID,
|
||||
"action": action,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewAlarmRaisedEvent 构建报警触发事件
|
||||
func NewAlarmRaisedEvent(alarmID int, alarmCode string, level string, equipmentID int, source string) Event {
|
||||
return Event{
|
||||
ID: fmt.Sprintf("alarm-%d-%d", alarmID, time.Now().UnixNano()),
|
||||
Type: EventAlarmRaised,
|
||||
Source: source,
|
||||
EntityType: "alarm",
|
||||
EntityID: strconv.Itoa(alarmID),
|
||||
Timestamp: time.Now(),
|
||||
Payload: map[string]any{
|
||||
"alarmId": alarmID,
|
||||
"alarmCode": alarmCode,
|
||||
"level": level,
|
||||
"equipmentId": equipmentID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewAlarmAckedEvent 构建报警确认事件
|
||||
func NewAlarmAckedEvent(alarmID int) Event {
|
||||
return Event{
|
||||
ID: fmt.Sprintf("alarm-ack-%d-%d", alarmID, time.Now().UnixNano()),
|
||||
Type: EventAlarmAcked,
|
||||
Source: "api",
|
||||
EntityType: "alarm",
|
||||
EntityID: strconv.Itoa(alarmID),
|
||||
Timestamp: time.Now(),
|
||||
Payload: map[string]any{
|
||||
"alarmId": alarmID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewEquipmentAvailabilityChangedEvent 构建设备可用状态变更事件
|
||||
func NewEquipmentAvailabilityChangedEvent(equipmentID int, equipmentName string, enabled bool) Event {
|
||||
return Event{
|
||||
ID: fmt.Sprintf("equipment-availability-%d-%d", equipmentID, time.Now().UnixNano()),
|
||||
Type: EventEquipmentAvailabilityChanged,
|
||||
Source: "api",
|
||||
EntityType: "equipment",
|
||||
EntityID: strconv.Itoa(equipmentID),
|
||||
Timestamp: time.Now(),
|
||||
Payload: map[string]any{
|
||||
"equipmentId": equipmentID,
|
||||
"equipmentName": equipmentName,
|
||||
"enabled": enabled,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewManualActionCreatedEvent 构建人工交互创建事件
|
||||
func NewManualActionCreatedEvent(actionID int, actionType string, orderID int) Event {
|
||||
return Event{
|
||||
ID: fmt.Sprintf("ma-%d-%d", actionID, time.Now().UnixNano()),
|
||||
Type: EventManualActionCreated,
|
||||
Source: "processor",
|
||||
EntityType: "manual_action",
|
||||
EntityID: strconv.Itoa(actionID),
|
||||
Timestamp: time.Now(),
|
||||
Payload: map[string]any{
|
||||
"actionId": actionID,
|
||||
"actionType": actionType,
|
||||
"orderId": orderID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewManualActionResolvedEvent 构建人工交互解决事件
|
||||
func NewManualActionResolvedEvent(actionID int, actionType string, resolvedAction string) Event {
|
||||
return Event{
|
||||
ID: fmt.Sprintf("ma-resolve-%d-%d", actionID, time.Now().UnixNano()),
|
||||
Type: EventManualActionResolved,
|
||||
Source: "api",
|
||||
EntityType: "manual_action",
|
||||
EntityID: strconv.Itoa(actionID),
|
||||
Timestamp: time.Now(),
|
||||
Payload: map[string]any{
|
||||
"actionId": actionID,
|
||||
"actionType": actionType,
|
||||
"resolvedAction": resolvedAction,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LocalBus 进程内事件总线
|
||||
type LocalBus struct {
|
||||
mu sync.RWMutex
|
||||
handlers map[EventType][]handlerEntry
|
||||
nextID uint64
|
||||
}
|
||||
|
||||
type localSubscription struct {
|
||||
bus *LocalBus
|
||||
eventType EventType
|
||||
id uint64
|
||||
}
|
||||
|
||||
// NewLocalBus 创建进程内事件总线
|
||||
func NewLocalBus() *LocalBus {
|
||||
return &LocalBus{
|
||||
handlers: make(map[EventType][]handlerEntry),
|
||||
}
|
||||
}
|
||||
|
||||
// Publish 发布事件
|
||||
func (b *LocalBus) Publish(ctx context.Context, event Event) error {
|
||||
if event.ID == "" {
|
||||
event.ID = fmt.Sprintf("%d", time.Now().UnixNano())
|
||||
}
|
||||
if event.Timestamp.IsZero() {
|
||||
event.Timestamp = time.Now()
|
||||
}
|
||||
|
||||
b.mu.RLock()
|
||||
typeHandlers := make([]handlerEntry, len(b.handlers[event.Type]))
|
||||
copy(typeHandlers, b.handlers[event.Type])
|
||||
allHandlers := make([]handlerEntry, len(b.handlers[EventAll]))
|
||||
copy(allHandlers, b.handlers[EventAll])
|
||||
b.mu.RUnlock()
|
||||
|
||||
for _, h := range typeHandlers {
|
||||
if err := h.handler(ctx, event); err != nil {
|
||||
slog.Error("event handler error", "type", event.Type, "err", err)
|
||||
}
|
||||
}
|
||||
for _, h := range allHandlers {
|
||||
if err := h.handler(ctx, event); err != nil {
|
||||
slog.Error("event handler error", "type", event.Type, "err", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Subscribe 订阅事件
|
||||
func (b *LocalBus) Subscribe(eventType EventType, handler EventHandler) Subscription {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
b.nextID++
|
||||
id := b.nextID
|
||||
|
||||
b.handlers[eventType] = append(b.handlers[eventType], handlerEntry{
|
||||
id: id,
|
||||
handler: handler,
|
||||
})
|
||||
|
||||
return &localSubscription{bus: b, eventType: eventType, id: id}
|
||||
}
|
||||
|
||||
// SubscribeAll 订阅所有事件类型
|
||||
func (b *LocalBus) SubscribeAll(handler EventHandler) Subscription {
|
||||
return b.Subscribe(EventAll, handler)
|
||||
}
|
||||
|
||||
// Start 启动(LocalBus 为空操作)
|
||||
func (b *LocalBus) Start(_ context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop 停止(LocalBus 为空操作)
|
||||
func (b *LocalBus) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func (b *LocalBus) Close() error {
|
||||
b.mu.Lock()
|
||||
b.handlers = make(map[EventType][]handlerEntry)
|
||||
b.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *localSubscription) Unsubscribe() {
|
||||
s.bus.mu.Lock()
|
||||
defer s.bus.mu.Unlock()
|
||||
|
||||
handlers := s.bus.handlers[s.eventType]
|
||||
for i, h := range handlers {
|
||||
if h.id == s.id {
|
||||
s.bus.handlers[s.eventType] = append(handlers[:i], handlers[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user