| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import { parseTime } from '@/utils'
- import {
- subscribe,
- unsubscribe
- } from '@/utils/mqtt'
- export default {
- inject: ['dashboard'],
- data () {
- return {
- groupOptions: this.createGroupOptions({ ignore: true })
- }
- },
- created () {
- this.$timer = -1
- this.refreshGroups(true)
- },
- beforeDestroy () {
- this.onResetGroupOptions()
- },
- methods: {
- createGroupOptions (options) {
- return {
- ignore: false,
- loading: false,
- loaded: false,
- list: [],
- ...options
- }
- },
- refreshGroups (force) {
- // todo
- console.log(force)
- },
- onResetGroupOptions () {
- this.groupOptions.ignore = true
- this.groupOptions.list = []
- clearTimeout(this.$timer)
- if (this.groupOptions.topics) {
- unsubscribe(this.groupOptions.topics, this.onMessage)
- }
- },
- createTopics ({ id, productId }) {
- const topic = `${productId}/${id}`
- return [
- `${topic}/online`,
- `${topic}/offline`/* ,
- `${topic}/calendar/update` */
- ]
- },
- onSubscribe (topics) {
- if (topics.length) {
- this.groupOptions.topics = topics
- subscribe(this.groupOptions.topics, this.onMessage)
- }
- },
- onMessage (topic) {
- if (!this.groupOptions.loaded || this.groupOptions.ignore) {
- return
- }
- const result = /^\d+\/(\d+)\/(online|offline|calendar\/update)$/.exec(topic)
- if (!result) {
- return
- }
- const deviceId = result[1]
- const topicKey = result[2]
- const device = this.groupOptions.map?.[deviceId]
- if (device) {
- switch (topicKey) {
- case 'calendar/update':
- device.flag = -Date.now()
- break
- default:
- if (topicKey !== (device.onlineStatus === 1 ? 'online' : 'offline')) {
- if (topicKey === 'online') {
- device.onlineStatus = 1
- } else {
- device.onlineStatus = 2
- }
- device.lastOnline = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
- this.onStatusChanged(topicKey)
- }
- break
- }
- }
- },
- onStatusChanged (topicKey) {
- this.dashboard?.onDeviceStatusChanged(topicKey === 'online' ? 1 : -1)
- },
- transformDevice ({ id, name, productId, onlineStatus, lastOnline, address }) {
- return {
- id, name, productId, onlineStatus, lastOnline, address,
- flag: 0
- }
- }
- }
- }
|