package com.huimv.acquisition.socket.eartagrecive.eartagsocket;

import com.huimv.acquisition.socket.eartagrecive.service.IClientDataPacketService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@ChannelHandler.Sharable
@Component
@Slf4j
public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
    
    /*@Autowired
    private IEartagDataService eartagDataService;*/
    @Autowired
    private IClientDataPacketService iClientDataPacketService;



//    @Override
//    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        ByteBuf data = (ByteBuf) msg;
//        System.out.println("收到的信息为："+data.toString(CharsetUtil.UTF_8));
//        ctx.writeAndFlush(msg);
////        ctx.write(msg);
    // ctx.writeAndFlush(msg);
// ctx.writeAndFlush(data);
// ctx.writeAndFlush(Unpooled.copiedBuffer(askText.getBytes()));
//    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf data = (ByteBuf) msg;
        String clientAsk = data.toString(CharsetUtil.UTF_8);


        /* 处理客户端请求数据 */
        String answer = iClientDataPacketService.handleClientSendPacket(clientAsk);
        if (answer != null) {
            log.info("###应答响应>>" + answer + "\n");
            ctx.writeAndFlush(Unpooled.copiedBuffer(answer.getBytes()));
        }else{
            log.info("###应答数据>>" + answer + "，不需要应答。\n");
        }
    }


   

   
}
