<template>
    <view class="container" v-show="show">
        <prodnum-06 v-if="detail.prodnum == '06'" :control="control" />
        <prodnum-MT03 v-else-if="detail.prodnum == 'MT03'" :control="control" />
        <prodnum-MT02 v-else-if="detail.prodnum == 'MT02'" :control="control" />
    </view>
</template>

<script>
let monitoring = null;
export default {
    components: {},
    name: "control",
    props: {
        detail: Object
    },
    data() {
        return {
            show: false,
            uninitialized: true,
            control: {}
        }
    },
    methods: {
        initialize(init = false) {
            if (init) this.getControl(true);
        },
        getControl(init) {
            if (init) this.control = null;
            this.$Http.basic({
                "id": "20230628084901",
                "content": {
                    "w_deviceid": this.detail.w_deviceid
                }
            }).then(res => {
                console.log("设备控制", res)
                if (this.cutoff(res.msg)) return;
                this.uninitialized = false;
                this.control = res.data;
                this.$Http.setControlItem = this.setControlItem.bind(this);
                this.setMonitoring()
            })
        },
        setControlItem(w_functionid, params, title = "已发送修改指令") {
            return this.$Http.basic({
                "id": "20230627163701",
                "content": {
                    "w_deviceid": this.detail.w_deviceid,
                    w_functionid,
                    params
                }
            }).then(res => {
                if (this.cutoff(res.msg, title)) return;
                console.log("更新设备控制", res)
                this.getControl(true);
                return res.msg == '成功'
            })
        },
        setMonitoring() {
            clearInterval(monitoring)
            monitoring = setInterval(() => {
                this.$Http.devicevaluecheck({
                    "w_deviceid": this.detail.w_deviceid,
                }).then(res => {
                    if (res == true) {
                        /* uni.showModal({
                            title: "通知",
                            content: '设备控制参数已更新',
                            showCancel: false,
                        }) */
                        this.getControl(true);
                    }
                })
            }, 5000);
        }
    },
    beforeDestroy() {
        clearInterval(monitoring)
    },
}
</script>

<style lang="scss" scoped>
.container {
    width: 355px;
    margin: 0 auto;
}
</style>