81041
2019-12-27 db534d98d3cc95b3949fee9b9f929c697b650c09
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
'use strict';
 
//var CORS = 'http://118.89.139.230:8919/Device_Manage/';        // 开启跨域请求(调试时开启)
//var CORS = 'http://49.234.20.113:8919/Device_Manage/';        // 开启跨域请求(调试时开启)
var 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) {
    // bui.ajax({
    //     method: 'POST',
    //     async: true,
    //     url: CORS+option.url,
    //     data: option.data,
    //     dataType: 'json',
    //     needNative: true,
    // }).then(function(res) {
    //     if(typeof(option.success) == 'function') {
    //         option.success(res);
    //     }
    //     if(typeof(option.complete) == 'function') {
    //         option.complete();
    //     }
    // },
    // function() {
    //     if(typeof(option.complete) == 'function') {
    //         option.complete();
    //     }
    // });
    $.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;
};
 
// 根据设备的id获取设备的基本信息
function getDevBaseInfo(dev_id) {
    var result = {
        name: '未知',
        key: '未知',
        reg: '',
        workstates: [],
        stopreasons: [],
        alarmstates: []
    };
    // 遍历DEVICEINFO
    for (var i = 0; i < DEVICEINFO.length; i++) {
        var data = DEVICEINFO[i];
        if (regEquipType(dev_id, data.reg)) {
            result = data;
        }
    }
    return result;
}
 
// 验证设备类型
function regEquipType(eId, pattern) {
    if (pattern.test(eId)) {
        return true;
    }
    return false;
}
 
function getBattstate(state) {
    var str = "";
    switch (state) {
        case 1:
            str = battstate[1];break;
        case 2:
            str = battstate[2];break;
        case 3:
            str = battstate[3];break;
        case 4:
            str = battstate[4];break;
        default:
            str = battstate[0];
    }
    return str;
}
 
// 对数据进行处理
var HandleData = function HandleData() {};
// 处理数据
HandleData.prototype.handle = function (oldData, newData, keys) {
    var result = {
        del: [],
        add: []
    };
    // 添加被删除的数据
    this._addDel(result, oldData, newData, keys);
 
    // 添加被添加的数据
    this._addAdd(result, oldData, newData, keys);
 
    // 删除数据
    this._del(result, oldData, keys);
 
    // 添加数据
    this._add(result, oldData, keys);
};
// 添加被删除的数据
HandleData.prototype._addDel = function (result, oldData, newData, keys) {
    // 遍历old数据并和newData对比,筛选出需要删除的数据
    for (var i = 0; i < oldData.length; i++) {
        var _oldData = oldData[i];
        var exist = this.checkExistObj(_oldData, newData, keys);
        if (!exist) {
            result.del.push(_oldData);
        }
    }
};
 
// 添加被添加的数据
HandleData.prototype._addAdd = function (result, oldData, newData, keys) {
    // 遍历new数据和old数据对比,筛选出需要添加的数据
    for (var i = 0; i < newData.length; i++) {
        var _newData = newData[i];
        var exist = this.checkExistObj(_newData, oldData, keys);
        if (!exist) {
            result.add.push(_newData);
        }
    }
};
 
// 删除数据
HandleData.prototype._del = function (result, oldData, keys) {
    // 删除数据
    for (var i = 0; i < result.del.length; i++) {
        var del = result.del[i];
        for (var k = 0; k < oldData.length; k++) {
            var _oldData = oldData[k];
            var equal = this.checkObjEqual(_oldData, del, keys);
            if (equal || typeof equal == 'undefined') {
                oldData.splice(k, 1);
                break;
            }
        }
    }
};
 
// 添加数据
HandleData.prototype._add = function (result, oldData, keys) {
    // 添加数据
    for (var i = 0; i < result.add.length; i++) {
        var add = result.add[i];
        oldData.push(add);
    }
};
 
// 检测对象是否存在于对象集合
HandleData.prototype.checkExistObj = function (obj, list, keys) {
    var exist = false;
    // 遍历list
    for (var i = 0; i < list.length; i++) {
        var _list = list[i];
        // 遍历键值集合
        var equal = this.checkObjEqual(obj, _list, keys);
        // 对象与对象集合中第i个等效
        if (equal) {
            exist = true;
            break;
        }
    }
    // 返回内容
    return exist;
};
 
