riku
2024-02-04 b6ce6a9739d7114ee740981cbbeabbf1e7e10f28
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
;
// (function($, document, CheckBox, FChart, echarts, HttpService, DataUtil, MapUtil, Util) {
function RealTimeMode(options) {
  this.inited = false
  this.isEnable = false
 
  this.factorMode = 'type0' //type0: 无人机和车载;type1:无人船
 
  this.fetchingTask = undefined
  this.isFetching = false //记录本次获取实时数据操作是否完成
  this.lastestTime = '' //当前获取的数据的最新时间
  this.deviceCode = '0a0000000001' // 当前展示的设备编号
  this.originData = [] // 原始数据
 
  this.selectedFactor = ["1"] //实时仪表盘默认展示的监测因子
  this.lastestData = new Object() // 最新的一个实时数据
  this.chartOptions = new Map() //实时仪表盘的配置
 
  this.lineChart
  this.lineChartOption
  this.xAxis = []
  this.realTimeDatas = {
    datas: [],
    physicalQuantity: []
  } //实时数据
 
  this.setCenter = true
  this.factorType = 0 //3d地图当前展示的监测因子id
  this.factorName = 'NO2'
  this.drawMode = 0 //绘制模式,0:自动模式,自动计算当前数据的范围,绘制合适的比例;1:手动模式,根据页面设置的绘图范围进行绘制
  /**
   * {
   * factorId: Factor
   * }
   */
  this.factorDatas = new FactorDatas()
 
  this.mapMaker = options.mapMaker
 
}
 
