DeviceInfo.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <box
  3. title="信息"
  4. can-full-screen
  5. fullscreen
  6. v-on="$listeners"
  7. >
  8. <div
  9. v-for="(row, index) in rows"
  10. :key="index"
  11. class="l-flex--row c-sibling-item--v far c-info__row"
  12. >
  13. <div
  14. v-for="item in row"
  15. :key="item.key"
  16. class="l-flex--row l-flex__fill"
  17. >
  18. <div class="l-flex__fill l-flex--row center row__label">
  19. {{ item.label }}
  20. </div>
  21. <div class="l-flex__fill">{{ device[item.key] }}</div>
  22. </div>
  23. </div>
  24. <div class="l-flex--row l-flex__fill center">
  25. <button
  26. class="l-flex__none o-button"
  27. @click="onReboot"
  28. >
  29. <i
  30. class="o-button__icon"
  31. :class="{
  32. 'el-icon-loading': rebooting,
  33. 'el-icon-refresh-right': !rebooting,
  34. }"
  35. />
  36. 重启
  37. </button>
  38. </div>
  39. </box>
  40. </template>
  41. <script>
  42. import {
  43. publish,
  44. subscribe,
  45. unsubscribe
  46. } from '@/utils/mqtt'
  47. import Box from './Box'
  48. export default {
  49. name: 'DeviceInfo',
  50. components: {
  51. Box
  52. },
  53. props: {
  54. device: {
  55. type: Object,
  56. required: true
  57. }
  58. },
  59. data () {
  60. return {
  61. online: this.device.onlineStatus === 1,
  62. rebooting: false,
  63. rows: [
  64. [
  65. {
  66. label: '名称',
  67. key: 'name'
  68. },
  69. {
  70. label: 'MAC',
  71. key: 'mac'
  72. }
  73. ],
  74. [
  75. {
  76. label: '序列号',
  77. key: 'serialNumber'
  78. },
  79. {
  80. label: '分辨率',
  81. key: 'resolutionRatio'
  82. }
  83. ],
  84. [
  85. {
  86. label: '配置',
  87. key: 'productName'
  88. },
  89. {
  90. label: '位置',
  91. key: 'address'
  92. }
  93. ]
  94. ]
  95. }
  96. },
  97. created () {
  98. subscribe([
  99. `${this.device.productId}/${this.device.id}/online`,
  100. `${this.device.productId}/${this.device.id}/offline`
  101. ], this.onMessage)
  102. },
  103. beforeDestroy () {
  104. unsubscribe([
  105. `${this.device.productId}/${this.device.id}/online`,
  106. `${this.device.productId}/${this.device.id}/offline`
  107. ], this.onMessage)
  108. },
  109. methods: {
  110. onMessage (topic) {
  111. const result = topic.match(/^(\d+)\/(\d+)\/(online|offline)$/)
  112. if (result && this.device.productId === result[1] && this.device.id === result[2]) {
  113. this.rebooting = false
  114. this.online = result[3] === 'online'
  115. }
  116. },
  117. onReboot () {
  118. if (!this.online) {
  119. this.$message({
  120. type: 'warning',
  121. message: '设备未上线'
  122. })
  123. return
  124. }
  125. if (this.rebooting) {
  126. return
  127. }
  128. this.$confirm(
  129. `立即重启?`,
  130. { type: 'warning' }
  131. ).then(() => {
  132. publish(
  133. `${this.device.productId}/${this.deviceId}/restart/ask`,
  134. JSON.stringify({ timestamp: `${Date.now()}` })
  135. ).then(
  136. () => {
  137. this.rebooting = true
  138. this.$message({
  139. type: 'success',
  140. message: '执行成功'
  141. })
  142. },
  143. () => {
  144. this.$message({
  145. type: 'warning',
  146. message: '正在连接,请稍后重试'
  147. })
  148. }
  149. )
  150. })
  151. },
  152. sendTopic (invoke, inputs = []) {
  153. publish(
  154. `${this.device.productId}/${this.deviceId}/function/invoke`,
  155. JSON.stringify({
  156. timestamp: `${Date.now()}`,
  157. function: invoke,
  158. inputs
  159. }),
  160. true
  161. ).then(
  162. () => {
  163. this.$message({
  164. type: 'success',
  165. message: '执行成功'
  166. })
  167. },
  168. () => {
  169. this.$message({
  170. type: 'warning',
  171. message: '正在连接,请稍后重试'
  172. })
  173. }
  174. )
  175. }
  176. }
  177. }
  178. </script>
  179. <style lang="scss" scoped>
  180. .c-info {
  181. &__row {
  182. height: 80px;
  183. background: #313a5a;
  184. .row__label {
  185. color: #9ea9cd;
  186. }
  187. }
  188. }
  189. .o-button {
  190. width: 200px;
  191. height: 80px;
  192. font-size: 36px;
  193. & ~ & {
  194. margin-left: 150px;
  195. }
  196. &__icon {
  197. font-size: 36px;
  198. }
  199. }
  200. </style>