import { setallLampHttp, getStatusHttp } from '@/services/ant-design-pro/equipment';
import { message } from 'antd';

// 定时调用 查询指令接口
export const checkChangeLightStatusSuccess = (paramsArr, result, fn) => {
  let checkParams = paramsArr;
  checkParams.forEach((item) => {
    item.orderSeqNum = result.orderSeqNum;
  });
  getStatusHttp(checkParams).then((res) => {
    judgeChangeResponeseSuccess(res, paramsArr, fn);
  });
};

//   检查开关指令下发状态
export const judgeChangeResponeseSuccess = (response, paramsArr, fn) => {
  let result = response.content[0];
  if (result.result === 'success') {
    if (result.state === 200 && result.exeResult === 'success') {
      console.log('成功了');
      fn(); // 查询灯设备
    } else if (
      (result.state === 200 && result.exeResult === 'processing:rcvd') ||
      (result.state === 202 && !result.exeResult)
    ) {
      setTimeout(() => {
        checkChangeLightStatusSuccess(paramsArr, result, fn);
      }, 500);
    }else if (result.state === 200 && result.exeResult === 'fail:timeout') {
        message.error('网络超时，请重试');
    } else {
      message.error('操作失败，请重试');
    }
  } else {
    message.error('操作失败，请重试');
  }
};
//  开关
export const setallLamps = (paramsArr, fn) => {
  setallLampHttp(paramsArr).then((res) => {
    judgeChangeResponeseSuccess(res, paramsArr, fn);
  });
};
