vince
2024-07-03 69e8acc5dd1f760eb60e914472c151bfa8126a52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.fzzy.protocol.bhzn.v0.server;
 
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiCommonDevice;
import com.fzzy.api.service.ApiCommonService;
import com.fzzy.api.utils.SpringUtil;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.service.GatewayDeviceService;
import com.ld.io.api.IoSession;
import com.ld.io.api.IoSessionListener;
import lombok.extern.slf4j.Slf4j;
 
import java.util.List;
 
/**
 *
 */
@Slf4j
public class BhznGrainV0SessionListener implements IoSessionListener {
 
    private GatewayDeviceService gatewayDeviceService;
 
    /**
     * 设备创建在线,需要注意当前使用主机模式,主机上线默认所有相同SN配置的分机全部上线
     *
     * @param session
     */
    @Override
    public void onCreate(IoSession session) {
        //添加到内存
        BhznGrainV0ServerUtils.addSession(session);
        log.info("++++新建连接++++-IP={},PORT={}", session.getAddress(), session.getPort());
 
        // 添加自定义业务ID
        session.setBusinessKey(BhznGrainV0ServerUtils.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);
        }
 
    }
}