app.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. const parseResponse = async (r) => {
  2. const contentType = r.headers.get('content-type') || '';
  3. if (contentType.includes('application/json')) {
  4. return r.json();
  5. }
  6. const text = await r.text();
  7. return text ? { text } : {};
  8. };
  9. const API = {
  10. get: async (url) => {
  11. const r = await fetch(url);
  12. if (!r.ok) {
  13. const text = await r.text();
  14. throw new Error(`${url}: ${r.status} ${text}`);
  15. }
  16. return parseResponse(r);
  17. },
  18. post: async (url, body = {}) => {
  19. const r = await fetch(url, {
  20. method: 'POST',
  21. headers: { 'Content-Type': 'application/json' },
  22. body: JSON.stringify(body)
  23. });
  24. if (!r.ok) {
  25. const text = await r.text();
  26. throw new Error(`${url}: ${r.status} ${text}`);
  27. }
  28. return parseResponse(r);
  29. },
  30. del: async (url) => {
  31. const r = await fetch(url, { method: 'DELETE' });
  32. if (!r.ok) {
  33. const text = await r.text();
  34. throw new Error(`${url}: ${r.status} ${text}`);
  35. }
  36. return parseResponse(r);
  37. }
  38. };
  39. let currentGroup = null;
  40. let scanPollInterval = null;
  41. let controlsGloballyDisabled = false;
  42. let tempPreview = null;
  43. let selectedSampleEl = null;
  44. function log(msg) {
  45. const panel = document.getElementById('log-panel');
  46. const line = document.createElement('div');
  47. line.textContent = `${new Date().toLocaleTimeString()} ${msg}`;
  48. panel.appendChild(line);
  49. while (panel.children.length > 200) {
  50. panel.removeChild(panel.firstChild);
  51. }
  52. panel.scrollTop = panel.scrollHeight;
  53. }
  54. function setStatus(msg) {
  55. document.getElementById('status').textContent = `状态:${msg}`;
  56. }
  57. function setControlsDisabled(disabled) {
  58. controlsGloballyDisabled = disabled;
  59. ['btn-scan', 'btn-poll-start', 'btn-poll-stop', 'btn-preview', 'btn-add'].forEach(id => {
  60. const el = document.getElementById(id);
  61. if (el) el.disabled = disabled;
  62. });
  63. ['inp-pan', 'inp-tilt', 'inp-zoom', 'inp-dwell'].forEach(id => {
  64. const el = document.getElementById(id);
  65. if (el) el.disabled = disabled;
  66. });
  67. }
  68. async function withDisabled(id, fn) {
  69. const el = document.getElementById(id);
  70. el.disabled = true;
  71. try {
  72. return await fn();
  73. } finally {
  74. if (!controlsGloballyDisabled) {
  75. el.disabled = false;
  76. }
  77. }
  78. }
  79. function parseStrictFloat(value, name) {
  80. const num = Number(value);
  81. if (!Number.isFinite(num)) {
  82. throw new Error(`${name} 必须是有效数字`);
  83. }
  84. return num;
  85. }
  86. function escapeHtml(text) {
  87. const div = document.createElement('div');
  88. div.textContent = text;
  89. return div.innerHTML;
  90. }
  91. function setSelectedPreview(url) {
  92. const img = document.getElementById('selected-preview');
  93. if (url) {
  94. img.src = url;
  95. img.style.display = 'block';
  96. } else {
  97. img.src = '';
  98. img.style.display = 'none';
  99. }
  100. }
  101. function selectSample(sample) {
  102. document.getElementById('inp-pan').value = sample.pan.toFixed(2);
  103. document.getElementById('inp-tilt').value = sample.tilt.toFixed(2);
  104. document.getElementById('inp-zoom').value = sample.zoom;
  105. tempPreview = { path: sample.thumbnail };
  106. setSelectedPreview(`/api/sample-image?path=${encodeURIComponent(sample.thumbnail)}`);
  107. if (sampleCanvas) sampleCanvas.setSelected(sample.pan, sample.tilt);
  108. }
  109. class SampleCanvas {
  110. constructor(canvasId, wrapperId) {
  111. this.canvas = document.getElementById(canvasId);
  112. this.wrapper = document.getElementById(wrapperId);
  113. this.ctx = this.canvas.getContext('2d');
  114. this.samples = [];
  115. this.pans = [];
  116. this.tilts = [];
  117. this.sampleMap = new Map();
  118. this.images = new Map();
  119. this.cellW = 160;
  120. this.cellH = 120;
  121. this.captionH = 20;
  122. this.scale = 1;
  123. this.offsetX = 0;
  124. this.offsetY = 0;
  125. this.selectedPan = null;
  126. this.selectedTilt = null;
  127. this.isDragging = false;
  128. this.dragStart = { x: 0, y: 0, ox: 0, oy: 0 };
  129. this.pendingDraw = false;
  130. this.resize();
  131. window.addEventListener('resize', () => this.resize());
  132. this.setupEvents();
  133. }
  134. resize() {
  135. const rect = this.wrapper.getBoundingClientRect();
  136. this.canvas.width = rect.width;
  137. this.canvas.height = rect.height;
  138. this.draw();
  139. }
  140. setSamples(samples) {
  141. this.samples = samples || [];
  142. this.pans = Array.from(new Set(this.samples.map(s => s.pan))).sort((a, b) => a - b);
  143. this.tilts = Array.from(new Set(this.samples.map(s => s.tilt))).sort((a, b) => b - a);
  144. this.sampleMap = new Map();
  145. this.samples.forEach(s => this.sampleMap.set(`${s.pan},${s.tilt}`, s));
  146. this.images = new Map();
  147. this.samples.forEach(s => {
  148. const img = new Image();
  149. img.crossOrigin = 'anonymous';
  150. img.src = `/api/sample-image?path=${encodeURIComponent(s.thumbnail)}`;
  151. img.onload = () => this.draw();
  152. this.images.set(`${s.pan},${s.tilt}`, img);
  153. });
  154. this.fitToView();
  155. this.draw();
  156. }
  157. contentWidth() {
  158. return this.pans.length * this.cellW;
  159. }
  160. contentHeight() {
  161. return this.tilts.length * (this.cellH + this.captionH);
  162. }
  163. fitToView() {
  164. const pw = this.canvas.width / this.contentWidth();
  165. const ph = this.canvas.height / this.contentHeight();
  166. this.scale = Math.min(pw, ph, 1);
  167. this.offsetX = (this.canvas.width - this.contentWidth() * this.scale) / 2;
  168. this.offsetY = (this.canvas.height - this.contentHeight() * this.scale) / 2;
  169. this.updateZoomLabel();
  170. }
  171. resetView() {
  172. this.fitToView();
  173. this.draw();
  174. }
  175. updateZoomLabel() {
  176. const label = document.getElementById('zoom-level');
  177. if (label) label.textContent = `${Math.round(this.scale * 100)}%`;
  178. }
  179. setupEvents() {
  180. this.canvas.addEventListener('wheel', (e) => {
  181. e.preventDefault();
  182. const rect = this.canvas.getBoundingClientRect();
  183. const mx = e.clientX - rect.left;
  184. const my = e.clientY - rect.top;
  185. const factor = e.deltaY < 0 ? 1.1 : 0.9;
  186. const newScale = Math.max(0.1, Math.min(5.0, this.scale * factor));
  187. this.offsetX = mx - (mx - this.offsetX) * (newScale / this.scale);
  188. this.offsetY = my - (my - this.offsetY) * (newScale / this.scale);
  189. this.scale = newScale;
  190. this.updateZoomLabel();
  191. this.draw();
  192. }, { passive: false });
  193. this.canvas.addEventListener('mousedown', (e) => {
  194. if (e.button !== 0) return;
  195. this.isDragging = false;
  196. this.dragMoved = false;
  197. this.dragStart = { x: e.clientX, y: e.clientY, ox: this.offsetX, oy: this.offsetY };
  198. this.wrapper.style.cursor = 'grabbing';
  199. });
  200. window.addEventListener('mousemove', (e) => {
  201. if (this.dragStart == null) return;
  202. const dx = e.clientX - this.dragStart.x;
  203. const dy = e.clientY - this.dragStart.y;
  204. if (!this.isDragging && (Math.abs(dx) > 3 || Math.abs(dy) > 3)) {
  205. this.isDragging = true;
  206. this.dragMoved = true;
  207. }
  208. if (this.isDragging) {
  209. this.offsetX = this.dragStart.ox + dx;
  210. this.offsetY = this.dragStart.oy + dy;
  211. this.draw();
  212. }
  213. });
  214. window.addEventListener('mouseup', () => {
  215. this.dragStart = null;
  216. if (this.isDragging) {
  217. this.isDragging = false;
  218. this.wrapper.style.cursor = 'grab';
  219. }
  220. });
  221. this.canvas.addEventListener('click', (e) => {
  222. if (this.isDragging || this.dragMoved) return;
  223. const rect = this.canvas.getBoundingClientRect();
  224. const mx = e.clientX - rect.left;
  225. const my = e.clientY - rect.top;
  226. const worldX = (mx - this.offsetX) / this.scale;
  227. const worldY = (my - this.offsetY) / this.scale;
  228. const rowH = this.cellH + this.captionH;
  229. // 使用最近单元格,避免边界处 floor 导致点不到
  230. const col = Math.round((worldX - this.cellW / 2) / this.cellW);
  231. const row = Math.round((worldY - rowH / 2) / rowH);
  232. if (col < 0 || col >= this.pans.length || row < 0 || row >= this.tilts.length) return;
  233. const pan = this.pans[col];
  234. const tilt = this.tilts[row];
  235. const s = this.sampleMap.get(`${pan},${tilt}`);
  236. if (s) {
  237. selectSample(s);
  238. }
  239. });
  240. }
  241. setSelected(pan, tilt) {
  242. this.selectedPan = pan;
  243. this.selectedTilt = tilt;
  244. this.draw();
  245. }
  246. draw() {
  247. if (this.pendingDraw) return;
  248. this.pendingDraw = true;
  249. requestAnimationFrame(() => {
  250. this.pendingDraw = false;
  251. this._draw();
  252. });
  253. }
  254. _draw() {
  255. const ctx = this.ctx;
  256. const w = this.canvas.width;
  257. const h = this.canvas.height;
  258. ctx.clearRect(0, 0, w, h);
  259. if (this.samples.length === 0) {
  260. ctx.fillStyle = '#94a3b8';
  261. ctx.font = '14px sans-serif';
  262. ctx.fillText('暂无扫描样本,请先执行 360° 扫描', 20, 30);
  263. return;
  264. }
  265. const rowH = this.cellH + this.captionH;
  266. const startCol = Math.floor((-this.offsetX / this.scale) / this.cellW);
  267. const endCol = Math.ceil((w - this.offsetX) / this.scale / this.cellW);
  268. const startRow = Math.floor((-this.offsetY / this.scale) / rowH);
  269. const endRow = Math.ceil((h - this.offsetY) / this.scale / rowH);
  270. ctx.save();
  271. ctx.translate(this.offsetX, this.offsetY);
  272. ctx.scale(this.scale, this.scale);
  273. for (let r = Math.max(0, startRow); r <= Math.min(this.tilts.length - 1, endRow); r++) {
  274. for (let c = Math.max(0, startCol); c <= Math.min(this.pans.length - 1, endCol); c++) {
  275. const pan = this.pans[c];
  276. const tilt = this.tilts[r];
  277. const x = c * this.cellW;
  278. const y = r * rowH;
  279. const s = this.sampleMap.get(`${pan},${tilt}`);
  280. ctx.fillStyle = '#0f172a';
  281. ctx.fillRect(x, y, this.cellW, rowH);
  282. const img = this.images.get(`${pan},${tilt}`);
  283. if (img && img.complete && img.naturalWidth) {
  284. const sx = 0, sy = 0, sw = img.naturalWidth, sh = img.naturalHeight;
  285. const dw = this.cellW - 4;
  286. const dh = this.cellH - 4;
  287. const scale = Math.min(dw / sw, dh / sh);
  288. const iw = sw * scale;
  289. const ih = sh * scale;
  290. const ix = x + 2 + (dw - iw) / 2;
  291. const iy = y + 2 + (dh - ih) / 2;
  292. ctx.drawImage(img, ix, iy, iw, ih);
  293. } else {
  294. ctx.fillStyle = '#1e293b';
  295. ctx.fillRect(x + 2, y + 2, this.cellW - 4, this.cellH - 4);
  296. }
  297. ctx.fillStyle = '#cbd5e1';
  298. ctx.font = '11px sans-serif';
  299. ctx.textAlign = 'center';
  300. ctx.fillText(`P:${pan.toFixed(0)} T:${tilt.toFixed(0)}`, x + this.cellW / 2, y + this.cellH + 14);
  301. if (this.selectedPan === pan && this.selectedTilt === tilt) {
  302. ctx.strokeStyle = '#22c55e';
  303. ctx.lineWidth = 2;
  304. ctx.strokeRect(x + 1, y + 1, this.cellW - 2, rowH - 2);
  305. }
  306. }
  307. }
  308. ctx.restore();
  309. }
  310. }
  311. let sampleCanvas = null;
  312. async function loadSamples(groupId) {
  313. try {
  314. const data = await API.get(`/api/samples/${groupId}`);
  315. if (!sampleCanvas) {
  316. sampleCanvas = new SampleCanvas('sample-canvas', 'sample-grid-wrapper');
  317. }
  318. sampleCanvas.setSamples(data.samples || []);
  319. } catch (e) {
  320. log(`加载扫描样本失败: ${e.message}`);
  321. }
  322. }
  323. async function loadGroups() {
  324. try {
  325. const status = await API.get('/api/status');
  326. const select = document.getElementById('group-select');
  327. select.innerHTML = '';
  328. const gids = Object.keys(status.groups || {});
  329. gids.forEach(gid => {
  330. const opt = document.createElement('option');
  331. opt.value = gid;
  332. opt.textContent = gid;
  333. select.appendChild(opt);
  334. });
  335. if (gids.length === 0) {
  336. currentGroup = null;
  337. setStatus('未配置摄像头组');
  338. setControlsDisabled(true);
  339. return;
  340. }
  341. setControlsDisabled(false);
  342. if (gids.includes(currentGroup)) {
  343. select.value = currentGroup;
  344. } else {
  345. currentGroup = select.options[0].value;
  346. onGroupChange();
  347. }
  348. } catch (e) {
  349. log(`获取状态失败: ${e.message}`);
  350. }
  351. }
  352. function resetSampleZoom() {
  353. if (sampleCanvas) sampleCanvas.resetView();
  354. }
  355. function onGroupChange() {
  356. currentGroup = document.getElementById('group-select').value;
  357. selectedSampleEl = null;
  358. tempPreview = null;
  359. setSelectedPreview(null);
  360. resetSampleZoom();
  361. loadSamples(currentGroup);
  362. renderVideos(currentGroup);
  363. loadPoints(currentGroup);
  364. }
  365. function renderVideos(groupId) {
  366. const grid = document.getElementById('video-grid');
  367. grid.innerHTML = '';
  368. ['panorama', 'ptz'].forEach(cam => {
  369. const box = document.createElement('div');
  370. box.className = 'video-box';
  371. const title = document.createElement('div');
  372. title.className = 'title';
  373. title.textContent = `${cam} - ${groupId}`;
  374. const img = document.createElement('img');
  375. img.src = `/api/live/${cam}/${groupId}?marked=1&t=${Date.now()}`;
  376. img.alt = cam;
  377. box.appendChild(title);
  378. box.appendChild(img);
  379. grid.appendChild(box);
  380. });
  381. }
  382. async function loadPoints(groupId) {
  383. try {
  384. const data = await API.get(`/api/points/${groupId}`);
  385. const ul = document.getElementById('points');
  386. ul.innerHTML = '';
  387. data.points.forEach(p => {
  388. const li = document.createElement('li');
  389. const span = document.createElement('span');
  390. span.textContent = `P:${p.pan.toFixed(0)} T:${p.tilt.toFixed(0)}`;
  391. const previewBtn = document.createElement('button');
  392. previewBtn.textContent = '预览';
  393. previewBtn.onclick = async () => {
  394. document.getElementById('inp-pan').value = p.pan.toFixed(2);
  395. document.getElementById('inp-tilt').value = p.tilt.toFixed(2);
  396. document.getElementById('inp-zoom').value = p.zoom;
  397. await runPreview(groupId, p.pan, p.tilt, p.zoom, p.id);
  398. };
  399. const delBtn = document.createElement('button');
  400. delBtn.dataset.id = String(p.id);
  401. delBtn.textContent = '删除';
  402. delBtn.onclick = async () => {
  403. if (!currentGroup) {
  404. log('未选择摄像头组');
  405. return;
  406. }
  407. if (!confirm('确定删除该扫描点?')) return;
  408. delBtn.disabled = true;
  409. try {
  410. await API.del(`/api/points/${groupId}/${p.id}`);
  411. loadPoints(groupId);
  412. } catch (e) {
  413. log(`删除失败: ${e.message}`);
  414. delBtn.disabled = false;
  415. }
  416. };
  417. li.appendChild(span);
  418. li.appendChild(previewBtn);
  419. li.appendChild(delBtn);
  420. ul.appendChild(li);
  421. });
  422. } catch (e) {
  423. log(`加载扫描点失败: ${e.message}`);
  424. }
  425. }
  426. async function updateStatus() {
  427. if (!currentGroup) return;
  428. try {
  429. const status = await API.get('/api/status');
  430. const g = status.groups[currentGroup];
  431. if (g) {
  432. setStatus(g.polling_state);
  433. }
  434. } catch (e) {
  435. // ignore
  436. }
  437. }
  438. document.getElementById('group-select').addEventListener('change', onGroupChange);
  439. document.getElementById('btn-scan').addEventListener('click', async () => {
  440. if (!currentGroup) {
  441. log('未选择摄像头组');
  442. return;
  443. }
  444. if (scanPollInterval) return;
  445. const scannedGroup = currentGroup;
  446. const scanBtn = document.getElementById('btn-scan');
  447. scanBtn.disabled = true;
  448. setStatus('扫描中...');
  449. try {
  450. await API.post(`/api/scan/${scannedGroup}`);
  451. log(`开始扫描: ${scannedGroup}`);
  452. scanPollInterval = setInterval(async () => {
  453. try {
  454. const prog = await API.get(`/api/scan/${scannedGroup}/progress`);
  455. const progress = prog.total > 0 ? (prog.current / prog.total) * 100 : 0;
  456. if (prog.state === 'done' || prog.state === 'failed' || progress >= 100) {
  457. clearInterval(scanPollInterval);
  458. scanPollInterval = null;
  459. if (!controlsGloballyDisabled) scanBtn.disabled = false;
  460. if (prog.state === 'done') {
  461. log('扫描完成');
  462. resetSampleZoom();
  463. loadSamples(scannedGroup);
  464. loadPoints(scannedGroup);
  465. } else if (prog.state === 'failed') {
  466. log(`扫描失败: ${prog.error || 'unknown'}`);
  467. }
  468. } else {
  469. setStatus(`扫描中... ${progress.toFixed(0)}%`);
  470. }
  471. } catch (e) {
  472. clearInterval(scanPollInterval);
  473. scanPollInterval = null;
  474. if (!controlsGloballyDisabled) scanBtn.disabled = false;
  475. setStatus('扫描失败');
  476. log(`扫描进度获取失败: ${e.message}`);
  477. }
  478. }, 1000);
  479. } catch (e) {
  480. log(`扫描失败: ${e.message}`);
  481. if (!controlsGloballyDisabled) scanBtn.disabled = false;
  482. setStatus('扫描失败');
  483. }
  484. });
  485. document.getElementById('btn-poll-start').addEventListener('click', async () => {
  486. if (!currentGroup) {
  487. log('未选择摄像头组');
  488. return;
  489. }
  490. await withDisabled('btn-poll-start', async () => {
  491. try {
  492. await API.post(`/api/poll/${currentGroup}/start`);
  493. log(`开始轮询: ${currentGroup}`);
  494. } catch (e) {
  495. log(`轮询启动失败: ${e.message}`);
  496. }
  497. });
  498. });
  499. document.getElementById('btn-poll-stop').addEventListener('click', async () => {
  500. if (!currentGroup) {
  501. log('未选择摄像头组');
  502. return;
  503. }
  504. await withDisabled('btn-poll-stop', async () => {
  505. try {
  506. await API.post(`/api/poll/${currentGroup}/stop`);
  507. log(`停止轮询: ${currentGroup}`);
  508. } catch (e) {
  509. log(`停止失败: ${e.message}`);
  510. }
  511. });
  512. });
  513. async function runPreview(groupId, pan, tilt, zoom, pointId = null) {
  514. const payload = { pan, tilt, zoom };
  515. const result = await API.post(`/api/preview/${groupId}`, payload);
  516. log(`预览位置: P=${pan.toFixed(1)} T=${tilt.toFixed(1)} Z=${zoom}`);
  517. if (result.snapshot_url) {
  518. log(`预览抓拍已保存: ${result.snapshot_path}`);
  519. // 预览抓拍显示在右侧,但不更新保存点图片
  520. setSelectedPreview(result.snapshot_url);
  521. tempPreview = { path: result.snapshot_path };
  522. }
  523. return result;
  524. }
  525. document.getElementById('btn-preview').addEventListener('click', async () => {
  526. if (!currentGroup) {
  527. log('未选择摄像头组');
  528. return;
  529. }
  530. let pan, tilt, zoom;
  531. try {
  532. pan = parseStrictFloat(document.getElementById('inp-pan').value, 'pan');
  533. tilt = parseStrictFloat(document.getElementById('inp-tilt').value, 'tilt');
  534. zoom = Number(document.getElementById('inp-zoom').value);
  535. } catch (e) {
  536. log(`错误:${e.message}`);
  537. return;
  538. }
  539. if (!Number.isInteger(zoom) || zoom < 1) {
  540. log('错误:zoom 必须是大于等于 1 的整数');
  541. return;
  542. }
  543. await withDisabled('btn-preview', async () => {
  544. try {
  545. await runPreview(currentGroup, pan, tilt, zoom);
  546. } catch (e) {
  547. log(`预览失败: ${e.message}`);
  548. }
  549. });
  550. });
  551. document.getElementById('btn-add').addEventListener('click', async () => {
  552. if (!currentGroup) {
  553. log('未选择摄像头组');
  554. return;
  555. }
  556. await withDisabled('btn-add', async () => {
  557. let pan, tilt, zoom, dwellTime;
  558. try {
  559. pan = parseStrictFloat(document.getElementById('inp-pan').value, 'pan');
  560. tilt = parseStrictFloat(document.getElementById('inp-tilt').value, 'tilt');
  561. zoom = Number(document.getElementById('inp-zoom').value);
  562. dwellTime = parseStrictFloat(document.getElementById('inp-dwell').value, '停留时间');
  563. } catch (e) {
  564. log(`错误:${e.message}`);
  565. return;
  566. }
  567. if (pan < 0 || pan > 360) {
  568. log('错误:pan 必须是 0-360 之间的有限数值');
  569. return;
  570. }
  571. if (tilt < -90 || tilt > 90) {
  572. log('错误:tilt 必须是 -90-90 之间的有限数值');
  573. return;
  574. }
  575. if (!Number.isInteger(zoom) || zoom < 1) {
  576. log('错误:zoom 必须是大于等于 1 的整数');
  577. return;
  578. }
  579. if (dwellTime <= 0) {
  580. log('错误:停留时间必须是大于 0 的有限数值');
  581. return;
  582. }
  583. const payload = { pan, tilt, zoom, dwell_time: dwellTime };
  584. if (tempPreview) {
  585. payload.preview_image = tempPreview.path;
  586. }
  587. try {
  588. await API.post(`/api/points/${currentGroup}`, payload);
  589. log('扫描点已保存');
  590. selectedSampleEl = null;
  591. tempPreview = null;
  592. setSelectedPreview(null);
  593. loadPoints(currentGroup);
  594. } catch (e) {
  595. log(`保存失败: ${e.message}`);
  596. }
  597. });
  598. });
  599. document.getElementById('btn-zoom-in').addEventListener('click', () => {
  600. if (sampleCanvas) {
  601. sampleCanvas.scale = Math.min(5.0, sampleCanvas.scale * 1.2);
  602. sampleCanvas.updateZoomLabel();
  603. sampleCanvas.draw();
  604. }
  605. });
  606. document.getElementById('btn-zoom-out').addEventListener('click', () => {
  607. if (sampleCanvas) {
  608. sampleCanvas.scale = Math.max(0.1, sampleCanvas.scale / 1.2);
  609. sampleCanvas.updateZoomLabel();
  610. sampleCanvas.draw();
  611. }
  612. });
  613. document.getElementById('btn-zoom-reset').addEventListener('click', () => {
  614. resetSampleZoom();
  615. });
  616. setControlsDisabled(true);
  617. loadGroups();
  618. setInterval(updateStatus, 2000);