LowPower.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div class="lowpower-containner">
  3. <!-- <el-button :loading="isSubscribe" @click="subscribe"> 订阅</el-button> -->
  4. <el-row>
  5. <!-- 检测步骤 -->
  6. <el-col :span="6" class="main-left">
  7. <!-- 步骤组件 模块 -->
  8. <check-steps :check-steps-parames="checkStepsParames" />
  9. </el-col>
  10. <el-col :span="18" class="main-right">
  11. <div class="main-right-container">
  12. <!-- MQTT报文 消息 -->
  13. <!-- <div @click="delClick">111</div> -->
  14. <el-table v-if="processData" :data="processData" stripe>
  15. <el-table-column type="expand">
  16. <template slot-scope="scope">
  17. <el-form label-position="left" inline class="demo-table-expand">
  18. <el-form-item v-for="(item,index) in scope.row.eid_list" :key="index+item.original_message" :label="`数据${index+1}`">
  19. <span>test_cate: {{ item.test_cate }}</span><br>
  20. <span>test_item: {{ item.test_item }}</span><br>
  21. <span>eid: {{ item.eid }}</span><br>
  22. <span>original_message: {{ item.original_message }}</span>
  23. </el-form-item>
  24. </el-form>
  25. </template>
  26. </el-table-column>
  27. <el-table-column prop="message" label="message" />
  28. <el-table-column prop="task_id" label="task_id" />
  29. <!-- <el-table-column fixed="right" label="操作" width="160">
  30. <template slot-scope="scope">
  31. <el-button
  32. class="table-act"
  33. type="text"
  34. icon="el-icon-delete"
  35. size="small"
  36. @click="delClick(scope.row, scope.$index)"
  37. >删除</el-button>
  38. </template>
  39. </el-table-column> -->
  40. <!-- 隐藏 列 这里不用显示,但是在编辑的时候需要获取此数据 -->
  41. <el-table-column
  42. v-if="false"
  43. prop="deviceCheckId"
  44. label="序号"
  45. />
  46. <!-- 隐藏 列 -->
  47. </el-table>
  48. </div>
  49. </el-col>
  50. </el-row>
  51. </div>
  52. </template>
  53. <script>
  54. import mqtt from 'mqtt' // 引入 mqtt 库
  55. import CheckSteps from './CheckSteps.vue'
  56. export default {
  57. name: 'MQTT',
  58. components: { CheckSteps },
  59. props: {
  60. deviceDetail: {
  61. type: Object,
  62. default: function() {
  63. return
  64. }
  65. }
  66. },
  67. data() {
  68. return {
  69. startTopic: '/V2.0.0/detection/start',
  70. client: null, // MQTT 客户端
  71. clientConnected: false,
  72. message: [], // 接收到的消息
  73. stepActionStatus: 'cancel',
  74. isSubscribe: false, // MQTT是否订阅
  75. // 步骤组件所需相关参数
  76. checkStepsParames: {
  77. checkPlans: this.deviceDetail.deviceCheckPlans
  78. },
  79. processData: [],
  80. itemData: [],
  81. messageData: []
  82. }
  83. },
  84. methods: {
  85. // 订阅MQTT消息
  86. subscribe() {
  87. this.isSubscribe = true
  88. // 连接 mqtt 客户端
  89. this.client = mqtt.connect('ws://192.168.1.236:8083/mqtt')
  90. // console.log('this.client=', this.client)
  91. // console.log('this.client.connected=', this.client.connected)
  92. // console.log('this.client["connected"]=', this.client['connected'])
  93. this.client.on('connect', () => {
  94. // 关闭订阅按钮读取状态
  95. this.isSubscribe = false
  96. // 设置连接状态为 true
  97. this.clientConnected = true
  98. // 发送消息
  99. this.publish(this.startTopic)
  100. // 监听消息
  101. this.client.subscribe([this.startTopic, '/V2.0.0/detection/process', '/V2.0.0/detection/complete/item', '/V2.0.0/detection/complete/message'])
  102. this.client.on('message', (topic, payload) => {
  103. // 接收所有消息
  104. // console.log(`Received message ${payload.toString()} on topic ${topic}`)
  105. // 更新接受到的消息到 message
  106. this.message.push(JSON.parse(payload.toString()))
  107. // 按订阅主题分别接收mqtt消息
  108. switch (topic) {
  109. // 检测开始
  110. case '/V2.0.0/detection/start':
  111. // 发生检测请求
  112. break
  113. case '/V2.0.0/detection/start/response':
  114. // 无线组网平台应答
  115. break
  116. // 检测过程中
  117. case '/V2.0.0/detection/process':
  118. // 无线组网平台发送的mqtt消息
  119. this.processData.push(JSON.parse(payload.toString()))
  120. break
  121. // 检测完成后
  122. case '/V2.0.0/detection/complete/item':
  123. // 无线组网平台发送的全部检查项结果的mqtt消息
  124. this.itemData.push(JSON.parse(payload.toString()))
  125. break
  126. case '/V2.0.0/detection/complete/message':
  127. // 无线组网平台发送的全部检查项所有的日志信息
  128. this.messageData.push(JSON.parse(payload.toString()))
  129. break
  130. default:
  131. break
  132. }
  133. })
  134. })
  135. },
  136. // 发送MQTT消息
  137. publish(publishTopic) {
  138. if (this.clientConnected) {
  139. // 生成随机task_id
  140. const min = 10000
  141. const max = 99999
  142. const randomNum = Math.floor(Math.random() * (max - min) + min)
  143. const taskId = parseInt(new Date().getTime() / 1000).toString() + randomNum.toString()
  144. const msg = {
  145. 'task_id': parseInt(taskId),
  146. 'task_name': '微功耗测试',
  147. 'eid_list': [{
  148. 'equip_type': 0,
  149. 'eid': '22c608001234',
  150. 'fre_point': 4,
  151. 'business_cycle': 20000
  152. }]
  153. }
  154. this.client.publish(
  155. publishTopic,
  156. JSON.stringify(msg)
  157. )
  158. } else {
  159. alert('Please connect to MQTT broker first.')
  160. }
  161. },
  162. // 获取步骤执行状态 动态显示效果 同 controlRefresh
  163. UpdateDeviceStatus() {
  164. // console.log('refreshAll started')
  165. },
  166. delClick() {
  167. // console.log('this.message=', this.message)
  168. // console.log('this.processData=', this.processData)
  169. // console.log('this.itemData=', this.itemData)
  170. // console.log('this.messageData=', this.messageData)
  171. }
  172. }
  173. }
  174. </script>