package com.mortals.xhx.daemon.netty.server.controlserver.handler;

import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.xhx.module.device.service.DeviceService;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

import static com.mortals.xhx.common.key.Constant.PARAM_SERVER_HTTP_URL;


/**
 * 业务处理
 *
 * @author: zxfei
 * @date: 2022/4/8 10:20
 */
public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    private DeviceService deviceService;

    public NettyUDPServerHandler(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
        // TODO 设备返回服务端配置
        String URL = GlobalSysInfo.getParamValue(PARAM_SERVER_HTTP_URL, "http://192.168.0.100:11021");
        String req = msg.content().toString(CharsetUtil.UTF_8);
        System.out.println(req);
        ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("服务器地址：" + URL, CharsetUtil.UTF_8), msg.sender()));

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // TODO Auto-generated method stub
        ctx.close();
        cause.printStackTrace();
    }
}
