chenyc
2026-05-20 c8ba0f92b3f84273a78f06de25359db20c1b2a4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const fs = require('fs');
const http = require('http');
const path = require('path');
 
const CONTENT_TYPES = {
  '.html': 'text/html; charset=utf-8',
  '.css': 'text/css; charset=utf-8',
  '.js': 'application/javascript; charset=utf-8',
  '.json': 'application/json; charset=utf-8',
};
 
function getDashboardDir(options = {}) {
  if (options.dashboardDir) {
    return options.dashboardDir;
  }
 
  if (process.pkg) {
    return path.join(path.dirname(process.execPath), 'runtime', 'dashboard');
  }
 
  return path.join(__dirname, 'dashboard');
}
 
function nowIso() {
  return new Date().toISOString();
}
 
function buildDashboardSnapshot({ devices = [], cache, tcpService, config = {} }) {
  const connectionMap = new Map();
  const cacheMap = cache && typeof cache.getSnapshotMap === 'function'
    ? cache.getSnapshotMap()
    : new Map();
  const staleDataMs = Number(config.staleDataMs) > 0 ? Number(config.staleDataMs) : 180000;
  const now = Date.now();
 
  if (tcpService && typeof tcpService.getConnectionSnapshot === 'function') {
    for (const item of tcpService.getConnectionSnapshot()) {
      connectionMap.set(item.deviceId, item);
    }
  }
 
  const deviceSnapshots = devices.map((device) => {
    const connection = connectionMap.get(device.deviceId) || null;
    const cacheEntry = cacheMap.get(device.deviceId) || null;
    const lastUpdateAt = cacheEntry ? cacheEntry.lastUpdateAt : 0;
    const ageMs = lastUpdateAt > 0 ? now - lastUpdateAt : null;
    const dataStatus = !lastUpdateAt
      ? 'waiting'
      : (ageMs <= staleDataMs ? 'active' : 'stale');
 
    return {
      deviceId: device.deviceId,
      name: device.name || device.deviceId,
      ip: device.ip,
      online: Boolean(connection),
      dataStatus,
      dataAgeMs: ageMs,
      connectedAt: connection ? connection.connectedAt : 0,
      lastSocketDataAt: connection ? connection.lastDataAt : 0,
      lastUpdateAt,
      lastRealtimeAt: cacheEntry ? cacheEntry.lastRealtimeAt : 0,
      lastBloodPressureAt: cacheEntry ? cacheEntry.lastBloodPressureAt : 0,
      payload: cacheEntry ? cacheEntry.payload : {},
    };
  });
 
  const onlineCount = deviceSnapshots.filter((item) => item.online).length;
  const activeCount = deviceSnapshots.filter((item) => item.dataStatus === 'active').length;
  const waitingCount = deviceSnapshots.filter((item) => item.dataStatus === 'waiting').length;
  const staleCount = deviceSnapshots.filter((item) => item.dataStatus === 'stale').length;
 
  return {
    title: config.title || 'JH2028 设备中央监测大屏',
    generatedAt: now,
    generatedAtText: nowIso(),
    totals: {
      devices: deviceSnapshots.length,
      online: onlineCount,
      offline: deviceSnapshots.length - onlineCount,
      active: activeCount,
      waiting: waitingCount,
      stale: staleCount,
    },
    devices: deviceSnapshots,
  };
}
 
class DashboardService {
  constructor(options = {}) {
    this.config = options.config || {};
    this.logger = options.logger || console;
    this.getSnapshot = typeof options.getSnapshot === 'function' ? options.getSnapshot : () => ({ devices: [] });
    this.dashboardDir = getDashboardDir(options);
    this.server = null;
    this.clients = new Set();
  }
 
  async start() {
    if (this.config.enabled === false) {
      this.logger.info('[DASHBOARD] 大屏服务未启用');
      return;
    }
 
    this.server = http.createServer((request, response) => {
      this.handleRequest(request, response);
    });
 
    await new Promise((resolve, reject) => {
      this.server.once('listening', resolve);
      this.server.once('error', reject);
      this.server.listen({
        host: this.config.host || '0.0.0.0',
        port: this.config.port || 9100,
      });
    });
 
    const host = this.config.host || '0.0.0.0';
    const port = this.config.port || 9100;
    const localUrl = `http://127.0.0.1:${port}`;
    const listenText = host === '0.0.0.0'
      ? '监听所有网卡,局域网访问请使用服务器真实 IP'
      : `监听地址=${host}`;
 
    this.logger.info(`[DASHBOARD] 大屏服务已启动 ${listenText} 本机访问=${localUrl}`);
  }
 
  async stop() {
    for (const client of this.clients) {
      client.end();
    }
 
    this.clients.clear();
 
    if (!this.server) {
      return;
    }
 
    await new Promise((resolve) => {
      this.server.close(resolve);
    });
    this.server = null;
    this.logger.info('[DASHBOARD] 大屏服务已停止');
  }
 
  handleRequest(request, response) {
    const url = new URL(request.url, 'http://localhost');
 
    if (url.pathname === '/api/snapshot') {
      this.sendJson(response, this.getSnapshot());
      return;
    }
 
    if (url.pathname === '/events') {
      this.handleEvents(response);
      return;
    }
 
    const pathname = url.pathname === '/' ? '/index.html' : url.pathname;
    this.serveStatic(pathname, response);
  }
 
  handleEvents(response) {
    response.writeHead(200, {
      'Content-Type': 'text/event-stream; charset=utf-8',
      'Cache-Control': 'no-cache',
      Connection: 'keep-alive',
      'Access-Control-Allow-Origin': '*',
    });
    response.write('\n');
    this.clients.add(response);
    this.writeEvent(response, this.getSnapshot());
 
    response.on('close', () => {
      this.clients.delete(response);
    });
  }
 
  broadcastSnapshot() {
    const snapshot = this.getSnapshot();
 
    for (const client of this.clients) {
      this.writeEvent(client, snapshot);
    }
  }
 
  writeEvent(response, snapshot) {
    response.write(`event: snapshot\n`);
    response.write(`data: ${JSON.stringify(snapshot)}\n\n`);
  }
 
  sendJson(response, payload) {
    response.writeHead(200, {
      'Content-Type': 'application/json; charset=utf-8',
      'Cache-Control': 'no-cache',
    });
    response.end(JSON.stringify(payload));
  }
 
  serveStatic(pathname, response) {
    const normalizedPath = path.normalize(pathname.replace(/^\/+/, ''));
 
    if (path.isAbsolute(normalizedPath) || normalizedPath.startsWith('..')) {
      response.writeHead(403);
      response.end('Forbidden');
      return;
    }
 
    const filePath = path.join(this.dashboardDir, normalizedPath);
 
    if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
      response.writeHead(404);
      response.end('Not Found');
      return;
    }
 
    const extension = path.extname(filePath).toLowerCase();
    response.writeHead(200, {
      'Content-Type': CONTENT_TYPES[extension] || 'application/octet-stream',
    });
    fs.createReadStream(filePath).pipe(response);
  }
}
 
module.exports = {
  DashboardService,
  buildDashboardSnapshot,
  getDashboardDir,
};