feat: 会话令牌与守卫、看板组件及工位终端功能完善
- 用户表扩展:MES/WMS user 实体新增字段,新增 active_token 迁移 SQL(2026-11-11) - 会话机制:MES 新增 session_guard,重构 token/auth/jwt/perm 与路由;WMS/工作站同步令牌续签与鉴权 - 看板(board):调整 MES 看板产线/生产/侧栏/仓库组件及样式;WMS 看板展示页同步调整 - 工位终端后端:完善报警流、绑定、PLC 对账、拧紧、proxy、上报、站点告警/操作等 handler,补充 mes client/store - 工位终端前端:登录/主界面/各操作面板调整,新增滚动分页与多语言 yaml 文案,刷新构建产物 - MES 业务:新增流程链 logic,调整待审核/产能支撑/产品工艺/设置页、PLC 监控与帮助文档,刷新构建产物 - 更新问题记录文档
This commit is contained in:
@@ -16,32 +16,32 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// Manager 工位报警事件流(谁受影响谁播报,2026-10-20 轮询改事件驱动):
|
||||
// Manager 工位报警事件流(谁受影响谁播报,2026-10-20 轮询改事件驱动;多会话版 2026-11-11):
|
||||
// 后台长连接 MES GET /api/internal/station/alerts/stream(SSE,X-API-TOKEN 鉴权),
|
||||
// 只认 station.alert 事件;按本机固定工位号过滤「工位N」相关报警后,广播给本机前端订阅者。
|
||||
// 只认 station.alert 事件;按每个前端订阅方自带的工位号过滤「工位N」相关报警后分发
|
||||
// (一台终端实例可同时登录多个虚拟工位,各订阅方只收到自己工位的报警)。
|
||||
// 断线自动重连(3 秒退避);前端断线重连后的一次性补拉走 GET /api/alerts/station(前端自己调)。
|
||||
type Manager struct {
|
||||
mc *mes.Client
|
||||
stationNo int
|
||||
mc *mes.Client
|
||||
|
||||
mu sync.Mutex
|
||||
clients map[chan string]struct{}
|
||||
clients map[chan string]int // channel → 订阅方工位号(<=0 表示不过滤,全收)
|
||||
}
|
||||
|
||||
// New 新建报警流管理器。
|
||||
func New(mc *mes.Client, stationNo int) *Manager {
|
||||
// New 新建报警流管理器(多会话版:不再绑定单一固定工位)。
|
||||
func New(mc *mes.Client) *Manager {
|
||||
return &Manager{
|
||||
mc: mc,
|
||||
stationNo: stationNo,
|
||||
clients: make(map[chan string]struct{}),
|
||||
mc: mc,
|
||||
clients: make(map[chan string]int),
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe 本机前端订阅(每个 EventSource 连接一个 channel)。
|
||||
func (m *Manager) Subscribe() chan string {
|
||||
// Subscribe 本机前端订阅(每个 EventSource 连接一个 channel),stationNo 为订阅方工位号;
|
||||
// <=0 时不按工位过滤(兼容固定工位模式/未知来源),全部转发。
|
||||
func (m *Manager) Subscribe(stationNo int) chan string {
|
||||
ch := make(chan string, 32)
|
||||
m.mu.Lock()
|
||||
m.clients[ch] = struct{}{}
|
||||
m.clients[ch] = stationNo
|
||||
m.mu.Unlock()
|
||||
return ch
|
||||
}
|
||||
@@ -59,7 +59,10 @@ func (m *Manager) Unsubscribe(ch chan string) {
|
||||
func (m *Manager) broadcast(data string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for ch := range m.clients {
|
||||
for ch, st := range m.clients {
|
||||
if st > 0 && !mentionStation(data, st) {
|
||||
continue // 只推给「工位N」相关订阅方(多会话:一台实例服务多工位互不串音)
|
||||
}
|
||||
select {
|
||||
case ch <- data:
|
||||
default: // 订阅者堆积(前端卡死)直接丢帧,下一条报警自然追上
|
||||
@@ -67,12 +70,21 @@ func (m *Manager) broadcast(data string) {
|
||||
}
|
||||
}
|
||||
|
||||
// Run 主循环:连 MES SSE → 解析分发 → 断线 3 秒重连,直到 ctx 取消。
|
||||
func (m *Manager) Run(ctx context.Context) {
|
||||
if m.stationNo <= 0 {
|
||||
logx.Errorf("报警事件流未启动:Station.StationNo=%d 无效", m.stationNo)
|
||||
return
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Run 主循环:连 MES SSE → 解析分发 → 断线 3 秒重连,直到 ctx 取消。
|
||||
// 多会话版:不再依赖固定工位号配置,未配置工位的终端(虚拟工位实例)也保持连接。
|
||||
func (m *Manager) Run(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -96,8 +108,8 @@ func (m *Manager) runOnce(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
defer body.Close()
|
||||
logx.Infof("工位报警事件流已连接 MES(工位%d)", m.stationNo)
|
||||
defer logx.Infof("工位报警事件流连接关闭(工位%d)", m.stationNo)
|
||||
logx.Infof("工位报警事件流已连接 MES")
|
||||
defer logx.Infof("工位报警事件流连接关闭")
|
||||
|
||||
rd := bufio.NewReader(body)
|
||||
event := ""
|
||||
@@ -130,7 +142,7 @@ func (m *Manager) runOnce(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// dispatch 解析报警 JSON,命中本工位(target/content/title 含「工位N」)才广播。
|
||||
// dispatch 解析 MES 报警事件(station.alert),交给 broadcast 按订阅方工位分发。
|
||||
type alertPayload struct {
|
||||
ID int `json:"id"`
|
||||
Type string `json:"type"`
|
||||
@@ -145,11 +157,5 @@ func (m *Manager) dispatch(data string) {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user