package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.ep;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ep.IEpRfidDispatchService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.rfid.ReaderUtil;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.rfid.RequestPacketModel;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.rfid.RfidDataAreaEnum;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpRfidDispatchModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class EpRfidDispatchService implements IEpRfidDispatchService {

    @Override
    public String doRfidDispatch(EpRfidDispatchModel model) {
        String rfidNo = null;
        try {
            byte[] tagIdSource = new byte[0];
            for (int i = 1; i < 11; i ++) {
                tagIdSource = ReaderUtil.sendCommand(new RequestPacketModel()
                        .buildListTagIdCommand((byte) RfidDataAreaEnum.valueOfMen(model.getMem()), model.getWorkCenterCode(), model.getWorkCellCode()),
                        model.getIp(), model.getPort());
                if (null != tagIdSource) {
                    log.info("生产线[{}]工位[{}]doRfidDispatch获取tagIdSource:[{}]执行[{}]次",
                            model.getWorkCenterCode(), model.getWorkCellCode(), ReaderUtil.bytes2HexString(tagIdSource), i);
                    tagIdSource = ReaderUtil.getTagsId(tagIdSource);
                }
                if (null != tagIdSource) {
                    log.info("生产线[{}]工位[{}]doRfidDispatch截取tagIdSource:[{}]执行[{}]次",
                            model.getWorkCenterCode(), model.getWorkCellCode(), ReaderUtil.bytes2HexString(tagIdSource), i);
                    break;
                }
                Thread.sleep(model.getTime());
            }

            if (null == tagIdSource) {
                log.info("生产线[{}]工位[{}]doRfidDispatch获取tagIdSource失败,已执行[10]次,每次间隔[{}]毫秒", model.getWorkCenterCode(), model.getWorkCellCode(), model.getTime());
                return rfidNo;
            }

            byte[] resultSource = new byte[0];
            Byte[] tagIdSourceB = new Byte[tagIdSource.length];
            for (int i = 0; i<tagIdSource.length; i++) {
                tagIdSourceB[i] = tagIdSource[i];
            }

            Thread.sleep(model.getTime());

            for (int i = 1; i < 11; i ++) {
                resultSource = ReaderUtil.sendCommand(new RequestPacketModel()
                        .buildReadTagCommand(tagIdSourceB, (byte) RfidDataAreaEnum.valueOfMen(model.getMem()), model.getLen(), model.getWorkCenterCode(), model.getWorkCellCode()),
                        model.getIp(), model.getPort());
                if (null != resultSource) {
                    log.info("生产线[{}]工位[{}]doRfidDispatch获取resultSource:[{}]执行[{}]次",
                            model.getWorkCenterCode(), model.getWorkCellCode(), ReaderUtil.bytes2HexString(resultSource), i);
                    resultSource = ReaderUtil.getTagsResult(resultSource);
                }
                if (null != resultSource) {
                    rfidNo = ReaderUtil.bytes2HexString(resultSource);
                    log.info("生产线[{}]工位[{}]doRfidDispatch截取resultSource:[{}]执行[{}]次",
                            model.getWorkCenterCode(), model.getWorkCellCode(), rfidNo, i);
                    break;
                }
                Thread.sleep(model.getTime());
            }

            if (null == resultSource) {
                log.info("生产线[{}]工位[{}]doRfidDispatch获取resultSource失败,已执行[10]次,每次间隔[{}]毫秒", model.getWorkCenterCode(), model.getWorkCellCode(), model.getTime());
            }
        } catch (Exception e) {
            log.info("生产线[{}]工位[{}]doRfidDispatch出现异常:{}", model.getWorkCenterCode(), model.getWorkCellCode(), e.getMessage());
        }

        return rfidNo;
    }
}
