2026-08-27 10:56:41 +08:00
|
|
|
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"
|
|
|
|
|
|
2026-08-28 09:22:19 +08:00
|
|
|
// 装配线操作日志事件(工单/工件/操作步骤/人员可追溯)
|
|
|
|
|
EventBomSaved EventType = "BOM_SAVED"
|
|
|
|
|
EventMaterialRequested EventType = "MATERIAL_REQUESTED"
|
|
|
|
|
EventPlcProcessSent EventType = "PLC_PROCESS_SENT"
|
|
|
|
|
EventTorqueReported EventType = "TORQUE_REPORTED"
|
|
|
|
|
EventScanReported EventType = "SCAN_REPORTED"
|
|
|
|
|
EventSemiFlow EventType = "SEMI_FLOW"
|
|
|
|
|
EventAgvTaskCreated EventType = "AGV_TASK_CREATED"
|
|
|
|
|
|
2026-08-27 10:56:41 +08:00
|
|
|
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
|
|
|
|
|
}
|