feat(geofences): 新增地理围栏模块并优化地图瓦片缓存

在 rid-shared 中实现二维 WGS84 地理围栏核心逻辑,支持圆形与多边形
两种几何体,包含坐标合法性校验、边界包含判断及自相交检测。
realtime-core 的 Store 新增观察者回调,在观测数据接受后按到达顺序
通知监听器。

前端方面,新增 geofence-map-adapter.js 提供基于 MapLibre 的围栏绘制
与编辑能力,配套 geofences.css 完成响应式布局样式。map-loader.js
增加懒加载入口,仅在实时工作区按需引入围栏地图模块。

同时引入 map-tile-buffer 瓦片缓冲机制,改善地图切换与刷新时的加载
体验;将默认缩放级别从 11 调整为 15,使初始视图更贴近实际监测场景。
```
This commit is contained in:
2026-10-23 13:55:08 +08:00
parent f310aa9c14
commit cd1b717e74
41 changed files with 2622 additions and 63 deletions
+43
View File
@@ -0,0 +1,43 @@
// Real SQLite and server monitor, without a browser or physical receiver.
import assert from 'node:assert/strict';
import {mkdtemp,writeFile} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import path from 'node:path';
import net from 'node:net';
import {spawn} from 'node:child_process';
const exe=process.argv[2]||'D:/Rust_Contact/util/target/Rid_Drone_Receivev/release/fpv-rid-registry-fullstack.exe';
const root=await mkdtemp(path.join(tmpdir(),'RID-monitoring-'));
const probe=net.createServer();await new Promise(r=>probe.listen(0,'127.0.0.1',r));const port=probe.address().port;await new Promise(r=>probe.close(r));const base='http://127.0.0.1:'+port;
let child,output='',seq=0;const delay=ms=>new Promise(r=>setTimeout(r,ms));
function start(){child=spawn(exe,['--data-root',root,'--simulation-api'],{env:{...process.env,RID_TEST_PORT:String(port)},windowsHide:true,stdio:['ignore','pipe','pipe']});child.stdout.on('data',b=>output+=b);child.stderr.on('data',b=>output+=b);child.on('error',e=>output+=e);}
async function stop(){if(child?.exitCode===null){const done=new Promise(r=>child.once('exit',r));child.kill();await done;}}
async function ready(){for(let i=0;i<150;i++){try{if((await fetch(base+'/api/geofences')).ok)return}catch{}await delay(100)}throw Error(output);}
async function json(url,method='GET',body){const r=await fetch(base+url,{method,headers:body?{'content-type':'application/json'}:{},body:body?JSON.stringify(body):undefined});assert.equal(r.status,200,await r.clone().text());return r.json();}
async function list(kind){const s=await json('/api/target-lists');return json('/api/target-lists/DRONE-A','PUT',{list_kind:kind,note:'测试',expected_revision:s.revision});}
async function ingest(lon=120.0){return json('/api/realtime/ingest','POST',[{target_id:'DRONE-A',receiver_id:'TEST',source_epoch:1,sequence:++seq,longitude:lon,latitude:30.0,altitude_m:100,heading_deg:30}]);}
async function events(){return (await json('/api/monitoring/events?limit=200')).entries;}
async function count(expected){for(let i=0;i<80;i++){const es=await events();if(es.length===expected)return es;await delay(50)}throw Error('event count '+JSON.stringify(await events()));}
let report={root,exe};
try{
start();await ready();assert.equal((await json('/api/maps/preferences')).zoom,15);
let fences=await json('/api/geofences');assert.equal(fences.entries.length,0);
const fence={id:'TEST-FENCE',name:'隔离验收区域',enabled:true,geometry:{type:'circle',center:[120,30],radius_m:500}};
fences=await json('/api/geofences/TEST-FENCE','PUT',{fence,expected_revision:0});assert.equal(fences.revision,1);
assert.equal((await fetch(base+'/api/geofences/TEST-FENCE',{method:'PUT',headers:{'content-type':'application/json'},body:JSON.stringify({fence,expected_revision:0})})).status,400);
const bad={...fence,geometry:{type:'polygon',vertices:[[0,0],[1,1],[0,1],[1,0]]}};
assert.equal((await fetch(base+'/api/geofences/TEST-FENCE',{method:'PUT',headers:{'content-type':'application/json'},body:JSON.stringify({fence:bad,expected_revision:1})})).status,400);
await list('white');await ingest();await delay(400);await count(0);
await list('black');let es=await count(2);assert.deepEqual(new Set(es.map(e=>e.event_type)),new Set(['blacklist','geofence_enter']));
for(let i=0;i<6;i++)await ingest();await delay(400);await count(2);
// Multiple accepted crossings faster than the monitor interval must not be coalesced away.
await ingest(121);await ingest(120);await ingest(121);await ingest(120);es=await count(4);
assert.equal((await json('/api/monitoring/events?type=blacklist&target_id=DRONE-A')).entries.length,1);
assert.equal((await json('/api/monitoring/events?fence_id=TEST-FENCE&limit=2')).entries.length,2);
const page1=(await json('/api/monitoring/events?limit=2')).entries;const page2=(await json('/api/monitoring/events?limit=2&before='+page1.at(-1).id)).entries;assert.equal(new Set([...page1,...page2].map(e=>e.id)).size,4);
assert.equal((await fetch(base+'/api/monitoring/events',{headers:{origin:'https://foreign.invalid'}})).status,403);
await stop();start();await ready();await ingest();await delay(400);assert.equal((await events()).length,4);assert.equal((await json('/api/geofences')).entries[0].name,fence.name);
const h=await json('/api/monitoring/status');assert.deepEqual(h,{droppedObservations:0,error:null});
await list('white');await delay(400);await list('black');await count(6);
await json('/api/geofences/TEST-FENCE?revision=1','DELETE');await ingest(121);await ingest(120);await delay(400);await count(6);
report={...report,status:'PASS',events:(await events()).length,health:h,checks:['default zoom15','versioned CRUD','invalid geometry rejected','white exemption','blacklist + fence enter','dedup frames','rapid crossings','filter and cursor pagination','foreign origin rejected','restart durable history and dedup','list transitions','deleted fence silent','no browser required']};console.log(JSON.stringify(report));
}finally{await stop();await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));await writeFile(path.join(root,'server.log'),output);}