Newer
Older
shipFront / src / views / overview / left.vue
[wangxitong] on 30 May 2022 4 KB v1.1.2
<template>
  <div class="container">
    <div class="title">
      AIS数据接收
      <div ref="circle" class="circle" :style="{'background-color': color}"/>
      (调用{{times}}次)
    </div>
    <position ref="position" class="position"/>
    <weather ref="weather" class="weather"/>
    <!--<water ref="water" class="water"/>-->
  </div>
</template>

<script>

  import Position from '@/views/overview/position'
  import Weather from '@/views/overview/weather'
  import { Notification } from 'element-ui'
  // import Water from '@/views/overview/water'
  import { getShipShipsInCircle, getShipLog } from '@/api/ship'
export default {
  name: 'Left',
  components: { Weather, Position, Notification},
  data() {
    return {
      color: '#00ff01',
      times:0,
      socket: null
    }
  },
  created() {
    getShipLog().then(res =>{
      if (res.code === 200) {
        this.times = res.data
      }
    })
  },
  mounted(){
    this.initWebSocket()
  },
  beforeDestroy(){
    this.destoryWebSocket()
  },
  methods: {
    initWebSocket() {
      let that = this
      if (typeof (WebSocket) === 'undefined') {
        Notification({
          title: '提示',
          message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!',
          type: 'warning',
          duration: 0
        })
      } else {
        const socketUrl = this.baseConfig.webSocketUrl + this.$store.getters.id
        console.log(socketUrl,'*********************')
        that.socket = new WebSocket(socketUrl)
        // 监听socket打开
        that.socket.onopen = function() {
          console.log('浏览器WebSocket已打开')
        }
        // 监听socket消息接收
        that.socket.onmessage = function(msg) {
          // 转换为json对象
          const msgdata = JSON.parse(msg.data)
          console.log(msgdata)
          const data = msgdata.messageObject
          if (msgdata.type === 'alarm') {
            let message
            switch (data.alarmType) {
              case '1':
                message = data.robotId + '号' + data.alarmTypeName + ':' + data.alarmValue + '%'
                break
              case '2':
                message = data.robotId + '号' + data.alarmTypeName + ':' + data.alarmValue + '海里'
                break
              case '3':
                message = data.alarmTypeName + ':' + data.alarmValue + 'm/s'
                break
              case '4':
                message = data.robotId + '号' + data.alarmTypeName + ':' + data.alarmValue + '海里'
                break
              case '5':
                message = data.robotId + '号' + data.alarmTypeName + ':' + data.alarmValue + '海里'
                break
              case '6':
                message = data.alarmTypeName + ':' + data.alarmValue + 'kb/s'
                break
            }
            Notification({
              title: '新报警来了',
              // 这里也可以把返回信息加入到message中显示
              message: message,
              type: 'warning',
            })
          }else if(msgdata.type === 'shelterPosition'){
            that.init()
          }
        }
        // 监听socket错误
        that.socket.onerror = function() {
          Notification({
            title: '服务器错误',
            message: '无法接收实时报警信息,请检查服务器后重新刷新页面',
            type: 'error',
            duration: 0
          })
        }
        // 监听socket关闭
        that.socket.onclose = function() {
          console.log('WebSocket已关闭')
        }
      }
    },
    destoryWebSocket(){
      this.socket.close()
    },
    init(){
      this.$refs.position.init()
      this.$refs.weather.init()
      // this.$refs.water.init()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .container{
    display: flex;
    flex-direction: column;
    .title{
      margin: 10px 0px 0px 30px;
      color: white;
      font-size: 18px;
      display: flex;
      .circle{
        width: 18px;
        height: 18px;
        border-radius: 50%;
        display: inline-block;
        margin-right: 5px;
        margin-left: 10px;
      }
    }
    .position{
      margin: 5px 0px;
      width: 100%;
      height: 38%;
    }
    .weather{
      margin: 5px 0px;
      width: 100%;
      height: 62%;
    }
    .water{
      margin: 5px 0px;
      width: 100%;
      height: 20%;
    }
  }
</style>