فهرست منبع

websocket预警

hfxc226 2 سال پیش
والد
کامیت
70ed1a5b9e
5فایلهای تغییر یافته به همراه177 افزوده شده و 3 حذف شده
  1. 1 0
      package.json
  2. BIN
      src/assets/alarm/alarm_police.wav
  3. 8 3
      src/views/opc/OpcInfo.vue
  4. 161 0
      src/views/socket/RemoteOpcWarn.vue
  5. 7 0
      vue.config.js

+ 1 - 0
package.json

@@ -14,6 +14,7 @@
   "dependencies": {
     "@antv/data-set": "^0.10.2",
     "@antv/g2": "^4.0.11",
+    "@sven0706/websocket": "^1.0.1",
     "ant-design-vue": "^1.4.5",
     "antd": "^3.23.6",
     "axios": "^0.19.0",

BIN
src/assets/alarm/alarm_police.wav


+ 8 - 3
src/views/opc/OpcInfo.vue

@@ -22,7 +22,7 @@
             <template slot="title">
               {{ item.description + '(' + item.time + ')' }}
             </template>
-<!--            <div class="info" @click="handleInfo(item)" @click.right.prevent="handleView(item)">-->
+            <!--            <div class="info" @click="handleInfo(item)" @click.right.prevent="handleView(item)">-->
             <div class="info" @click="handleInfo(item)">
               <span :style="{'color':item.warnFirstColor}" v-if="(+item.result)<=item.warnFirst&&item.warnFirst!==null">{{ item.result }}</span>
               <span :style="{'color':item.warnSecondColor}" v-else-if="item.warnFirst<(+item.result)&&(+item.result)<item.warnSecond">{{ item.result }}</span>
@@ -96,6 +96,7 @@
     </div>
     <detail ref="detailModal" @ok="handleOk" />
     <detail-log ref="detailLogModal" @ok="handleOk" />
+    <remote-opc-warn></remote-opc-warn>
   </div>
 </template>
 
@@ -108,10 +109,14 @@ import {
 } from '@/api/remote/opc'
 import Detail from '@/views/remote/opc/modules/Detail.vue'
 import DetailLog from '@/views/remote/opc-log/modules/Detail.vue'
+import RemoteOpcWarn from '@/views/socket/RemoteOpcWarn'
+import RemoteOpcList from '@/views/remote/opc/RemoteOpc'
 export default {
   name: 'Opc',
   components: {
+    RemoteOpcList,
     VueDragResize,
+    RemoteOpcWarn,
     Detail,
     DetailLog
   },
@@ -225,10 +230,10 @@ export default {
       this.getOpcInfo(this.positionId)
       this.getImg()
     },
-    /*handleInfo (remoteOpc) {
+    /* handleInfo (remoteOpc) {
       const model = this.$refs.detailLogModal
       model.base(null, { positionNum: remoteOpc.positionNum })
-    },*/
+    }, */
     handleInfo (remoteOpc) {
       const model = this.$refs.detailLogModal
       model.base(remoteOpc)

+ 161 - 0
src/views/socket/RemoteOpcWarn.vue

@@ -0,0 +1,161 @@
+<template>
+  <a-modal
+    :title="modalTitle"
+    :width="1200"
+    :visible="visible"
+    :confirmLoading="confirmLoading"
+    @cancel="handleCancel"
+  >
+    <a-list
+      class="demo-loadmore-list"
+      item-layout="horizontal"
+      :data-source="data"
+    >
+      <a-list-item slot="renderItem" slot-scope="item" @click="clickQualityItem(item)">
+        <div>名称:{{ item.sbName }}</div>
+        <div>点位:{{ item.positionNum }}</div>
+        <div>低低限值:{{ item.warnFirst }}</div>
+        <div>低限值:{{ item.warnSecond }}</div>
+        <div>高限值:{{ item.warnThird }}</div>
+        <div>高高限值:{{ item.warnFour }}</div>
+      </a-list-item>
+    </a-list>
+    <audio
+      v-show="false"
+      ref="audio"
+      loop="true"
+      controls="controls"
+      src="@/assets/alarm/alarm_police.wav"
+      id="audio"></audio>
+    <template slot="footer">
+      <a-button :loading="confirmLoading" type="primary" @click="handleCancel()">返回</a-button>
+    </template>
+  </a-modal>
+</template>
+
+<script>
+import { mapGetters } from 'vuex'
+import DetailList from '@/components/tools/DetailList'
+const DetailListItem = DetailList.Item
+export default {
+  name: 'RemoteOpcWarn',
+  components: {
+    DetailList,
+    DetailListItem
+  },
+  watch: {
+  },
+  computed: {
+    ...mapGetters(['pcAlarmVoice', 'catchComponents']),
+    key () {
+      return this.$route.path
+    }
+  },
+  data () {
+    return {
+      confirmLoading: false,
+      modalTitle: '预警通知',
+      visible: false,
+      typeMap: {},
+      data: null,
+      websock: null,
+      lockReconnect: false
+    }
+  },
+  created () {
+  },
+  mounted () {
+    console.log('进入页面')
+    this.initWebSocket()
+  },
+  methods: {
+    handleCancel () {
+      this.visible = false
+      this.confirmLoading = false
+      const audio = document.querySelector('#audio')
+      audio.pause()
+    },
+    initWebSocket: function () {
+      // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
+      let url = ''
+      if (process.env.NODE_ENV === 'production') {
+        // 生产环境
+        // url = 'wss://192.168.31.46:30008/?bindType=2&bindContent=' + bindContent // 正试服
+        url = 'ws://oa.catlsc.com:7766/websocket/100' // 正试服
+      } else {
+        url = 'ws://localhost:8000/websocket/100' // 测试服
+      }
+      console.log('初始化websocket', url)
+      this.websock = new WebSocket(url)
+      this.websock.onopen = this.websocketOnopen
+      this.websock.onerror = this.websocketOnerror
+      this.websock.onmessage = this.websocketOnmessage
+      this.websock.onclose = this.websocketOnclose
+    },
+    websocketOnopen: function () {
+      console.log('页面WebSocket连接成功')
+      // 心跳检测重置
+      // this.heartCheck.reset().start()
+    },
+    // 连接失败后的回调函数
+    websocketOnerror: function (e) {
+      console.log('WebSocket连接发生错误', e)
+      this.reconnect()
+    },
+    // 当从服务器接受到信息时的回调函数
+    websocketOnmessage: function (e) {
+      this.visible = true
+      // 收到消息
+      this.data = JSON.parse(e.data)
+      const audio = document.querySelector('#audio')
+      if (audio != null) {
+        audio.play()
+        audio.currentTime = 0
+      }
+    },
+    websocketOnclose: function (e) {
+      // console.log('connection closed (' + e.code + ')')
+      this.reconnect()
+    },
+    websocketSend (text) {
+      // 数据发送
+      try {
+        this.websock.send(text)
+      } catch (err) {
+        // console.log('send failed (' + err.code + ')')
+      }
+    },
+    reconnect () {
+      const context = this
+      if (context.lockReconnect) return
+      context.lockReconnect = true
+      // 没连接上会一直重连,设置延迟避免请求过多
+      clearTimeout(this.time1)
+      this.time1 = setTimeout(function () {
+        // console.info('尝试重连...')
+        context.initWebSocket()
+        context.lockReconnect = false
+      }, 5000)
+    },
+    clickQualityItem (item) {
+      this.$message.info('click')
+      const a = document.createElement('a')
+      if (item.type === 4) {
+        a.href = '/quality/item/stable?id=' + item.id
+      } else if (item.type === 3) {
+        a.href = '/quality/item/research?id=' + item.id
+      } else if (item.type === 2) {
+        a.href = '/quality/item/material?id=' + item.id
+      } else {
+        a.href = '/quality/item?id=' + item.id
+      }
+      a.target = '_blank'
+      a.click()
+    }
+  },
+  beforeDestroy () {
+    this.websock.close()
+    console.log('页面关闭websocket')
+  }
+}
+</script>

+ 7 - 0
vue.config.js

@@ -115,6 +115,13 @@ const vueConfig = {
         target: 'http://localhost:5000',
         ws: false,
         changeOrigin: true
+      },
+      '/websocket': {
+        target: 'ws://localhost:5000',
+        ws: true,
+        changeOrigin: false,
+        logLevel: 'debug',
+        secure: false
       }
     }
   },