Procházet zdrojové kódy

2023-10-19 蓝牙网关通知消息通知,以及入库出库更新redis。

chaiyunlong před 2 roky
rodič
revize
c441788e96

+ 47 - 1
zd-modules/zd-airbottle/src/main/java/com/zd/airbottle/controller/DbStockController.java

@@ -17,14 +17,15 @@ import com.zd.airbottle.service.DbUsageRecordService;
 import com.zd.airbottle.utils.PageUtil;
 import com.zd.common.core.annotation.Log;
 import com.zd.common.core.log.BusinessType;
+import com.zd.common.core.redis.RedisService;
 import com.zd.common.core.security.TokenService;
 import com.zd.common.core.utils.StringUtils;
 import com.zd.common.core.web.controller.AbstractController;
 import com.zd.laboratory.api.dto.CheckSubjectDto;
 import com.zd.laboratory.api.feign.RemoteLaboratoryService;
+import com.zd.model.constant.BaseConstants;
 import com.zd.model.constant.HttpStatus;
 import com.zd.model.domain.ResultData;
-import com.zd.model.entity.SysUser;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.BeanUtils;
@@ -33,6 +34,7 @@ import org.springframework.web.bind.annotation.*;
 
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 /**
@@ -60,6 +62,9 @@ public class DbStockController extends AbstractController {
     @Autowired
     private TokenService tokenService;
 
+    @Autowired
+    private static RedisService redisService;
+
     /**
      * 库存入库
      *
@@ -96,6 +101,8 @@ public class DbStockController extends AbstractController {
             dbInOutRecord.setType(1);
             dbInOutRecordService.save(dbInOutRecord);
             dbBeaconService.update(new LambdaUpdateWrapper<DbBeacon>().eq(DbBeacon::getBeaconTag, dbInOutRecord.getBeaconTag()).set(DbBeacon::getState, Boolean.TRUE).set(DbBeacon::getBindingTime, LocalDateTime.now()));
+            //todo 重新加载入库的信标到redis  chai
+            new Thread(()->loadBeaconDate(dbStock)).start();
             return ResultData.success("操作成功");
         }
         return ResultData.fail("操作失败");
@@ -119,6 +126,8 @@ public class DbStockController extends AbstractController {
             dbInOutRecord.setType(2);
             dbInOutRecordService.save(dbInOutRecord);
             dbBeaconService.update(new LambdaUpdateWrapper<DbBeacon>().eq(DbBeacon::getBeaconTag, dbInOutRecord.getBeaconTag()).set(DbBeacon::getState, 0));
+            //todo 重新加载入库的信标到redis  chai
+            new Thread(()->remBeaconDate(stock)).start();
             return ResultData.success("出库成功");
         }
         return ResultData.fail("出库失败");
@@ -143,6 +152,9 @@ public class DbStockController extends AbstractController {
             return ResultData.success("信标标签已存在!");
         }
         if (dbStockService.update(new LambdaUpdateWrapper<DbStock>().eq(DbStock::getId, dbStockBo.getId()).set(DbStock::getBeaconTag, dbStockBo.getBeaconTag()))) {
+            //todo 重新加载入库的信标到redis  chai
+            DbStock dbStock = dbStockService.getById(dbStockBo.getId());
+            new Thread(()->loadBeaconDate(dbStock)).start();
             return ResultData.success("操作成功");
         }
         return ResultData.fail("操作失败");
@@ -254,6 +266,40 @@ public class DbStockController extends AbstractController {
         return ResultData.success(list);
     }
 
+
+    /**
+     * 每次入库,修改,需要重新加载对应的实验室信标列表和对应的信标
+     *
+     * @param dbStock
+     * @return
+     */
+    public void loadBeaconDate(DbStock dbStock){
+        //查询库存信标列表,存入redis
+        LambdaQueryWrapper<DbStock> queryWrapper = new LambdaQueryWrapper();
+        queryWrapper.eq(DbStock::getSubjectId, dbStock.getSubjectId());
+        List<DbStock> list = dbStockService.list(queryWrapper);
+        redisService.setCacheObject(BaseConstants.BEACON_MATE_INFO+"~"+ dbStock.getSubjectId(),list, 7L, TimeUnit.DAYS);
+        //将提交的库存数据,存入redis
+        redisService.setCacheObject(BaseConstants.BEACON_MATE_DET+"~"+ dbStock.getBeaconTag()+"~"+dbStock.getSubjectId()+"~"+dbStock.getGasName(),dbStock.getBeaconTag(), 120L, TimeUnit.SECONDS);
+    }
+
+    /**
+     * 每次出库需要重新加载对应的实验室信标列表和移除对应信标
+     *
+     * @param dbStock
+     * @return
+     */
+    public void remBeaconDate(DbStock dbStock){
+        //查询库存信标列表,存入redis
+        LambdaQueryWrapper<DbStock> queryWrapper = new LambdaQueryWrapper();
+        queryWrapper.eq(DbStock::getSubjectId, dbStock.getSubjectId());
+        List<DbStock> list = dbStockService.list(queryWrapper);
+        redisService.setCacheObject(BaseConstants.BEACON_MATE_INFO+"~"+ dbStock.getSubjectId(),list, 7L, TimeUnit.DAYS);
+        //将信标从redis中移除
+        redisService.deleteObject(BaseConstants.BEACON_MATE_DET+"~"+ dbStock.getBeaconTag()+"~"+dbStock.getSubjectId()+"~"+dbStock.getGasName());
+    }
+
+
     /***
      *
      * @param

+ 6 - 2
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/event/RedisExpiredAndWorkListener.java

@@ -11,6 +11,7 @@ import com.zd.laboratory.domain.LabHardware;
 import com.zd.laboratory.domain.vo.LabDealyNotifyVo;
 import com.zd.laboratory.domain.vo.LabHardwareVO;
 import com.zd.laboratory.mapper.LabHardwareMapper;
+import com.zd.laboratory.mqtt.service.impl.SubMessageSendManager;
 import com.zd.laboratory.service.ILabControlService;
 import com.zd.laboratory.socket.command.Symbol;
 import com.zd.laboratory.socket.service.SocketService;
@@ -67,6 +68,9 @@ public class RedisExpiredAndWorkListener extends KeyExpirationEventMessageListen
     @Autowired
     private RemoteAirBottleService remoteAirBottleService;
 
+    @Autowired
+    private SubMessageSendManager messageSendService;
+
     private static final Logger log = LoggerFactory.getLogger(RedisExpiredAndWorkListener.class);
 
     public RedisExpiredAndWorkListener(RedisMessageListenerContainer listenerContainer)
@@ -157,14 +161,14 @@ public class RedisExpiredAndWorkListener extends KeyExpirationEventMessageListen
                         redisService.setCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+ beaconStr[2],beaconNoticeList, 30L, TimeUnit.MINUTES);
                     }
                     //下发通知,告诉前端页面,查询redis获取相关信息
-
+                    messageSendService.bluetoothGatewayToMac(Long.parseLong(beaconStr[2]));
                 }else{
                     log.info("34.=====================================》通知消息:");
                     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]));
                 }
             }
 

+ 3 - 3
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/mqtt/service/impl/SubMessageSendManager.java

@@ -281,12 +281,12 @@ public class SubMessageSendManager {
      * 蓝牙网关下发通知 稍后用*****
      *
      */
