feat: 新增收集式校验错误弹窗及工位报警语音播报
新增 ErrorListDialog 组件用于统一展示结构化校验错误信息,实现报错一次收全;新增工位报警语音播报功能,通过浏览器 SpeechSynthesis 播放本工位相关报警;调整物料类别及序列号字段文案,移除「其他」类别;新增工单实际开始字段及采集方式配置;优化 AGV 及 PlcSend 页签刷新逻辑;新增看板物料追溯及工位报警接口;删除 PLC 强制写 M 位及相关测试代码;同步数据库字段注释及补丁。
This commit is contained in:
@@ -2,7 +2,9 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -129,13 +131,54 @@ func (s *Service) CreateAlert(ctx context.Context, typ, level, target, message s
|
||||
// (曾致 T6/T7 对账预警 title 恒超 120 字节、落预警中心失败),故按 rune 边界回退截断。
|
||||
title := truncateUTF8(message, 255)
|
||||
content := truncateUTF8(message, 500)
|
||||
_, err := s.ctx.EntClient.Alert.Create().
|
||||
a, err := s.ctx.EntClient.Alert.Create().
|
||||
SetRuleId(0).SetType(typ).SetLevel(level).SetTarget(target).
|
||||
SetTitle(title).SetContent(content).
|
||||
SetStatus("UNREAD").Save(ctx)
|
||||
if err == nil {
|
||||
s.publishStationAlert(a) // 写入成功即向工位订阅者广播(谁受影响谁播报)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// publishStationAlert 报警写入成功后经 SSE Hub 广播 station.alert 事件(载荷=报警 JSON)。
|
||||
// 终端侧按「工位N」自行过滤(Hub 是广播语义,按工位点对点过滤交给唯一消费者=终端后端)。
|
||||
func (s *Service) publishStationAlert(a *ent.Alert) {
|
||||
if s.ctx.SSE == nil || a == nil {
|
||||
return
|
||||
}
|
||||
if b, jerr := json.Marshal(a); jerr == nil {
|
||||
s.ctx.SSE.Publish("station.alert", string(b))
|
||||
}
|
||||
}
|
||||
|
||||
// StationAlerts 工位终端「本工位相关报警」查询(谁受影响谁播报):
|
||||
// 取最近 minutes 分钟内未处置(UNREAD)、且 目标或内容 命中「工位N」的报警。
|
||||
// 工位相关报警写入约定:target="工位N"(plc_mismatch)或 content 含「工位N」(torque_ng);
|
||||
// 缺料/叫料/设备类报警后续落库时按同约定写 target,即自动进入本查询。
|
||||
func (s *Service) StationAlerts(ctx context.Context, stationNo, minutes int) ([]*ent.Alert, error) {
|
||||
if stationNo <= 0 {
|
||||
return nil, errors.New("缺少有效的 stationNo")
|
||||
}
|
||||
if minutes <= 0 {
|
||||
minutes = 10
|
||||
}
|
||||
if minutes > 120 {
|
||||
minutes = 120 // 时间窗封顶 2 小时,防止终端误传超长窗口拉爆查询
|
||||
}
|
||||
tgt := fmt.Sprintf("工位%d", stationNo)
|
||||
since := time.Now().Add(-time.Duration(minutes) * time.Minute)
|
||||
return s.ctx.EntClient.Alert.Query().
|
||||
Where(
|
||||
alert.Status("UNREAD"),
|
||||
alert.CreatedAtGTE(since),
|
||||
alert.Or(alert.TargetEQ(tgt), alert.ContentContainsFold(tgt)),
|
||||
).
|
||||
Order(ent.Desc(alert.FieldCreatedAt), ent.Desc(alert.FieldID)).
|
||||
Limit(20).
|
||||
All(ctx)
|
||||
}
|
||||
|
||||
// truncateUTF8 按字节安全截断:最多保留 maxBytes 字节,且按 rune 边界回退,
|
||||
// 不切断 UTF-8 多字节字符(中文 3 字节/字),避免 ent MaxLen(按字节校验)失败。
|
||||
func truncateUTF8(s string, maxBytes int) string {
|
||||
@@ -199,10 +242,13 @@ func (s *Service) Evaluate(ctx context.Context, typ string, value float64, title
|
||||
}
|
||||
|
||||
func (s *Service) createAlert(ctx context.Context, ruleId int, typ, title, content, refType, refId, orderNo, receiver string) error {
|
||||
_, err := s.ctx.EntClient.Alert.Create().
|
||||
a, err := s.ctx.EntClient.Alert.Create().
|
||||
SetRuleId(ruleId).SetType(typ).SetTitle(truncateUTF8(title, 255)).
|
||||
SetContent(truncateUTF8(content, 500)).SetRefType(refType).SetRefId(refId).
|
||||
SetOrderNo(orderNo).
|
||||
SetReceiver(receiver).SetStatus("UNREAD").Save(ctx)
|
||||
if err == nil {
|
||||
s.publishStationAlert(a) // 规则命中路径同样广播(如 inventory_low 落库后终端可感知)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user