UNPKG

@openhps/web-sensors

Version:

Open Hybrid Positioning System - Web Sensors API

2 lines 4.88 kB
import{AbsoluteOrientationSensor as e,Acceleration as t,AccelerationUnit as s,Accelerometer as r,AngularVelocity as n,AngularVelocityUnit as i,DataFrame as o,DataObject as a,Gyroscope as c,LinearAccelerationSensor as u,Magnetism as h,MagnetismUnit as d,Magnetometer as l,Orientation as m,Quaternion as _,RelativeOrientationSensor as p,SourceNode as w}from"./openhps-core.es.min.js";class SensorSourceNode extends w{constructor(e){super(e),this._subscriptions=new Map,this._values=new Map,this._lastPush=0,this._running=!1,this.options.interval=this.options.interval||100,this.options.autoStart&&this.once("build",this.start.bind(this)),this.once("destroy",this.stop.bind(this))}static checkPermissions(e){return this.requestPermissions(e)}static requestPermissions(e){return new Promise((t,s)=>{if(void 0===navigator.permissions||void 0===e||0===e.length)return t(!0);Promise.all(e.map(e=>this.getPermissions(e).map(e=>navigator.permissions.query({name:e}))).reduce((e,t)=>[...e,...t])).then(e=>{e.every(e=>"granted"===e.state)?t(!0):t(!1)}).catch(s)})}requestPermissions(){return SensorSourceNode.requestPermissions(this.options.sensors)}start(){return new Promise(e=>{if(this._running=!0,this._subscriptions.size>0)return e();this.options.sensors.forEach(e=>{const t=this.findSensor(e);if(void 0===t)return;const s=new t({frequency:Math.round(1e3/this.options.interval)});s.addEventListener("reading",t=>{this._running&&(this._values.set(e,t.target),this._isUpdated()&&(this._lastPush=t.timeStamp,this.createFrame().catch(e=>{this.logger("error","Unable to create sensor data frame!",e)})))}),s.start(),this._subscriptions.set(e,s)}),e()})}_isUpdated(){return Array.from(this._values.values()).filter(e=>e.timestamp>this._lastPush).length===Array.from(this._subscriptions.values()).filter(e=>e.activated).length}stop(){return new Promise(e=>{this.options.softStop?this._running=!1:(this._subscriptions.forEach(e=>e.stop()),this._subscriptions=new Map,this._values=new Map),e()})}createFrame(){return new Promise(a=>{const w=new o;w.source=this.source;const v=this._values.get(r),g=this._values.get(u),S=this._values.get(c),P=this._values.get(e),E=this._values.get(p),f=this._values.get(l),y=this.source?this.source.uid:this.uid,R=1e3/this.options.interval;v&&w.addSensor(new r(y+"_accel",new t(v.x,v.y,v.z,s.METER_PER_SECOND_SQUARE),R)),g&&w.addSensor(new u(y+"_linearaccel",new t(g.x,g.y,g.z,s.METER_PER_SECOND_SQUARE),R)),S&&w.addSensor(new c(y+"_gyro",new n(S.x,S.y,S.z,i.RADIAN_PER_SECOND),R)),P&&w.addSensor(new e(y+"_absoluteorientation",m.fromQuaternion(new _(...P.quaternion)),R)),E&&w.addSensor(new p(y+"_relativeorientation",m.fromQuaternion(new _(...E.quaternion)),R)),f&&w.addSensor(new l(y+"_mag",new h(f.x,f.y,f.z,d.MICROTESLA),R)),this.push(w),a()})}onPull(){return new Promise(e=>{e(void 0)})}findSensor(t){switch(t){case p:return p;case e:return e;case u:return u;case c:return c;case l:return window.Magnetometer;case r:return r;default:return}}static getPermissions(t){switch(t){case p:case e:return["gyroscope","accelerometer","magnetometer"];case c:return["gyroscope"];case l:return["magnetometer"];case u:case r:return["accelerometer"];default:return}}}class SensorSourceNode_SensorSourceNode extends w{constructor(e){var t;super(e),this.options.source=null!==(t=this.options.source)&&void 0!==t?t:new a(this.uid),this.options.autoStart&&this.once("build",this.start.bind(this)),this.once("destroy",this.stop.bind(this))}static requestPermissions(...e){return"function"==typeof DeviceMotionEvent.requestPermission?DeviceMotionEvent.requestPermission().then(e=>{if("granted"!==e)return Promise.reject(new Error("Permission to access device motion was denied."))}):Promise.resolve()}start(){return new Promise(a=>{const u=this.source?this.source.uid:this.uid;window.addEventListener("devicemotion",a=>{const h=new o,d=1e3/a.interval;h.addSensor(new r(u+"_accl",new t(a.accelerationIncludingGravity.x,a.accelerationIncludingGravity.y,a.accelerationIncludingGravity.z,s.METER_PER_SECOND_SQUARE),d)),h.addSensor(new c(u+"_gyro",new n(a.rotationRate.beta,a.rotationRate.gamma,a.rotationRate.alpha,i.RADIAN_PER_SECOND),d)),h.addSensor(new r(u+"_linearaccl",new t(a.acceleration.x,a.acceleration.y,a.acceleration.z,s.METER_PER_SECOND_SQUARE),d));const l=this.source;l.getPosition().angularVelocity=h.getSensor(c).value,h.addSensor(new e(u+"_orientation",l.getPosition().orientation)),h.source=l,this.push(h)},!0),window.addEventListener("deviceorientation",e=>{this.source.getPosition().orientation=m.fromEuler([e.beta,e.gamma,e.alpha])}),this.logger("debug","Browser orientation and motion events registered!"),a()})}stop(){return Promise.resolve()}onPull(){return new Promise(e=>{e(void 0)})}}let v=SensorSourceNode;"Accelerometer"in window||(v=SensorSourceNode_SensorSourceNode);export{v as SensorSourceNode}; //# sourceMappingURL=openhps-web-sensors.es.min.js.map