-    public void bluetoothGatewayToMac(Map <String,Object> map) {
-        if (map == null) {
+    public void bluetoothGatewayToMac(Long subId) {
+        if (subId == null) {
             return;
         }
         MessageBody messageBody = new MessageBody();
-        messageBody.setData(map);
+        messageBody.setData(subId);
         commonSend.send(MqttConstants.TOPIC_AIR_BLUETOOTH_GATEWAY , messageBody, SendMode.ONCE);
     }
 

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

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.zd.airbottle.api.feign.RemoteAirBottleService;
 import com.zd.common.core.redis.RedisService;
 import com.zd.common.core.utils.SpringUtils;
+import com.zd.laboratory.mqtt.service.impl.SubMessageSendManager;
 import com.zd.laboratory.socket.service.BeaconMate;
 import com.zd.model.constant.BaseConstants;
 import com.zd.model.domain.ResultData;
@@ -31,6 +32,8 @@ public class BeaconMateImpl implements BeaconMate {
 
     private RemoteAirBottleService remoteAirBottleService = SpringUtils.getBean(RemoteAirBottleService.class);
 
+    private SubMessageSendManager messageSendService = SpringUtils.getBean(SubMessageSendManager.class);
+
     public void sendBeaconMate(String dataStr){
         log.info("1.=====================================》上报蓝牙网关编码:"+dataStr);
         JSONObject jsonObject=JSONObject.parseObject(dataStr);
@@ -46,20 +49,21 @@ public class BeaconMateImpl implements BeaconMate {
             //todo redis存储蓝牙网关mac地址,用于在线离线问题。
             redisService.setCacheObject(BaseConstants.BEACON_MAC+"~"+ mac,mac, 60L, TimeUnit.SECONDS);
 
-            //todo 根据实验室id获取蓝牙信标列表
+
             ResultData<Map<String,Object>> resultData = remoteAirBottleService.getSubByMac(mac);
             if(resultData.getCode()==200){
                 log.info("2.=====================================》获取实验室id:"+resultData.getData().get("subId"));
                 Long subId = Long.parseLong(resultData.getData().get("subId")+"");
                 List <Map<String,Object>> beaconList = redisService.getCacheObject(BaseConstants.BEACON_MATE_INFO+"~"+subId);
-//                if(beaconList==null || initFlag){
+                if(beaconList==null || initFlag){
+                    //todo 根据实验室id获取蓝牙信标列表
                     ResultData<List <Map<String,Object>>> beaconData = remoteAirBottleService.findBySubId(subId);
                     log.info("2.1=====================================》如果信标列表为空,查询信标列表:");
                     if(beaconData.getCode()==200){
                         log.info("2.2=====================================》beaconList查询信标列表数据列表大小:"+beaconData.getData().size());
                         redisService.setCacheObject(BaseConstants.BEACON_MATE_INFO+"~"+ subId,beaconData.getData(), 7L, TimeUnit.DAYS);
                     }
-//                }
+                }
 
                 if(beaconList!=null){
                     log.info("3.=====================================》上报信标列表:"+jsonObject.getJSONArray("devices"));
@@ -68,7 +72,9 @@ public class BeaconMateImpl implements BeaconMate {
                     //todo 这里需要查看信标通知列表,移除通知列表对应的信标气瓶数据
                     List <Map<String,Object>> beaconNoticeList = redisService.getCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+subId);
                     List <Map<String,Object>> newNoticeList = new ArrayList <>();
-                    newNoticeList.addAll(beaconNoticeList);
+                    if(beaconNoticeList!=null){
+                        newNoticeList.addAll(beaconNoticeList);
+                    }
                     //todo 循环检查实验室的信标比对上报的信标,将配对好的信标重新设置默认时间
                     for(int x=0;x<beaconList.size();x++){
                         if(array.size()>0){
@@ -102,7 +108,9 @@ public class BeaconMateImpl implements BeaconMate {
                     //判断通知消息集合有数据,说明信标有变动,需要重新变更加通知
                     redisService.setCacheObject(BaseConstants.BEACON_MATE_NOTICE+"~"+ subId,newNoticeList, 30L, TimeUnit.MINUTES);
                     //下发通知,告诉前端页面,查询redis获取相关信息
-
+                    if(beaconNoticeList!=null && beaconNoticeList.size()!=newNoticeList.size()){
+                        messageSendService.bluetoothGatewayToMac(subId);
+                    }
                 }
             }
         }