package com.ld.igds.protocol.iot.n2.fzzy.server;
|
|
|
import com.ld.igds.common.CoreSerService;
|
import com.ld.igds.constant.Constant;
|
import com.ld.igds.io.notify.ServerNotifyInvoker;
|
import com.ld.igds.models.Device;
|
import com.ld.igds.models.DeviceSer;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.SpringUtil;
|
import com.ld.io.api.IoSession;
|
import com.ld.io.api.IoSessionListener;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
@Slf4j
|
@Service
|
public class N2FzzySessionListener implements IoSessionListener {
|
private ServerNotifyInvoker serverNotifyInvoker;
|
@Autowired
|
private CoreSerService coreSerService;
|
@Override
|
public void onCreate(IoSession session) {
|
|
log.info("++++新建连接++++-IP={},PORT={}", session.getAddress(), session.getPort());
|
|
// 添加自定义业务ID
|
session.setBusinessKey(N2FzzyServerUtils.getServerKey(session.getAddress(), session.getPort()));
|
DeviceSer deviceSer = coreSerService.getCacheSerByIp(ContextUtil.getDefaultCompanyId(),session.getAddress());
|
if(deviceSer!= null ){
|
deviceSer.setIp(session.getAddress());
|
deviceSer.setPort(session.getPort());
|
deviceSer.setStatus(Constant.YN_Y);
|
coreSerService.updateByData(deviceSer);
|
}
|
if (null == serverNotifyInvoker) {
|
serverNotifyInvoker = SpringUtil.getBean(ServerNotifyInvoker.class);
|
}
|
serverNotifyInvoker.connectActive(session.getBusinessKey(),
|
session.getAddress(), session.getPort());
|
}
|
|
@Override
|
public void onDestroy(IoSession session) {
|
log.info("----连接断开-----IP={},PORT={}", session.getAddress(), session.getPort());
|
|
if (null == serverNotifyInvoker) {
|
serverNotifyInvoker = SpringUtil.getBean(ServerNotifyInvoker.class);
|
}
|
DeviceSer deviceSer = coreSerService.getCacheSerByIp(ContextUtil.getDefaultCompanyId(),session.getAddress());
|
if(deviceSer!= null ){
|
deviceSer.setIp(session.getAddress());
|
deviceSer.setPort(session.getPort());
|
deviceSer.setStatus(Constant.YN_N);
|
coreSerService.updateByData(deviceSer);
|
}
|
serverNotifyInvoker.connectDestory(session.getBusinessKey(),
|
session.getAddress(), session.getPort());
|
}
|
}
|