156 lines
3.8 KiB
Go
156 lines
3.8 KiB
Go
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"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Manager 工位报警事件流(谁受影响谁播报,2026-10-20 轮询改事件驱动):
|
|||
|
|
// 后台长连接 MES GET /api/internal/station/alerts/stream(SSE,X-API-TOKEN 鉴权),
|
|||
|
|
// 只认 station.alert 事件;按本机固定工位号过滤「工位N」相关报警后,广播给本机前端订阅者。
|
|||
|
|
// 断线自动重连(3 秒退避);前端断线重连后的一次性补拉走 GET /api/alerts/station(前端自己调)。
|
|||
|
|
type Manager struct {
|
|||
|
|
mc *mes.Client
|
|||
|
|
stationNo int
|
|||
|
|
|
|||
|
|
mu sync.Mutex
|
|||
|
|
clients map[chan string]struct{}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// New 新建报警流管理器。
|
|||
|
|
func New(mc *mes.Client, stationNo int) *Manager {
|
|||
|
|
return &Manager{
|
|||
|
|
mc: mc,
|
|||
|
|
stationNo: stationNo,
|
|||
|
|
clients: make(map[chan string]struct{}),
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Subscribe 本机前端订阅(每个 EventSource 连接一个 channel)。
|
|||
|
|
func (m *Manager) Subscribe() chan string {
|
|||
|
|
ch := make(chan string, 32)
|
|||
|
|
m.mu.Lock()
|
|||
|
|
m.clients[ch] = struct{}{}
|
|||
|
|
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()
|
|||
|
|
for ch := range m.clients {
|
|||
|
|
select {
|
|||
|
|
case ch <- data:
|
|||
|
|
default: // 订阅者堆积(前端卡死)直接丢帧,下一条报警自然追上
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Run 主循环:连 MES SSE → 解析分发 → 断线 3 秒重连,直到 ctx 取消。
|
|||
|
|
func (m *Manager) Run(ctx context.Context) {
|
|||
|
|
if m.stationNo <= 0 {
|
|||
|
|
logx.Errorf("报警事件流未启动:Station.StationNo=%d 无效", m.stationNo)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
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()
|
|||
|
|
logx.Infof("工位报警事件流已连接 MES(工位%d)", m.stationNo)
|
|||
|
|
defer logx.Infof("工位报警事件流连接关闭(工位%d)", m.stationNo)
|
|||
|
|
|
|||
|
|
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")忽略
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// dispatch 解析报警 JSON,命中本工位(target/content/title 含「工位N」)才广播。
|
|||
|
|
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
|
|||
|
|
}
|
|||
|
|
hit := strings.Contains(a.Target, "工位"+strconv.Itoa(m.stationNo)) ||
|
|||
|
|
strings.Contains(a.Content, "工位"+strconv.Itoa(m.stationNo)) ||
|
|||
|
|
strings.Contains(a.Title, "工位"+strconv.Itoa(m.stationNo))
|
|||
|
|
if !hit {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
m.broadcast(data)
|
|||
|
|
}
|