| | |
| | | import com.ard.work.component.netty.handler.MessageProtocolEncoder; |
| | | import com.ard.work.component.netty.cache.TyTcpServerCache; |
| | | import com.ard.work.domain.entity.ArdServerInfo; |
| | | import com.ard.work.service.IArdDroneInfoService; |
| | | import com.ard.work.service.IArdServerInfoService; |
| | | import io.netty.bootstrap.Bootstrap; |
| | | import io.netty.channel.*; |
| | |
| | | private IArdServerInfoService ardServerService; // ← 从数据库读取边缘服务器配置 |
| | | |
| | | @Resource |
| | | private IArdDroneInfoService ardDroneService; |
| | | |
| | | @Resource |
| | | private ClientHandler clientHandler; |
| | | |
| | | /** |
| | |
| | | @Override |
| | | protected void initChannel(SocketChannel socketChannel) { |
| | | ChannelPipeline pipeline = socketChannel.pipeline(); |
| | | pipeline.addLast("heart", new IdleStateHandler(0, 3, 0, TimeUnit.SECONDS)); |
| | | pipeline.addLast("heart", new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS)); |
| | | pipeline.addLast("encoder", new MessageProtocolEncoder()); |
| | | pipeline.addLast("decoder", new MessageProtocolDecoder()); |
| | | pipeline.addLast("handler", clientHandler); |
| | |
| | | Channel channel = f.channel(); |
| | | channel.attr(AttributeKey.valueOf("serverId")).set(serverId); |
| | | TyTcpServerCache.putTcpServerCache(serverId, true); |
| | | // 断开服务器,修改状态为: 0 - 在线 |
| | | ardServerService.updateServerStatus(serverId, "1"); |
| | | // 修改状态为: 1 - 在线 |
| | | updateServerAndDroneStatus(serverId, "1"); |
| | | |
| | | // 成功后再加入 channels |
| | | TyTcpServerCache.putChannel(serverId, channel); |
| | |
| | | log.warn("❌ 服务器 [{}] 连接失败", serverId); |
| | | TyTcpServerCache.putTcpServerCache(serverId, false); |
| | | // 断开服务器,修改状态为: 0 - 离线 |
| | | ardServerService.updateServerStatus(serverId, "0"); |
| | | updateServerAndDroneStatus(serverId, "0"); |
| | | } |
| | | }); |
| | | } |
| | |
| | | channel.close(); |
| | | log.info("断开服务器 [{}] 连接", serverId); |
| | | // 断开服务器,修改状态为: 0 - 离线 |
| | | ardServerService.updateServerStatus(serverId, "0"); |
| | | updateServerAndDroneStatus(serverId, "0"); |
| | | TyTcpServerCache.removeChannel(serverId); |
| | | } |
| | | TyTcpServerCache.removeTcpServerCache(serverId); |
| | | } |
| | | |
| | | /** |
| | | * 同步修改服务和无人机状态 |
| | | */ |
| | | private void updateServerAndDroneStatus(String serverId, String status) { |
| | | ardServerService.updateServerStatus(serverId, status); |
| | | ardDroneService.updateDroneStatus(serverId, status); |
| | | } |
| | | |
| | | /** |
| | | * 启动时从数据库加载所有边缘服务器并建立 TCP 连接 |
| | |
| | | @PostConstruct |
| | | public void initConnectServers() { |
| | | try { |
| | | var serverList = ardServerService.selectArdServerInfoList(new ArdServerInfo()); // ← 从数据库查询所有服务器配置 |
| | | var serverList = ardServerService.selectArdServerInfoList(new ArdServerInfo() {{ setUseDataScope(false); }}); // ← 从数据库查询所有服务器配置 |
| | | if (serverList == null || serverList.isEmpty()) { |
| | | log.warn("未在数据库中找到边缘服务器配置,跳过连接初始化"); |
| | | return; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 定时向所有已连接的六合服务器发送心跳 |
| | | * 定时向所有已连接的边缘服务器服务器发送心跳(写空闲已经发心跳了,这里暂时取消) |
| | | */ |
| | | @Scheduled(fixedRate = 5000) // 每5秒执行一次 |
| | | // @Scheduled(fixedRate = 5000) // 每5秒执行一次 |
| | | public void sendHeartBeat() { |
| | | long timestamp = System.currentTimeMillis(); |
| | | TyTcpServerCache.getChannels().stream() |
| | |
| | | String serverId = (String) channel.attr(AttributeKey.valueOf("serverId")).get(); |
| | | HeartbeatCmd heartbeat = new HeartbeatCmd(timestamp); |
| | | channel.writeAndFlush(heartbeat); |
| | | log.info("→ 向边缘服务器 [{}]发送心跳,时间戳={}", serverId, timestamp); |
| | | log.info("❤\uFE0F 向边缘服务器 [{}]发送心跳,时间戳={}", serverId, timestamp); |
| | | }); |
| | | } |
| | | } |