DevicePower.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <script>
  2. import { ThirdPartyDevice } from '@/constant'
  3. import { parseTime } from '@/utils'
  4. import {
  5. addListener,
  6. removeListener
  7. } from '@/utils/adapter'
  8. import {
  9. Power,
  10. Status
  11. } from '@/utils/adapter/nova'
  12. export default {
  13. name: 'DevicePower',
  14. props: {
  15. device: {
  16. type: Object,
  17. required: true
  18. }
  19. },
  20. data () {
  21. return {
  22. colorClass: ''
  23. }
  24. },
  25. computed: {
  26. online () {
  27. return this.device.onlineStatus === 1
  28. }
  29. },
  30. watch: {
  31. online (val) {
  32. if (val) {
  33. addListener(this.device.id, this.onMessage)
  34. } else {
  35. removeListener(this.device.id, this.onMessage)
  36. }
  37. }
  38. },
  39. mounted () {
  40. if (this.online) {
  41. addListener(this.device.id, this.onMessage)
  42. }
  43. },
  44. beforeDestroy () {
  45. if (this.online) {
  46. removeListener(this.device.id, this.onMessage)
  47. }
  48. },
  49. methods: {
  50. onMessage (value) {
  51. const multiCard = value[ThirdPartyDevice.MULTI_FUNCTION_CARD]
  52. this.device.power = multiCard.status
  53. if (multiCard.status === Status.OK) {
  54. this.device.timestamp = parseTime(multiCard.timestamp, '{y}-{m}-{d} {h}:{i}:{s}')
  55. switch (multiCard.realSwitchStatus) {
  56. case Power.ON:
  57. this.colorClass = 'u-color--success'
  58. break
  59. case Power.OFF:
  60. this.colorClass = 'u-color--error dark'
  61. break
  62. default:
  63. this.colorClass = 'u-color--warning'
  64. break
  65. }
  66. }
  67. }
  68. },
  69. render (h) {
  70. if (!this.online || this.device.power === Status.NONE) {
  71. return h('i', null, '-')
  72. }
  73. if (this.device.power === Status.LOADING) {
  74. return h('i', {
  75. staticClass: 'el-icon-loading'
  76. })
  77. }
  78. return h('i', {
  79. staticClass: `o-status ${this.colorClass}`
  80. })
  81. }
  82. }
  83. </script>