"use strict";global.Promise=require("bluebird");var co=require("co");var deviceLinkPromise=require("./deviceLinkPromise");var maccRequest=require("./maccRequest");var util=require("util");var path=require("path");var jsUtil=require("./jsUtil");var fs=require("fs");var _retryArray=[5e3,1e4,2e4,2e4,2e4,4e4,6e4];function _retryIntervalMs(e){var r=e>=_retryArray.length?_retryArray.length-1:e;return _retryArray[r]}exports.init=function(e){if(e==undefined){e=path.join(jsUtil.getDataPath(),"/tmpData")}jsUtil.ensureFS(e);co(regeneratorRuntime.mark(function r(){var t,a,s,i,n,o,u,c,l,f;return regeneratorRuntime.wrap(function m(r){while(1){switch(r.prev=r.next){case 0:t=0;case 1:if(!true){r.next=59;break}a=true;s=100;r.prev=4;r.next=7;return Promise.promisify(fs.readdir)(e);case 7:i=r.sent;if(!(Array.isArray(i)&&i.length>0)){r.next=48;break}n=i.length;i.sort();r.t0=regeneratorRuntime.keys(i);case 12:if((r.t1=r.t0()).done){r.next=46;break}o=r.t1.value;u=path.join(e,i[o]);c=false;r.prev=16;r.next=19;return Promise.promisify(fs.readFile)(u,"utf-8");case 19:l=r.sent;f=JSON.parse(l);if(typeof f.timeout!=="number"||f.timeout<=4e3){f.timeout=8e3}r.next=24;return jsUtil.commonPostNoLog(f.host,f.route,f.data,f.headers,f.timeout);case 24:c=true;a=false;r.next=31;break;case 28:r.prev=28;r.t2=r["catch"](16);if(jsUtil.isOffline(r.t2.toString())){if(n>=s){c=true;a=false}else{a=true}}else{c=true;a=false}case 31:if(!c){r.next=41;break}r.prev=32;r.next=35;return Promise.promisify(fs.unlink)(u);case 35:n--;r.next=41;break;case 38:r.prev=38;r.t3=r["catch"](32);console.error("in statistics:",r.t3);case 41:if(!(t>=3&&n<=s)){r.next=44;break}r.next=44;return Promise.delay(150);case 44:r.next=12;break;case 46:r.next=49;break;case 48:a=false;case 49:r.next=54;break;case 51:r.prev=51;r.t4=r["catch"](4);console.error("in statistics 1:",r.t4);case 54:if(a){t++}else{t=0}r.next=57;return Promise.delay(_retryIntervalMs(t));case 57:r.next=1;break;case 59:case"end":return r.stop()}}},r,this,[[4,51],[16,28],[32,38]])}))};