package com.cisdi.data.caliper.gateway;

import com.cisdi.data.common.exception.BusinessException;
import com.cisdi.data.util.HexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 采用TCP/IP通信，本机做客户端，连上服务器后主动上报采集数据，数据采用大端单精度浮点数上传。具体如下：
 * 上传数据: f5 f5 f5     02 3f 9e 04 19 40 0f 02 0c        f6 f6 f6
 * <p>
 * 起始帧 0xF5 0xF5 0xF5
 * 数据长度(字节) 0x08
 * 宽度(直径1.2345mm) 0x3F/63 0x9E/158 0x04/4 0x19/25
 * 厚度(直径2.2345mm) 0x40 0x0F 0x02 0x0C
 * 校验码0x57 数据之和取低八位数据  0x3f+0x9e+0x04+0x19+0x40+0x0f+0x02+0x0c=0x157
 * 结束帧 0xF6 0xF6 0xF6
 *
 * 3F 9E 04 19
 *
 * @className: CaliperVoDecoder
 * @description: CaliperVo解码器
 * @author: zhang.zhipeng
 * @date: 2024/9/11 8:53
 */
public class CaliperVoDecoder {

    public static List<CaliperItemVo> Decode(ByteBuf in) {
        List<CaliperItemVo> result = new ArrayList<>();
        //消息体部分
        int length;
        if (in == null) {
            throw new BusinessException("ByteBuf 入参为null");
        } else if (in.readableBytes() == 14) {//起始帧3位，结束帧3位，中间8位
            length = 8;
        } else {
            throw new BusinessException("报文长度不匹配 ,期望：14，实际：" + in.readableBytes());
        }
        //去掉报文头 和 结束符
        ByteBuf proceedIn = in.retainedSlice(3, length);
        result.add(split(proceedIn));
        ReferenceCountUtil.release(proceedIn);
        return result;
    }


    /**
     * @param in 去掉头尾的报文
     * @Description: 拆分报文
     * @author: zhang.zhipeng
     * @Date: 2024/9/11 8:53
     * @return: com.cisdi.data.RFID.gateway.RfidItemVo
     */
    public static CaliperItemVo split(ByteBuf in) {
        CaliperItemVo caliperItemVo = new CaliperItemVo();
        ByteBuf buf = in.readBytes(8);
        // 编号
        String code = buf.toString(CharsetUtil.UTF_8);
        caliperItemVo.setCode(code);
        ReferenceCountUtil.release(buf);
        return caliperItemVo;
    }

    public static void main(String[] args) throws Exception {
        // 标签读取
//	    byte[] readBytes1 = HexUtil.hexStringToBytes("001c1952000b000e3000eabcdef01234567890003001000400000501");
//	    ByteBuf copiedBuffer = Unpooled.copiedBuffer(readBytes1);
//	    RfidItemVo decoderVo = RfidVoDecoder.Decode(copiedBuffer);
//	    logger.info("vo1={}", decoderVo);
//
//	    // 标签离开
//        readBytes1 = HexUtil.hexStringToBytes("00161942050c000e3000eabcdef01234567890003001");
//        copiedBuffer = Unpooled.copiedBuffer(readBytes1);
//        decoderVo = RfidVoDecoder.Decode(copiedBuffer);
//        logger.info("vo1={}", decoderVo);

        //标签读取


        byte[] readBytes1 = HexUtil.hexStringToBytes("02524630313030303103");
        ByteBuf copiedBuff = Unpooled.copiedBuffer(readBytes1);
        List<CaliperItemVo> caliperItemVoList = CaliperVoDecoder.Decode(copiedBuff);
        caliperItemVoList.forEach(System.out::println);


        readBytes1 = HexUtil.hexStringToBytes("0248656172744265617403");
        copiedBuff = Unpooled.copiedBuffer(readBytes1);
        caliperItemVoList = CaliperVoDecoder.Decode(copiedBuff);
        caliperItemVoList.forEach(System.out::println);

        CaliperFrameDecoder decoder = new CaliperFrameDecoder(null);
        readBytes1 = HexUtil.hexStringToBytes("0248656172744265617403");
        copiedBuff = Unpooled.copiedBuffer(readBytes1);
        decoder.decode(null, copiedBuff, new ArrayList<Object>());

    }
}
