whychdw
2019-12-09 064c6d5b84fd6ddbbe4c20c41d139f7371460985
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import $ from 'jquery';
const CORS= 'http://localhost:8080/LD_9/';        // 开启跨域请求(调试时开启)
// const CORS='';           // 关闭跨域请求(发布时开启)
 
//定义计时器
function Interval() {
    this.timer = null;
    this.time = '';
    this.callback = '';
}
// 开启计时器并添加
Interval.prototype.start = function(callback, time) {
    // 先关闭计时器
    this.stop();
    // 配置执行函数
    if(typeof callback == 'function' && typeof time == 'number') {
        this.callback = callback;
        this.time = time;
        callback();
        this.timer = setInterval(callback, time);
    }else {
        console.warn('未完整配置参数!');
    }
};
// 开启计时器
Interval.prototype.open = function() {
    var callback = this.callback;
    var time = this.time;
    this.start(callback, time);
};
 
// 关闭计时器
Interval.prototype.stop = function() {
    clearInterval(this.timer);
};
 
 
// 延时计时器
function Timeout() {
    this.timer = null;
    this.time = '';
    this.callback = '';
}
// 开启计时器并添加
Timeout.prototype.start = function(callback, time, exe) {
    // 先关闭计时器
    this.stop();
    // 配置执行函数
    if(typeof callback == 'function' && typeof time == 'number') {
        this.callback = callback;
        this.time = time;
        if(exe != 'exe') {
            callback();
        }
        this.timer = setTimeout(callback, time);
    }else {
        console.warn('未完整配置参数!');
    }
};
// 开启计时器
Timeout.prototype.open = function() {
    var callback = this.callback;
    var time = this.time;
    this.start(callback, time, 'exe');
};
 
// 关闭计时器
Timeout.prototype.stop = function() {
    clearTimeout(this.timer);
};
 
/*从多维数组中获取最大值*/
function getMaxFromArr(arr) {
    var newArray = arr.join(",").split(",");
    return Math.max.apply({},newArray);
}
 
/*从多维数组中获取最小值*/
function getMinFromArr (arr) {
    var newArray = arr.join(",").split(",");
    return Math.min.apply({},newArray);
}
/*从多维数组中获取和*/
function getSumFromArr (arr) {
    var newArray = arr.join(",").split(",");
    var sum =0;
    for(var i=0; i<newArray.length; i++) {
        var _newArray = newArray[i];
        sum += Number(_newArray);
    }
    return sum;
}
/*从多维数组中获取平均值*/
function getAvgFromArr (arr) {
    var avg = 0;
    if(arr.length !=0) {
        var sum = getSumFromArr(arr);
        avg = sum/arr.length;
    }
    return avg;
}
 
function ajax(option) {
    $.ajax({
        type: 'post',
        async: true,
        url: CORS+option.url,
        data: option.data,
        dataType: 'json',
        success: option.success,
        error: option.error,
        complete: option.complete
    });
}
 
//将秒转化成时:分:秒
function formatSeconds(value) {
    if(value > 0){
    
    }else{
        value = 0;
    }
    var theTime = parseInt(value);// 秒
    var theTime1 = 0;// 分
    var theTime2 = 0;// 小时
    // alert(theTime);
    if(theTime >= 60) {
        theTime1 = parseInt(theTime/60);
        theTime = parseInt(theTime%60);
        //alert(theTime1+"-"+theTime);
        if(theTime1 >= 60) {
            theTime2 = parseInt(theTime1/60);
            theTime1 = parseInt(theTime1%60);
        }
    }
    var result = (theTime<10?"0":"")+parseInt(theTime);
    if(theTime1 >= 0) {
        result =(theTime1<10?"0":"")+parseInt(theTime1)+":"+result;
    }
    if(theTime2 >= 0) {
        result =(theTime2<10?"0":"")+parseInt(theTime2)+":"+result;
    }
    //console.info(result);
    return result;
}
 
// 设置后备时间(续航时间)
function sethoubeiTime(value){
    value = Math.abs(value);
    var str = "";
    if(value>0){
        var hour = parseInt(value);
        var min = parseInt((parseFloat(value) - hour)*60);
        if(hour<10){
            str = "0";
        }
        str+=hour+"H";
        if(min<10){
            str+="0";
        }
        str+=min+"M";
    }else{
        str = "00H00M";        
    }
    return str;
}
 