// 检测对象是否等效(返回undefined说明keys存在问题)
HandleData.prototype.checkObjEqual = function (obj1, obj2, keys) {
    // 遍历键值集合
    var equal = true;
    for (var k = 0; k < keys.length; k++) {
        var key = keys[k];
        if (obj1.hasOwnProperty(key) && obj2.hasOwnProperty(key)) {
            if (obj1[key] != obj2[key]) {
                equal = false;
                break;
            }
        } else {
            equal = undefined;
        }
    }
    return equal;
};
 
// F2封装
function FGraph(el, type) {
    this.id = el.getAttribute("id");
    this.minMax = {
        min: 0,
        max: 0
    };
    this.colors = {
        min: 'red',
        max: 'green',
        normal: 'blue'
    };
    this.chart = "";
    this.txtShapes = {
        show: true,
        list: []
    };
    if (this.id) {
        this._init(this.id, type);
    } else {
        this._init(null, type);
    }
};
 
// 初始化
FGraph.prototype._init = function (id, type) {
    var self = this;
    // 创建 Chart 对象
    this.chart = new F2.Chart({
        id: id,
        pixelRatio: window.devicePixelRatio, // 指定分辨率
        syncY: true,
        animate: false
    });
 
    // 设置x轴线
    this.chart.axis("x", {
        line: {
            lineWidth: 1,
            stroke: '#bbb',
            top: true // 展示在最上层
        }
    });
    // 设置y轴线
    this.chart.axis("y", {
        line: {
            lineWidth: 1,
            stroke: '#bbb',
            top: true // 展示在最上层
        },
        grid: {
            lineWidth: 1,
            stroke: '#bbb'
        }
    });
 
    // 设置提示框
    this.chart.tooltip({
        alwaysShow: false
    });
 
    // 载入数据源
    this.chart.source([]);
 
    // 设置最大值和最小值
    this.setMinMax([]);
 
    // 设置图表类型
    this._setType(type);
 
    // 不显示legend
    this.chart.legend(false);
 
    // 渲染图表
    this.chart.render();
};
// 设置图表类型
FGraph.prototype._setType = function (type) {
    var self = this;
    // 根据类型设置图表
    switch (type) {
        case 'line':
            this.chart.line().position('x*y');
            // 不显示文本
            this.txtShapes.show = false;
            break;
        default:
            // 设置颜色
            this.chart.interval().position('x*y').color('x*y', function (x, y) {
                var minMax = self.minMax;
                var colors = self.colors;
                var color = colors.normal;
                if (y == minMax.min) {
                    color = colors.min;
                } else if (y == minMax.max) {
                    color = colors.max;;
                }
                return color;
            });
            break;
    }
};
// 更新数据
FGraph.prototype.changeData = function (data) {
    // 设置最大值和最小值
    this.setMinMax(data);
    // 更改数据
    this.chart.changeData(data);
};
 
// 设置文本
FGraph.prototype._addTxtShape = function (data) {
    //  判断是否显示文本
    if (!this.txtShapes.show) {
        return;
    }
    var self = this;
    var chart = this.chart;
    // 移除文本
    this._txtShapesDestory();
    // 绘制柱状图文本
    var offset = -5;
    var canvas = chart.get('canvas');
    var group = canvas.addGroup();
    data.forEach(function (obj) {
        var point = chart.getPosition(obj);
        var text = group.addShape('text', {
            attrs: {
                x: point.x,
                y: point.y + offset,
                text: obj.y,
                textAlign: 'center',
                textBaseline: 'bottom',
                fill: '#808080'
            }
        });
        self.txtShapes.list.push(text);
    });
};
// 修改大小
FGraph.prototype.changeSize = function (width, height) {
    width = width ? width : null;
    height = height ? height : null;
    this.chart.changeSize(width, height);
};
 
// 根据data的值获取最大值和最小值
FGraph.prototype.setMinMax = function (data) {
    var rs = {
        min: 0,
        max: 0
    };
    // 设置data第一个值到rs中
    if (data.length != 0) {
        rs.min = data[0].y;
        rs.max = data[0].y;
    }
    // 遍历data的值
    for (var i = 1; i < data.length; i++) {
        var _data = data[i];
        // 设置最大值和最小值
        rs.min = rs.min > _data.y ? _data.y : rs.min;
        rs.max = rs.max < _data.y ? _data.y : rs.max;
    }
    this.minMax = rs;
};
// 销毁txtShapes
FGraph.prototype._txtShapesDestory = function () {
    var txtShapes = this.txtShapes.list;
    // 遍历txtShapes
    for (var i = 0; i < txtShapes.length; i++) {
        var txtShape = txtShapes[i];
        // 销毁
        txtShape.destroy();
    }
    // 重置数组
    this.txtShapes.list = [];
};