|
|
@@ -0,0 +1,233 @@
|
|
|
+<script>
|
|
|
+import {
|
|
|
+ ThirdPartyDevice,
|
|
|
+ PowerAction
|
|
|
+} from '@/constant'
|
|
|
+import { publish } from '@/utils/mqtt'
|
|
|
+import {
|
|
|
+ Status,
|
|
|
+ GET_MULTI_POWER_TIMING,
|
|
|
+ SET_MULTI_POWER_TIMING,
|
|
|
+ addListener,
|
|
|
+ removeListener,
|
|
|
+ addInjectListener,
|
|
|
+ removeInjectListener
|
|
|
+} from '@/utils/adapter'
|
|
|
+import { savePowerLogger } from '@/api/platform'
|
|
|
+
|
|
|
+export default {
|
|
|
+ name: 'DevicePowerDecreaseTask',
|
|
|
+ props: {
|
|
|
+ device: {
|
|
|
+ type: Object,
|
|
|
+ required: true
|
|
|
+ },
|
|
|
+ dateRange: {
|
|
|
+ type: Array,
|
|
|
+ required: true
|
|
|
+ },
|
|
|
+ actions: {
|
|
|
+ type: Array,
|
|
|
+ required: true
|
|
|
+ },
|
|
|
+ invoke: {
|
|
|
+ type: Number,
|
|
|
+ default: 0
|
|
|
+ }
|
|
|
+ },
|
|
|
+ data () {
|
|
|
+ return {
|
|
|
+ status: 0,
|
|
|
+ retryCount: 0,
|
|
|
+ cardInfo: null
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted () {
|
|
|
+ this.$timer = -1
|
|
|
+ addListener(this.device.id, this.onMessage)
|
|
|
+ addInjectListener(this.device.id, this.onInjectMessage)
|
|
|
+ },
|
|
|
+ beforeDestroy () {
|
|
|
+ clearTimeout(this.$timer)
|
|
|
+ removeListener(this.device.id, this.onMessage)
|
|
|
+ removeInjectListener(this.device.id, this.onInjectMessage)
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ onMessage (value) {
|
|
|
+ const multiCard = value[ThirdPartyDevice.MULTI_FUNCTION_CARD]
|
|
|
+ if (multiCard.status === Status.OK) {
|
|
|
+ if (!this.cardInfo) {
|
|
|
+ const { connectIndex, portIndex } = multiCard.powers[0]
|
|
|
+ const type = multiCard.powers.find(({ type }) => type === '屏体电源' || type === '默认')?.type || multiCard.powers.find(({ type }) => !!type)?.type
|
|
|
+ this.cardInfo = { type, connectIndex, portIndex }
|
|
|
+ if (type) {
|
|
|
+ this.onSync()
|
|
|
+ } else {
|
|
|
+ this.status = 4
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ onInjectMessage (message) {
|
|
|
+ if (message.messageId === this.$messageId) {
|
|
|
+ this.$messageId = null
|
|
|
+ if (message.code !== 0) {
|
|
|
+ this.status = 3
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const data = message.data ? JSON.parse(message.data.replaceAll("'", '"')) : {}
|
|
|
+ if (data.logined === false) {
|
|
|
+ this.status = 3
|
|
|
+ return
|
|
|
+ }
|
|
|
+ switch (message.function) {
|
|
|
+ case GET_MULTI_POWER_TIMING:
|
|
|
+ clearTimeout(this.$timer)
|
|
|
+ this.sendTasks(data.data)
|
|
|
+ break
|
|
|
+ case SET_MULTI_POWER_TIMING:
|
|
|
+ clearTimeout(this.$timer)
|
|
|
+ this.status = 2
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ onSync () {
|
|
|
+ this.$timer = setTimeout(this.retry, 5000)
|
|
|
+ this.sendTopic(GET_MULTI_POWER_TIMING, { sn: this.device.serialNumber }).then(messageId => {
|
|
|
+ this.$messageId = messageId
|
|
|
+ }, () => {
|
|
|
+ clearTimeout(this.$timer)
|
|
|
+ })
|
|
|
+ },
|
|
|
+ createTasks () {
|
|
|
+ const type = this.cardInfo.type
|
|
|
+ return this.tasks.map(task => {
|
|
|
+ return {
|
|
|
+ ...task,
|
|
|
+ type
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ sendTasks (tasks) {
|
|
|
+ const data = []
|
|
|
+ const { connectIndex, portIndex } = this.cardInfo
|
|
|
+ const currTasks = tasks.find(item => item.connectIndex === connectIndex && item.portIndex === portIndex)?.conditions
|
|
|
+ if (currTasks) {
|
|
|
+ const { change, data: newTasks } = this.checkAndTransfromTasks(currTasks)
|
|
|
+ if (change) {
|
|
|
+ this.status = 1
|
|
|
+ data.push({
|
|
|
+ connectIndex,
|
|
|
+ portIndex,
|
|
|
+ enable: true,
|
|
|
+ conditions: newTasks.map(({ status, ...task }) => task)
|
|
|
+ })
|
|
|
+ this.$timer = setTimeout(this.retry, 5000)
|
|
|
+ this.sendTopic(SET_MULTI_POWER_TIMING, {
|
|
|
+ sn: this.device.serialNumber,
|
|
|
+ taskInfo: data
|
|
|
+ }).then(messageId => {
|
|
|
+ this.$messageId = messageId
|
|
|
+ const [startDate, endDate] = this.dateRange
|
|
|
+ const date = startDate === endDate ? startDate : `${startDate} - ${endDate}`
|
|
|
+ const action = this.actions.length ? this.actions.map(action => action === PowerAction.OPEN ? '开启任务' : '关闭任务').join('/') : '所有定时任务'
|
|
|
+ savePowerLogger({
|
|
|
+ description: `设备【${this.device.name}】 ${['停用', '启用', '移除'][this.invoke]} ${date} ${action}`,
|
|
|
+ method: '电源定时任务设置',
|
|
|
+ params: JSON.stringify({
|
|
|
+ id: this.device.id,
|
|
|
+ name: this.device.name,
|
|
|
+ dateRange: this.dateRange,
|
|
|
+ actions: this.actions,
|
|
|
+ invoke: this.invoke
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }, () => {
|
|
|
+ clearTimeout(this.$timer)
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.status = 2
|
|
|
+ },
|
|
|
+ checkAndTransfromTasks (tasks) {
|
|
|
+ const data = []
|
|
|
+ let change = false
|
|
|
+ const [startTime, endTime] = this.dateRange
|
|
|
+ const needCheckAction = this.actions.length > 0
|
|
|
+ const isDel = this.invoke === 2
|
|
|
+ const actionEnable = isDel ? false : !!this.invoke
|
|
|
+ for (let i = 0; i < tasks.length; i++) {
|
|
|
+ const task = tasks[i]
|
|
|
+ if (!(task.startTime > endTime || task.endTime < startTime)) {
|
|
|
+ if (!needCheckAction || this.actions.includes(task.action)) {
|
|
|
+ if (isDel) {
|
|
|
+ change = true
|
|
|
+ continue
|
|
|
+ } else if (actionEnable !== task.enable) {
|
|
|
+ change = true
|
|
|
+ data.push({
|
|
|
+ ...task,
|
|
|
+ enable: actionEnable
|
|
|
+ })
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data.push({ ...task })
|
|
|
+ }
|
|
|
+ return { change, data }
|
|
|
+ },
|
|
|
+ retry () {
|
|
|
+ this.retryCount += 1
|
|
|
+ this.status = 1
|
|
|
+ this.onSync()
|
|
|
+ },
|
|
|
+ sendTopic (invoke, inputs) {
|
|
|
+ const timestamp = `${Date.now()}`
|
|
|
+ const messageId = `${invoke}_${timestamp}`
|
|
|
+ return publish(
|
|
|
+ `${this.device.productId}/${this.device.id}/multifunctionCard/invoke`,
|
|
|
+ JSON.stringify({
|
|
|
+ messageId,
|
|
|
+ timestamp,
|
|
|
+ 'function': invoke,
|
|
|
+ inputs: JSON.stringify(inputs || [])
|
|
|
+ }),
|
|
|
+ true
|
|
|
+ ).then(() => messageId)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ render (h) {
|
|
|
+ if (this.status === 3 || this.status === 4) {
|
|
|
+ return h('el-tooltip', {
|
|
|
+ props: {
|
|
|
+ content: this.status === 3 ? '连接失败,请联系管理员' : '无可用端口,请联系管理员',
|
|
|
+ placement: 'left',
|
|
|
+ enterable: false
|
|
|
+ }
|
|
|
+ }, [
|
|
|
+ h('el-tag', {
|
|
|
+ staticClass: 'o-tag u-readonly',
|
|
|
+ props: {
|
|
|
+ size: 'medium',
|
|
|
+ 'disable-transitions': true,
|
|
|
+ type: 'danger'
|
|
|
+ }
|
|
|
+ }, '失败')
|
|
|
+ ])
|
|
|
+ }
|
|
|
+ return h('el-tag', {
|
|
|
+ staticClass: 'o-tag u-readonly',
|
|
|
+ props: {
|
|
|
+ size: 'medium',
|
|
|
+ 'disable-transitions': true,
|
|
|
+ type: ['primary', 'warning', 'success'][this.status]
|
|
|
+ }
|
|
|
+ }, ['等待', this.retryCount === 0 ? '同步中' : `重试(${this.retryCount})`, '成功'][this.status])
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|