blob: 13a6c32527792c2e639bf6b49e7ac89b363176ff [file] [log] [blame]
import{State as e}from"./model.js";const i="./v1/devices",t="./v1/captures";class s{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(){var e;return null===(e=this.device.visible)||void 0===e||e}set visible(e){this.device.visible=e}toggleChipState(i,t){if("bt"in i&&i.bt){if(void 0===t)return void console.log("netsim-ui: must specify lowEnergy or classic for Bluetooth");"lowEnergy"===t&&"lowEnergy"in i.bt&&i.bt.lowEnergy&&"state"in i.bt.lowEnergy&&(i.bt.lowEnergy.state=i.bt.lowEnergy.state===e.ON?e.OFF:e.ON),"classic"===t&&"classic"in i.bt&&i.bt.classic&&"state"in i.bt.classic&&(i.bt.classic.state=i.bt.classic.state===e.ON?e.OFF:e.ON)}"wifi"in i&&i.wifi&&"state"in i.wifi&&(i.wifi.state=i.wifi.state===e.ON?e.OFF:e.ON),"uwb"in i&&i.uwb&&"state"in i.uwb&&(i.uwb.state=i.uwb.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,o.patchDevice({device:{name:i.name,chips:i.chips}}))}}const o=new class{constructor(){this.observers=[],this.simulationInfo={devices:[],captures:[],selectedId:"",dimension:{x:10,y:10,z:0}},this.invokeGetDevice(),this.invokeListCaptures()}invokeGetDevice(){fetch(i,{method:"GET"}).then((e=>e.json())).then((e=>{this.fetchDevice(e.devices)})).catch((e=>{console.log("Cannot connect to netsim web server",e)}))}invokeListCaptures(){fetch(t,{method:"GET"}).then((e=>e.json())).then((e=>{this.simulationInfo.captures=e.captures})).catch((e=>{console.log("Cannot connect to netsim web server",e)}))}fetchDevice(e){this.simulationInfo.devices=[];for(const i of e)this.simulationInfo.devices.push(new s(i));this.notifyObservers()}patchSelected(e){this.simulationInfo.selectedId=e,this.notifyObservers()}handleDrop(e,i,t){for(const s of this.simulationInfo.devices)if(e===s.name){s.position={x:i,y:t,z:s.position.z},this.patchDevice({device:{name:s.name,position:s.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(;;)o.invokeGetDevice(),o.invokeListCaptures(),await e(1e3)}();export{s as Device,o as simulationState};