Newer
Older
smartKitchenMiniProgram / packageA / home_center / common_panel / index.js
dutingting on 28 Dec 2022 4 KB 设备调整
// miniprogram/pages/home_center/common_panel/index.js.js
require('regenerator-runtime')
import { getDevFunctions, getDeviceDetails, deviceControl } from '../../../utils/api/device-api'
import wxMqtt from '../../../utils/mqtt/wxMqtt'


Page({

  /**
   * 页面的初始数据
   */
  data: {
		device_name: '',
		devide_img: '',
    titleItem: {
      name: '',
      value: '',
    },
    roDpList: {}, //只上报功能点
    rwDpList: {}, //可上报可下发功能点
    isRoDpListShow: false,
    isRwDpListShow: false,
		forest: '../../image/forest@2x.png',
		typeTitle: {
			temp_current: '当前温度',
			fault: '故障告警',
			total_runtime: '累计工作时间',
			cooker_state_l: '左灶状态',
			cooker_state_r: '右灶状态',
			cooker_remain_time_l: '左灶保持时间',
			cooker_remain_time_r: '右灶保持时间',
      runtime_total: '累计工作时间',
      pm25: 'PM2.5',
      countdown_set: '倒计时',
			countdown_left: '倒计时剩余时间',
			filter: '滤芯使用率',
		},
		typeValue: {
			close: '关',
			cancle: '取消',
		}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { device_id } = options
    this.setData({ device_id })

    // mqtt消息监听
    wxMqtt.on('message', (topic, newVal) => {
      const { status } = newVal
      console.log(newVal)
      this.updateStatus(status)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: async function () {
    const { device_id } = this.data
    const [{ name, status, icon }, { functions = [] }] = await Promise.all([
      getDeviceDetails(device_id),
      getDevFunctions(device_id),
    ]);
		console.log('总状态', functions)
    const { roDpList, rwDpList } = this.reducerDpList(status, functions)

    // 获取头部展示功能点信息
    let titleItem = {
      name: '',
      value: '',
    };
    if (Object.keys(roDpList).length > 0) {
      let keys = Object.keys(roDpList)[0];
      titleItem = roDpList[keys];
    } else {
      let keys = Object.keys(rwDpList)[0];
      titleItem = rwDpList[keys];
    }

    const roDpListLength = Object.keys(roDpList).length;
    const isRoDpListShow = Object.keys(roDpList).length > 0;
		const isRwDpListShow = Object.keys(rwDpList).length > 0;
		const devide_img = 'https://images.tuyacn.com/' + icon;

		this.setData({ titleItem, roDpList, rwDpList, device_name: name, isRoDpListShow, isRwDpListShow, roDpListLength, icon, devide_img})
		console.log('rwDpList可控制状态', rwDpList)
		console.log('roDpList', roDpList)
	},

  // 分离只上报功能点,可上报可下发功能点
  reducerDpList: function (status, functions) {
    // 处理功能点和状态的数据
    let roDpList = {};
    let rwDpList = {};
    if (status && status.length) {
      status.map((item) => {
        const { code, value } = item;
				let isExit = functions.find(element => element.code == code);
				// 单独处理空气净化器的倒计时设置
				// if(code === 'countdown_set' && functions.find(element => element.code == 'countdown')) {
				// 	isExit = functions.find(element => element.code == 'countdown')
				// }
				console.log('isExit',isExit)
        if (isExit) {
          let rightvalue = value
          // 兼容初始拿到的布尔类型的值为字符串类型
          if (isExit.type === 'Boolean') {
            rightvalue = value == 'true'
          }

          rwDpList[code] = {
            code,
            value: rightvalue,
            type: isExit.type,
            values: isExit.values,
            name: isExit.name,
          };
        } else {
          roDpList[code] = {
            code,
            value,
            name: code,
          };
        }
      });
		}
    return { roDpList, rwDpList }
  },

	//下发指令
  sendDp: async function (e) {
    const { dpCode, value } = e.detail
    const { device_id } = this.data
		const success = await deviceControl(device_id, dpCode, value)
		if(!success) return wx.showToast({
			title: '下发dp失败',
			icon: 'error'
		})
  },

	//更新状态
  updateStatus: function (newStatus) {
    let { roDpList, rwDpList, titleItem } = this.data

    newStatus.forEach(item => {
      const { code, value } = item

      if (typeof roDpList[code] !== 'undefined') {
        roDpList[code]['value'] = value;
      } else if (rwDpList[code]) {
        rwDpList[code]['value'] = value;
      }
    })

    // 更新titleItem
    if (Object.keys(roDpList).length > 0) {
      let keys = Object.keys(roDpList)[0];
      titleItem = roDpList[keys];
    } else {
      let keys = Object.keys(rwDpList)[0];
      titleItem = rwDpList[keys];
    }
 
    this.setData({ titleItem, roDpList: { ...roDpList }, rwDpList: { ...rwDpList } })
  },

  jumpTodeviceEditPage: function(){
    console.log('jumpTodeviceEditPage')
    const { icon, device_id, device_name } = this.data
    wx.navigateTo({
      url: `/packageA/home_center/device_manage/index?device_id=${device_id}&device_name=${device_name}&device_icon=${icon}`,
    })
  }
})