RealTimeMode.prototype = {
  init: function() {
    this.initChart()
    this.initLineChart()
    this.initDeviceType()
    var that = this
    $('#realtime_device_code').on('change', function () {
      that.deviceCode = $(this).val()
      
        // that.mapMaker.stop()
 
        // that.initChart()
        // that.initCheckBox()
 
        // that.fetchingData()
    })
    $('#btn_r_search').on('click', function () {
      
        that.mapMaker.stop()
        MapUtil
 
        that.initChart()
        that.initCheckBox()
 
        that.fetchingData()
    })
  },
  enable: function() {
    this.isEnable = true
    this.uichange(true)
    // this.mapMaker.changeSpeed(1)
    this.mapMaker.setDynamicSpeed(true, 4)//打开动态绘制速度调整,并设置两个坐标点之间的时间间隔
      MapUtil.clear3D()
    if (!this.inited) {
      this.fetchingData()
      this.inited = true
    } else {
      //todo 1.将原来保存的数据重新绘制
      //todo 2.重新刷新界面上的绘制范围
    }
  },
  disable: function() {
    this.isEnable = false
    this.uichange(false)
    this.mapMaker.stop()
  },
  uichange: function(isShow) {
    // var r = $('#btn_realtime')
    // r.addClass('btn-unselected')
    if (isShow) {
      setTimeout(() => {
        // r.removeClass()
        // r.addClass('btn-selected margin-left-8')
 
        $('#realtimebox').slideDown("fast")
        $('#realtimebox2').slideDown("fast")
        // $('#backgroundbox').slideDown("fast")
        $('#rmode_type').slideDown("fast")
 
        $('#legend').css('right', '0px')
        $('#legend').css('left', 'auto')
        $('#rangeset').removeClass('map-range-set')
        $('#rangeset').addClass('map-range-set-2')
 
        $('#rangeset').slideUp('fast')
        $('#legend').show("fast")
 
        $('.map-factor-selector').show("fast")
      }, 500)
    } else {
      // DataUtil.hideAll()
    }
  },
  //设置绘图范围
  setRange: function(range) {
    this.drawMode = 1
    this.factorDatas.setRange(this.factorType + 1 + "", range)
    this.drawMesh(false, this)
  },
  resetRange: function() {
    this.drawMode = 0
    this.factorDatas.resetRange(this.factorType + 1 + "")
    this.drawMesh(false, this)
  },
  standardRange: function() {
    this.factorDatas.standardRange(this.factorType + 1 + "")
    this.drawMesh(false, this)
  },
  //设置3d地图当前展示的监测因子
  setFactorType: function(factorType, factorMode, factorName) {
    this.factorMode = factorMode
    this.factorType = factorType
    this.factorName = factorName
    this.mapMaker.setFactorType(factorType)
    this.refreshLegend(this.factorDatas, true)
    if (!this.mapMaker.runStatus()) {
      this.drawMesh(false, this)
    }
  },
  /********************************* 设备类型选择 - start ************************************/
  initDeviceType: function() {
    $('#rmode_type_box').empty()
    CheckBox.newRadioBox('rmode_type_box', 'deviceType', Util.deviceType, ['0a'], this.onChangeType, this)
  },
  //切换类型。目前一共有两组数据,车载和无人机为一组,无人船为一组,具体监测因子见 util.js
  onChangeType: function(obj, that) {
    // $(obj).attr('checked', true)
    var type = obj.value.split(',')[0]
    var tD = $('#realtime_device_code')
    tD.empty()
    var options2 = []
    switch (type) {
      case "0a":
        that.factorMode = "type0"
        options2.push('0a0000000001')
        options2.push('0a0000000002')
      case "0b":
        that.factorMode = "type0"
        options2.push('0b0000000001')
            options2.push('0b0000000002')
        break;
      case "0c":
        that.factorMode = "type1"
        options2.push('0c0000000001')
        break;
      default:
        that.factorMode = "type0"
        options2.push('0a0000000001')
        options2.push('0a0000000002')
        break;
    }
    options2.forEach(element => {
      var op = $('<option></option>')
      op.text(element)
      tD.append(op)
    });
    that.deviceCode = tD.val()
  },
  /********************************* 设备类型选择 - end ************************************/
  /********************************* 实时数据获取 - start ************************************/
  fetchInit: function() {
    this.isFirstFetch = true
    this.originData = []
    this.xAxis = []
    this.realTimeDatas = {
      datas: [],
      physicalQuantity: []
    }
    this.factorDatas.clearData()
  },
  fetchingData: function() {
    console.log('deviceCode: ' + this.deviceCode);
    this.fetchInit()
    var that = this
    // 先获取最新的100个数据作为初始数据进行图表的绘制
    DataUtil.toggleLoading()
    HttpService.getRealtimeData(that.deviceCode, 100, function(datas) {
      // HttpService.getHistoryData(that.deviceCode, "2021-11-01 10:00:00", "2021-11-01 10:00:20", 1, 100, function(datas) {
      DataUtil.toggleLoading()
      if (datas.length == 0) {
        that.lastestTime = '----/--/-- --:--:--'
      } else {
        var type = DataUtil.refreshVehicleType(that.deviceCode)
        that.mapMaker.setVehicleType(type)
        
        // 更新最新的数据时间
        that.lastestTime = datas[datas.length - 1].time
        // datas.forEach(d => {
        that.onOneData(datas, that)
        // });
        that.onFetchDataDone(that)
      }
    })
 
    if (that.fetchingTask != undefined) {
      clearInterval(that.fetchingTask)
      that.isFetching = false
    }
 
    var debugCount = 0
    // 开始定时获取最新的数据
    that.fetchingTask = setInterval(() => {
 
      if (!that.isEnable || that.isFetching) {
        console.log("disEnable");
        return
      }
      var perPage = 3
      if (debugCount == 3) {
        perPage = 50
        debugCount = 0
      }
 
      that.isFetching = true
      HttpService.getNextData(that.deviceCode, that.lastestTime, perPage, function(datas) {
        console.log(new Date());
        if (datas.length == 0) {
          that.isFetching = false
          return
        }
        // 更新最新的数据时间
        that.lastestTime = datas[datas.length - 1].time
        // datas.forEach(d => {
        that.onOneData(datas, that)
        // });
        that.onFetchDataDone(that)
 
        that.isFetching = false
      })
 
      debugCount++
    }, 10000);
  },
  onOneData: function(data, that) {
    if (data.length == 0) {
      return
    }
    if (data[0].lng == 0) {
      return
    }
    that.originData.push.apply(that.originData, data)
    that.onLineChartOneData(data, that)
    that.onMapOneData(data, that)
  },
  onFetchDataDone: function(that) {
    // 根据当前绘图范围统一计算所有点的高度
    // for (const k in that.factorDatas.factor) {
    //     that.factorDatas.factor[k].getHeight()
    // }
    // 刷新仪表盘数据
    that.refreshChartData(that)
    // 刷新折线图数据
    that.refreshLineChart(true, that)
    // 刷新3d地图
    // that.drawMesh(false, that)
  },
  /********************************* 实时数据获取 - end ************************************/
 
  /********************************* 实时仪表盘区域 - start ************************************/
  initChart: function() {
    // 1.初始化监测因子复选框
    $('#realtime_chart_factorbox').empty()
    CheckBox.newCheckBox('realtime_chart_factorbox', Util.selectOptions3[this.factorMode], this.selectedFactor, this.onCheckChart, this)
  },
  // 监测因子复选框点击事件监听
  onCheckChart: function(obj, that) {
    var factorType = obj.value
    that.refreshFactors(factorType, that)
  },
  // 刷新监测因子选择记录
  refreshFactors: function(factorType, that) {
    var index = that.selectedFactor.indexOf(factorType)
    if (index == -1) {
      that.selectedFactor.push(factorType)
    } else {
      that.selectedFactor.splice(index, 1)
    }
    that.refreshChart(that.selectedFactor, that)
  },
  // 根据选择的监测因子id显示或隐藏仪表盘
  refreshChart: function(factorIdList, that) {
    var datas = []
    that.lastestData.values.forEach(f => {
      if (factorIdList.indexOf(f.factorId) != -1) {
        datas.push(f)
      }
    });
 
    var chartDiv = $('#realtime_chart')
    var needHideCharts = []
    var a = chartDiv.children()
    for (let i = 0; i < a.length; i++) {
      const d = a[i];
      needHideCharts.push(d.id)
    }
 
    datas.forEach(e => {
      var chartId = "realtime_chart_" + e.factorId
 
      var index = needHideCharts.indexOf(chartId)
      if (index != -1) {
        needHideCharts.splice(index, 1)
      }
 
      if (document.getElementById(chartId)) {
        $('#' + chartId).fadeIn('fast')
      } else {
        var div = document.createElement("div")
        div.id = chartId
        div.style.height = '280px';
        div.style.width = '260px';
        chartDiv.append(div)
 
        var gauge = echarts.init(div)
        var option = FChart.genGauge(gauge, e.factorName + '\n(μg/m³)', e.factorData)
        this.chartOptions.set(e.factorId, [gauge, option])
      }
    });
 
    needHideCharts.forEach(e => {
      $('#' + e).fadeOut('fast')
    });
  },
  // 刷新仪表盘数据
  refreshChartData: function(that) {
    // console.log("newData");
    if (!that.originData || that.originData.length == 0) return
    var data = that.originData[that.originData.length - 1].values
    $('#realtime_temp').text("温度:" + data[7].factorData.toFixed(2) + "℃")
    $('#realtime_hum').text("湿度:" + data[8].factorData.toFixed(2) + "%")
    $('#realtime_v').text("车速:" + data[13].factorData.toFixed(2) + "km/h")
    $('#realtime_wind_direction').text("风向:" + Util.windDir(data[16].factorData))
    $('#realtime_wind_speed').text("风速:" + data[15].factorData.toFixed(2) + "m/s")
    $('#realtime_time').text("采样时间:" + data[14].statusList[0])
 
    var isFirst = that.lastestData.values == undefined
    that.lastestData['values'] = data
    if (isFirst) {
      that.refreshChart(that.selectedFactor, that)
    }
 
    for (const e of data) {
      var o = that.chartOptions.get(e.factorId)
      if (o != undefined) {
        o[1].series[0].data[0].value = e.factorData;
        o[0].setOption(o[1], true);
      }
    }
  },
  /********************************* 实时仪表盘区域 - end ************************************/
 
  /********************************* 实时折线图区域 - start ************************************/
  initLineChart: function() {
    // 1.初始化折线图
    var div = document.getElementById('chart_2')
    $('#chart_2').empty()
    this.lineChart = echarts.init(div)
    // 2.初始化监测因子复选框
    this.initCheckBox()
    // 3.创建初始折线图
    this.drawLineChart(undefined, undefined, this)
  },
  initCheckBox: function() {
    $('#realtime_chart_2_factorbox').empty()
    CheckBox.newCheckBox('realtime_chart_2_factorbox', Util.selectOptions3[this.factorMode], ['1'], this.onCheckLineChart, this)
  },
  // 复选框监测因子监听事件
  onCheckLineChart: function(obj, that) {
    var _factorType = parseInt(obj.value) - 1
 
    that.realTimeDatas.datas[_factorType].show = obj.checked
    that.realTimeDatas.physicalQuantity[_factorType].show = obj.checked
 
    var series = that.realTimeDatas.datas
    that.lineChartOption = FChart.lineChartOnLegend(that.lineChart, that.xAxis, series, that.lineChartOption)
  },
  // 格式化单个数据
  onLineChartOneData: function(dataList, that) {
    dataList.forEach(data => {
      that.xAxis.push(data.time.split(" ")[1])
      for (let i = 0; i < data.values.length; i++) {
        var v = data.values[i]
        if (that.realTimeDatas.datas.length < i + 1) {
          that.realTimeDatas.datas.push({
            name: Util.factorName[v.factorName],
            type: 'line',
            data: [v.factorData],
            showAllSymbol: true,
            animationDelay: function(idx) {
              return idx * 10;
            },
            show: i == 0
          })
          that.realTimeDatas.physicalQuantity.push({
            name: Util.factorName[v.factorName] + '-物理量',
            type: 'line',
            data: [v.physicalQuantity],
            showAllSymbol: true,
            show: i == 0
          })
        } else {
          that.realTimeDatas.datas[i].data.push(v.factorData)
          that.realTimeDatas.physicalQuantity[i].data.push(v.physicalQuantity)
        }
      };
    });
  },
  // 创建初始折线图
  drawLineChart: function(data, merge, that) {
    that.lineChartOption = FChart.genLineChart(that.lineChart, that.xAxis, [], false)
  },
  refreshLineChart: function(merge, that) {
    if (merge) {
      var series = that.realTimeDatas.datas
      that.lineChartOption = FChart.lineChartOnLegend(that.lineChart, that.xAxis, series, that.lineChartOption, 100, 100)
    } else {
      //x坐标
      that.xAxis = []
      //y轴数据集
      that.realTimeDatas.datas = []
      that.realTimeDatas.physicalQuantity = []
 
      that.drawLineChart(undefined, merge, that)
    }
  },
  /********************************* 实时折线图区域 - end ************************************/
 
  /********************************* 实时3d曲线图区域 - start ************************************/
  onMapOneData: function(dataList, that) {
    var startIndex = that.factorDatas.length() - 1
    if (that.isFirstFetch) {
      startIndex = dataList.length - 2
      that.isFirstFetch = false
    }
    startIndex = startIndex < 0 ? 0 : startIndex
    that.factorDatas.addData(dataList, that.drawMode, true, function () {
      that.refreshLegend(that.factorDatas)
 
      that.mapMaker.moveAnimation(that.factorDatas, that.factorType, startIndex)
    })
 
    // var gps = []
    // dataList.forEach(data => {
    //   gps.push([data.lng, data.lat])
    // });
 
    // MapUtil.convertFromGPS(gps, function(result) {
      
    // })
  },
  refreshLegend: function(_factorDatas, animation) {
    var f = _factorDatas.factor[this.factorType + 1 + ""]
    $('#in_min').val(f.min.toFixed(1))
    $('#in_max').val(f.max.toFixed(1))
    Legend.refreshLegend('legend_content', f.factorName, animation == true, f.legendType, f.min, f.max)
  },
  drawMesh: function(merge, that) {
    // 1.获取当前选择的监测因子的数据
    var f = that.factorDatas.factor[that.factorType + 1 + ""]
    // 3.绘制3d图形
    MapUtil.drawMesh_Test(that.factorDatas.lnglats_GD, that.factorDatas.coors_GD, f.heights, f.colors, f.bottomColor, merge, that.setCenter)
 
    // that.setCenter = false
  },
  /********************************* 实时3d曲线图区域 - end ************************************/
}
 
$.fn.realTimeMode = function(options) {
  return new RealTimeMode(options);
}
// })(jQuery, document, CheckBox, FChart, echarts, HttpService, DataUtil, MapUtil, Util);