blob: 6b8492efe9e9c0eb23170c9b663fd9c0f6100856 [file] [log] [blame]
import{State as e}from"./netsim/model.js";const i="./v1/devices",t="./v1/captures";class o{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 this.device.visible===e.ON}set visible(i){this.device.visible=i?e.ON:e.OFF}toggleChipState(i){i.state=i.state===e.ON?e.OFF:e.ON}toggleCapture(i,t){"capture"in t&&t.capture&&(t.capture=t.capture===e.ON?e.OFF:e.ON,s.patchDevice({device:{name:i.name,chips:i.chips}}))}}const s=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(i,{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(t,{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 o(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,i){fetch(t+"/"+e,{method:"PATCH",headers:{"Content-Type":"text/plain","Content-Length":i.length.toString()},body:i}),this.notifyObservers()}patchDevice(e){const t=JSON.stringify(e);fetch(i,{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 s.invokeListCaptures(),await s.invokeGetDevice(),await e(1e3)}(),async function(){for(await s.invokeGetDevice();;){const e=JSON.stringify({lastModified:s.getLastModified()});await fetch(i,{method:"SUBSCRIBE",headers:{"Content-Type":"application/json","Content-Length":e.length.toString()},body:e}).then((e=>e.json())).then((e=>{s.fetchDevice(e.devices),s.updateLastModified(e.lastModified)})).catch((e=>{console.log("Cannot connect to netsim web server",e)}))}}();export{o as Device,s as simulationState};