2026-10-20 16:36:26 +08:00
|
|
|
|
package alertstream
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bufio"
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_workstation/internal/mes"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// Manager 工位事件流(谁受影响谁播报,2026-10-20 轮询改事件驱动;多会话版 2026-11-11;
|
|
|
|
|
|
// 2026-11-25 扩展为工位事件流:报警+工件到位+点位变化+停单状态,终端页面零轮询):
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// 后台长连接 MES GET /api/internal/station/alerts/stream(SSE,X-API-TOKEN 鉴权),
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// 认 station.* 事件;按每个前端订阅方自带的工位号过滤后分发
|
|
|
|
|
|
// (一台终端实例可同时登录多个虚拟工位,各订阅方只收到自己工位的事件)。
|
|
|
|
|
|
// 断线自动重连(3 秒退避);前端断线重连后的一次性补拉由前端自己做。
|
2026-10-20 16:36:26 +08:00
|
|
|
|
type Manager struct {
|
2026-11-11 14:36:16 +08:00
|
|
|
|
mc *mes.Client
|
2026-10-20 16:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
mu sync.Mutex
|
2026-11-26 17:18:58 +08:00
|
|
|
|
clients map[chan workEvent]int // channel → 订阅方工位号(<=0 表示不过滤,全收)
|
2026-10-20 16:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// workEvent 单条工位事件:Type=SSE 事件名(station.alert/station.workpiece/…),Data=JSON 载荷。
|
|
|
|
|
|
type workEvent struct {
|
|
|
|
|
|
Type string
|
|
|
|
|
|
Data string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// New 新建工位事件流管理器(多会话版:不再绑定单一固定工位)。
|
2026-11-11 14:36:16 +08:00
|
|
|
|
func New(mc *mes.Client) *Manager {
|
2026-10-20 16:36:26 +08:00
|
|
|
|
return &Manager{
|
2026-11-11 14:36:16 +08:00
|
|
|
|
mc: mc,
|
2026-11-26 17:18:58 +08:00
|
|
|
|
clients: make(map[chan workEvent]int),
|
2026-10-20 16:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// Subscribe 本机前端订阅(每个 EventSource 连接一个 channel),stationNo 为订阅方工位号;
|
|
|
|
|
|
// <=0 时不按工位过滤(兼容固定工位模式/未知来源),全部转发。
|
2026-11-26 17:18:58 +08:00
|
|
|
|
func (m *Manager) Subscribe(stationNo int) chan workEvent {
|
|
|
|
|
|
ch := make(chan workEvent, 32)
|
2026-10-20 16:36:26 +08:00
|
|
|
|
m.mu.Lock()
|
2026-11-11 14:36:16 +08:00
|
|
|
|
m.clients[ch] = stationNo
|
2026-10-20 16:36:26 +08:00
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
|
return ch
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Unsubscribe 取消订阅并关闭 channel。
|
2026-11-26 17:18:58 +08:00
|
|
|
|
func (m *Manager) Unsubscribe(ch chan workEvent) {
|
2026-10-20 16:36:26 +08:00
|
|
|
|
m.mu.Lock()
|
|
|
|
|
|
if _, ok := m.clients[ch]; ok {
|
|
|
|
|
|
delete(m.clients, ch)
|
|
|
|
|
|
close(ch)
|
|
|
|
|
|
}
|
|
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// PublishLocal 本地事件广播(不经 MES):拧紧枪连接状态等本机变化,直接推给前端订阅方。
|
|
|
|
|
|
func (m *Manager) PublishLocal(typ, data string) {
|
|
|
|
|
|
m.dispatch(typ, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Manager) broadcast(ev workEvent) {
|
2026-10-20 16:36:26 +08:00
|
|
|
|
m.mu.Lock()
|
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-11 14:36:16 +08:00
|
|
|
|
for ch, st := range m.clients {
|
2026-11-26 17:18:58 +08:00
|
|
|
|
deliver := true
|
|
|
|
|
|
switch ev.Type {
|
|
|
|
|
|
case "station.alert":
|
|
|
|
|
|
deliver = st <= 0 || mentionStation(ev.Data, st)
|
|
|
|
|
|
case "station.workpiece", "station.state":
|
|
|
|
|
|
if st > 0 {
|
|
|
|
|
|
var p struct {
|
|
|
|
|
|
StationNo int `json:"stationNo"`
|
|
|
|
|
|
}
|
|
|
|
|
|
deliver = json.Unmarshal([]byte(ev.Data), &p) == nil && p.StationNo == st
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !deliver {
|
|
|
|
|
|
continue
|
2026-11-11 14:36:16 +08:00
|
|
|
|
}
|
2026-10-20 16:36:26 +08:00
|
|
|
|
select {
|
2026-11-26 17:18:58 +08:00
|
|
|
|
case ch <- ev:
|
|
|
|
|
|
default: // 订阅者堆积(前端卡死)直接丢帧,下一事件自然追上
|
2026-10-20 16:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// mentionStation 判断报警 JSON 文本是否提及指定工位(target/content/title 含「工位N」)。
|
|
|
|
|
|
func mentionStation(data string, stationNo int) bool {
|
|
|
|
|
|
var a alertPayload
|
|
|
|
|
|
if err := json.Unmarshal([]byte(data), &a); err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
tag := "工位" + strconv.Itoa(stationNo)
|
|
|
|
|
|
return strings.Contains(a.Target, tag) ||
|
|
|
|
|
|
strings.Contains(a.Content, tag) ||
|
|
|
|
|
|
strings.Contains(a.Title, tag)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// Run 主循环:连 MES SSE → 解析分发 → 断线 3 秒重连,直到 ctx 取消。
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// 多会话版:不再依赖固定工位号配置,未配置工位的终端(虚拟工位实例)也保持连接。
|
2026-10-20 16:36:26 +08:00
|
|
|
|
func (m *Manager) Run(ctx context.Context) {
|
|
|
|
|
|
for {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return
|
|
|
|
|
|
default:
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := m.runOnce(ctx); err != nil {
|
|
|
|
|
|
logx.Errorf("工位报警事件流断开,3 秒后重连: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return
|
|
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Manager) runOnce(ctx context.Context) error {
|
|
|
|
|
|
body, err := m.mc.GetStream("/api/internal/station/alerts/stream", nil)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
defer body.Close()
|
2026-11-11 14:36:16 +08:00
|
|
|
|
logx.Infof("工位报警事件流已连接 MES")
|
|
|
|
|
|
defer logx.Infof("工位报警事件流连接关闭")
|
2026-10-20 16:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
rd := bufio.NewReader(body)
|
|
|
|
|
|
event := ""
|
|
|
|
|
|
for {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
|
default:
|
|
|
|
|
|
}
|
|
|
|
|
|
line, rerr := rd.ReadString('\n')
|
|
|
|
|
|
if rerr != nil {
|
|
|
|
|
|
if rerr == io.EOF {
|
|
|
|
|
|
return fmt.Errorf("MES 流关闭(EOF)")
|
|
|
|
|
|
}
|
|
|
|
|
|
return rerr
|
|
|
|
|
|
}
|
|
|
|
|
|
line = strings.TrimRight(line, "\r\n")
|
|
|
|
|
|
switch {
|
|
|
|
|
|
case strings.HasPrefix(line, "event:"):
|
|
|
|
|
|
event = strings.TrimSpace(strings.TrimPrefix(line, "event:"))
|
|
|
|
|
|
case strings.HasPrefix(line, "data:"):
|
|
|
|
|
|
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
2026-11-26 17:18:58 +08:00
|
|
|
|
if mesStationEvents[event] && data != "" {
|
|
|
|
|
|
m.dispatch(event, data)
|
2026-10-20 16:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
event = ""
|
|
|
|
|
|
default:
|
|
|
|
|
|
// 空行/心跳注释(": ping")忽略
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// mesStationEvents MES 侧下发的工位事件类型白名单(与 MES station_alert_stream.go 同口径)。
|
|
|
|
|
|
var mesStationEvents = map[string]bool{
|
|
|
|
|
|
"station.alert": true,
|
|
|
|
|
|
"station.workpiece": true,
|
|
|
|
|
|
"station.points": true,
|
|
|
|
|
|
"station.state": true,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// dispatch 按事件类型广播(station.alert 先解析校验脏帧,其余类型 JSON 直传由订阅方过滤)。
|
|
|
|
|
|
func (m *Manager) dispatch(typ, data string) {
|
|
|
|
|
|
if typ == "station.alert" {
|
|
|
|
|
|
var a alertPayload
|
|
|
|
|
|
if err := json.Unmarshal([]byte(data), &a); err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
m.broadcast(workEvent{Type: typ, Data: data})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// alertPayload 报警事件载荷(station.alert,2026-10-20 原有结构不变)。
|
2026-10-20 16:36:26 +08:00
|
|
|
|
type alertPayload struct {
|
|
|
|
|
|
ID int `json:"id"`
|
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
|
Level string `json:"level"`
|
|
|
|
|
|
Target string `json:"target"`
|
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
|
}
|