puzhibing
2 天以前 ea7595c4c75926f85388574b261b8ba90cf60e0d
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
81
82
83
84
85
86
87
88
89
90
91
package com.ruoyi.dataInterchange.server;
 
import com.ruoyi.dataInterchange.model.DOWNConnectRsp;
import com.ruoyi.dataInterchange.model.enu.DataType;
import com.ruoyi.dataInterchange.netty.client.ChannelMap;
import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket;
import com.ruoyi.dataInterchange.wapper.UPConnect;
import com.ruoyi.system.api.feignClient.EnterpriseClient;
import com.ruoyi.system.api.model.Enterprise;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
 
/**
 * 从链路连接应答
 *
 * @author zhibing.pu
 * @Date 2025/3/6 16:11
 */
@Slf4j
@Component
public class DOWNConnectRspService {
    
    @Resource
    private RedisTemplate redisTemplate;
    
    @Resource
    private ConnectReqService connectReqService;
 
    @Resource
    private EnterpriseClient enterpriseClient;
    
    
    /**
     * 从链路连接应答
     *
     * @param ctx
     * @param outerPacket
     */
    public void connectRsp(ChannelHandlerContext ctx, OuterPacket outerPacket) {
        ByteBuf byteBuf = Unpooled.wrappedBuffer(outerPacket.getBody());
        DOWNConnectRsp downConnectRsp = new DOWNConnectRsp().decode(byteBuf);
    }
    
    
    /**
     * 从链路连接保持请求
     *
     */
    public void downLinkTest(Integer inferiorPlatformId) {
        //获取从链路通道
        Channel channel = ChannelMap.getClientChannel(inferiorPlatformId);
        if (null != channel && channel.isActive()) {
            OuterPacket out = new OuterPacket(DataType.DOWN_LINKTEST_REQ.getCode(), inferiorPlatformId, null);
            channel.writeAndFlush(out);
            log.info("从链路连接保持请求({}):{}", DataType.DOWN_LINKTEST_REQ.getCode(), "");
            redisTemplate.opsForValue().set("login:" + inferiorPlatformId, System.currentTimeMillis(), 1, TimeUnit.MINUTES);
        } else {
            //记录失败次数,然后再重新连接
            Integer times = ChannelMap.getTimes(inferiorPlatformId);
            if(null == times){
                times = 0;
            }
            if (times >= 18) {
                UPConnect ipAndPort = ChannelMap.getIpAndPort(inferiorPlatformId);
                boolean b = connectReqService.downConnect(inferiorPlatformId, ipAndPort.getDownLinkIp(), ipAndPort.getDownLinkPort(), ipAndPort.getVerifyCode());
                if (b) {
                    times = 0;
                } else {
                    times++;
                }
            } else {
                times++;
            }
            ChannelMap.saveTimes(inferiorPlatformId, times);
        }
    }
}