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-11 14:36:16 +08:00
|
|
|
|
// Manager 工位报警事件流(谁受影响谁播报,2026-10-20 轮询改事件驱动;多会话版 2026-11-11):
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// 后台长连接 MES GET /api/internal/station/alerts/stream(SSE,X-API-TOKEN 鉴权),
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// 只认 station.alert 事件;按每个前端订阅方自带的工位号过滤「工位N」相关报警后分发
|
|
|
|
|
|
// (一台终端实例可同时登录多个虚拟工位,各订阅方只收到自己工位的报警)。
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// 断线自动重连(3 秒退避);前端断线重连后的一次性补拉走 GET /api/alerts/station(前端自己调)。
|
|
|
|
|
|
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-11 14:36:16 +08:00
|
|
|
|
clients map[chan string]int // channel → 订阅方工位号(<=0 表示不过滤,全收)
|
2026-10-20 16:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// New 新建报警流管理器(多会话版:不再绑定单一固定工位)。
|
|
|
|
|
|
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,
|
|
|
|
|
|
clients: make(map[chan string]int),
|
2026-10-20 16:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// Subscribe 本机前端订阅(每个 EventSource 连接一个 channel),stationNo 为订阅方工位号;
|
|
|
|
|
|
// <=0 时不按工位过滤(兼容固定工位模式/未知来源),全部转发。
|
|
|
|
|
|
func (m *Manager) Subscribe(stationNo int) chan string {
|
2026-10-20 16:36:26 +08:00
|
|
|
|
ch := make(chan string, 32)
|
|
|
|
|
|
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。
|
|
|
|
|
|
func (m *Manager) Unsubscribe(ch chan string) {
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
|
if _, ok := m.clients[ch]; ok {
|
|
|
|
|
|
delete(m.clients, ch)
|
|
|
|
|
|
close(ch)
|
|
|
|
|
|
}
|
|
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Manager) broadcast(data string) {
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-11 14:36:16 +08:00
|
|
|
|
for ch, st := range m.clients {
|
|
|
|
|
|
if st > 0 && !mentionStation(data, st) {
|
|
|
|
|
|
continue // 只推给「工位N」相关订阅方(多会话:一台实例服务多工位互不串音)
|
|
|
|
|
|
}
|
2026-10-20 16:36:26 +08:00
|
|
|
|
select {
|
|
|
|
|
|
case ch <- data:
|
|
|
|
|
|
default: // 订阅者堆积(前端卡死)直接丢帧,下一条报警自然追上
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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:"))
|
|
|
|
|
|
if event == "station.alert" && data != "" {
|
|
|
|
|
|
m.dispatch(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
event = ""
|
|
|
|
|
|
default:
|
|
|
|
|
|
// 空行/心跳注释(": ping")忽略
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 14:36:16 +08:00
|
|
|
|
// dispatch 解析 MES 报警事件(station.alert),交给 broadcast 按订阅方工位分发。
|
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"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Manager) dispatch(data string) {
|
|
|
|
|
|
var a alertPayload
|
|
|
|
|
|
if err := json.Unmarshal([]byte(data), &a); err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
m.broadcast(data)
|
|
|
|
|
|
}
|