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

import com.zt.plat.module.qms.iot.tcpserver.IotPacket;
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;

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

//    @Autowired
//    private IUserMeasurePointService userMeasurePointService;
    
    @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;
        log.info("{},检查控制点由谁控制", realClient);
        String token = channelContext.getToken();
        IotPacket resppacket = null;
//        List<UserMeasurePoint> userMeasurePointList = userMeasurePointService.list(Wrappers.<UserMeasurePoint>query().lambda().eq(UserMeasurePoint::getUserId, Func.toLong(userId)));
//        for (UserMeasurePoint userMeasurePoint : userMeasurePointList) {            
//            //处理计量点由谁控制
//            ChannelContext measurePointChannelContext = Tio.getByBsId(channelContext.getTioConfig(), userMeasurePoint.getMeasurePointId().toString());
//            if (measurePointChannelContext == null) {//计量点不在线
//                continue;
//            }
//            IotClientSessionContext measurePointSessionContext = (IotClientSessionContext) measurePointChannelContext.get(IotClientSessionContext.DEFAULT_CLIENT_SESSION_CONTEXT_KEY);
//            ChannelContext controlConsoleClientChannelContext = measurePointSessionContext.getControlConsoleClientChannelContext();
//            if (controlConsoleClientChannelContext == null) {//计量点未被控制
//                continue;
//            }
//            
//            //计量点被控制
//            JSONObject 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(userMeasurePoint.getMeasurePointId().toString());
//            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;
    }

}
