"use strict";global.Promise=require("bluebird");var co=require("co");var jsUtil=require("./jsUtil");var util=require("util");var path=require("path");var deviceLink=require("./deviceLink");var deviceLinkPromise=require("./deviceLinkPromise");exports.upgrade=function(e,r,t,n){var o=0;var i=function c(e,r){var n=new deviceLink;return co(regeneratorRuntime.mark(function i(){var a,c;return regeneratorRuntime.wrap(function s(i){while(1){switch(i.prev=i.next){case 0:i.next=2;return deviceLinkPromise.connectPro(n,e);case 2:a=util.format("upgrade download tftp://%s/toolkit/upgradeInfo/%s/%s",n.getLocalIp(),r,e.updateInfo.fileName);console.log("cmd:",a);i.next=6;return deviceLinkPromise.execPro(n,a);case 6:c=i.sent;if(c.indexOf("error")!==-1||c.indexOf("failed")!==-1){e.updateInfo.msg="设备无法通过tftp下载版本,请确保您的电脑未开启第三方tftp服务器，设备可以ping通您的PC,且不要将网线接入设备的mgmt口"}n.disConnect();o++;if(typeof t==="function"){t(o)}case 11:case"end":return i.stop()}}},i,this)})).catch(function(e){n.disConnect();o++;if(typeof t==="function"){t(o)}})};var a=[];e.forEach(function(e){if(e.updateInfo.status=="wait"){a.push(e)}});return Promise.map(a,function(e){return i(e,r)},{concurrency:10}).asCallback(n)};exports.check=function(e,r,t,n){var o=[];e.forEach(function(e){if(e.updateInfo.status=="wait"){o.push(e)}});var i=0;var a=0;var c=function s(e){var r=new deviceLink;i++;return co(regeneratorRuntime.mark(function n(){var o;return regeneratorRuntime.wrap(function c(n){while(1){switch(n.prev=n.next){case 0:n.next=2;return deviceLinkPromise.connectPro(r,e);case 2:o=r.getDeviceInfo();if(o.softVer.length>0&&o.softVer==e.updateInfo.recommandSoftVer){e.deviceInfo.softVer=o.softVer;e.updateInfo.status="success"}else{e.updateInfo.status="failed";e.deviceInfo.softVer=o.softVer;console.log("最新设备的信息",o," info信息:",e);if(e.updateInfo.msg.length==0){e.updateInfo.msg=util.format("系统检测到此时版本 %s 与推荐版本 %s 不同, 请通过设备EWEB确认是否仍在升级中(个别设备升级时间可能需要数十分钟,请知悉)",o.softVer,e.updateInfo.recommandSoftVer)}}r.disConnect();a++;if(typeof t==="function"){t(i,a)}case 7:case"end":return n.stop()}}},n,this)})).catch(function(e){r.disConnect();if(typeof t==="function"){t(i,a)}})};return co(regeneratorRuntime.mark(function u(){return regeneratorRuntime.wrap(function e(t){while(1){switch(t.prev=t.next){case 0:if(!(o.length==0)){t.next=2;break}return t.abrupt("return",0);case 2:t.prev=2;t.next=5;return Promise.map(o,function(e){return c(e)},{concurrency:r});case 5:t.next=10;break;case 7:t.prev=7;t.t0=t["catch"](2);console.error("in check function: error:",t.t0);case 10:return t.abrupt("return",a);case 11:case"end":return t.stop()}}},u,this,[[2,7]])}))};exports.uploadDatas=function(e,r){return co(regeneratorRuntime.mark(function t(){var r,n;return regeneratorRuntime.wrap(function o(t){while(1){switch(t.prev=t.next){case 0:r=[];e.forEach(function(e){r.push({serialNumber:e.seriaNo,productClass:e.deviceModel,remark:"tookitwifi"})});t.prev=2;t.next=5;return jsUtil.commonPost2(jsUtil.getPkgInfo().spec.info.maccHost,"/service/api/device/plugin/import/ap?appid=3b29e84372364f22857e2253bf2f4fae&secret=fe888d68bff74e0fae5017fd9df593a9",{apList:r},{},12e3);case 5:n=t.sent;t.next=11;break;case 8:t.prev=8;t.t0=t["catch"](2);console.log("upload AP data failed: ",t.t0);case 11:case"end":return t.stop()}}},t,this,[[2,8]])})).asCallback(r)};