Procházet zdrojové kódy

2023-10-26 优化蓝牙网关查询通过redis中间层实现。

chaiyunlong před 2 roky
rodič
revize
aa4f2cb40b

+ 11 - 1
zd-modules/zd-airbottle/src/main/java/com/zd/airbottle/ZdAirbottleApplication.java

@@ -1,7 +1,9 @@
 package com.zd.airbottle;
 
 import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
+import com.zd.airbottle.domain.DbBluetoothGateway;
 import com.zd.airbottle.domain.DbStock;
+import com.zd.airbottle.service.DbBluetoothGatewayService;
 import com.zd.airbottle.service.DbStockService;
 import com.zd.common.core.annotation.EnableCustomConfig;
 import com.zd.common.core.annotation.EnableZdFeignClients;
@@ -38,6 +40,9 @@ public class ZdAirbottleApplication implements ApplicationRunner {
     private DbStockService dbStockService;
 
     @Autowired
+    private DbBluetoothGatewayService dbBluetoothGatewayService;
+
+    @Autowired
     private RedisService redisService;
 
     public static void main(String[] args) {
@@ -53,7 +58,12 @@ public class ZdAirbottleApplication implements ApplicationRunner {
         //TODO 启动气瓶服务的时候,需要把信标全部载入redis一次,(主要防止服务器断电重启后,被带离的气瓶无法知道)
         List <DbStock> list = dbStockService.list();
         for(DbStock db:list){
-            redisService.setCacheObject(BaseConstants.BEACON_MATE_DET+"~"+ db.getBeaconTag()+"~"+db.getSubjectId()+"~"+db.getGasName(),db.getBeaconTag(), 120L, TimeUnit.SECONDS);
+            DbBluetoothGateway dbBluetoothGateway = new DbBluetoothGateway();
+            dbBluetoothGateway.setSubId(db.getSubjectId());
+            List <DbBluetoothGateway> bluetoothGatewayList = dbBluetoothGatewayService.selectDbBluetoothGatewayList(dbBluetoothGateway);
+            if(bluetoothGatewayList.size()>0){
+                redisService.setCacheObject(BaseConstants.BEACON_MATE_DET+"~"+ db.getBeaconTag()+"~"+db.getSubjectId()+"~"+db.getGasName(),db.getBeaconTag(), 120L, TimeUnit.SECONDS);
+            }
         }
     }
 }

+ 36 - 3
zd-modules/zd-airbottle/src/main/java/com/zd/airbottle/controller/DbBluetoothGatewayController.java

@@ -1,7 +1,10 @@
 package com.zd.airbottle.controller;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.zd.airbottle.domain.DbBluetoothGateway;
+import com.zd.airbottle.domain.DbStock;
 import com.zd.airbottle.service.DbBluetoothGatewayService;
+import com.zd.airbottle.service.DbStockService;
 import com.zd.common.core.annotation.Log;
 import com.zd.common.core.annotation.PreAuthorize;
 import com.zd.common.core.log.BusinessType;
@@ -37,6 +40,9 @@ public class DbBluetoothGatewayController extends BaseController {
     @Autowired
     private RedisService redisService;
 
+    @Autowired
+    private DbStockService dbStockService;
+
     /**
      * 查询蓝牙网关列表
      */
@@ -69,8 +75,15 @@ public class DbBluetoothGatewayController extends BaseController {
     @Log(title = "蓝牙网关", businessType = BusinessType.INSERT)
     @PostMapping
     public ResultData<Boolean> add(@RequestBody DbBluetoothGateway dbBluetoothGateway) {
+        int flag = dbBluetoothGatewayService.insertDbBluetoothGateway(dbBluetoothGateway);
+        LambdaQueryWrapper<DbStock> queryWrapper = new LambdaQueryWrapper();
+        queryWrapper.eq(DbStock::getSubjectId, dbBluetoothGateway.getSubId());
+        List<DbStock> list = dbStockService.list(queryWrapper);
+        for(DbStock stock : list){
+            redisService.setCacheObject(BaseConstants.BEACON_MATE_DET + "~" + stock.getBeaconTag() + "~" + stock.getSubjectId() + "~" + stock.getGasName(), stock.getBeaconTag(), 120L, TimeUnit.SECONDS);
+        }
         redisService.deleteObject(BaseConstants.BEACON_SUBMAC);
-        return ResultData.result(dbBluetoothGatewayService.insertDbBluetoothGateway(dbBluetoothGateway));
+        return ResultData.result(flag);
     }
 
     /**
@@ -81,8 +94,17 @@ public class DbBluetoothGatewayController extends BaseController {
     @Log(title = "蓝牙网关", businessType = BusinessType.UPDATE)
     @PutMapping
     public ResultData<Boolean> edit(@RequestBody DbBluetoothGateway dbBluetoothGateway) {
+        dbBluetoothGateway.setGatewayOnline(0);
+        int flag = dbBluetoothGatewayService.updateDbBluetoothGateway(dbBluetoothGateway);
+        LambdaQueryWrapper<DbStock> queryWrapper = new LambdaQueryWrapper();
+        queryWrapper.eq(DbStock::getSubjectId, dbBluetoothGateway.getSubId());
+        List<DbStock> list = dbStockService.list(queryWrapper);
+        for(DbStock stock : list){
+            redisService.setCacheObject(BaseConstants.BEACON_MATE_DET + "~" + stock.getBeaconTag() + "~" + stock.getSubjectId() + "~" + stock.getGasName(), stock.getBeaconTag(), 120L, TimeUnit.SECONDS);
+        }
+        redisService.deleteObject(BaseConstants.BEACON_MAC+"~"+ dbBluetoothGateway.getGatewayMac());
         redisService.deleteObject(BaseConstants.BEACON_SUBMAC);
-        return ResultData.result(dbBluetoothGatewayService.updateDbBluetoothGateway(dbBluetoothGateway));
+        return ResultData.result(flag);
     }
 
     /**
@@ -93,8 +115,19 @@ public class DbBluetoothGatewayController extends BaseController {
     @Log(title = "删除蓝牙网关", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public ResultData<Boolean> remove(@PathVariable Long[] ids) {
+        DbBluetoothGateway bluetoothGateway = dbBluetoothGatewayService.selectDbBluetoothGatewayById(ids[0]);
+        int flag = dbBluetoothGatewayService.deleteeDbBluetoothGatewayByIds(ids);
+        if(bluetoothGateway!=null){
+            LambdaQueryWrapper<DbStock> queryWrapper = new LambdaQueryWrapper();
+            queryWrapper.eq(DbStock::getSubjectId, bluetoothGateway.getSubId());
+            List<DbStock> list = dbStockService.list(queryWrapper);
+            for(DbStock stock : list){
+                redisService.setCacheObject(BaseConstants.BEACON_MATE_DET + "~" + stock.getBeaconTag() + "~" + stock.getSubjectId() + "~" + stock.getGasName(), stock.getBeaconTag(), 120L, TimeUnit.SECONDS);
+            }
+            redisService.deleteObject(BaseConstants.BEACON_MAC+"~"+ bluetoothGateway.getGatewayMac());
+        }
         redisService.deleteObject(BaseConstants.BEACON_SUBMAC);
-        return ResultData.result(dbBluetoothGatewayService.deleteeDbBluetoothGatewayByIds(ids));
+        return ResultData.result(flag);
     }
 
 

+ 7 - 0
zd-modules/zd-airbottle/src/main/java/com/zd/airbottle/service/impl/DbBluetoothGatewayServiceImpl.java

@@ -4,6 +4,8 @@ import com.zd.airbottle.domain.DbBluetoothGateway;
 import com.zd.airbottle.mapper.DbBluetoothGatewayMapper;
 import com.zd.airbottle.service.DbBluetoothGatewayService;
 import com.zd.common.core.exception.ServiceException;
+import com.zd.common.core.utils.DateUtils;
+import com.zd.common.core.utils.SecurityUtils;
 import com.zd.model.domain.AjaxResult;
 import com.zd.system.api.feign.RemoteDeptService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -44,6 +46,9 @@ public class DbBluetoothGatewayServiceImpl implements DbBluetoothGatewayService
         dbBluetoothGateway.setDeptName(map.get("deptName"));
         int flag=0;
         try{
+            dbBluetoothGateway.setUserId(SecurityUtils.getUserId());
+            dbBluetoothGateway.setCreateBy(SecurityUtils.getUsername());
+            dbBluetoothGateway.setCreateTime(DateUtils.getNowDate());
             flag = dbBluetoothGatewayMapper.insertDbBluetoothGateway(dbBluetoothGateway);
         }catch (Exception e){
             throw new ServiceException("网关不能重复");
@@ -58,6 +63,8 @@ public class DbBluetoothGatewayServiceImpl implements DbBluetoothGatewayService
         dbBluetoothGateway.setDeptName(map.get("deptName"));
         int flag=0;
         try{
+            dbBluetoothGateway.setUpdateBy(SecurityUtils.getUsername());
+            dbBluetoothGateway.setUpdateTime(DateUtils.getNowDate());
             flag = dbBluetoothGatewayMapper.updateDbBluetoothGateway(dbBluetoothGateway);
         }catch (Exception e){
             throw new ServiceException("网关不能重复");

+ 3 - 0
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/event/RedisExpiredAndWorkListener.java

@@ -175,6 +175,9 @@ public class RedisExpiredAndWorkListener extends KeyExpirationEventMessageListen
                             flag = Boolean.FALSE;
                         }
                     }
+                    if(resultList.size()==0){
+                        flag = Boolean.FALSE;
+                    }
                 }
                 log.info("3------------------------------------》判别网关是否在线情况:"+flag);
                 if(flag){

+ 1 - 1
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/socket/service/impl/BeaconMateImpl.java

@@ -52,7 +52,7 @@ public class BeaconMateImpl implements BeaconMate {
             }
 
             ResultData<Map<String,Object>> resultData = remoteAirBottleService.getSubByMac(mac);
-            if(resultData.getCode()==200){
+            if(resultData.getCode()==200 && resultData.getData() != null){
                 //todo redis存储蓝牙网关mac地址,用于在线离线问题。
                 redisService.setCacheObject(BaseConstants.BEACON_MAC+"~"+ mac,mac, 60L, TimeUnit.SECONDS);
                 log.info("2=====================================》获取实验室id:"+resultData.getData().get("subId"));