package com.bonus.tcp.smartIdentify.controller;


import com.alibaba.fastjson.JSON;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.tcp.TcpMessage;
import com.bonus.tcp.server.config.BootNettyChannel;
import com.bonus.tcp.server.config.BootNettyChannelCache;
import com.bonus.tcp.server.server.BootNettyServer;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 蚂蚁舞
 */
@Slf4j
@RestController
@RequestMapping("/netty")
public class BootNettyController {


    @Autowired
    BootNettyServer nettyServer;
    @PostMapping("/setMessage")
    public AjaxResult setMessage(@RequestBody TcpMessage message) {
        try{
            Map<String,Object> map=new HashMap<>();
            if(StringUtils.isEmpty(message.getDevCode())){
                return  AjaxResult.error("请选择设备!");
            }
            if(message.getDistance()==0l){
                return   AjaxResult.error("距离不能为0!");
            }
            map.put("type",1);
            map.put("id",message.getDevCode());
            map.put("distance",message.getDistance());
            String msg= JSON.toJSONString(map);
            String result= nettyServer.sendMessage(message.getDevCode(),msg);
            return AjaxResult.success(result);
        }catch (Exception e){
            return  AjaxResult.error("服务异常,请联系管理员");
        }
    }

}