// 格式化时间
Date.prototype.format =function(format)
{
    var o = {
    "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(),    //day
        "h+" : this.getHours(),   //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S" : this.getMilliseconds() //millisecond
    };
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length==1? o[k] :
    ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
 
function Title(){
    this.min=0;                //最小值
    this.max=0;                //最大值
    this.avg=0;                //平均值
    this.sum=0;                //总和
    this.ahight=0;            //高告警阀值
    this.alow=0;            //低告警阀值
    this.clow=0;            //次低更换阀值
    this.lc=0;                //低告警单体数
    this.lp=0;                //低告警百分比
}
 
function createTitle(){
    var obj=new Object();
    obj.min=0;
    obj.max=0;
    obj.avg=0;
    obj.sum=0;
    obj.ahight=0;
    obj.alow=0;
    obj.clow=0;    
    obj.lc=0;
    obj.lp=0;
    return obj;
}
 
Title.prototype.setMin=function(min){
    this.min=min;
}; 
 
Title.prototype.getMin=function(){
    return this.min;
}; 
 
Title.prototype.setMax=function(max){
    this.max=max;
};
 
Title.prototype.getMax=function(){
    return this.max;
};
 
Title.prototype.setAvg=function(avg){
    this.avg=avg;
};
 
Title.prototype.getAvg=function(){
    return this.avg;
};
 
Title.prototype.setSum=function(sum){
    this.sum=sum;
};
 
Title.prototype.getSum=function(){
    return this.sum;
};
 
Title.prototype.setAhight=function(ahight){
    this.ahight=ahight;
};
 
Title.prototype.getAhight=function(){
return this.ahight;
};
 
Title.prototype.setAlow=function(alow){
    this.alow=alow;
};
 
Title.prototype.getAlow=function(){
    return this.alow;
};
 
Title.prototype.setClow=function(clow){
    this.clow=clow;
};
 
Title.prototype.getClow=function(){
    return this.clow;
};
 
Title.prototype.setLc=function(lc){
    this.lc=lc;
};
 
Title.prototype.getLc=function(){
    return this.lc;
};
 
Title.prototype.setLp=function(lp){
    this.lp=lp;
};
 
Title.prototype.getLp=function(){
    return lp;
};
 
Title.prototype.getAllTile=function(lname){
    //alert(this.avg);
    var title="";
    var maxText='最大值';            //最大值
    var minText='最小值';            //最小值
    var avgText='平均值';             //平均值
    var lowText='落后值';            //落后值
    var lcText='落后数量';                //落后数量
    var lpText='落后数量比';             //落后数量比
    if("Voltage"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(3))+"V;"+minText+"="+(parseFloat(this.min).toFixed(3))+"V;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"V;"+lowText+"="+this.alow+"V;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(3))+"V;"+minText+"="+(parseFloat(this.min).toFixed(3))+"V;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"V;累加和="+(parseFloat(this.sum).toFixed(3))+"V";
    }else if("Resistance"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(3))+"mΩ;"+minText+"="+(parseFloat(this.min).toFixed(3))+"mΩ;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"mΩ;"+lowText+"="+this.alow+"mΩ;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(3))+"mΩ;"+minText+"="+(parseFloat(this.min).toFixed(3))+"mΩ;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"mΩ";
    }else if("Temperature"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(1))+"℃;"+minText+"="+(parseFloat(this.min).toFixed(1))+"℃;"+avgText+"="+(parseFloat(this.avg).toFixed(1))+"℃;"+lowText+"="+this.alow+"℃;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(1))+"℃;"+minText+"="+(parseFloat(this.min).toFixed(1))+"℃;"+avgText+"="+(parseFloat(this.avg).toFixed(1))+"℃";
    }else if("Conductance"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(0))+";"+minText+"="+(parseFloat(this.min).toFixed(0))+";"+avgText+"="+(parseFloat(this.avg).toFixed(0))+";"+lowText+"="+this.alow+";"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(0))+";"+minText+"="+(parseFloat(this.min).toFixed(0))+";"+avgText+"="+(parseFloat(this.avg).toFixed(0));
    }else if("MonJHCurr"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(3))+"V;"+minText+"="+(parseFloat(this.min).toFixed(3))+"V;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"V;"+lowText+"="+this.alow+"V;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(3))+"mA;"+minText+"="+(parseFloat(this.min).toFixed(3))+"mA;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"mA";
    }else if("Serpercent"==lname || "Percent_total_capacity" == lname){
        //title=maxText+"="+this.max+"%;"+minText+"="+this.min+"%;"+avgText+"="+this.avg+"%;"+lowText+"="+this.alow+"%;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+this.max+"%;"+minText+"="+this.min+"%;"+avgText+"="+this.avg+"%";
    }else if("Actual_capacity"==lname || "Residual_capacity" == lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(0))+"AH;"+minText+"="+(parseFloat(this.min).toFixed(0))+"AH;"+avgText+"="+(parseFloat(this.avg).toFixed(0))+"AH;"+lowText+"="+this.alow+"AH;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(0))+"AH;"+minText+"="+(parseFloat(this.min).toFixed(0))+"AH;"+avgText+"="+(parseFloat(this.avg).toFixed(0))+"AH";
    }
    return title;
};
 
