package com.zt.plat.module.qms.iot.tcpserver.consoleclient;

import com.alibaba.fastjson.JSON;
import com.zt.plat.module.qms.iot.tcpserver.consoleclient.bean.Register;
import com.zt.plat.module.qms.iot.tcpserver.handler.IotDataHander;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import tech.zzjc.tio.core.ChannelContext;
import tech.zzjc.tio.core.Tio;

/**
 * <b>IotConsoleClientRegisterHander</b>
 * <p>更新历史：</p>
 * <pre> 版本         更新时间            更新者        更新内容
 * V1.0      2021-7-2          王兴荣         Add</pre>
 * <b>Copyright (C)  云南志者竟成科技有限公司</b> 
 * @author 王兴荣 &lt;xingrong_wang@will-way.cn&gt;
 * @version V1.0 
 * @since 2021-7-2
 */
@Slf4j
@Component
public class IotConsoleClientRegisterHander implements IotDataHander {


    @Override
    public Object hander(String msgId, String data, ChannelContext channelContext) throws Exception {
        //获取客户端连接信息
        String realClientIp = channelContext.get("realClientIp") == null ? channelContext.getClientNode().getIp() : channelContext.get("realClientIp").toString();
        String realClientPort = channelContext.get("realClientPort") == null ? channelContext.getClientNode().getPort() + "" : channelContext.get("realClientPort").toString();
        String realClient = realClientIp + ":" + realClientPort;
        Register register = JSON.parseObject(data, Register.class);
        log.info("{}，, APP客户端注册租户ID：{}，用户ID：{}", realClient, register.getTenantId(), register.getUserId());
        Tio.bindUser(channelContext, register.getUserId());
        Tio.bindToken(channelContext, register.getToken());
//        UserTokenContext.setToken(register.getToken());
//        channelContext.set("regTime", new Date());
//        channelContext.set("user-real-name", register.getUserRealName());
//        IotPacket resppacket = new IotPacket();
//        resppacket.setBody((ReplyResult.success(msgId, ClientType.CONSOLE_CLIENT.getName(), Command.REPLY_REGISTER.getName()) + "\r\n").getBytes(Charset.forName(IotPacket.CHARSET)));
//        //回复注册成功
//        Tio.send(channelContext, resppacket);
//
//        Result<List<QmsBasMeasurePointWithUserAndDevice>> userMeasurePointResult = qmsApi.listByUserId(register.getUserId());
//        if (!userMeasurePointResult.isSuccess()) {
//			return null;
//		}
//        List<QmsBasMeasurePointWithUserAndDevice> userMeasurePointList = userMeasurePointResult.getResult();
//        for (QmsBasMeasurePointWithUserAndDevice measurePointWithUserAndDevice : userMeasurePointList) {
//        	//绑定计量点
//        	Tio.bindGroup(channelContext, measurePointWithUserAndDevice.getId());
//        	//处理计量点由谁控制
//            ChannelContext measurePointChannelContext = Tio.getByBsId(channelContext.getTioConfig(), measurePointWithUserAndDevice.getId());
//            if (measurePointChannelContext == null) {//计量点不在线
//            	continue;
//            }
//
//            //通知计量点重新上报计量数据
//            JSONObject json = new JSONObject();
//            json.put("msgId", msgId);
//            json.put("cmd", Command.MEASURE_POINT_REUPLOAD.getName());
//            json.put("clientType", ClientType.MEASURE_POINT.getName());
//            json.put("data", null);
//            resppacket = new IotPacket();
//            resppacket.setBody((json.toJSONString() + "\r\n").getBytes(Charset.forName(IotPacket.CHARSET)));
//            Tio.send(measurePointChannelContext, resppacket);
//
//            IotClientSessionContext measurePointSessionContext = (IotClientSessionContext) measurePointChannelContext.get(IotClientSessionContext.DEFAULT_CLIENT_SESSION_CONTEXT_KEY);
//            ChannelContext controlConsoleClientChannelContext = measurePointSessionContext.getControlConsoleClientChannelContext();
//            if (controlConsoleClientChannelContext == null) {//计量点未被控制
//                continue;
//            }
//
//            //计量点被控制
//            json = new JSONObject();
//            json.put("msgId", msgId);
//            json.put("cmd", Command.REPLY_CONTROL_MEASURE_POINT.getName());
//            json.put("clientType", ClientType.CONSOLE_CLIENT.getName());
//            ControlMeasurePoint controlMeasurePoint = new ControlMeasurePoint();
//            controlMeasurePoint.setMeasurePointId(measurePointWithUserAndDevice.getId());
//            controlMeasurePoint.setControlUserName(controlConsoleClientChannelContext.get("user-real-name").toString());
//            controlMeasurePoint.setSuccess(true);
//            controlMeasurePoint.setIsControl(true);
//            controlMeasurePoint.setMsg("计量点被“" + controlMeasurePoint.getControlUserName() + "”接管控制！");
//            json.put("data", controlMeasurePoint);
//            resppacket = new IotPacket();
//            resppacket.setBody((json.toJSONString() + "\r\n").getBytes(Charset.forName(IotPacket.CHARSET)));
//            Tio.send(channelContext, resppacket);
//		}
        
        return null;
    }

}
