Преглед изворни кода

2023-10-25 redis信标失效后,加锁执行,优化查询网关数据列表,维护到redis。

chaiyunlong пре 2 година
родитељ
комит
0d3f651c59

+ 5 - 0
zd-model/src/main/java/com/zd/model/constant/BaseConstants.java

@@ -210,6 +210,11 @@ public interface BaseConstants {
     String BEACON_MATE_NOTICE = "beaconMateNotice";
 
     /**
+     * 蓝牙网关列表
+     */
+    String BEACON_SUBMAC = "beaconSubmac";
+
+    /**
      * Redis前缀,需统一
      */
     String REDIS_LOCK = "redis_lock:";

+ 21 - 5
zd-modules/zd-airbottle/src/main/java/com/zd/airbottle/controller/DbBluetoothGatewayController.java

@@ -18,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Controller
@@ -68,6 +69,7 @@ public class DbBluetoothGatewayController extends BaseController {
     @Log(title = "蓝牙网关", businessType = BusinessType.INSERT)
     @PostMapping
     public ResultData<Boolean> add(@RequestBody DbBluetoothGateway dbBluetoothGateway) {
+        redisService.deleteObject(BaseConstants.BEACON_SUBMAC);
         return ResultData.result(dbBluetoothGatewayService.insertDbBluetoothGateway(dbBluetoothGateway));
     }
 
@@ -79,6 +81,7 @@ public class DbBluetoothGatewayController extends BaseController {
     @Log(title = "蓝牙网关", businessType = BusinessType.UPDATE)
     @PutMapping
     public ResultData<Boolean> edit(@RequestBody DbBluetoothGateway dbBluetoothGateway) {
+        redisService.deleteObject(BaseConstants.BEACON_SUBMAC);
         return ResultData.result(dbBluetoothGatewayService.updateDbBluetoothGateway(dbBluetoothGateway));
     }
 
@@ -90,6 +93,7 @@ public class DbBluetoothGatewayController extends BaseController {
     @Log(title = "删除蓝牙网关", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public ResultData<Boolean> remove(@PathVariable Long[] ids) {
+        redisService.deleteObject(BaseConstants.BEACON_SUBMAC);
         return ResultData.result(dbBluetoothGatewayService.deleteeDbBluetoothGatewayByIds(ids));
     }
 
@@ -110,11 +114,23 @@ public class DbBluetoothGatewayController extends BaseController {
     @ApiOperation(value = "获取蓝牙网关信息")
     @GetMapping(value = "/getSubByMac")
     public ResultData<DbBluetoothGateway> getSubByMac(String mac) {
-        DbBluetoothGateway dbBluetoothGateway = new DbBluetoothGateway();
-        dbBluetoothGateway.setGatewayMac(mac);
-        List <DbBluetoothGateway> list = dbBluetoothGatewayService.selectDbBluetoothGatewayList(dbBluetoothGateway);
-        if(list.size()>0){
-            return ResultData.success(list.get(0));
+        List<DbBluetoothGateway> gatewayList = redisService.getCacheObject(BaseConstants.BEACON_SUBMAC);
+        if(gatewayList==null){
+            DbBluetoothGateway dbBluetoothGateway = new DbBluetoothGateway();
+            List <DbBluetoothGateway> list = dbBluetoothGatewayService.selectDbBluetoothGatewayList(dbBluetoothGateway);
+            redisService.setCacheObject(BaseConstants.BEACON_SUBMAC,list, 7L, TimeUnit.DAYS);
+
+            for(DbBluetoothGateway gateway:list){
+                if(mac.equals(gateway.getGatewayMac())){
+                    return ResultData.success(gateway);
+                }
+            }
+        }else{
+            for(DbBluetoothGateway gateway:gatewayList){
+                if(mac.equals(gateway.getGatewayMac())){
+                    return ResultData.success(gateway);
+                }
+            }
         }
         return ResultData.fail("查询不到网关");
     }

+ 58 - 49
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/event/RedisExpiredAndWorkListener.java

@@ -143,61 +143,70 @@ public class RedisExpiredAndWorkListener extends KeyExpirationEventMessageListen
             Date date = new Date();
             SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             map.put("riskPlanTriggerTime",dateFormat.format(date));
-            if(beaconStr[2]!=null){
-                LabBuildFloorLayout labBuildFloorLayout = new LabBuildFloorLayout();
-                labBuildFloorLayout.setSubId(Long.parseLong(beaconStr[2]));
-                List <LabBuildFloorLayoutVo> layoutVoList = labBuildFloorLayoutService.selectLabBuildFloorLayoutList(labBuildFloorLayout);
-                for(LabBuildFloorLayoutVo layoutVo:layoutVoList){
-                    map.put("buildId",layoutVo.getBuildId());
-                    map.put("floorId",layoutVo.getFloorId());
-                }
-            }
-            log.info("2------------------------------------》信标丢失:"+beaconStr[1]+",实验室id:"+beaconStr[2]);
-            // todo 这里需要根据实验室id查询对应的网关,通过网关查询redis,排查是不是有网关掉线了,如果有,就不通知报警,如果没有,发出报警
-            Boolean flag = Boolean.TRUE;
-            ResultData<List<Map<String,Object>>> resultData = remoteAirBottleService.getBluetoothBySubId(Long.parseLong(beaconStr[2]));
-            if(resultData.getCode()==200){
-                List<Map<String,Object>> resultList = resultData.getData();
-                for(Map mapGateway : resultList){
-                    if(redisService.getCacheObject(BaseConstants.BEACON_MAC+"~"+ mapGateway.get("gatewayMac"))==null){
-                        flag = Boolean.FALSE;
+
+            String key = BaseConstants.REDIS_LOCK + ""+beaconStr[2];
+            if (!redisService.isExistLock(key)) {
+                try{
+                    redisService.lock(key,5);
+
+                    if(beaconStr[2]!=null){
+                        LabBuildFloorLayout labBuildFloorLayout = new LabBuildFloorLayout();
+                        labBuildFloorLayout.setSubId(Long.parseLong(beaconStr[2]));
+                        List <LabBuildFloorLayoutVo> layoutVoList = labBuildFloorLayoutService.selectLabBuildFloorLayoutList(labBuildFloorLayout);
+                        for(LabBuildFloorLayoutVo layoutVo:layoutVoList){
+                            map.put("buildId",layoutVo.getBuildId());
+                            map.put("floorId",layoutVo.getFloorId());
+                        }
                     }
-                }
-            }
-            log.info("3------------------------------------》判别网关是否在线情况:"+flag);
-            if(flag){
-                List <Map<String,Object>> beaconNoticeList = redisService.getCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+beaconStr[2]);
-                if(beaconNoticeList != null && beaconNoticeList.size()>0){
-                    log.info("4------------------------------------》通知消息列表大小:"+beaconNoticeList.size());
-                    Boolean noticeFlag = Boolean.TRUE;
-                    for(Map notice:beaconNoticeList){
-                        if(map.get("beaconTag").equals(notice.get("beaconTag"))){
-                            noticeFlag = Boolean.FALSE;
+                    log.info("2------------------------------------》信标丢失:"+beaconStr[1]+",实验室id:"+beaconStr[2]);
+                    // todo 这里需要根据实验室id查询对应的网关,通过网关查询redis,排查是不是有网关掉线了,如果有,就不通知报警,如果没有,发出报警
+                    Boolean flag = Boolean.TRUE;
+                    ResultData<List<Map<String,Object>>> resultData = remoteAirBottleService.getBluetoothBySubId(Long.parseLong(beaconStr[2]));
+                    if(resultData.getCode()==200){
+                        List<Map<String,Object>> resultList = resultData.getData();
+                        for(Map mapGateway : resultList){
+                            if(redisService.getCacheObject(BaseConstants.BEACON_MAC+"~"+ mapGateway.get("gatewayMac"))==null){
+                                flag = Boolean.FALSE;
+                            }
                         }
                     }
-                    if(noticeFlag){
-                        beaconNoticeList.add(map);
-                        redisService.setCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+ beaconStr[2],beaconNoticeList, 30L, TimeUnit.MINUTES);
+                    log.info("3------------------------------------》判别网关是否在线情况:"+flag);
+                    if(flag){
+                        List <Map<String,Object>> beaconNoticeList = redisService.getCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+beaconStr[2]);
+                        if(beaconNoticeList != null && beaconNoticeList.size()>0){
+                            log.info("4------------------------------------》通知消息列表大小:"+beaconNoticeList.size());
+                            Boolean noticeFlag = Boolean.TRUE;
+                            for(Map notice:beaconNoticeList){
+                                if(map.get("beaconTag").equals(notice.get("beaconTag"))){
+                                    noticeFlag = Boolean.FALSE;
+                                }
+                            }
+                            if(noticeFlag){
+                                beaconNoticeList.add(map);
+                                redisService.setCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+ beaconStr[2],beaconNoticeList, 30L, TimeUnit.MINUTES);
+                            }
+                            //下发通知,告诉前端页面,查询redis获取相关信息
+                            messageSendService.bluetoothGatewayToMac(Long.parseLong(beaconStr[2]));
+                            //向前端发送mqtt预案触发提示
+                            messageSendService.riskPlanTriggerNotice();
+                        }else{
+                            log.info("5------------------------------------》新消息通知:"+map);
+                            List<Map <String,Object>> beaconList = new ArrayList <>();
+                            beaconList.add(map);
+                            redisService.setCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+ beaconStr[2],beaconList, 30L, TimeUnit.MINUTES);
+                            //下发通知,告诉前端页面,查询redis获取相关信息
+                            messageSendService.bluetoothGatewayToMac(Long.parseLong(beaconStr[2]));
+                            //向前端发送mqtt预案触发提示
+                            messageSendService.riskPlanTriggerNotice();
+                        }
+                        Map <String,String> tagMap = new HashMap<>();
+                        tagMap.put("beaconTag",beaconStr[1]);
+                        remoteAirBottleService.beaconAdd(tagMap);
                     }
-                    //下发通知,告诉前端页面,查询redis获取相关信息
-                    messageSendService.bluetoothGatewayToMac(Long.parseLong(beaconStr[2]));
-                    //向前端发送mqtt预案触发提示
-                    messageSendService.riskPlanTriggerNotice();
-                }else{
-                    log.info("5------------------------------------》新消息通知:"+map);
-                    List<Map <String,Object>> beaconList = new ArrayList <>();
-                    beaconList.add(map);
-                    redisService.setCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+ beaconStr[2],beaconList, 30L, TimeUnit.MINUTES);
-                    //下发通知,告诉前端页面,查询redis获取相关信息
-                    messageSendService.bluetoothGatewayToMac(Long.parseLong(beaconStr[2]));
-                    //向前端发送mqtt预案触发提示
-                    messageSendService.riskPlanTriggerNotice();
+                }finally {
+                    redisService.unLock(key);
                 }
-                Map <String,String> tagMap = new HashMap<>();
-                tagMap.put("beaconTag",beaconStr[1]);
-                remoteAirBottleService.beaconAdd(tagMap);
             }
-
         }
 
     }