From 3a722ebc823d6ffb983fbcecb13889896fb87a18 Mon Sep 17 00:00:00 2001 From: "Mr.Wang" Date: Tue, 23 Apr 2024 15:55:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=A4=E6=8D=A2=E6=9C=BA?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/constant/RedisKeyConstants.java | 5 ++ .../controller/DcSwitchController.java | 50 +++++++++++++ .../java/com/zc/business/domain/DcSwitch.java | 21 ++++++ .../zc/business/mapper/DcSwitchMapper.java | 2 + .../zc/business/service/DcSwitchService.java | 6 ++ .../service/impl/DcDeviceServiceImpl.java | 5 ++ .../service/impl/DcSwitchServiceImpl.java | 71 ++++++++++++++++--- .../mapper/business/DcSwitchMapper.xml | 17 +++++ 8 files changed, 168 insertions(+), 9 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java b/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java index 96985260..573eb12b 100644 --- a/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java +++ b/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java @@ -11,6 +11,11 @@ public class RedisKeyConstants */ public static final String DC_DEVICES = "dc:devices"; + /** + * dc设备 + */ + public static final String DC_DEVICE_ID = "dc:deviceId"; + /** * 路段 */ diff --git a/zc-business/src/main/java/com/zc/business/controller/DcSwitchController.java b/zc-business/src/main/java/com/zc/business/controller/DcSwitchController.java index 7b1d0f33..71d5fd56 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcSwitchController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcSwitchController.java @@ -6,12 +6,20 @@ import com.zc.business.domain.DcSwitch; import com.zc.business.service.DcSwitchService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import java.io.IOException; +import java.net.InetAddress; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; /** * 交换机Controller @@ -46,10 +54,52 @@ public class DcSwitchController extends BaseController { /** * 查询所有数据 + * * @return */ @GetMapping("/list") public AjaxResult getSwitchListAll() { return AjaxResult.success(dcSwitchService.getSwitchListAll()); } + + /** + * 定时更新交换机网络状态 + */ + @Scheduled(cron = "0 0/30 * * * ?") + public void updateNetWorkStatus() { + List switchList = dcSwitchService.getSwitchList(new DcSwitch()); + ExecutorService executor = Executors.newFixedThreadPool(100); + List collect = switchList.stream() + .filter(dcSwitch -> { + return dcSwitch.getAncestors().split(",").length > 1; + }).collect(Collectors.toList()); + CountDownLatch latch = new CountDownLatch(collect.size()); + collect.forEach(dcSwitch -> { + executor.execute(() -> { + try { + InetAddress inet = InetAddress.getByName(dcSwitch.getSwitchIp()); + if (inet.isReachable(5000)) { + // 成功 + dcSwitch.setNetWorkStatus(1); + } else { + // 失败 + dcSwitch.setNetWorkStatus(0); + } + } catch (IOException e) { + e.getMessage(); + } finally { + latch.countDown(); + } + }); + }); + try { + latch.await(); // 等待所有线程执行完毕 + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + executor.shutdown(); + } + // 批量修改 + dcSwitchService.updateBatchByNetWorkStatus(collect); + } } diff --git a/zc-business/src/main/java/com/zc/business/domain/DcSwitch.java b/zc-business/src/main/java/com/zc/business/domain/DcSwitch.java index f0963526..9fd63400 100644 --- a/zc-business/src/main/java/com/zc/business/domain/DcSwitch.java +++ b/zc-business/src/main/java/com/zc/business/domain/DcSwitch.java @@ -10,6 +10,7 @@ import lombok.NoArgsConstructor; import java.util.Date; import java.util.List; +import java.util.Map; /** * 交换机对象 dc_switch @@ -72,6 +73,20 @@ public class DcSwitch { @ApiModelProperty("交换机ip") private String switchIp; + /** + * 网路状态 + */ + @Excel(name = "网路状态") + @ApiModelProperty("网路状态") + private Integer netWorkStatus; + + /** + * 环网 + */ + @Excel(name = "环网") + @ApiModelProperty("环网") + private Integer ringNetWork; + /** * 使用状态 */ @@ -98,4 +113,10 @@ public class DcSwitch { private List children; + /** + * 故障数量 + */ + private int numberOfFaults; + private Map> dcDeviceList; + } diff --git a/zc-business/src/main/java/com/zc/business/mapper/DcSwitchMapper.java b/zc-business/src/main/java/com/zc/business/mapper/DcSwitchMapper.java index 4c393f70..7b59135e 100644 --- a/zc-business/src/main/java/com/zc/business/mapper/DcSwitchMapper.java +++ b/zc-business/src/main/java/com/zc/business/mapper/DcSwitchMapper.java @@ -14,4 +14,6 @@ import java.util.List; @Mapper public interface DcSwitchMapper extends BaseMapper { List getSwitchList(DcSwitch dcSwitch); + + int updateBatchByNetWorkStatus(List list); } diff --git a/zc-business/src/main/java/com/zc/business/service/DcSwitchService.java b/zc-business/src/main/java/com/zc/business/service/DcSwitchService.java index c1c98440..5fb43223 100644 --- a/zc-business/src/main/java/com/zc/business/service/DcSwitchService.java +++ b/zc-business/src/main/java/com/zc/business/service/DcSwitchService.java @@ -28,4 +28,10 @@ public interface DcSwitchService { * @return */ List getSwitchListAll(); + + /** + * 批量修改网络状态 + * @return + */ + int updateBatchByNetWorkStatus(List list); } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java index 464622c0..f659096d 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java @@ -68,13 +68,18 @@ public class DcDeviceServiceImpl extends ServiceImpl i // 清楚 redis 缓存数据 redisCache.deleteObject(RedisKeyConstants.DC_DEVICES); + redisCache.deleteObject(RedisKeyConstants.DC_DEVICE_ID); // 添加 redis 缓存数据 dcDevices.forEach(val -> { String iotDeviceId = val.getIotDeviceId(); + Long id = val.getId(); if (StringUtils.hasText(iotDeviceId)) { redisCache.setCacheMapValue(RedisKeyConstants.DC_DEVICES, val.getIotDeviceId(), val); } + if (StringUtils.hasText(id.toString())) { + redisCache.setCacheMapValue(RedisKeyConstants.DC_DEVICE_ID, val.getId(), val); + } }); } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcSwitchServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcSwitchServiceImpl.java index 1c6a2d05..5d77cf24 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcSwitchServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcSwitchServiceImpl.java @@ -1,19 +1,20 @@ package com.zc.business.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.StringUtils; +import com.zc.business.constant.RedisKeyConstants; import com.zc.business.domain.DcDevice; import com.zc.business.domain.DcSwitch; -import com.zc.business.mapper.DcDeviceMapper; import com.zc.business.mapper.DcSwitchMapper; import com.zc.business.service.DcSwitchService; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; +import java.util.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.stream.Collectors; /** @@ -29,14 +30,14 @@ public class DcSwitchServiceImpl extends ServiceImpl i private DcSwitchMapper dcSwitchMapper; @Resource - private DcDeviceMapper dcDeviceMapper; + private RedisCache redisCache; /** * 查询交换机 */ @Override public List getSwitchList(DcSwitch dcSwitch) { - return dcSwitchMapper.getSwitchList(dcSwitch); + return getDeviceListBySwitch(dcSwitchMapper.getSwitchList(dcSwitch)); } /** @@ -48,7 +49,12 @@ public class DcSwitchServiceImpl extends ServiceImpl i return new ArrayList<>(); } List devices = Arrays.stream(deviceList.split(",")).collect(Collectors.toList()); - return dcDeviceMapper.selectBatchIds(devices); + List dcDevices = new ArrayList<>(); + devices.forEach(device -> { + DcDevice cacheMapValue = redisCache.getCacheMapValue(RedisKeyConstants.DC_DEVICE_ID,device); + dcDevices.add(cacheMapValue); + }); + return dcDevices; } /** @@ -58,7 +64,8 @@ public class DcSwitchServiceImpl extends ServiceImpl i */ @Override public List getSwitchListAll() { - List dcSwitches = dcSwitchMapper.getSwitchList(new DcSwitch()); + List deviceListBySwitch = dcSwitchMapper.getSwitchList(new DcSwitch()); + List dcSwitches = getDeviceListBySwitch(deviceListBySwitch); List returnList = new ArrayList<>(); List tempList = new ArrayList<>(); for (DcSwitch switche : dcSwitches) { @@ -77,6 +84,52 @@ public class DcSwitchServiceImpl extends ServiceImpl i return returnList; } + /** + * 处置交换机数据-获取设备数据 + * @param list + * @return + */ + public List getDeviceListBySwitch(List list) { + ExecutorService executor = Executors.newFixedThreadPool(100); + CountDownLatch latch = new CountDownLatch(list.size()); + list.forEach(dcSwitch -> { + executor.execute(() ->{ + try { + if (StringUtils.isNotEmpty(dcSwitch.getDeviceList())) { + // 说明是设备交换机 + List deviceList = getDeviceList(dcSwitch.getDeviceList()); + if (deviceList.size() > 0) { + Map> collect = deviceList.stream().collect(Collectors.groupingBy(DcDevice::getDeviceType)); + dcSwitch.setDcDeviceList(collect); + } + } + }catch (Exception e) { + e.printStackTrace(); + }finally { + latch.countDown(); + } + }); + }); + + try { + latch.await(); // 等待所有线程执行完毕 + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + executor.shutdown(); + } + return list; + } + + /** + * 批量修改网络状态 + * @return + */ + @Override + public int updateBatchByNetWorkStatus(List list) { + return dcSwitchMapper.updateBatchByNetWorkStatus(list); + } + private void recursionFn(List list, DcSwitch t) { // 得到子节点列表 List childList = getChildList(list, t); diff --git a/zc-business/src/main/resources/mapper/business/DcSwitchMapper.xml b/zc-business/src/main/resources/mapper/business/DcSwitchMapper.xml index eedb45cc..8f3e77be 100644 --- a/zc-business/src/main/resources/mapper/business/DcSwitchMapper.xml +++ b/zc-business/src/main/resources/mapper/business/DcSwitchMapper.xml @@ -12,6 +12,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + @@ -26,6 +28,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" stake_mark, device_list, switch_ip, + ring_netWork, + netWork_status, user_state, create_time, update_time @@ -40,4 +44,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + update dc_switch set netWork_status = + + when #{item.switchId} then #{item.netWorkStatus} + + where switch_id in + + #{item.switchId} + + +