package com.fzzy.protocol.bhzn.v01.server;
|
|
import com.fzzy.api.Constant;
|
import com.fzzy.api.data.ApiCommonDevice;
|
import com.fzzy.api.utils.SpringUtil;
|
import com.fzzy.gateway.service.GatewayDeviceService;
|
import com.ld.io.api.IoSession;
|
import com.ld.io.api.IoSessionListener;
|
import lombok.extern.slf4j.Slf4j;
|
|
/**
|
*
|
*/
|
@Slf4j
|
public class BhznGrainV01SessionListener implements IoSessionListener {
|
|
private GatewayDeviceService gatewayDeviceService;
|
|
/**
|
* 设备创建在线,需要注意当前使用主机模式,主机上线默认所有相同SN配置的分机全部上线
|
*
|
* @param session
|
*/
|
@Override
|
public void onCreate(IoSession session) {
|
//添加到内存
|
BhznGrainV01ServerUtils.addSession(session);
|
log.info("++++新建连接++++-IP={},PORT={}", session.getAddress(), session.getPort());
|
|
// 添加自定义业务ID
|
session.setBusinessKey(BhznGrainV01ServerUtils.getServerKey(session.getAddress(), session.getPort()));
|
// GatewayDeviceService gatewayDeviceService = SpringUtil.getBean(GatewayDeviceService.class);
|
// List<GatewayDevice> devices = gatewayDeviceService.listAll();
|
// //执行分机上线
|
// if(devices!= null && devices.size()>0){
|
// ApiCommonDevice device =null;
|
// for (GatewayDevice d:devices) {
|
// device = new ApiCommonDevice();
|
// device.setIp(d.getIp());
|
// device.setPort(d.getPort());
|
// device.setStatus(Constant.YN_N);
|
// device.setSn(d.getDeviceSn());
|
// device.setCode("ERROR");
|
// device.setMsg("设备离线");
|
// }
|
//
|
//
|
// contextDeviceMap.put(device.getIp(), device);
|
//
|
// }
|
Constant.updateCacheOnline(session.getAddress(), session.getPort());
|
|
}
|
|
/**
|
* 注意,当前采用主机模式,主机离线所有相同SN配置的分机全部离线
|
*
|
* @param session
|
*/
|
@Override
|
public void onDestroy(IoSession session) {
|
log.info("----连接断开-----IP={},PORT={}", session.getAddress(), session.getPort());
|
|
//设置分机掉线
|
ApiCommonDevice commonDevice = Constant.updateCacheOffline(session.getAddress(), session.getPort());
|
|
if (null == commonDevice) return;
|
|
if (null == gatewayDeviceService) {
|
gatewayDeviceService = SpringUtil.getBean(GatewayDeviceService.class);
|
|
gatewayDeviceService.OfflineByCommonDevice(commonDevice);
|
}
|
|
}
|
}
|