File size: 40,046 Bytes
aec7019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10ec042
1199a79
aec7019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10ec042
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1199a79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aec7019
 
 
 
 
 
1199a79
aec7019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10ec042
1199a79
10ec042
1199a79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10ec042
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aec7019
10ec042
 
 
 
aec7019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10ec042
 
1199a79
aec7019
 
 
 
 
 
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>数据接入配置中心 · 泵站 AI 节能优化</title>
<style>
:root{--bg:#07131f;--surface:#0d1d2c;--panel:#102538;--panel2:#132d43;--line:#24445e;--text:#e9f3fa;--muted:#8fa8ba;--cyan:#43c7d7;--blue:#4a8dff;--green:#38d39f;--amber:#f2c45c;--red:#ff6b78;--shadow:0 18px 50px rgba(0,0,0,.28)}
*{box-sizing:border-box}html,body{margin:0;min-height:100%;font-family:"Segoe UI","Microsoft YaHei",sans-serif;background:var(--bg);color:var(--text)}
body:before{content:"";position:fixed;inset:0;pointer-events:none;background:radial-gradient(circle at 75% 0,rgba(67,199,215,.12),transparent 35%),radial-gradient(circle at 10% 85%,rgba(74,141,255,.09),transparent 30%)}
button,input,select{font:inherit}button{cursor:pointer}.app{display:grid;grid-template-columns:244px 1fr;min-height:100vh;position:relative}
.sidebar{background:rgba(9,27,42,.94);border-right:1px solid var(--line);padding:26px 18px;position:sticky;top:0;height:100vh}
.logo{display:flex;align-items:center;gap:12px;padding:0 8px 24px;border-bottom:1px solid var(--line)}.logo-mark{width:42px;height:42px;border-radius:14px;background:linear-gradient(145deg,var(--cyan),var(--blue));display:grid;place-items:center;color:#05202b;font-weight:900;box-shadow:0 0 28px rgba(67,199,215,.28)}
.logo strong{display:block;font-size:15px}.logo span{font-size:11px;color:var(--muted)}.nav{margin-top:24px;display:grid;gap:8px}.nav button,.back-link{width:100%;border:0;background:transparent;color:var(--muted);padding:12px 13px;border-radius:10px;text-align:left;display:flex;align-items:center;gap:11px;text-decoration:none;font-size:13px}
.nav button:hover,.nav button.active{background:var(--panel);color:var(--text)}.nav button.active{box-shadow:inset 3px 0 var(--cyan)}.num{width:24px;height:24px;border:1px solid var(--line);border-radius:8px;display:grid;place-items:center;font-size:11px}.nav button.active .num{background:var(--cyan);border-color:var(--cyan);color:#08202b;font-weight:700}
.side-bottom{position:absolute;bottom:22px;left:18px;right:18px}.back-link{border:1px solid var(--line);justify-content:center}.back-link:hover{border-color:var(--cyan);color:var(--text)}
.main{min-width:0}.topbar{min-height:82px;border-bottom:1px solid var(--line);display:flex;align-items:center;padding:18px 30px;gap:18px;background:rgba(7,19,31,.82);backdrop-filter:blur(12px);position:sticky;top:0;z-index:20}.topbar h1{font-size:20px;margin:0}.topbar p{margin:4px 0 0;color:var(--muted);font-size:12px}.spacer{flex:1}.status-pill{display:flex;align-items:center;gap:8px;border:1px solid var(--line);background:var(--surface);padding:8px 12px;border-radius:20px;font-size:12px}.dot{width:8px;height:8px;border-radius:50%;background:var(--muted)}.dot.ok{background:var(--green);box-shadow:0 0 10px var(--green)}.dot.warn{background:var(--amber)}.dot.bad{background:var(--red)}
.content{padding:28px 30px 60px;max-width:1500px;margin:0 auto}.view{display:none}.view.active{display:block}.hero{display:grid;grid-template-columns:1.5fr 1fr;gap:18px;margin-bottom:20px}.card{background:linear-gradient(145deg,rgba(16,37,56,.96),rgba(11,29,45,.96));border:1px solid var(--line);border-radius:16px;box-shadow:var(--shadow)}.hero-main{padding:28px;position:relative;overflow:hidden}.hero-main:after{content:"OPC";position:absolute;right:22px;bottom:-24px;font-size:100px;font-weight:800;color:rgba(67,199,215,.055)}.eyebrow{font-size:11px;letter-spacing:1.8px;color:var(--cyan);text-transform:uppercase}.hero h2{font-size:28px;margin:10px 0}.hero p{color:var(--muted);line-height:1.7;margin:0;max-width:700px}.summary{padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:12px}.metric{background:var(--surface);border:1px solid var(--line);border-radius:12px;padding:15px}.metric label{display:block;font-size:11px;color:var(--muted);margin-bottom:7px}.metric strong{font-size:18px}.metric small{font-size:11px;color:var(--muted)}
.section{padding:22px;margin-bottom:18px}.section-head{display:flex;align-items:flex-start;gap:14px;margin-bottom:20px}.section-head h2{font-size:17px;margin:0 0 5px}.section-head p{font-size:12px;color:var(--muted);margin:0}.section-head .actions{margin-left:auto}.form-grid{display:grid;grid-template-columns:repeat(12,1fr);gap:15px}.field{grid-column:span 4}.field.wide{grid-column:span 8}.field.full{grid-column:1/-1}.field.half{grid-column:span 6}.field label{display:flex;justify-content:space-between;font-size:12px;color:var(--muted);margin-bottom:7px}.field input,.field select,.browse-bar input,.toolbar input,.toolbar select{width:100%;background:#091b2a;border:1px solid var(--line);color:var(--text);padding:11px 12px;border-radius:9px;outline:none}.field input:focus,.field select:focus,.browse-bar input:focus,.toolbar input:focus{border-color:var(--cyan);box-shadow:0 0 0 3px rgba(67,199,215,.09)}.hint{font-size:11px;color:var(--muted);margin-top:6px}.mode-switch{display:grid;grid-template-columns:1fr 1fr;gap:12px}.mode{border:1px solid var(--line);background:var(--surface);color:var(--muted);padding:16px;border-radius:12px;text-align:left}.mode strong{display:block;color:var(--text);margin-bottom:5px}.mode.active{border-color:var(--cyan);background:rgba(67,199,215,.08);box-shadow:inset 0 0 0 1px rgba(67,199,215,.18)}.mode.active strong{color:var(--cyan)}
.btn{border:1px solid var(--line);background:var(--panel2);color:var(--text);padding:10px 15px;border-radius:9px}.btn:hover{border-color:var(--cyan)}.btn.primary{border-color:var(--cyan);background:var(--cyan);color:#06232c;font-weight:700}.btn.safe{border-color:var(--green);color:var(--green);background:rgba(56,211,159,.08)}.btn.danger{border-color:rgba(255,107,120,.6);color:var(--red);background:rgba(255,107,120,.06)}.btn:disabled{opacity:.45;cursor:not-allowed}.button-row{display:flex;gap:10px;flex-wrap:wrap}
.result{display:none;margin-top:16px;border:1px solid var(--line);border-radius:12px;padding:15px;background:var(--surface);font-size:13px;line-height:1.65}.result.show{display:block}.result.ok{border-color:rgba(56,211,159,.45)}.result.bad{border-color:rgba(255,107,120,.45)}.result-title{font-weight:700;margin-bottom:5px}.ns-list{display:flex;gap:6px;flex-wrap:wrap;margin-top:9px}.chip{font-size:11px;border:1px solid var(--line);border-radius:15px;padding:4px 8px;color:var(--muted)}
.toolbar,.browse-bar{display:flex;align-items:center;gap:10px;margin-bottom:15px}.toolbar input{max-width:300px}.toolbar select{max-width:160px}.counter{margin-left:auto;color:var(--muted);font-size:12px}.table-wrap{overflow:auto;border:1px solid var(--line);border-radius:12px}.table{width:100%;border-collapse:collapse;min-width:1080px}.table th{position:sticky;top:0;background:#0b2031;color:var(--muted);font-size:11px;text-align:left;padding:11px;border-bottom:1px solid var(--line);z-index:2}.table td{padding:9px 11px;border-bottom:1px solid rgba(36,68,94,.65);font-size:12px}.table tr:hover{background:rgba(67,199,215,.035)}.table tr.selected{background:rgba(67,199,215,.09)}.table input,.table select{width:100%;background:#081a29;border:1px solid transparent;color:var(--text);padding:7px;border-radius:6px}.table input:focus,.table select:focus{border-color:var(--cyan);outline:none}.table input[type=checkbox]{width:auto;accent-color:var(--cyan)}.tag-name{font-weight:600}.required{color:var(--amber);font-size:10px}.write{color:var(--blue);font-size:10px}.row-select{border:1px solid var(--line);background:transparent;color:var(--muted);border-radius:6px;padding:5px 8px}.row-select.active{border-color:var(--cyan);color:var(--cyan)}
.browse-layout{display:grid;grid-template-columns:1fr 1fr;gap:16px}.browser-list{border:1px solid var(--line);border-radius:12px;max-height:480px;overflow:auto}.browser-row{display:grid;grid-template-columns:1fr auto;gap:10px;padding:12px;border-bottom:1px solid var(--line)}.browser-row:hover{background:rgba(67,199,215,.04)}.node-name{font-weight:600;font-size:13px}.node-id{font-size:11px;color:var(--muted);margin-top:4px;word-break:break-all}.node-class{font-size:10px;color:var(--cyan)}.empty{padding:38px;text-align:center;color:var(--muted);font-size:13px}
.preview-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(210px,1fr));gap:12px}.preview-card{padding:16px;background:var(--surface);border:1px solid var(--line);border-radius:12px}.preview-card.bad{border-color:rgba(255,107,120,.45)}.preview-label{font-size:11px;color:var(--muted)}.preview-value{font-size:23px;font-weight:700;margin:9px 0}.preview-meta{font-size:10px;color:var(--muted);word-break:break-all}.toast{position:fixed;right:24px;bottom:24px;min-width:280px;max-width:460px;background:#10283b;border:1px solid var(--line);border-radius:12px;padding:14px 16px;box-shadow:var(--shadow);transform:translateY(120px);opacity:0;transition:.25s;z-index:100}.toast.show{transform:none;opacity:1}.toast.ok{border-color:var(--green)}.toast.bad{border-color:var(--red)}.advanced{display:none}.advanced.show{display:contents}
@media(max-width:980px){.app{grid-template-columns:1fr}.sidebar{display:none}.topbar{padding:15px 18px}.content{padding:18px}.hero{grid-template-columns:1fr}.field,.field.wide,.field.half{grid-column:1/-1}.browse-layout{grid-template-columns:1fr}.topbar .status-pill{display:none}}
</style>
</head>
<body>
<div class="app">
<aside class="sidebar">
  <div class="logo"><div class="logo-mark">B</div><div><strong>数据接入配置中心</strong><span>Biditech Pump Intelligence</span></div></div>
  <nav class="nav">
    <button class="active" data-view="overview"><span class="num">01</span>接入总览</button>
    <button data-view="connection"><span class="num">02</span>OPC 连接</button>
    <button data-view="mapping"><span class="num">03</span>点位映射</button>
    <button data-view="browse"><span class="num">04</span>节点浏览</button>
    <button data-view="preview"><span class="num">05</span>实时预览</button>
    <button data-view="cloud"><span class="num">06</span>云端转发</button>
    <button data-view="brand"><span class="num">07</span>品牌/关于</button>
  </nav>
  <div class="side-bottom"><a class="back-link" href="./index.html">← 返回运行大屏</a></div>
</aside>
<main class="main">
<header class="topbar">
  <div><h1 id="pageTitle">接入总览</h1><p>配置保存在服务器,Node-RED 自动同步,现场无需修改流程文件</p></div>
  <div class="spacer"></div>
  <div class="status-pill"><span class="dot" id="backendDot"></span><span id="backendStatus">正在连接后台</span></div>
  <button class="btn primary" id="saveTop">保存配置</button>
</header>
<div class="content">
<section class="view active" id="view-overview">
  <div class="hero">
    <div class="card hero-main"><div class="eyebrow">Integration Control Center</div><h2>把复杂的 OPC 对接,变成五步配置</h2><p>先选择数据来源,再测试连接、映射点位、预览实时值。当前阶段保持只读,不向 PLC 写入控制命令。</p></div>
    <div class="card summary">
      <div class="metric"><label>数据来源</label><strong id="sumMode">--</strong></div>
      <div class="metric"><label>配置版本</label><strong id="sumRevision">--</strong></div>
      <div class="metric"><label>已配置点位</label><strong id="sumTags">--</strong></div>
      <div class="metric"><label>Node-RED</label><strong id="sumNodeRed">--</strong></div>
    </div>
  </div>
  <div class="card section">
    <div class="section-head"><div><h2>推荐调试顺序</h2><p>PC Server 准备好之后,按顺序完成即可。</p></div></div>
    <div class="form-grid">
      <div class="metric" style="grid-column:span 3"><label>1 · 网络</label><strong>填写 OPC 地址</strong><small>确认边缘 AI 设备能访问 OPC Server</small></div>
      <div class="metric" style="grid-column:span 3"><label>2 · 握手</label><strong>测试连接</strong><small>读取服务器命名空间</small></div>
      <div class="metric" style="grid-column:span 3"><label>3 · 点表</label><strong>映射变量</strong><small>液位、流量及五台泵数据</small></div>
      <div class="metric" style="grid-column:span 3"><label>4 · 验证</label><strong>实时预览</strong><small>核对数值、单位和质量</small></div>
    </div>
  </div>
  <div class="card section">
    <div class="section-head"><div><h2>当前工作模式</h2><p>切到真实 OPC 前,建议先用仿真完成页面和算法验证。</p></div></div>
    <div class="mode-switch">
      <button class="mode active" data-mode="simulation"><strong>仿真数据</strong><span>无需现场服务器,适合界面和算法演示</span></button>
      <button class="mode" data-mode="opcua"><strong>真实 OPC UA</strong><span>由 Node-RED 连接 PC Server 并采集点位</span></button>
    </div>
  </div>
</section>

<section class="view" id="view-connection">
  <div class="card section">
    <div class="section-head"><div><h2>OPC UA 服务器连接</h2><p>第一轮联调建议使用 None;连接成功后再升级证书与加密策略。</p></div><div class="actions"><button class="btn safe" id="testBtn">测试连接</button></div></div>
    <div class="form-grid">
      <div class="field wide"><label>服务器地址 <span>Endpoint URL</span></label><input id="endpoint" placeholder="opc.tcp://192.168.0.20:4840/server/"><div class="hint">这里填写未来 PC Server 的局域网地址;容器内仿真地址为 opc.tcp://opcua-sim:4840/biditech/server/</div></div>
      <div class="field"><label>采样周期 <span>ms</span></label><input id="sampling" type="number" min="200" max="60000" step="100"></div>
      <div class="field"><label>安全策略</label><select id="securityPolicy"><option>None</option><option>Basic256Sha256</option><option>Aes128_Sha256_RsaOaep</option><option>Aes256_Sha256_RsaPss</option></select></div>
      <div class="field"><label>安全模式</label><select id="securityMode"><option>None</option><option>Sign</option><option>SignAndEncrypt</option></select></div>
      <div class="field"><label>连接超时 <span>秒</span></label><input id="timeout" type="number" min="1" max="30" step="1"></div>
      <div class="field"><label>用户名 <span>可选</span></label><input id="username" autocomplete="off"></div>
      <div class="field"><label>密码 <span id="passwordHint">未配置</span></label><input id="password" type="password" autocomplete="new-password" placeholder="留空则保持原密码"></div>
      <div class="field"><label>断线重连 <span>秒</span></label><input id="reconnect" type="number" min="1" max="300"></div>
      <div class="advanced field wide"><label>客户端证书路径</label><input id="certificatePath" placeholder="/certs/client-cert.pem"></div>
      <div class="advanced field"><label>客户端私钥路径</label><input id="privateKeyPath" placeholder="/certs/client-key.pem"></div>
      <div class="advanced field"><label>服务器证书路径</label><input id="serverCertificatePath" placeholder="/certs/server.der"></div>
    </div>
    <div class="result" id="testResult"></div>
  </div>
</section>

<section class="view" id="view-mapping">
  <div class="card section">
    <div class="section-head"><div><h2>标准点位映射</h2><p>系统字段保持不变,只需要把 NodeId 对应到现场 PC Server 的变量。</p></div><div class="actions button-row"><button class="btn" id="previewSelected">读取已启用点位</button></div></div>
    <div class="toolbar"><input id="tagSearch" placeholder="搜索字段、名称或 NodeId"><select id="tagFilter"><option value="all">全部点位</option><option value="required">必需点位</option><option value="writable">可写点位</option><option value="enabled">已启用</option></select><span class="counter" id="tagCounter"></span></div>
    <div class="table-wrap"><table class="table"><thead><tr><th>启用</th><th>系统字段</th><th>现场名称</th><th>NodeId</th><th>类型</th><th>单位</th><th>倍率</th><th>偏移</th><th>映射</th></tr></thead><tbody id="tagBody"></tbody></table></div>
  </div>
</section>

<section class="view" id="view-browse">
  <div class="browse-layout">
    <div class="card section">
      <div class="section-head"><div><h2>浏览 OPC 节点</h2><p>先测试连接,然后逐层进入对象,找到目标变量。</p></div></div>
      <div class="browse-bar"><input id="browseParent" value="i=85" placeholder="父节点 NodeId"><button class="btn primary" id="browseBtn">浏览</button></div>
      <div class="browser-list" id="browseList"><div class="empty">尚未读取节点</div></div>
    </div>
    <div class="card section">
      <div class="section-head"><div><h2>映射目标</h2><p>先在“点位映射”中选择一行,再点击节点旁的“映射”。</p></div></div>
      <div class="metric"><label>当前选中系统字段</label><strong id="selectedTagLabel">未选择</strong><small id="selectedTagKey">请先选择需要映射的点位</small></div>
      <div class="result show" style="margin-top:16px"><div class="result-title">操作说明</div>“进入”用于浏览对象的子节点;“映射”会把该 NodeId 写入当前选中的系统字段。保存前仍可修改或撤销。</div>
    </div>
  </div>
</section>

<section class="view" id="view-preview">
  <div class="card section">
    <div class="section-head"><div><h2>实时数据预览</h2><p>读取前会先保存当前配置。这里只读取,不会写入 PLC。</p></div><div class="actions button-row"><button class="btn safe" id="previewBtn">读取实时值</button></div></div>
    <div class="preview-grid" id="previewGrid"><div class="empty">点击“读取实时值”核对点位、单位和数据质量</div></div>
  </div>
</section>

<section class="view" id="view-cloud">
  <div class="card section">
    <div class="section-head"><div><h2>云端转发(MQTT → InfluxDB)</h2><p>边缘把实时数据(含算好的系统 SEC)经 MQTT 转发到云端 InfluxDB,Grafana 出能效看板;断网自动缓存补传。Node-RED 无需修改,配置在此。</p></div><div class="actions button-row"><button class="btn safe" id="cloudTestBtn">测试连接</button><button class="btn primary" id="cloudSaveBtn">保存</button></div></div>
    <div class="form-grid">
      <div class="field half"><label>启用转发</label><select id="cloudEnabled"><option value="false">停用</option><option value="true">启用</option></select></div>
      <div class="field half"><label>上传周期(秒)</label><input id="cloudInterval" type="number" min="1" value="10"></div>
      <div class="field"><label>MQTT 地址</label><input id="cloudHost" placeholder="mqtt.internal 或内网 IP"></div>
      <div class="field"><label>端口</label><input id="cloudPort" type="number" value="1883"></div>
      <div class="field"><label>TLS</label><select id="cloudTls"><option value="false">否(1883)</option><option value="true">是(8883)</option></select></div>
      <div class="field"><label>用户名</label><input id="cloudUser"></div>
      <div class="field"><label>密码 <span id="cloudPwHint" class="required"></span></label><input id="cloudPw" type="password" placeholder="留空则不修改"></div>
      <div class="field"><label>Client ID</label><input id="cloudClientId" value="biditech-pump-edge"></div>
      <div class="field full"><label>主题 Topic({station} 自动替换为站名)</label><input id="cloudTopic" value="biditech/pump/{station}/telemetry"></div>
      <div class="field half"><label>QoS</label><select id="cloudQos"><option value="0">0</option><option value="1">1</option></select></div>
      <div class="field half"><label>断网缓存上限(条)</label><input id="cloudBuffer" type="number" value="5000"></div>
    </div>
    <div class="hint" id="cloudStatus" style="margin-top:14px"></div>
  </div>
</section>

<section class="view" id="view-brand">
  <div class="card section">
    <div class="section-head"><div><h2>品牌 / 首页信息</h2><p>首页(登录/封面页)与大屏标题展示的水厂名、系统名、公司与联系方式,全部可配。</p></div><div class="actions button-row"><button class="btn primary" id="brandSaveBtn">保存</button></div></div>
    <div class="form-grid">
      <div class="field half"><label>水厂 / 项目名称</label><input id="bPlant" placeholder="如:胶州水厂二期"></div>
      <div class="field half"><label>系统名称</label><input id="bTitle" placeholder="泵站节能优化系统"></div>
      <div class="field full"><label>副标题 / 一句话介绍</label><input id="bSub" placeholder="AI 驱动的泵组能效优化与运行监控"></div>
      <div class="field"><label>公司名称</label><input id="bCompany"></div>
      <div class="field"><label>联系人</label><input id="bPerson"></div>
      <div class="field"><label>联系电话</label><input id="bPhone"></div>
      <div class="field"><label>邮箱</label><input id="bEmail"></div>
      <div class="field"><label>网址</label><input id="bWeb"></div>
      <div class="field"><label>Logo(data:URI,可选)</label><input id="bLogo" placeholder="data:image/png;base64,..."></div>
      <div class="field half"><label>启用登录口令</label><select id="bLogin"><option value="false">否(直接进入)</option><option value="true">是(首页需口令)</option></select></div>
      <div class="field half"><label>登录口令 <span id="bPwHint" class="required"></span></label><input id="bPw" type="password" placeholder="留空则不修改"></div>
    </div>
    <div class="hint" style="margin-top:12px">保存后:首页 <b>/home.html</b>(单容器下为根路径 <b>/</b>)与大屏标题即显示以上信息。</div>
  </div>
</section>
</div>
</main>
</div>
<div class="toast" id="toast"></div>
<script>
var state={config:null,selectedTag:-1,browseNodes:[],status:null};
var titles={overview:"接入总览",connection:"OPC 连接",mapping:"点位映射",browse:"节点浏览",preview:"实时预览",cloud:"云端转发",brand:"品牌 / 关于"};

function esc(value){return String(value==null?"":value).replace(/[&<>"']/g,function(c){return({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"})[c]})}
async function request(path,options){
  var response=await fetch(path,Object.assign({headers:{"Content-Type":"application/json"}},options||{}));
  var data;try{data=await response.json()}catch(e){data={detail:await response.text()}}
  if(!response.ok)throw new Error(typeof data.detail==="string"?data.detail:JSON.stringify(data.detail||data));
  return data;
}
function toast(message,type){
  var el=document.getElementById("toast");el.textContent=message;el.className="toast show "+(type||"ok");
  clearTimeout(window.toastTimer);window.toastTimer=setTimeout(function(){el.className="toast"},3600);
}
function setView(name){
  document.querySelectorAll(".view").forEach(function(el){el.classList.toggle("active",el.id==="view-"+name)});
  document.querySelectorAll(".nav button").forEach(function(el){el.classList.toggle("active",el.dataset.view===name)});
  document.getElementById("pageTitle").textContent=titles[name]||name;
  if(name==="preview")renderPreview([]);
  if(name==="cloud")loadCloud();
  if(name==="brand")loadBrand();
}
async function loadBrand(){try{var b=await request("/api/branding");
  document.getElementById("bPlant").value=b.plant_name||"";document.getElementById("bTitle").value=b.system_title||"";
  document.getElementById("bSub").value=b.subtitle||"";document.getElementById("bCompany").value=b.company||"";
  document.getElementById("bPerson").value=b.contact_person||"";document.getElementById("bPhone").value=b.contact_phone||"";
  document.getElementById("bEmail").value=b.contact_email||"";document.getElementById("bWeb").value=b.website||"";
  document.getElementById("bLogo").value=b.logo_data_uri||"";document.getElementById("bLogin").value=String(!!b.login_enabled);
  document.getElementById("bPw").value="";document.getElementById("bPwHint").textContent=b.password_configured?"已保存":"未配置";
}catch(e){}}
async function saveBrand(){try{var o={plant_name:document.getElementById("bPlant").value.trim(),system_title:document.getElementById("bTitle").value.trim()||"泵站节能优化系统",
  subtitle:document.getElementById("bSub").value.trim(),company:document.getElementById("bCompany").value.trim(),
  contact_person:document.getElementById("bPerson").value.trim(),contact_phone:document.getElementById("bPhone").value.trim(),
  contact_email:document.getElementById("bEmail").value.trim(),website:document.getElementById("bWeb").value.trim(),
  logo_data_uri:document.getElementById("bLogo").value.trim(),login_enabled:document.getElementById("bLogin").value==="true"};
  var pw=document.getElementById("bPw").value;if(pw)o.password=pw;
  await request("/api/branding",{method:"PUT",body:JSON.stringify(o)});toast("品牌信息已保存","ok");loadBrand();
}catch(e){toast(e.message,"bad")}}
function fillCloud(c){
  document.getElementById("cloudEnabled").value=String(!!c.enabled);
  document.getElementById("cloudInterval").value=c.publish_interval_s||10;
  document.getElementById("cloudHost").value=c.mqtt_host||"";
  document.getElementById("cloudPort").value=c.mqtt_port||1883;
  document.getElementById("cloudTls").value=String(!!c.tls);
  document.getElementById("cloudUser").value=c.username||"";
  document.getElementById("cloudPw").value="";
  document.getElementById("cloudPwHint").textContent=c.password_configured?"已安全保存":"未配置";
  document.getElementById("cloudClientId").value=c.client_id||"biditech-pump-edge";
  document.getElementById("cloudTopic").value=c.topic||"biditech/pump/{station}/telemetry";
  document.getElementById("cloudQos").value=String(c.qos||0);
  document.getElementById("cloudBuffer").value=c.buffer_limit||5000;
}
function cloudFromForm(){
  var o={enabled:document.getElementById("cloudEnabled").value==="true",
    publish_interval_s:Number(document.getElementById("cloudInterval").value),
    mqtt_host:document.getElementById("cloudHost").value.trim(),
    mqtt_port:Number(document.getElementById("cloudPort").value),
    tls:document.getElementById("cloudTls").value==="true",
    username:document.getElementById("cloudUser").value.trim(),
    client_id:document.getElementById("cloudClientId").value.trim(),
    topic:document.getElementById("cloudTopic").value.trim(),
    qos:Number(document.getElementById("cloudQos").value),
    buffer_limit:Number(document.getElementById("cloudBuffer").value)};
  var pw=document.getElementById("cloudPw").value;if(pw)o.password=pw;return o;
}
async function loadCloud(){try{fillCloud(await request("/api/cloud/config"));cloudStatus()}catch(e){}}
async function saveCloud(){try{await request("/api/cloud/config",{method:"PUT",body:JSON.stringify(cloudFromForm())});toast("云端转发配置已保存","ok");loadCloud()}catch(e){toast(e.message,"bad")}}
async function testCloud(){try{var d=await request("/api/cloud/test",{method:"POST",body:"{}"});toast(d.success?d.message:d.error,d.success?"ok":"bad")}catch(e){toast(e.message,"bad")}}
async function cloudStatus(){try{var s=await request("/api/cloud/status");document.getElementById("cloudStatus").textContent="状态:转发"+(s.enabled?"已启用":"停用")+" · 连接"+(s.connected?"正常":"未连")+" · 待发缓存 "+s.pending+" 条 · 累计已发 "+s.published+" 条"+(s.last_error?" · 最近错误:"+s.last_error:"")}catch(e){}}
function setMode(mode){
  if(!state.config)return;state.config.source_mode=mode;
  document.querySelectorAll(".mode").forEach(function(el){el.classList.toggle("active",el.dataset.mode===mode)});
  updateSummary();
}
function fillConnection(){
  var c=state.config.connection;
  document.getElementById("endpoint").value=c.endpoint||"";
  document.getElementById("sampling").value=c.sampling_interval_ms||1000;
  document.getElementById("securityPolicy").value=c.security_policy||"None";
  document.getElementById("securityMode").value=c.security_mode||"None";
  document.getElementById("timeout").value=c.timeout_s||5;
  document.getElementById("username").value=c.username||"";
  document.getElementById("password").value="";
  document.getElementById("passwordHint").textContent=c.password_configured?"已安全保存":"未配置";
  document.getElementById("reconnect").value=c.reconnect_interval_s||5;
  document.getElementById("certificatePath").value=c.certificate_path||"";
  document.getElementById("privateKeyPath").value=c.private_key_path||"";
  document.getElementById("serverCertificatePath").value=c.server_certificate_path||"";
  toggleSecurity();
}
function connectionFromForm(){
  var c=Object.assign({},state.config.connection,{
    endpoint:document.getElementById("endpoint").value.trim(),
    sampling_interval_ms:Number(document.getElementById("sampling").value),
    security_policy:document.getElementById("securityPolicy").value,
    security_mode:document.getElementById("securityMode").value,
    timeout_s:Number(document.getElementById("timeout").value),
    username:document.getElementById("username").value.trim(),
    reconnect_interval_s:Number(document.getElementById("reconnect").value),
    certificate_path:document.getElementById("certificatePath").value.trim(),
    private_key_path:document.getElementById("privateKeyPath").value.trim(),
    server_certificate_path:document.getElementById("serverCertificatePath").value.trim()
  });
  var password=document.getElementById("password").value;if(password)c.password=password;else delete c.password;
  return c;
}
function toggleSecurity(){
  var advanced=document.getElementById("securityPolicy").value!=="None";
  document.querySelectorAll(".advanced").forEach(function(el){el.classList.toggle("show",advanced)});
  if(!advanced)document.getElementById("securityMode").value="None";
}
function updateSummary(){
  if(!state.config)return;
  document.getElementById("sumMode").textContent=state.config.source_mode==="opcua"?"真实 OPC":"仿真数据";
  document.getElementById("sumRevision").textContent="R"+state.config.revision;
  document.getElementById("sumTags").textContent=state.config.tags.filter(function(t){return t.enabled&&t.node_id}).length;
  document.getElementById("sumNodeRed").textContent=state.status&&state.status.node_red&&state.status.node_red.reachable?"在线":"待连接";
  document.querySelectorAll(".mode").forEach(function(el){el.classList.toggle("active",el.dataset.mode===state.config.source_mode)});
}
async function loadAll(){
  try{
    var results=await Promise.all([request("/api/integration/config"),request("/api/integration/status")]);
    state.config=results[0];state.status=results[1];fillConnection();renderTags();updateSummary();
    document.getElementById("backendDot").className="dot ok";document.getElementById("backendStatus").textContent="配置后台在线";
  }catch(e){
    document.getElementById("backendDot").className="dot bad";document.getElementById("backendStatus").textContent="后台连接失败";toast(e.message,"bad");
  }
}
async function saveConfig(showMessage){
  if(!state.config)throw new Error("配置尚未加载");
  state.config.connection=connectionFromForm();
  var payload={source_mode:state.config.source_mode,connection:state.config.connection,tags:state.config.tags};
  var saved=await request("/api/integration/config",{method:"PUT",body:JSON.stringify(payload)});
  state.config=saved;fillConnection();renderTags();updateSummary();
  if(showMessage!==false)toast("配置已保存,Node-RED 将在 10 秒内自动同步","ok");
  return saved;
}
async function testConnection(){
  var btn=document.getElementById("testBtn"),box=document.getElementById("testResult");btn.disabled=true;btn.textContent="连接中…";
  box.className="result show";box.innerHTML='<div class="result-title">正在连接 OPC UA 服务器…</div>';
  try{
    var data=await request("/api/integration/opc/test",{method:"POST",body:JSON.stringify({connection:connectionFromForm()})});
    box.className="result show "+(data.success?"ok":"bad");
    if(data.success){
      box.innerHTML='<div class="result-title">✓ '+esc(data.message)+' · '+esc(data.latency_ms)+' ms</div><div>端点:'+esc(data.endpoint)+'</div><div class="ns-list">'+(data.namespaces||[]).map(function(ns,i){return '<span class="chip">ns='+i+' · '+esc(ns)+'</span>'}).join("")+'</div>';
    }else box.innerHTML='<div class="result-title">连接失败</div><div>'+esc(data.error||data.message)+'</div>';
  }catch(e){box.className="result show bad";box.innerHTML='<div class="result-title">测试请求失败</div>'+esc(e.message)}
  finally{btn.disabled=false;btn.textContent="测试连接"}
}
function filteredTags(){
  var q=document.getElementById("tagSearch").value.trim().toLowerCase(),filter=document.getElementById("tagFilter").value;
  return state.config.tags.map(function(t,i){return{t:t,i:i}}).filter(function(x){
    var t=x.t,ok=!q||[t.key,t.label,t.node_id].join(" ").toLowerCase().indexOf(q)>=0;
    if(filter==="required")ok=ok&&t.required;if(filter==="writable")ok=ok&&t.writable;if(filter==="enabled")ok=ok&&t.enabled;return ok;
  });
}
function renderTags(){
  if(!state.config)return;var rows=filteredTags();document.getElementById("tagCounter").textContent=rows.length+" / "+state.config.tags.length+" 个点位";
  document.getElementById("tagBody").innerHTML=rows.map(function(x){
    var t=x.t,i=x.i,flags=(t.required?'<span class="required">必需</span> ':'')+(t.writable?'<span class="write">可写</span>':'');
    return '<tr data-index="'+i+'" class="'+(state.selectedTag===i?"selected":"")+'"><td><input type="checkbox" data-field="enabled" '+(t.enabled?"checked":"")+'></td><td><div class="tag-name">'+esc(t.key)+'</div>'+flags+'</td><td><input data-field="label" value="'+esc(t.label)+'"></td><td style="min-width:270px"><input data-field="node_id" value="'+esc(t.node_id)+'"></td><td><select data-field="data_type">'+["Boolean","Int16","Int32","UInt16","UInt32","Float","Double","String"].map(function(v){return '<option '+(v===t.data_type?"selected":"")+'>'+v+'</option>'}).join("")+'</select></td><td><input data-field="unit" value="'+esc(t.unit)+'"></td><td><input data-field="scale" type="number" step="any" value="'+esc(t.scale)+'"></td><td><input data-field="offset" type="number" step="any" value="'+esc(t.offset)+'"></td><td><button class="row-select '+(state.selectedTag===i?"active":"")+'" data-select="'+i+'">'+(state.selectedTag===i?"已选择":"选择")+'</button></td></tr>';
  }).join("");
}
function selectTag(index){
  state.selectedTag=index;renderTags();var t=state.config.tags[index];
  document.getElementById("selectedTagLabel").textContent=t.label;document.getElementById("selectedTagKey").textContent=t.key+" · "+(t.node_id||"尚未映射");
}
async function browse(){
  var list=document.getElementById("browseList");list.innerHTML='<div class="empty">正在读取节点…</div>';
  try{
    var data=await request("/api/integration/opc/browse",{method:"POST",body:JSON.stringify({node_id:document.getElementById("browseParent").value.trim(),connection:connectionFromForm(),limit:300})});
    if(!data.success)throw new Error(data.error);state.browseNodes=data.nodes;renderBrowse();
  }catch(e){list.innerHTML='<div class="empty" style="color:var(--red)">'+esc(e.message)+'</div>'}
}
function renderBrowse(){
  var list=document.getElementById("browseList");
  if(!state.browseNodes.length){list.innerHTML='<div class="empty">该节点没有子节点</div>';return}
  list.innerHTML=state.browseNodes.map(function(n,i){
    return '<div class="browser-row"><div><div class="node-name">'+esc(n.display_name||n.browse_name)+' <span class="node-class">'+esc(n.node_class)+'</span></div><div class="node-id">'+esc(n.node_id)+'</div></div><div class="button-row"><button class="btn" data-enter="'+i+'">进入</button><button class="btn safe" data-map="'+i+'">映射</button></div></div>';
  }).join("");
}
function mapNode(index){
  if(state.selectedTag<0){toast("请先在“点位映射”选择一个系统字段","bad");return}
  var node=state.browseNodes[index],tag=state.config.tags[state.selectedTag];tag.node_id=node.node_id;renderTags();selectTag(state.selectedTag);toast("已映射:"+tag.label,"ok");
}
async function preview(){
  var btn=document.getElementById("previewBtn");btn.disabled=true;btn.textContent="读取中…";
  try{
    await saveConfig(false);
    var data=await request("/api/integration/opc/read-preview",{method:"POST",body:JSON.stringify({keys:[],connection:connectionFromForm()})});
    if(!data.success)throw new Error(data.error);renderPreview(data.values);toast("读取完成,共 "+data.values.length+" 个点位","ok");
  }catch(e){toast(e.message,"bad");renderPreview([{label:"读取失败",success:false,error:e.message}])}
  finally{btn.disabled=false;btn.textContent="读取实时值"}
}
function renderPreview(values){
  var grid=document.getElementById("previewGrid");if(!values||!values.length){grid.innerHTML='<div class="empty">点击“读取实时值”核对点位、单位和数据质量</div>';return}
  grid.innerHTML=values.map(function(v){return '<div class="preview-card '+(v.success?"":"bad")+'"><div class="preview-label">'+esc(v.label||v.key)+'</div><div class="preview-value">'+(v.success?esc(v.value)+" <small>"+esc(v.unit||"")+"</small>":"读取失败")+'</div><div class="preview-meta">'+esc(v.success?v.quality:v.error)+'</div><div class="preview-meta">'+esc(v.node_id||"")+'</div></div>'}).join("");
}
async function resetConfig(){
  if(!confirm("恢复默认配置?当前配置会保留一个备份版本。"))return;
  try{state.config=await request("/api/integration/config/reset",{method:"POST",body:"{}"});fillConnection();renderTags();updateSummary();toast("已恢复默认配置","ok")}catch(e){toast(e.message,"bad")}
}

document.querySelectorAll(".nav button").forEach(function(b){b.addEventListener("click",function(){setView(b.dataset.view)})});
document.querySelectorAll(".mode").forEach(function(b){b.addEventListener("click",function(){setMode(b.dataset.mode)})});
document.getElementById("saveTop").addEventListener("click",function(){saveConfig(true).catch(function(e){toast(e.message,"bad")})});
document.getElementById("testBtn").addEventListener("click",testConnection);
document.getElementById("securityPolicy").addEventListener("change",toggleSecurity);
document.getElementById("tagSearch").addEventListener("input",renderTags);
document.getElementById("tagFilter").addEventListener("change",renderTags);
document.getElementById("tagBody").addEventListener("change",function(e){
  var row=e.target.closest("tr");if(!row)return;var i=Number(row.dataset.index),field=e.target.dataset.field;if(!field)return;
  var value=e.target.type==="checkbox"?e.target.checked:e.target.value;if(field==="scale"||field==="offset")value=Number(value);state.config.tags[i][field]=value;updateSummary();
});
document.getElementById("tagBody").addEventListener("click",function(e){if(e.target.dataset.select!==undefined)selectTag(Number(e.target.dataset.select))});
document.getElementById("browseBtn").addEventListener("click",browse);
document.getElementById("browseList").addEventListener("click",function(e){if(e.target.dataset.enter!==undefined){var n=state.browseNodes[Number(e.target.dataset.enter)];document.getElementById("browseParent").value=n.node_id;browse()}if(e.target.dataset.map!==undefined)mapNode(Number(e.target.dataset.map))});
document.getElementById("previewBtn").addEventListener("click",preview);
document.getElementById("cloudSaveBtn").addEventListener("click",saveCloud);
document.getElementById("cloudTestBtn").addEventListener("click",testCloud);
document.getElementById("brandSaveBtn").addEventListener("click",saveBrand);
document.getElementById("previewSelected").addEventListener("click",function(){setView("preview");preview()});
loadAll();
</script>
</body>
</html>