var  CapType_Rest = 0;            //当查询剩余容量时传递
var  CapType_Real = 1;            //当查询实际容量时传递
var STDAH;                        //标纯容量
var MonomerVolType;                //电池电压类型
// 获取标纯电流
function GetFDCurrent(stdcap,hourrate)
{
    var res = 0.055;
    switch(hourrate)
    {
        case 1: res = 0.514; break;
        case 2: res = 0.306; break;
        case 3: res = 0.250; break;
        case 4: res = 0.200; break;
        case 5: res = 0.166; break;
        case 6: res = 0.146; break;
        case 7: res = 0.131; break;
        case 8: res = 0.118; break;
        case 9: res = 0.108; break;
        case 10: res = 0.100; break;
        case 20: res = 0.055; break;
        default: res = 0.055; break;
    }
    
    return (stdcap * res);
}
 
// 获取放电小时率        stdah:标纯容量              current:当前电流    
function GetHourRate(stdah,current)
{
    var index = 0;
    var value=[5.14, 3.06, 2.50, 2.00, 1.66, 1.46, 1.31, 1.18, 1.08, 1.00, 0.55, 0.40];
    var res;
    current = Math.abs(current);
    res = current/(stdah/10);
    if(res >= 5.14) return 1;
    else if(res <= 0.55) return 20;
    else
    {
        for(var index=0; index<10; index++)
        {
            if((res<=value[index]) && (res>value[index+1]))
                break;
            else continue;
        }
        if((value[index]-res) < (res-value[index+1]))
        {
            return (index+1);
        }
        else
        {
            if(index+2 > 10) return (20);
            else return (index+2);
        }
    }  
}
 
function N_TO_10H(n_H)
{
    switch(n_H)
    {
        case  1 : return(1/0.55);
        case  2 : return(1/0.61);
        case  3 : return(1/0.75);
        case  4 : return(1/0.79);
        case  5 : return(1/0.833);
        case  6 : return(1/0.876);
        case  7 : return(1/0.917);
        case  8 : return(1/0.944);
        case  9 : return(1/0.974);
        case  10: return(1/1);
        case  20: return(1/1.1);
    }
    return 1.0;
}
 
 
//获取剩余容量    STDAH:标称容量            HourRate:放电小时率    SumAH:测试容量        MaxMonomerVol:最大电池电压        MonomerVol:当前电池组的最低单体电压
//MonomerVolType:电池标称电压        CapType:容量类型(定值是常量)
function GetMonomerCap(STDAH, HourRate, SumAH, MaxMonomerVol, MonomerVol, MonomerVolType,CapType)
{
    if((MaxMonomerVol - MonomerVolType*0.9) <= 0)
        return 0;
    if(SumAH < 0)
        SumAH *= (-1);
    var tmp_cap;
    tmp_cap = MonomerVol - MonomerVolType * 0.9;
    tmp_cap *= (STDAH - SumAH * N_TO_10H(HourRate));
    var dt_vol = MaxMonomerVol - MonomerVolType*0.9;
    if(dt_vol < 0.01)
        dt_vol = 0.01;
    tmp_cap = tmp_cap/dt_vol;
    if(tmp_cap < 0)
        tmp_cap = 0;
 
    if(CapType == CapType_Rest)
        return tmp_cap;
    else if(CapType == CapType_Real)
        return (tmp_cap + SumAH * N_TO_10H(HourRate));
    else
        return ((tmp_cap + SumAH * N_TO_10H(HourRate))*100 / STDAH);
}
 
 
//将秒转化成00M00H类型
function formatTime1(value) {
    var theTime = parseInt(value);// 秒
    var theTime1 = 0;// 分
    var theTime2 = 0;// 小时
    
    if(theTime >= 60) {
        theTime1 = parseInt(theTime/60);
        theTime = parseInt(theTime%60);
        // alert(theTime1+"-"+theTime);
        if(theTime1 >= 60) {
            theTime2 = parseInt(theTime1/60);
            theTime1 = parseInt(theTime1%60);
        }
    }
    var result = "";
    if(theTime1 >= 0) {
        result =(theTime1<10?"0":"")+parseInt(theTime1)+"M"+result;
    }
    if(theTime2 >= 0) {
        result =(theTime2<10?"0":"")+parseInt(theTime2)+"H"+result;
    }
    return result;
}
 
