DeviceInfo.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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="onSwitch(true)"
  28. >
  29. <i class="o-button__icon el-icon-switch-button" />
  30. 开机
  31. </button>
  32. <button
  33. class="l-flex__none o-button"
  34. @click="onSwitch(false)"
  35. >
  36. <i class="o-button__icon el-icon-switch-button" />
  37. 关机
  38. </button>
  39. <button
  40. class="l-flex__none o-button"
  41. @click="onReboot"
  42. >
  43. <i
  44. class="o-button__icon"
  45. :class="{
  46. 'el-icon-loading': rebooting,
  47. 'el-icon-refresh-right': !rebooting,
  48. }"
  49. />
  50. 重启
  51. </button>
  52. </div>
  53. </box>
  54. </template>
  55. <script>
  56. import {
  57. publish,
  58. subscribe,
  59. unsubscribe
  60. } from '@/utils/mqtt'
  61. import Box from './Box'
  62. export default {
  63. name: 'DeviceInfo',
  64. components: {
  65. Box
  66. },
  67. props: {
  68. device: {
  69. type: Object,
  70. required: true
  71. }
  72. },
  73. data () {
  74. return {
  75. online: this.device.activate && this.device.onlineStatus === 1,
  76. rebooting: false,
  77. rows: [
  78. [
  79. {
  80. label: '名称',
  81. key: 'name'
  82. },
  83. {
  84. label: 'MAC',
  85. key: 'mac'
  86. }
  87. ],
  88. [
  89. {
  90. label: '序列号',
  91. key: 'serialNumber'
  92. },
  93. {
  94. label: '分辨率',
  95. key: 'resolutionRatio'
  96. }
  97. ],
  98. [
  99. {
  100. label: '产品',
  101. key: 'productName'
  102. },
  103. {
  104. label: '位置',
  105. key: 'address'
  106. }
  107. ]
  108. ]
  109. }
  110. },
  111. created () {
  112. subscribe([
  113. `${this.device.productId}/${this.device.id}/online`,
  114. `${this.device.productId}/${this.device.id}/offline`
  115. ], this.onMessage)
  116. },
  117. beforeDestroy () {
  118. unsubscribe([
  119. `${this.device.productId}/${this.device.id}/online`,
  120. `${this.device.productId}/${this.device.id}/offline`
  121. ], this.onMessage)
  122. },
  123. methods: {
  124. onMessage (topic) {
  125. const result = topic.match(/^(\d+)\/(\d+)\/(online|offline)$/)
  126. if (result && this.device.productId === result[1] && this.device.id === result[2]) {
  127. this.rebooting = false
  128. this.online = result[3] === 'online'
  129. }
  130. },
  131. onSwitch (open) {
  132. if (!this.online) {
  133. this.$message({
  134. type: 'warning',
  135. message: '设备未上线,请稍后再试'
  136. })
  137. return
  138. }
  139. this.$confirm(
  140. `立即${open ? '开机' : '关机'}?`,
  141. { type: 'warning' }
  142. ).then(() => {
  143. this.sendTopic(open ? 'bootDevice' : 'shutdownDevice')
  144. })
  145. },
  146. onReboot () {
  147. if (!this.online) {
  148. this.$message({
  149. type: 'warning',
  150. message: '设备未上线,请稍后再试'
  151. })
  152. return
  153. }
  154. if (this.rebooting) {
  155. return
  156. }
  157. this.$confirm(
  158. `立即重启?`,
  159. { type: 'warning' }
  160. ).then(() => {
  161. publish(
  162. `${this.device.productId}/${this.deviceId}/restart/ask`,
  163. JSON.stringify({ timestamp: `${Date.now()}` })
  164. ).then(
  165. () => {
  166. this.rebooting = true
  167. this.$message({
  168. type: 'success',
  169. message: '执行成功'
  170. })
  171. },
  172. () => {
  173. this.$message({
  174. type: 'warning',
  175. message: '正在连接,请稍后再试'
  176. })
  177. }
  178. )
  179. })
  180. },
  181. sendTopic (invoke, inputs = []) {
  182. publish(
  183. `${this.device.productId}/${this.deviceId}/function/invoke`,
  184. JSON.stringify({
  185. timestamp: `${Date.now()}`,
  186. function: invoke,
  187. inputs
  188. }),
  189. true
  190. ).then(
  191. () => {
  192. this.$message({
  193. type: 'success',
  194. message: '执行成功'
  195. })
  196. },
  197. () => {
  198. this.$message({
  199. type: 'warning',
  200. message: '正在连接,请稍后再试'
  201. })
  202. }
  203. )
  204. }
  205. }
  206. }
  207. </script>
  208. <style lang="scss" scoped>
  209. .c-info {
  210. &__row {
  211. height: 80px;
  212. background: #313a5a;
  213. .row__label {
  214. color: #9ea9cd;
  215. }
  216. }
  217. }
  218. .o-button {
  219. width: 200px;
  220. height: 80px;
  221. font-size: 36px;
  222. & ~ & {
  223. margin-left: 150px;
  224. }
  225. &__icon {
  226. font-size: 36px;
  227. }
  228. }
  229. </style>