"use strict";var t=function(t){return t&&t.__esModule?t["default"]:t},e=function(){function t(t,e){for(var s in e){var o=e[s];o.configurable=!0,o.value&&(o.writable=!0)}Object.defineProperties(t,e)}return function(e,s,o){return s&&t(e.prototype,s),o&&t(e,o),e}}(),s=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=t(require("async")),r=function(){function t(){s(this,t),this.bs={},this.bs.miscState={ts:[],main_board_temp:0,cpu_temp:0,intake_temp:0,exhaust_temp:0,neck_motor_temp:0,torso_motor_temp:0,pelvis_motor_temp:0},this.bs.touchState={ts:[],touched:!1,pad_state:[0,0,0,0,0,0],changed:[0,0,0,0,0,0]},this.bs.powerState={ts:[],source:"",vsys:0,battery:{id:0,cycles:0,capacity:0,max_capacity:0,charge_rate:0,temp:0}}}return e(t,{init:{value:function(t,e){this.httpInterface="http://"+t.host+":"+t.port,this.socketUrl="ws:"+t.host+":"+t.port,o.parallel([this.createMiscSocket.bind(this),this.createTouchSocket.bind(this),this.createPowerSocket.bind(this)],e)}},getBatteryTemperature:{value:function(){return this.bs.powerState.battery.temp}},getBatteryLevel:{value:function(){return this.bs.powerState.battery.capacity/this.bs.powerState.battery.max_capacity*100}},getSystemVoltage:{value:function(){return this.bs.powerState.vsys}},isBatteryCharging:{value:function(){return"EXTERNAL"===this.bs.powerState.source}},getMainBoardTemperature:{value:function(){return this.bs.miscState.main_board_temp}},getCPUTemperature:{value:function(){return this.bs.miscState.cpu_temp}},getFanSpeed:{value:function(t){this._getSystemSetting("fan_speed",t)}},getBacklight:{value:function(t){this._getSystemSetting("lcd_backlight",t)}},_getSystemSetting:{value:function(t,e){if(void 0===this.httpInterface)return void e("Body Service is not available",null);var s=new XMLHttpRequest;s.open("GET",this.httpInterface+"/settings",!0),s.onreadystatechange=function(){if(4==s.readyState&&200==s.status)if(s.response){var o=JSON.parse(s.response);e(null,o[t])}else e("No data received from body service",null);else 4==s.readyState&&404==s.status&&e("Body service is unavailable",null)},s.send()}},setBacklight:{value:function(t,e){var s=this;if(0>t||t>1){var o="Backlight brightness value of "+t+" is out of range [0.0 - 1.0].";return void e(o)}this.getFanSpeed(function(o,r){o?e(o):!function(){var o={lcd_backlight:t,fan_speed:r},a=new XMLHttpRequest,n=s.httpInterface+"/settings";a.open("POST",n,!0),a.onreadystatechange=function(){4==a.readyState&&(204==a.status?e(null):404==a.status?e("Body service is unavailable"):400===a.status&&e("Bad request sent to "+n))},a.send(JSON.stringify(o))}()})}},createMiscSocket:{value:function(t){var e=this;this.MiscSocket=new WebSocket(this.socketUrl+"/misc"),this.MiscSocket.onerror=function(){console.error("Error opening misc state socket at "+e.socketUrl+"/misc")},this.MiscSocket.onmessage=function(t){e.bs.miscState=JSON.parse(t.data)},t()}},createTouchSocket:{value:function(t){var e=this;this.TouchSocket=new WebSocket(this.socketUrl+"/touch"),this.TouchSocket.onerror=function(){console.error("Error opening touch state socket at "+e.socketUrl+"/touch")},this.TouchSocket.onmessage=function(t){e.bs.touchState=JSON.parse(t.data)},t()}},createPowerSocket:{value:function(t){var e=this;this.PowerSocket=new WebSocket(this.socketUrl+"/power"),this.PowerSocket.onerror=function(){console.error("Error opening power state socket at "+e.socketUrl+"/power")},this.PowerSocket.onmessage=function(t){e.bs.powerState=JSON.parse(t.data)},t()}}}),t}();module.exports=new r;