Files
JiboSDK/node_modules/jibo/lib/lps-service.js
2026-03-22 03:21:45 +02:00

1 line
3.5 KiB
JavaScript

"use strict";var t=function(t){return t&&t.__esModule?t["default"]:t},e=function(){function t(t,e){for(var i in e){var o=e[i];o.configurable=!0,o.value&&(o.writable=!0)}Object.defineProperties(t,e)}return function(e,i,o){return i&&t(e.prototype,i),o&&t(e,o),e}}(),i=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=t(require("async")),a={DEBUG:0,PREVIEW:1,FULL:2},s=function(){function t(){i(this,t),this.lps={},this.lps.motionData={cameras:[],entities:[]},this.lps.audioData={entities:[]},this.PhotoType=a}return e(t,{getMotionData:{value:function(){return console.warn("DEPRECATED! Please use specific visual entity APIs (getClosestVisualEntity/getVisualEntityById)."),this.lps.motionData}},getClosestVisualEntity:{value:function(){var t=void 0;return void 0!==this.lps.motionData&&void 0!==this.lps.motionData.entities&&this.lps.motionData.entities.length>0&&(t=this.lps.motionData.entities[0]),t}},getVisualEntityById:{value:function(t){for(var e=void 0,i=0;i<this.lps.motionData.entities.length;i++){var o=this.lps.motionData.entities[i];if(o&&o.id===t){e=o;break}}return e}},getClosestAudibleEntity:{value:function(){var t=void 0;return void 0!==this.lps.audioData&&void 0!==this.lps.audioData.entities&&this.lps.audioData.entities.length>0&&(t=this.lps.audioData.entities[0]),t}},getAudibleEntityById:{value:function(t){for(var e=void 0,i=0;i<this.lps.audioData.entities.length;i++){var o=this.lps.audioData.entities[i];if(o&&o.id===t){e=o;break}}return e}},readBarcode:{value:function(t){if(void 0===this.httpUrl)return void t("Barcode service is not initialized",null);var e=new XMLHttpRequest;e.open("GET",this.httpUrl+"/barcode",!0),e.onreadystatechange=function(){if(4==e.readyState&&200==e.status)if(e.response){var i=JSON.parse(e.response);t(null,i.barcodes)}else t("No data received from barcode service",null);else 4==e.readyState&&404==e.status&&t("Barcode service is unavailable",null)},e.send()}},takePhoto:{value:function(t,e){var i=this;if(void 0===this.httpUrl)return void e("Photo service is not initialized",null);(t<this.PhotoType.DEBUG||t>this.PhotoType.FULL)&&(t=this.PhotoType.FULL);var o={camera:0,type:t},a=this.httpUrl+"/photo";!function(){var t=new XMLHttpRequest;t.open("POST",a,!0),t.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"),t.setRequestHeader("Accept","*/*"),t.onreadystatechange=function(){if(4==t.readyState)if(200===t.status)if(t.response){var o=JSON.parse(t.response);i._getPhoto(o.id,e)}else e("No data received from photo service",null);else 404==t.status&&e("Photo service unavailable",null)},t.send(JSON.stringify(o))}()}},_getPhoto:{value:function(t,e){var i=this.httpUrl+"/photo?id="+t;e(null,i)}},init:{value:function(t,e){this.lpsServiceUrl="ws:"+t.host+":"+t.port,this.httpUrl="http://"+t.host+":"+t.port,o.parallel([this._createMotionSocket.bind(this),this._createAudioSocket.bind(this)],e)}},_createMotionSocket:{value:function(t){var e=this;this.motionSocket=new WebSocket(this.lpsServiceUrl+"/visual_awareness"),this.motionSocket.onerror=function(){console.error("Error opening visual awareness socket at "+e.lpsServiceUrl+"/visual_awareness")},this.motionSocket.onmessage=function(t){e.lps.motionData=JSON.parse(t.data)},t()}},_createAudioSocket:{value:function(t){var e=this;this.audioSocket=new WebSocket(this.lpsServiceUrl+"/audible_awareness"),this.audioSocket.onerror=function(){console.error("Error opening audible awareness socket at "+e.lpsServiceUrl+"/audible_awareness")},this.audioSocket.onmessage=function(t){e.lps.audioData=JSON.parse(t.data)},t()}}}),t}();module.exports=new s;