blob: 9d6e8a9305f0bc0c33fcd068487eae813b811594 [file] [log] [blame]
const e="./v1/devices",i="./v1/captures";class t{constructor(e){this.device=e}get name(){return this.device.name}set name(e){this.device.name=e}get position(){const e={x:0,y:0,z:0};return"position"in this.device&&this.device.position&&"object"==typeof this.device.position&&("x"in this.device.position&&"number"==typeof this.device.position.x&&(e.x=this.device.position.x),"y"in this.device.position&&"number"==typeof this.device.position.y&&(e.y=this.device.position.y),"z"in this.device.position&&"number"==typeof this.device.position.z&&(e.z=this.device.position.z)),e}set position(e){this.device.position=e}get orientation(){const e={yaw:0,pitch:0,roll:0};return"orientation"in this.device&&this.device.orientation&&"object"==typeof this.device.orientation&&("yaw"in this.device.orientation&&"number"==typeof this.device.orientation.yaw&&(e.yaw=this.device.orientation.yaw),"pitch"in this.device.orientation&&"number"==typeof this.device.orientation.pitch&&(e.pitch=this.device.orientation.pitch),"roll"in this.device.orientation&&"number"==typeof this.device.orientation.roll&&(e.roll=this.device.orientation.roll)),e}set orientation(e){this.device.orientation=e}get chips(){var e;return null!==(e=this.device.chips)&&void 0!==e?e:[]}set chips(e){this.device.chips=e}get visible(){return Boolean(this.device.visible)}set visible(e){this.device.visible=e}toggleChipState(e){e.state=!e.state}toggleCapture(e,i){"capture"in i&&i.capture&&(i.capture=!i.capture,o.patchDevice({device:{name:e.name,chips:e.chips}}))}}const o=new class{constructor(){this.observers=[],this.simulationInfo={devices:[],captures:[],selectedId:"",dimension:{x:10,y:10,z:0},lastModified:""},this.invokeGetDevice(),this.invokeListCaptures()}async invokeGetDevice(){await fetch(e,{method:"GET"}).then((e=>e.json())).then((e=>{this.fetchDevice(e.devices),this.updateLastModified(e.lastModified)})).catch((e=>{console.log("Cannot connect to netsim web server",e)}))}async invokeListCaptures(){await fetch(i,{method:"GET"}).then((e=>e.json())).then((e=>{this.simulationInfo.captures=e.captures,this.notifyObservers()})).catch((e=>{console.log("Cannot connect to netsim web server",e)}))}fetchDevice(e){this.simulationInfo.devices=[],e&&(this.simulationInfo.devices=e.map((e=>new t(e)))),this.notifyObservers()}getLastModified(){return this.simulationInfo.lastModified}updateLastModified(e){this.simulationInfo.lastModified=e}patchSelected(e){this.simulationInfo.selectedId=e,this.notifyObservers()}handleDrop(e,i,t){for(const o of this.simulationInfo.devices)if(e===o.name){o.position={x:i,y:t,z:o.position.z},this.patchDevice({device:{name:o.name,position:o.position}});break}}patchCapture(e,t){fetch(i+"/"+e,{method:"PATCH",headers:{"Content-Type":"text/plain","Content-Length":t.length.toString()},body:t}),this.notifyObservers()}patchDevice(i){const t=JSON.stringify(i);fetch(e,{method:"PATCH",headers:{"Content-Type":"application/json","Content-Length":t.length.toString()},body:t}).then((e=>e.json())).catch((e=>{console.error("Error:",e)})),this.notifyObservers()}registerObserver(e){this.observers.push(e),e.onNotify(this.simulationInfo)}removeObserver(e){const i=this.observers.indexOf(e);this.observers.splice(i,1)}notifyObservers(){for(const e of this.observers)e.onNotify(this.simulationInfo)}getDeviceList(){return this.simulationInfo.devices}};!async function(){const e=e=>new Promise((i=>setTimeout(i,e)));for(;;)await o.invokeListCaptures(),await o.invokeGetDevice(),await e(1e3)}(),async function(){for(await o.invokeGetDevice();;){const i=JSON.stringify({lastModified:o.getLastModified()});await fetch(e,{method:"SUBSCRIBE",headers:{"Content-Type":"application/json","Content-Length":i.length.toString()},body:i}).then((e=>e.json())).then((e=>{o.fetchDevice(e.devices),o.updateLastModified(e.lastModified)})).catch((e=>{console.log("Cannot connect to netsim web server",e)}))}}();export{t as Device,o as simulationState};