feat: 大屏控制可看局域网设备并逐台切页 + 两种新导出(单页成绩 / 单位积分总明细)
## 大屏控制(用户:管理面板只能看到本机显示器,看不到局域网设备;也不能给某块屏单独切页) - 服务端:`POST /api/display/command` 新增可选 `targets []string` —— 空 = 广播(公告语义不变), 非空 = 只发给点名的实例并返回命中数;命中 0 个返回 404 +「目标大屏已离线,请刷新在线列表」 (不落审计、公告也不落库)。`DisplaySender` 签名随之扩展,新增 `Hub.SendCommandToInstances`(与按房间的 SendCommand 并存)。 - 服务端:WS `hello` 新增可选 `screen`,`realtime.Instance` 新增 `Screen`(不带 omitempty, 局域网浏览器恒为空串),这样管理端能把「本机显示器」与「在线实例」精确对上。 - 管理端「大屏控制」:写死的「在线看板」占位换成真实列表(5 秒轮询、页面隐藏停表), 列出实例 id / 在线离线 / 落后 / 屏号或「局域网浏览器」/ 当前页 / 来源 / 心跳; 每行可单独切页;本机显示器卡片补「当前页」与逐屏切页;离线时控件置灰并说明原因。 页面下拉改为共用大屏那份页名清单(管理端两处标签随之变为「个人积分奖牌榜」「分项成绩」)。 ## 导出(用户:保留多页工作簿的同时,再要一种全平铺单页;积分榜明细也要能导出) - 新增 `GET /api/export/all-flat.xlsx` → `赛事成绩总表(单页).xlsx`:一个工作簿一张表, 列 = 大项/小项/组别/名次/号码/单位/运动员-队/成绩/积分/状态/备注;前三列取 `EventSheet` 的结构化字段(不解析标题),后 8 列就是 `eventColumns` 本身(append 拼出, 两处不可能漂移)。**既有「全部」(all.xlsx,多 sheet)一字未动。** - 新增 `GET /api/export/teams.xlsx` → `单位积分总明细.xlsx`:一张带「单位」列的总表 + 每个有明细行的单位一张分表;**无明细行的单位不出分表**(空表没信息量,且会把假删除的 隐藏单位名字重新印进导出件);已隐藏但有流水的单位照常出表(它在榜单上)。 - 积分榜页:页头「导出总明细」、单位明细面板「导出该单位明细」;导出页新增「全部(单页)」。 测试:服务端 12 包全绿 / 347 个测试函数;前端 vue-tsc 通过、184 例全过;Console 18 文件。 docs/ 与 devdocs/ 同步(含新增 Q65–Q70 与两轮进度记录)。
This commit is contained in:
1 parent
793d2ac064
commit
782a38f82f
33 files changed
+2473
-119
No files matched your search
@@ -3,8 +3,10 @@ package realtime
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -390,7 +392,7 @@ func (h *Hub) handleHello(ctx context.Context, c *client, p helloPayload) {
|
||||
if id == "" {
|
||||
id = p.DisplayKey
|
||||
}
|
||||
h.registerDisplay(c, id, p.Page)
|
||||
h.registerDisplay(c, id, p.Page, p.Screen)
|
||||
h.join(c, RoomDisplay)
|
||||
}
|
||||
|
||||
@@ -528,7 +530,7 @@ func (h *Hub) SendCommandTo(room string, cmd DisplayCommand) {
|
||||
return
|
||||
}
|
||||
if cmd.Action == CmdGoto && (room == "" || room == RoomDisplay) {
|
||||
h.setInstancePage(pageFromPayload(cmd.Payload))
|
||||
h.setInstancePage(pageFromPayload(cmd.Payload), nil)
|
||||
}
|
||||
|
||||
h.sendMu.Lock()
|
||||
@@ -544,6 +546,62 @@ func (h *Hub) SendCommandTo(room string, cmd DisplayCommand) {
|
||||
}
|
||||
}
|
||||
|
||||
// SendCommandToInstances 按实例定向下发 display:command,返回命中的实例数。
|
||||
//
|
||||
// targets 是要控制的 instanceID(GET /api/display/instances 里的 id);为空时退化成广播,
|
||||
// 公告这类全屏指令靠的就是广播,这条语义不能改。已离线的实例不计入命中,
|
||||
// 调用方据此告诉现场「那块屏没收到」。非法 action 返回错误而不是静默丢弃,
|
||||
// 定向指令需要一个明确的失败信号。
|
||||
func (h *Hub) SendCommandToInstances(targets []string, cmd DisplayCommand) (int, error) {
|
||||
if !ValidDisplayAction(cmd.Action) {
|
||||
return 0, fmt.Errorf("未知的大屏指令 %q", cmd.Action)
|
||||
}
|
||||
if len(targets) == 0 {
|
||||
h.SendCommand(cmd)
|
||||
return h.onlineDisplayCount(), nil
|
||||
}
|
||||
|
||||
wanted := make(map[string]bool, len(targets))
|
||||
for _, id := range targets {
|
||||
if id = strings.TrimSpace(id); id != "" {
|
||||
wanted[id] = true
|
||||
}
|
||||
}
|
||||
|
||||
h.sendMu.Lock()
|
||||
matched := make(map[string]bool, len(wanted))
|
||||
for _, c := range h.roomClients(RoomDisplay) {
|
||||
c.mu.Lock()
|
||||
greeted, id := c.greeted, c.instanceID
|
||||
c.mu.Unlock()
|
||||
if !greeted || !wanted[id] {
|
||||
continue
|
||||
}
|
||||
c.sendEvent(EventDisplayCommand, cmd)
|
||||
matched[id] = true
|
||||
}
|
||||
h.sendMu.Unlock()
|
||||
|
||||
if cmd.Action == CmdGoto {
|
||||
h.setInstancePage(pageFromPayload(cmd.Payload), targets)
|
||||
}
|
||||
return len(matched), nil
|
||||
}
|
||||
|
||||
// onlineDisplayCount 统计已握手且在线的实例数,用于告知调用方广播命中了几块屏。
|
||||
func (h *Hub) onlineDisplayCount() int {
|
||||
seen := make(map[string]bool)
|
||||
for _, c := range h.roomClients(RoomDisplay) {
|
||||
c.mu.Lock()
|
||||
greeted, id := c.greeted, c.instanceID
|
||||
c.mu.Unlock()
|
||||
if greeted && id != "" {
|
||||
seen[id] = true
|
||||
}
|
||||
}
|
||||
return len(seen)
|
||||
}
|
||||
|
||||
// checkSlow 用 ack 版本差识别慢客户端;超阈值直接断开,由客户端重连后补齐。
|
||||
func (h *Hub) checkSlow(c *client, version int64) {
|
||||
lag := h.slowLagValue()
|
||||
|
||||
@@ -749,3 +749,162 @@ func TestGuardLoopRecoversPanic(t *testing.T) {
|
||||
t.Fatalf("panic 后关闭码应为 %d,得到 %d", websocket.StatusInternalError, c.closeCode)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 按实例定向下发 ----------
|
||||
|
||||
// instancePage 取注册表里某个实例的当前页;实例不存在返回空串。
|
||||
func instancePage(insts []Instance, id string) string {
|
||||
for _, st := range insts {
|
||||
if st.ID == id {
|
||||
return st.Page
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 定向下发只发给被点名的在线实例;注册表里的「当前页」也只更新那一块屏。
|
||||
func TestSendCommandTargetsSingleInstance(t *testing.T) {
|
||||
env := newTestEnv(t)
|
||||
a := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "screen-a", Page: "standings", Screen: "1"})
|
||||
b := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "screen-b", Page: "standings", Screen: "2"})
|
||||
a.recvEvent(EventSnapshot)
|
||||
b.recvEvent(EventSnapshot)
|
||||
|
||||
cmd, err := NewDisplayCommand(CmdGoto, map[string]string{"page": "medals"})
|
||||
if err != nil {
|
||||
t.Fatalf("构造 goto: %v", err)
|
||||
}
|
||||
matched, err := env.hub.SendCommandToInstances([]string{"screen-a"}, cmd)
|
||||
if err != nil {
|
||||
t.Fatalf("定向下发: %v", err)
|
||||
}
|
||||
if matched != 1 {
|
||||
t.Fatalf("命中实例数应为 1,得到 %d", matched)
|
||||
}
|
||||
if got := decode[DisplayCommand](t, a.recvEvent(EventDisplayCommand)); got.Action != CmdGoto {
|
||||
t.Fatalf("被点名的实例应收到 goto,得到 %s", got.Action)
|
||||
}
|
||||
// 没被点名的实例一条都不能收到(心跳之外的静默是这条断言的全部意义)。
|
||||
if ev, _, ok := b.recvWithin(200 * time.Millisecond); ok {
|
||||
t.Fatalf("未被点名的实例不该收到消息,得到 %s", ev)
|
||||
}
|
||||
|
||||
insts := env.hub.Instances()
|
||||
if got := instancePage(insts, "screen-a"); got != "medals" {
|
||||
t.Fatalf("被点名实例的当前页应更新为 medals,得到 %q", got)
|
||||
}
|
||||
if got := instancePage(insts, "screen-b"); got != "standings" {
|
||||
t.Fatalf("未被点名实例的当前页不该变,得到 %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// targets 为空保持广播语义:所有在线实例都收到。
|
||||
func TestSendCommandEmptyTargetsBroadcasts(t *testing.T) {
|
||||
env := newTestEnv(t)
|
||||
a := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "screen-a"})
|
||||
b := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "screen-b"})
|
||||
a.recvEvent(EventSnapshot)
|
||||
b.recvEvent(EventSnapshot)
|
||||
|
||||
cmd, err := NewDisplayCommand(CmdLock, map[string]any{})
|
||||
if err != nil {
|
||||
t.Fatalf("构造 lock: %v", err)
|
||||
}
|
||||
matched, err := env.hub.SendCommandToInstances(nil, cmd)
|
||||
if err != nil {
|
||||
t.Fatalf("广播: %v", err)
|
||||
}
|
||||
if matched != 2 {
|
||||
t.Fatalf("广播应命中 2 个在线实例,得到 %d", matched)
|
||||
}
|
||||
for _, c := range []*wsClient{a, b} {
|
||||
if got := decode[DisplayCommand](t, c.recvEvent(EventDisplayCommand)); got.Action != CmdLock {
|
||||
t.Fatalf("广播时每个实例都应收到 lock,得到 %s", got.Action)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 目标全都不在线(已断开或 id 不存在):命中 0,且没有任何连接收到指令。
|
||||
func TestSendCommandTargetsOfflineInstances(t *testing.T) {
|
||||
env := newTestEnv(t)
|
||||
a := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "screen-a"})
|
||||
a.recvEvent(EventSnapshot)
|
||||
a.close()
|
||||
waitFor(t, "实例离线", func() bool {
|
||||
insts := env.hub.Instances()
|
||||
return len(insts) == 1 && !insts[0].Online
|
||||
})
|
||||
|
||||
cmd, err := NewDisplayCommand(CmdReload, map[string]any{})
|
||||
if err != nil {
|
||||
t.Fatalf("构造 reload: %v", err)
|
||||
}
|
||||
matched, err := env.hub.SendCommandToInstances([]string{"screen-a", "never-seen"}, cmd)
|
||||
if err != nil {
|
||||
t.Fatalf("定向下发: %v", err)
|
||||
}
|
||||
if matched != 0 {
|
||||
t.Fatalf("离线/不存在的实例不该计入命中,得到 %d", matched)
|
||||
}
|
||||
}
|
||||
|
||||
// 目标里混了在线与离线:在线的收到,命中数只算在线的。
|
||||
func TestSendCommandTargetsMixedOnlineOffline(t *testing.T) {
|
||||
env := newTestEnv(t)
|
||||
a := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "screen-a"})
|
||||
b := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "screen-b"})
|
||||
a.recvEvent(EventSnapshot)
|
||||
b.recvEvent(EventSnapshot)
|
||||
b.close()
|
||||
waitFor(t, "screen-b 离线", func() bool {
|
||||
for _, st := range env.hub.Instances() {
|
||||
if st.ID == "screen-b" {
|
||||
return !st.Online
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
cmd, err := NewDisplayCommand(CmdGoto, map[string]string{"page": "results"})
|
||||
if err != nil {
|
||||
t.Fatalf("构造 goto: %v", err)
|
||||
}
|
||||
matched, err := env.hub.SendCommandToInstances([]string{"screen-a", "screen-b"}, cmd)
|
||||
if err != nil {
|
||||
t.Fatalf("定向下发: %v", err)
|
||||
}
|
||||
if matched != 1 {
|
||||
t.Fatalf("只应在线的 screen-a 计入命中,得到 %d", matched)
|
||||
}
|
||||
if got := decode[DisplayCommand](t, a.recvEvent(EventDisplayCommand)); got.Action != CmdGoto {
|
||||
t.Fatalf("在线的实例应收到 goto,得到 %s", got.Action)
|
||||
}
|
||||
}
|
||||
|
||||
// 非法 action 走定向通道要报错,不能静默丢掉。
|
||||
func TestSendCommandTargetsRejectsUnknownAction(t *testing.T) {
|
||||
env := newTestEnv(t)
|
||||
if _, err := env.hub.SendCommandToInstances([]string{"screen-a"}, DisplayCommand{Action: "nope"}); err == nil {
|
||||
t.Fatal("非法 action 应报错")
|
||||
}
|
||||
}
|
||||
|
||||
// 屏号上报:Console 窗口带 screen,局域网浏览器不带(空串),两者都要能查出来。
|
||||
func TestDisplayInstancesReportScreen(t *testing.T) {
|
||||
env := newTestEnv(t)
|
||||
console := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "console-1", Page: "standings", Screen: "2"})
|
||||
lan := env.dial(t, &helloPayload{Role: RoleDisplay, InstanceID: "lan-1", Page: "medals"})
|
||||
console.recvEvent(EventSnapshot)
|
||||
lan.recvEvent(EventSnapshot)
|
||||
|
||||
byID := make(map[string]Instance)
|
||||
for _, st := range env.hub.Instances() {
|
||||
byID[st.ID] = st
|
||||
}
|
||||
if got := byID["console-1"].Screen; got != "2" {
|
||||
t.Fatalf("Console 窗口的屏号应为 2,得到 %q", got)
|
||||
}
|
||||
if got := byID["lan-1"].Screen; got != "" {
|
||||
t.Fatalf("局域网浏览器不带屏号,应为空串,得到 %q", got)
|
||||
}
|
||||
}
|
||||
@@ -25,12 +25,17 @@ type Instance struct {
|
||||
RemoteAddr string `json:"remoteAddr,omitempty"`
|
||||
AckVersion int64 `json:"ackVersion"`
|
||||
Slow bool `json:"slow"`
|
||||
// Screen 是 Console 分配的屏号(URL 里的 screen=<display.id>),
|
||||
// 管理端靠它把「本机显示器」与「在线实例」对上;局域网浏览器直接开 /display 时为空串。
|
||||
// 用字符串而不是数字:显示器的 id 由 Electron 给,不保证是小的正整数。
|
||||
Screen string `json:"screen"`
|
||||
}
|
||||
|
||||
// instanceState 是注册表的内部记录;同一 instanceId 的多条连接合并成一个实例。
|
||||
type instanceState struct {
|
||||
id string
|
||||
page string
|
||||
screen string
|
||||
connectedAt time.Time
|
||||
lastHeartbeat time.Time
|
||||
remoteAddr string
|
||||
@@ -41,7 +46,7 @@ type instanceState struct {
|
||||
|
||||
// registerDisplay 把连接挂到大屏实例上;未带 instanceId 时由服务端生成,
|
||||
// 保证「刷新页面 = 同一实例」之外的连接也能被登记和查询。
|
||||
func (h *Hub) registerDisplay(c *client, id, page string) string {
|
||||
func (h *Hub) registerDisplay(c *client, id, page, screen string) string {
|
||||
if id == "" {
|
||||
id = "display-" + randomID()
|
||||
}
|
||||
@@ -71,6 +76,9 @@ func (h *Hub) registerDisplay(c *client, id, page string) string {
|
||||
if page != "" {
|
||||
st.page = page
|
||||
}
|
||||
if screen != "" {
|
||||
st.screen = screen
|
||||
}
|
||||
st.lastHeartbeat = now
|
||||
st.remoteAddr = c.remoteAddr
|
||||
return id
|
||||
@@ -122,16 +130,30 @@ func (h *Hub) touchInstance(c *client, now time.Time) {
|
||||
}
|
||||
|
||||
// setInstancePage 记录服务端刚下发的目标页,让注册表里的「当前页」跟得上 goto 指令。
|
||||
func (h *Hub) setInstancePage(page string) {
|
||||
// targets 为空表示广播(所有在线实例),否则只更新被点名的那几个实例——定向切页后
|
||||
// 每块屏的「当前页」必须各算各的。
|
||||
func (h *Hub) setInstancePage(page string, targets []string) {
|
||||
if page == "" {
|
||||
return
|
||||
}
|
||||
var picked map[string]bool
|
||||
if len(targets) > 0 {
|
||||
picked = make(map[string]bool, len(targets))
|
||||
for _, id := range targets {
|
||||
picked[id] = true
|
||||
}
|
||||
}
|
||||
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
for _, st := range h.instances {
|
||||
if st.conns > 0 {
|
||||
st.page = page
|
||||
if st.conns == 0 {
|
||||
continue
|
||||
}
|
||||
if picked != nil && !picked[st.id] {
|
||||
continue
|
||||
}
|
||||
st.page = page
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,6 +200,7 @@ func (h *Hub) Instances() []Instance {
|
||||
RemoteAddr: st.remoteAddr,
|
||||
AckVersion: st.ackVersion,
|
||||
Slow: st.slow,
|
||||
Screen: st.screen,
|
||||
})
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
||||
|
||||
@@ -90,14 +90,16 @@ func encode(event string, data any) ([]byte, error) {
|
||||
//
|
||||
// role 只是「申请」:服务端以会话身份为准,未登录连接申请 admin 会被拒绝,
|
||||
// 因此这里没有 userId —— 定向房间的 userId 只能来自会话。
|
||||
// instanceId / page 是协议字段之外的可选扩展:注册表要记「谁在线、在哪一页」,
|
||||
// 老客户端不带这两个字段也能正常工作。
|
||||
// instanceId / page / screen 是协议字段之外的可选扩展:注册表要记「谁在线、在哪一页、哪块屏」,
|
||||
// 老客户端不带这几个字段也能正常工作。
|
||||
type helloPayload struct {
|
||||
Role string `json:"role"`
|
||||
LastVersion *int64 `json:"lastVersion,omitempty"`
|
||||
DisplayKey string `json:"displayKey,omitempty"`
|
||||
InstanceID string `json:"instanceId,omitempty"`
|
||||
Page string `json:"page,omitempty"`
|
||||
// Screen 是 Console 从 URL 带下来的屏号(screen=<display.id>),局域网浏览器留空。
|
||||
Screen string `json:"screen,omitempty"`
|
||||
}
|
||||
|
||||
// ackPayload 是 C→S 的 ack 载荷;服务端据此识别慢客户端。
|
||||
|
||||
Reference in new issue
Block a user