jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
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());
    }
}