import { publish } from '@/utils/mqtt'

export default {
  props: {
    device: {
      type: Object,
      required: true
    },
    online: {
      type: [Boolean, String],
      default: false
    }
  },
  computed: {
    deviceId () {
      return this.device.id
    }
  },
  methods: {
    sendTopic (invoke, inputs = []) {
      console.log(invoke, inputs)
      publish(
        `${this.device.productId}/${this.deviceId}/function/invoke`,
        JSON.stringify({
          timestamp: Date.now(),
          'function': invoke,
          inputs
        }),
        true
      ).then(
        () => {
          this.$message({
            type: 'success',
            message: '执行成功'
          })
        },
        () => {
          this.$message({
            type: 'warning',
            message: '正在连接，请稍后再试'
          })
        }
      )
    }
  }
}
