monitor.js 10.8 KB
import EventBus from '../event-bus'
import mapService from './mapService.js'
import MAPGEO from './mapgeo'
import { Message } from 'element-ui'
import { services } from '../api'
const MONITOR_SERVICE = services.monitor

// 车辆监控
const monitor = {
  data: [],
  markers: {},
  infowins: {},
  allData: [],
  sourceType: '', // '':全部,'1':'有线', '0':'无线'
  isCompare: false,
  compareLine: null,
  compareText: null,
  bindEvent() {
    EventBus.$on('uiMap.monitor.draw', (data, autoOpenLastInfoWindow) => {
      this.draw(data, autoOpenLastInfoWindow)
    })

    EventBus.$on('uiMap.monitor.destroy', snSourceType => {
      const MAP = mapService.map
      if (snSourceType) {
        MAP.remove(this.markers[snSourceType])
        MAP.remove(this.infowins[snSourceType])
        delete this.markers[snSourceType]
        delete this.infowins[snSourceType]
        EventBus.$off(`uiMap.monitor.changeStatus.${snSourceType}`)
      } else {
        for (const snSourceType in this.markers) {
          MAP.remove(this.markers[snSourceType])
        }
        for (const snSourceType in this.infowins) {
          MAP.remove(this.infowins[snSourceType])
          EventBus.$off(`uiMap.monitor.changeStatus.${snSourceType}`)
        }
        this.markers = {}
        this.infowins = {}
        this.data = []
        this.allData = []
        this.clearTimeoutIds()
      }
    })

    EventBus.$on('uiMap.monitor.compareDestroy', () => {
      this.compareDestroy()
    })

    EventBus.$on('uiMap.monitor.changeSourceType', sourceType => {
      const MAP = mapService.map
      const rourceReg = /^\d+\:(\d)$/
      if (sourceType === '') {
        for (const snSourceType in this.markers) {
          this.markers[snSourceType].show()
        }
      } else {
        for (const snSourceType in this.markers) {
          const execResult = rourceReg.exec(snSourceType)
          if (execResult) {
            if (execResult[1] === sourceType) {
              this.markers[snSourceType].show()
            } else {
              this.markers[snSourceType].hide()
            }
          }
        }
        for (const snSourceType in this.infowins) {
          this.infowins[snSourceType].close()
        }
      }
    })

    EventBus.$on('uiMap.monitor.openInfoWindow', snSourceType => {
      if (this.markers[snSourceType]) {
        const MAP = mapService.map
        const lnglat = this.markers[snSourceType].getPosition()
        this.infowins[snSourceType].open(MAP, lnglat)
        MAP.setZoomAndCenter(MAP.getZoom(), lnglat)
      }
    })

    EventBus.$on('uiMap.monitor.changeELocate', item => {
      const snSourceType = `${item.sn}:${item.isSource}`
      const eLocation = item[item.eLocateType]
      if (this.markers[snSourceType]) {
        this.markers[snSourceType].setPosition(
          mapService.getLngLat(
            parseFloat(eLocation.elng),
            parseFloat(eLocation.elat)
          )
        )
        this.infowins[snSourceType].setPosition(
          mapService.getLngLat(
            parseFloat(eLocation.elng),
            parseFloat(eLocation.elat)
          )
        )
      }

      this.isCompare && this.compareLocation()
    })

    EventBus.$on('uiMap.monitor.compareLocation', data => {
      this.compareDestroy()
      this.isCompare = true
    })

    EventBus.$on('uiMap.monitor.wifiLocate', data => {
      EventBus.$emit('uiMap.monitor.terminate')
      const { info, infoDiv, gpsBtn, baseBtn, wifiBtn } = data
      MONITOR_SERVICE.getWifiLocationBySn({
        apType: 5,
        sn: info.sn
      }).then(res => {
        if (res.returnCode == 0) {
          const data = res.data
          if (data) {
            const transferObj = { elat: data.wifiLat, elng: data.wifiLng }
            MAPGEO.transferAddressByGroup({ data: [transferObj] }, () => {
              infoDiv.querySelector('[data-transferAddress]').textContent =
                transferObj.transferAddress
              infoDiv.querySelector('[data-time]').textContent =
                data.lastReportTime
            })

            const snSourceType = `${info.sn}:${info.isSource}`
            if (this.markers[snSourceType]) {
              this.markers[snSourceType].setPosition(
                mapService.getLngLat(
                  parseFloat(transferObj.elng),
                  parseFloat(transferObj.elat)
                )
              )
              this.infowins[snSourceType].setPosition(
                mapService.getLngLat(
                  parseFloat(transferObj.elng),
                  parseFloat(transferObj.elat)
                )
              )
            }
            gpsBtn && gpsBtn.classList.remove('el-button--primary')
            baseBtn && baseBtn.classList.remove('el-button--primary')
            wifiBtn && wifiBtn.classList.add('el-button--primary')
            info.eLocateType = 'wifi'
          } else {
            Message({
              type: 'info',
              message: '暂无wifi数据'
            })
          }
        }
        EventBus.$emit('uiMap.monitor.drawDone')
      })
    })

    // 激活位置点
    EventBus.$on('uiMap.monitor.drawMaker', this.drawMaker)
  },
  draw(data, autoOpenLastInfoWindow) {
    const MAP = mapService.map
    this.data = data
    const transferObj = []

    data.forEach(item => {
      if (item.gpsLocation.elat && item.gpsLocation.elng) {
        transferObj.push(item.gpsLocation)
      }
      if (item.bsLocation.elat && item.bsLocation.elng) {
        transferObj.push(item.bsLocation)
      }
    })

    MAPGEO.transferAddressByGroup({ data: transferObj }, () => {
      this.data.forEach((item, idx) => {
        const eLocation = item[item.eLocateType]
        if (!eLocation.elng && !eLocation.elat) return
        let isInfoOpen = false
        const tag = `${item.sn}:${item.isSource}`
        if (this.infowins[tag] && this.infowins[tag].getIsOpen()) {
          isInfoOpen = true
        }

        let infoWindow
        const snSourceType = `${item.sn}:${item.isSource}`
        if (this.markers[tag]) {
          this.markers[tag].setPosition(
            mapService.getLngLat(
              parseFloat(eLocation.elng),
              parseFloat(eLocation.elat)
            )
          )
          // this.infowins[tag].setPosition(mapService.getLngLat(parseFloat(eLocation.elng), parseFloat(eLocation.elat)));
          // infoWindow = mapService.bindMonitorInfoWindow(this.markers[tag], MAP, item, isInfoOpen);
          this.infowins[tag] = mapService.bindMonitorInfoWindow(
            this.markers[tag],
            MAP,
            item,
            isInfoOpen,
            !!this.isCompare
          )
          EventBus.$emit(`uiMap.monitor.changeStatus.${snSourceType}`)
        } else {
          this.infowins[tag] && MAP.remove(this.infowins[tag])
          const marker = mapService.createCarMarker(
            [parseFloat(eLocation.elng), parseFloat(eLocation.elat)],
            MAP
          )
          infoWindow = mapService.bindMonitorInfoWindow(
            marker,
            MAP,
            item,
            isInfoOpen,
            !!this.isCompare
          )
          this.markers[tag] = marker
          this.infowins[tag] = infoWindow
        }

        if (item.alarmModelList && item.alarmModelList.length) {
          mapService.setAlarmIcon(this.markers[tag])
        } else {
          mapService.setDefaultIcon(this.markers[tag], item.vehicleIcon)
        }

        if (autoOpenLastInfoWindow && idx === 0) {
          EventBus.$emit('uiMap.monitor.openInfoWindow', snSourceType)
        }
      })
      // mapService.setFitView(MAP);
      this.allData = this.allData.concat(data)
      EventBus.$emit('uiMap.monitor.drawDone')
      if (this.isCompare) {
        this.compareLocation(true)
      }
    })
  },
  compareLocation(isFitView) {
    if (this.data.length < 2) return
    const MAP = mapService.map
    if (!this.compareLine) {
      this.compareLine = new AMap.Polyline({
        map: MAP,
        strokeColor: '#80d8ff',
        isOutline: true,
        outlineColor: 'white'
      })
    }
    if (!this.compareText) {
      this.compareText = new AMap.Text({
        map: MAP,
        text: '',
        style: {
          'background-color': '#29b6f6',
          'border-color': '#e1f5fe',
          'font-size': '12px'
        }
      })
    }

    const m1 = this.markers[`${this.data[0].sn}:${this.data[0].isSource}`]
    const m2 = this.markers[`${this.data[1].sn}:${this.data[1].isSource}`]
    const p1 = m1.getPosition()
    const p2 = m2.getPosition()

    var textPos = p1.divideBy(2).add(p2.divideBy(2))
    var distance = Math.round(p1.distance(p2))

    var path = [p1, p2]
    this.compareLine.setPath(path)

    this.compareText.setText(
      `两点相距${distance > 1000 ? distance / 1000 + 'k' : distance}m`
    )
    this.compareText.setPosition(textPos)

    mapService.setFitView(MAP)
    /* let ms = []
        let infos = []
        for(let snSourceType in this.markers){
            ms.push(this.markers[snSourceType])
        }
        for(let snSourceType in this.infowins){
            infos.push(this.infowins[snSourceType])
        }
        console.log(infos) */
    // MAP.setFitView(ms.concat(infos))
    // isFitView && mapService.setFitView(MAP)
  },
  intervalDraw() {},
  compareDestroy: function() {
    const MAP = mapService.map

    for (const snSourceType in this.markers) {
      MAP.remove(this.markers[snSourceType])
    }
    for (const snSourceType in this.infowins) {
      MAP.remove(this.infowins[snSourceType])
    }
    this.markers = {}
    this.infowins = {}
    this.data = []
    this.allData = []

    this.isCompare = false
    this.compareLine && MAP.remove(this.compareLine)
    this.compareText && MAP.remove(this.compareText)
    this.compareLine = null
    this.compareText = null
    this.clearTimeoutIds()
  },
  clearTimeoutIds() {
    this.timeoutIds &&
      this.timeoutIds.forEach(item => {
        item && window.clearTimeout(item)
      })
    this.timeoutIds = []
  },

  // 激活位置
  drawMaker(data, autoOpenLastInfoWindow) {
    const MAP = mapService.map
    const marker = mapService.createCarMarker(
      [parseFloat(data.lng), parseFloat(data.lat)],
      MAP
    )
    const infoWindow = mapService.bindActiveLocationInfoWindow(
      marker,
      MAP,
      data,
      true
    )
    // this.markers[tag] = marker;
    // this.infowins[tag] = infoWindow;

    // 设置图标
    mapService.setDefaultIcon(marker, data.vehicleIcon)

    if (autoOpenLastInfoWindow) {
      // /EventBus.$emit("uiMap.monitor.openInfoWindow");
      monitor.openActiveInfoWindow(marker, infoWindow)
    }

    // mapService.setFitView(MAP);
    EventBus.$emit('uiMap.monitor.drawDone')
  },

  // 打开激活位置点弹框
  openActiveInfoWindow(makers, infoWindow) {
    const MAP = mapService.map
    const lnglat = makers.getPosition()
    infoWindow.open(MAP, lnglat)
    MAP.setZoomAndCenter(MAP.getZoom(), lnglat)
  }
}

export default monitor