//省-市-区县联动
function LinkagePlus() {
    this.data=[];
    this.provinces = [];
}
LinkagePlus.prototype.setData = function(data) {
    this.data = data;
    this._setProvinces(data);
};
 
// 设置所有的省
LinkagePlus.prototype._setProvinces = function(data) {
    var result = [];
    Object.keys(data).forEach(function(key){
        var temp = {};
        temp.key = key;
        temp.name = data[key].name;
        result.push(temp);
    });
    this.provinces =  result;
};
// 获取指定省的编号
LinkagePlus.prototype._getProvinceKey = function(province) {
    var result = {
        code: 0,
        province: province,
        key: 0,
        msg: '未查询到"'+province+'"的编号'
    };
    var provinces = this.provinces;
    for(var i=0; i<provinces.length; i++) {
        var _province = provinces[i];
        if(_province.name == province) {
            result.code=1;
            result.key = _province.key;
            result.msg = '获取"'+province+'"的编号成功';
        }
    }
 
    return result;
};
// 获取指定市的编号
LinkagePlus.prototype._getCityKey = function(province, city) {
    var result = {
        code: 0,
        province: province,
        provinceKey: 0,
        city: city,
        key: 0,
        msg: '未查询到"'+province+'-'+city+'"的编号'
    };
    var provinceKey = this._getProvinceKey(province);
    result.provinceKey = provinceKey.key;
    if(provinceKey.code == 1) {
        var cities = this.data[provinceKey.key].child;
        Object.keys(cities).forEach(function(key) {
            var _city = cities[key];
            if(_city.name == city || _city.name=='市辖区') {
                result.code = 1;
                result.key = key;
                result.city = _city.name;
                result.msg = '获取"'+province+'-'+_city.name+'"的编号成功';
            }
        });
    }else {
        result = $.extend({}, result, provinceKey);
    }
 
    return result;
};
 
// 获取省列表
LinkagePlus.prototype.getProvince = function() {
    return this.provinces;
};
// 获取市列表
LinkagePlus.prototype.getCity = function(province) {
    var result = {
        code: 0,
        province: province,
        data: [],
        msg:'未获取到"'+province+'"的市'
    };
    var provinceKey = this._getProvinceKey(province);
    if(provinceKey.code == 1) {
        var cities = this.data[provinceKey.key].child;
        result.code = 1;
        result.data = this._formatCity(cities);
        result.msg = '获取"'+province+'"的市成功';
    }
    return result;
};
// 格式化获取到的数据
LinkagePlus.prototype._formatCity = function(data) {
    var result = [];
    Object.keys(data).forEach(function(key){
        var temp = {};
        temp.key = key;
        temp.name = data[key].name;
        result.push(temp);
    });
    return result;
};
// 获取市区县
LinkagePlus.prototype.getCounty = function(province, city) {
    var result = {
        code: 0,
        province: province,
        city: city,
        data: [],
        msg:'未获取到"'+province+'-'+city+'"的区县'
    };
    var cityKey = this._getCityKey(province, city);
    if(cityKey.code == 1) {
        var cities = this.data[cityKey.provinceKey].child;
        var counties = cities[cityKey.key].child;
        result.code = 1;
        result.data = this._formatCounty(counties);
        result.city = cityKey.city;
        result.msg = '获取"'+province+'-'+cityKey.city+'"的区县成功';
    }else {
        result = $.extend({}, result, cityKey);
    }
    return result;
};
// 格式化获取到的数据
LinkagePlus.prototype._formatCounty = function(data) {
    var result = [];
    Object.keys(data).forEach(function(key){
        if(data[key] != '市辖区') {
            var temp = {};
            temp.key = key;
            temp.name = data[key];
            if(data[key])
            result.push(temp);
        }
        
    });
    return result;
};
 
export {
    CORS,           //  跨域请求内容
    Interval,       // 计时器
    Timeout,        // 延时计时器
    getMaxFromArr,  // 获取数组中的最大值
    getMinFromArr,   // 获取数组中的最小值
    ajax,            // 请求
    formatSeconds,   // 将秒转化成时:分:秒
    Title,
    getSumFromArr,   // 获取累加和
    getAvgFromArr,   // 获取平均值
    formatTime1,    // 续航时长
    GetMonomerCap,  // 剩余容量
    GetHourRate,    // 放电小时率
    LinkagePlus,    // 省-市-区县
}