feat(receiver): 持久化接收机发现端点并支持位置备注信息
This commit is contained in:
@@ -7,38 +7,78 @@ import vm from 'node:vm';
|
||||
const source=(await readFile(new URL('../crates/web-ui/public/history-map-adapter.js',import.meta.url),'utf8'))
|
||||
.replace(/^import .*;$/gm,'').replaceAll('export function ','function ');
|
||||
|
||||
test('historical segments stay separate and playback moves only the marker',()=>{
|
||||
const markerSource=await readFile(new URL('../crates/web-ui/workbench/drone-map.js',import.meta.url),'utf8');
|
||||
|
||||
test('replay keeps real segments, changes its basemap and advances the marker on the route',async()=>{
|
||||
let map,marker,fitCount=0,removed=false;
|
||||
const sources=new Map();
|
||||
class FakeMap{
|
||||
constructor(){map=this;this.handlers={};}
|
||||
constructor(options){
|
||||
map=this;this.handlers={};this.style=options.style;
|
||||
for(const [id,value] of Object.entries(this.style.sources)) this.addSource(id,value);
|
||||
}
|
||||
on(name,fn){this.handlers[name]=fn;}
|
||||
addControl(){}
|
||||
addSource(id,value){sources.set(id,{data:value.data,setData(data){this.data=data;}});}
|
||||
addSource(id,value){sources.set(id,{...value,setData(data){this.data=data;},setTiles(tiles){this.tiles=tiles;}});}
|
||||
addLayer(){}
|
||||
getSource(id){return sources.get(id);}
|
||||
getLayer(id){return this.style.layers.find(layer=>layer.id===id);}
|
||||
setLayoutProperty(id,key,value){this.getLayer(id).layout[key]=value;}
|
||||
fitBounds(){fitCount++;}
|
||||
resize(){}
|
||||
remove(){removed=true;}
|
||||
}
|
||||
class Marker{setLngLat(point){this.position=[...point];marker=this;return this;}addTo(){return this;}remove(){}}
|
||||
class Marker{
|
||||
constructor({element}){this.element=element;}
|
||||
getElement(){return this.element;}
|
||||
setLngLat(point){this.position=[...point];marker=this;return this;}
|
||||
getLngLat(){return {lng:this.position[0],lat:this.position[1]};}
|
||||
setRotation(angle){this.angle=angle;return this;}
|
||||
getRotation(){return this.angle;}
|
||||
addTo(){return this;}
|
||||
remove(){}
|
||||
}
|
||||
class MercatorCoordinate{
|
||||
constructor(x,y){Object.assign(this,{x,y});}
|
||||
static fromLngLat([lng,lat]){return new MercatorCoordinate(lng/360,lat/360);}
|
||||
toLngLat(){return {toArray:()=>[this.x*360,this.y*360]};}
|
||||
}
|
||||
class Bounds{constructor(){this.points=[];}extend(point){this.points.push(point);}isEmpty(){return this.points.length===0;}}
|
||||
const context=vm.createContext({window:{dispatchEvent(){}},document:{getElementById(){return {};}},
|
||||
ResizeObserver:class{observe(){}disconnect(){}},Event:class{},Number,Array,Math,
|
||||
ml:{setWorkerUrl(){},Map:FakeMap,Marker,LngLatBounds:Bounds,NavigationControl:class{},AttributionControl:class{}},
|
||||
attachTileBuffer:()=>({destroy(){}})});
|
||||
function element(){
|
||||
const styles = new Map();
|
||||
return {setAttribute(){},append(){},style:{setProperty:(name,value)=>styles.set(name,value),getPropertyValue:name=>styles.get(name)}};
|
||||
}
|
||||
const context=vm.createContext({window:{dispatchEvent(){}},document:{getElementById(){return {};},createElement:element},
|
||||
ResizeObserver:class{observe(){}disconnect(){}},Event:class{},Number,Array,Math,URLSearchParams,
|
||||
fetch:async()=>({ok:true,json:async()=>({entries:[{registered:true}]})}),
|
||||
ml:{setWorkerUrl(){},Map:FakeMap,Marker,MercatorCoordinate,LngLatBounds:Bounds,NavigationControl:class{},AttributionControl:class{}},
|
||||
attachTileBuffer:()=>({destroy(){},schedule(){}})});
|
||||
vm.runInContext(markerSource,context);
|
||||
vm.runInContext(source,context);
|
||||
vm.runInContext('open({longitude:118.77,latitude:32.04,zoom:15,basemap:"road"})',context);
|
||||
vm.runInContext('setPoints([{longitude:118,latitude:32,segmentStart:true},{longitude:119,latitude:32,segmentStart:false},{longitude:120,latitude:32,segmentStart:true},{longitude:121,latitude:32,segmentStart:false}])',context);
|
||||
vm.runInContext('open({longitude:118.77,latitude:32.04,zoom:15,basemap:"road",labels:false})',context);
|
||||
vm.runInContext(`setPoints([
|
||||
{longitude:118,latitude:32,timeMs:1000,headingDeg:90,segmentStart:true},
|
||||
{longitude:119,latitude:32,timeMs:2000,headingDeg:90,segmentStart:false},
|
||||
{longitude:120,latitude:32,timeMs:40000,headingDeg:180,segmentStart:true},
|
||||
{longitude:121,latitude:32,timeMs:41000,headingDeg:180,segmentStart:false}
|
||||
], 'TEST-OWN')`,context);
|
||||
map.handlers['style.load']();
|
||||
await new Promise(resolve=>setImmediate(resolve));
|
||||
assert.equal(marker.getElement().style.getPropertyValue('--rid-drone-fill'),'#26ad72');
|
||||
const features=sources.get('history-track').data.features;
|
||||
assert.equal(features.length,2);
|
||||
assert.deepEqual([...features[0].geometry.coordinates].map(p=>[...p]),[[118,32],[119,32]]);
|
||||
assert.deepEqual([...features[1].geometry.coordinates].map(p=>[...p]),[[120,32],[121,32]]);
|
||||
assert.deepEqual([...features].map(feature=>Array.from(feature.geometry.coordinates, point=>[...point])),
|
||||
[[[118,32],[119,32]],[[120,32],[121,32]]]);
|
||||
assert.equal(fitCount,1);
|
||||
vm.runInContext('setIndex(2)',context);
|
||||
vm.runInContext('setCursor(0,.5)',context);
|
||||
assert.deepEqual(marker.position,[118.5,32]);
|
||||
vm.runInContext('setCursor(1,.5)',context);
|
||||
assert.deepEqual(marker.position,[119,32]);
|
||||
vm.runInContext('setMapOptions({basemap:"imagery",labels:true}); setCursor(2,0)',context);
|
||||
assert.deepEqual([...sources.get('base').tiles],['/api/maps/tile/img_w/{z}/{x}/{y}']);
|
||||
assert.equal(map.getLayer('labels').layout.visibility,'visible');
|
||||
assert.deepEqual(marker.position,[120,32]);
|
||||
assert.equal(fitCount,1,'playback should not recenter the map repeatedly');
|
||||
assert.equal(marker.angle,180);
|
||||
assert.equal(fitCount,1);
|
||||
vm.runInContext('close()',context);
|
||||
assert.ok(removed);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user