Pu Zhibing
2025-04-25 f8a708d9d960db750c9dd029efbe4a9f63d53fe8
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
package com.ruoyi.dataInterchange.server;
 
import com.alibaba.fastjson.JSON;
import com.ruoyi.dataInterchange.model.enu.DataType;
import com.ruoyi.dataInterchange.netty.client.ChannelMap;
import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket;
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;
 
 
/**
 * @author zhibing.pu
 * @Date 2025/3/6 15:46
 */
@Slf4j
@Component
public class UPLinkTestReqService {
    
    @Resource
    private RedisTemplate redisTemplate;
    
    
    /**
     * 主链路连接保持
     *
     * @param ctx
     * @param out
     */
    public void linkTest(ChannelHandlerContext ctx, OuterPacket out) {
        if (!redisTemplate.hasKey("login:" + out.getGnsscenterId())) {
            log.error("链路还未登录校验,拒绝连接");
            ctx.close();
            return;
        }
        int gnsscenterId = out.getGnsscenterId();
        //保存链路
        ChannelMap.addServerChannel(gnsscenterId, ctx.channel());
        OuterPacket rep = new OuterPacket(DataType.UP_LINKTEST_RSP.getCode(), gnsscenterId, null);
        rep.setGnsscenterId(gnsscenterId);
        log.info("主链路连接保持应答({}):{}", DataType.UP_LINKTEST_RSP.getCode(), JSON.toJSONString(rep));
        ctx.writeAndFlush(rep);
    }
}