Commit 89fc4d3d by 朱建香

2017/12/27

#1:轮询方法优化
#4:getCid方法优化
parent ca597bec
......@@ -2,15 +2,14 @@
* @class
* loop
*/
//轮询通用方法:http://116.62.143.3:10080/iot-project-js/doorlock/issues/1
class Loop{
constructor(params) {
this.handle = null;
this.watcher = null;
this.time = null;
// this.url = params.request.url;
// this.opts = params.request.opts;
this.loopContent = params.loopContent;
this.timeout = params.timeout || 10;
this.fincb = params.fincb;
......@@ -21,15 +20,6 @@ class Loop{
this.canDo = true;
this.i = 0;
}
requestsend(url, opts, needAuth, isPublic, i){
let data = [0,0,0,0,0,0,0,1,0,0,0];
console.log("请求时间"+this.i+'-'+new Date().getTime());
setTimeout(() => {
console.log("返回时间"+this.i+'-'+new Date().getTime());
opts.success(data[this.i]);
},100);
}
clearTime(){
clearInterval(this.handle);
......@@ -74,15 +64,6 @@ class Loop{
start(){
console.log("开始");
// this.time = setTimeout(() => {
// this.clearTime();
// this.canDo = false;
// this.fincb.error('timeout');
// this.fincb.complete();
// console.log("超时");
// this.intervalcb = null;
// this.fincb = null;
// },this.timeout);
console.log("第"+this.i+"次发送了请求");
console.log("发送时间"+new Date().getTime());
this.i++;
......@@ -114,43 +95,4 @@ class Loop{
},this.intervalTime);
}
}
//new Loop({
// request: {
// url: 'http://www.baidu.com',
// opts: {
// type: 'post',
// data: {}
// }
// },
// timeout: 6000,
// interval:{
// delay: 1000,
// cb: {
// success: (response) => {
// console.log('intervalcbSuccess:'+response);
// },
// error: (error) => {
// console.log('intervalcbSuccess:'+error);
// }
// }
// },
// fincb: {
// success: (response) => {
// console.log('fincbSuccess:'+response);
// },
// error: (error) => {
// console.log('fincbError:'+error);
// },
// complete: () => {}
// },
// stopcondition: (response) => {
// if(response == 1){
// return true;
// }else{
// return false;
// }
// }
//}).start();
export default Loop;
//module.exports = Loop;
\ No newline at end of file
......@@ -4,6 +4,8 @@
*/
import uloop from './uloop';
//获取cid: http://116.62.143.3:10080/iot-project-js/doorlock/issues/4
class unotify{
//在一个周期内获取cid
static getCid(successCallback, errorCallback){
......@@ -15,40 +17,10 @@ class unotify{
}
//周期性获取cid,周期可调整
static getCidPeriotic(times){
// var self = this;
// return new Promise(function(resolve, reject){
// var i = 0;
// var cid = null;
// var handle = null;
// i++;
// cid = self.getCidOnce(i);
// console.log("第"+i+"次"+"获取的cid为"+cid);
// if(cid != null && cid != 'null' || i >= times){
// window.clearInterval(handle);
// resolve(cid);
// }else{
// handle = setInterval(function(){
// i++;
// cid = self.getCidOnce(i);
// console.log("第"+i+"次"+"获取的cid为"+cid);
// if(cid != null && cid != 'null' || i >= times){
// window.clearInterval(handle);
// resolve(cid);
// }
// },1000);
// }
// });
var self = this;
return new Promise(function(resolve, reject){
new uloop({
loopContent: () => {
// var urlParams = {
// type: 'get',
// data: {},
// };
// return iot.network.send('http://192.168.2.75:3000/get', urlParams);
console.log(self.getCidOnce());
return self.getCidOnce();
},
timeout: times,
......@@ -77,9 +49,6 @@ class unotify{
complete: () => {}
},
stopcondition: async (response) => {
// if(response.data.msg == 5){
// return true;
// }
if(response != null && response != 'null' ){
return true;
}
......@@ -88,14 +57,10 @@ class unotify{
});
}
//单次获取cid
static getCidOnce(i){
static getCidOnce(){
var clientInfo = plus.push.getClientInfo();
if(clientInfo && clientInfo.hasOwnProperty('clientid') && clientInfo.clientid){
// if(i == 6){
return(clientInfo.clientid);
// }else{
// return(null);
// }
return(clientInfo.clientid);
}else{
return(null);
}
......@@ -112,6 +77,7 @@ class unotify{
clienId: cid
},
success: (response) => {
console.log(response);
if(typeof successCallback == 'function'){
successCallback('cid updateSuccess');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment