Просмотр исходного кода

update 改变设备信息缓存方式

18339543638 3 лет назад
Родитель
Сommit
711a8ac38c

+ 2 - 2
coffee-common/src/main/java/com/coffee/common/cache/ClusterConfigStorage.java

@@ -16,7 +16,7 @@ public class ClusterConfigStorage implements ConfigStorage {
         cache=cacheManager.getCache(name);
     }
 
-    public Cache getCache(CacheManager cacheManager,String name) {
+    public Cache getCache() {
         return cache;
     }
 
@@ -49,7 +49,7 @@ public class ClusterConfigStorage implements ConfigStorage {
     @Override
     public Value getAndRemove(String key) {
         Value value = Value.simple(cache.get(key));
-        cache.evictIfPresent(key);
+        cache.evict(key);
         return value;
     }
 

+ 1 - 0
coffee-common/src/main/java/com/coffee/common/cache/manager/ClusterConfigStorageManager.java

@@ -26,6 +26,7 @@ public class ClusterConfigStorageManager implements ConfigStorageManager {
     }
 
     private Map<String,ClusterConfigStorage > storageMap=new ConcurrentHashMap<>();
+
     @Override
     public ConfigStorage getStorage(String id) {
         return  storageMap.computeIfAbsent(id, __ -> new ClusterConfigStorage(cacheManager,id));

+ 3 - 0
coffee-system/src/main/java/com/coffee/bus/entity/BusPumpEntity.java

@@ -126,6 +126,9 @@ public class BusPumpEntity extends TenantGenericEntity<String,String> {
     @ApiModelProperty(value = "是否为主泵数据, 0、副泵 1、主泵(即当前临床绑定的泵)")
     private Integer master;
 
+    @ApiModelProperty(value = "分包标记位")
+    private Integer mark;
+
     @ApiModelProperty(value = "泵类型")
     private String type;
 }

+ 194 - 0
coffee-system/src/main/java/com/coffee/bus/registry/device/ClusterDeviceOperator.java

@@ -0,0 +1,194 @@
+package com.coffee.bus.registry.device;
+
+import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.util.StrUtil;
+import com.coffee.bus.enums.NetPumpStatusEnum;
+import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
+import com.coffee.bus.registry.device.bean.DeviceOperator;
+import com.coffee.common.cache.ConfigStorage;
+import com.coffee.common.cache.value.Value;
+import lombok.AllArgsConstructor;
+import org.python.antlr.ast.Str;
+
+import java.util.Date;
+import java.util.*;
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName ClusterDeviceOperator.java
+ * @Description TODO
+ * @createTime 2022年04月06日 15:27:00
+ */
+@AllArgsConstructor
+public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
+
+    private final ConfigStorage configStorage;
+
+
+    @Override
+    public DeviceCacheInfo get() {
+        Map<String, Value> result = configStorage.getKeys(getAllKeys());
+        return DeviceCacheInfo.builder()
+                .deviceId(result.get("deviceId").asString())
+                .alias(result.get("alias").asString())
+                .enable(result.get("enable").asBoolean())
+                .tenantId(result.get("tenantId").asString())
+                .usingId(result.get("usingId").asString())
+                .startTime(result.get("startTime").asDate())
+                .patientCode(result.get("patientCode").asString())
+                .status(result.get("status").as(NetPumpStatusEnum.class))
+                .master(result.get("master").asBoolean())
+                .mark(result.get("mark").asInt())
+                .build();
+    }
+
+    @Override
+    public void set(DeviceCacheInfo all) {
+        Map<String, Object> map = new HashMap<>();
+        if(StrUtil.isNotEmpty(all.getDeviceId())){
+            map.put("deviceId",all.getDeviceId());
+        }
+        if(StrUtil.isNotEmpty(all.getAlias())){
+            map.put("alias",all.getAlias());
+        }
+        if(all.getEnable()!=null){
+            map.put("enable",all.getEnable());
+        }
+        if(StrUtil.isNotEmpty(all.getTenantId())){
+            map.put("tenantId",all.getTenantId());
+        }
+        if(StrUtil.isNotEmpty(all.getUsingId())){
+            map.put("usingId",all.getUsingId());
+        }
+        if(all.getStartTime()!=null){
+            map.put("startTime",all.getStartTime());
+        }
+        if(StrUtil.isNotEmpty(all.getPatientCode())){
+            map.put("patientCode",all.getPatientCode());
+        }
+        if(all.getStatus()!=null){
+            map.put("status",all.getStatus());
+        }
+        if(all.getMaster()!=null){
+            map.put("master",all.getMaster());
+        }
+        if(all.getMark()!=null){
+            map.put("mark",all.getMark());
+        }
+        configStorage.setConfigs(map);
+    }
+
+    @Override
+    public   Map<String, Value> getAll() {
+        return configStorage.getKeys(getAllKeys());
+    }
+
+    @Override
+    public void setDeviceId(String deviceId) {
+        configStorage.setConfig("deviceId",deviceId);
+    }
+
+    @Override
+    public String getDeviceId() {
+        return configStorage.getConfig("deviceId").asString();
+    }
+
+    @Override
+    public void setAlias(String alias) {
+        configStorage.setConfig("alias",alias);
+    }
+
+    @Override
+    public String getAlias() {
+        return configStorage.getConfig("alias").asString();
+    }
+
+    @Override
+    public void setEnable(Boolean enable) {
+        configStorage.setConfig("enable",enable);
+    }
+
+    @Override
+    public boolean getEnable() {
+        return configStorage.getConfig("enable").asBoolean();
+    }
+
+    @Override
+    public void setTenantId(String tenantId) {
+        configStorage.setConfig("tenantId",tenantId);
+    }
+
+    @Override
+    public String getTenantId() {
+        return configStorage.getConfig("tenantId").asString();
+    }
+
+    @Override
+    public void setUsingId(String usingId) {
+        configStorage.setConfig("usingId",usingId);
+    }
+
+    @Override
+    public String getUsingId() {
+        return configStorage.getConfig("usingId").asString();
+    }
+
+    @Override
+    public void setStartTime(Date startTime) {
+        configStorage.setConfig("startTime",startTime);
+    }
+
+    @Override
+    public Date getStartTime() {
+        return configStorage.getConfig("startTime").asDate();
+    }
+
+    @Override
+    public void setPatientCode(String patientCode) {
+        configStorage.setConfig("patientCode",patientCode);
+    }
+
+    @Override
+    public String getPatientCode() {
+        return configStorage.getConfig("patientCode").asString();
+    }
+
+    @Override
+    public NetPumpStatusEnum getStatus() {
+        return configStorage.getConfig("status").as(NetPumpStatusEnum.class);
+    }
+
+    @Override
+    public void setStatus(NetPumpStatusEnum status) {
+        configStorage.setConfig("status",status.ordinal());
+    }
+
+    @Override
+    public void setMaster(boolean master) {
+        configStorage.setConfig("master",master);
+    }
+
+    @Override
+    public boolean getMaster() {
+        return configStorage.getConfig("master").asBoolean();
+    }
+
+    @Override
+    public void setMark(String mark) {
+        configStorage.setConfig("mark",mark);
+    }
+
+    @Override
+    public Integer getMark() {
+        return configStorage.getConfig("mark").asInt();
+    }
+
+    @Override
+    public void clear() {
+        configStorage.clear();
+    }
+
+    private List<String> getAllKeys(){
+        return Arrays.asList("deviceId","alias","enable","tenantId","usingId","startTime","patientCode","status","master","mark");
+    }
+}

+ 20 - 21
coffee-system/src/main/java/com/coffee/bus/registry/device/ClusterDeviceRegistry.java

@@ -1,16 +1,16 @@
 package com.coffee.bus.registry.device;
 
 import cn.hutool.core.util.StrUtil;
-import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.coffee.bus.entity.BusDeviceEntity;
 import com.coffee.bus.entity.BusPumpEntity;
 import com.coffee.bus.registry.RegistryConstant;
 import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
+import com.coffee.bus.registry.device.bean.DeviceOperator;
 import com.coffee.bus.service.LocalBusDeviceService;
 import com.coffee.bus.service.LocalBusPumpService;
-import com.coffee.common.cache.ClusterConfigStorage;
 import com.coffee.common.cache.manager.ClusterConfigStorageManager;
+import com.coffee.common.cache.value.Value;
 import com.coffee.common.redis.RedisUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
@@ -27,52 +27,51 @@ import java.util.Map;
 @AllArgsConstructor
 @Service
 public class ClusterDeviceRegistry implements DeviceRegistry {
-    private final RedisUtils redisUtils;
     private final LocalBusPumpService pumpService;
     private final LocalBusDeviceService deviceRegisteredService;
     private final ClusterConfigStorageManager configStorageManager;
 
     @Override
-    public DeviceOperator getDevice(String deviceId) {
+    public DeviceOperator<DeviceCacheInfo> getDevice(String deviceId) {
         if(StrUtil.isEmpty(deviceId)){
-            return DeviceOperator.of(new DeviceCacheInfo(),redisUtils);
+            return null;
         }
         String key=getId()+deviceId;
-
-        Map<Object, Object> result = redisUtils.hmget(key);
+        ClusterDeviceOperator deviceOperator = new ClusterDeviceOperator( configStorageManager.getStorage(key));
+        Map<String, Value> result = deviceOperator.getAll();
         if(result==null||result.size()==0){
             //从数据库中获取数据
             BusPumpEntity pump = pumpService.getOne(new QueryWrapper<BusPumpEntity>().lambda().eq(BusPumpEntity::getDeviceId, deviceId));
             BusDeviceEntity device = deviceRegisteredService.getOne(new QueryWrapper<BusDeviceEntity>().lambda()
                     .eq(BusDeviceEntity::getDeviceId, deviceId));
             if(device==null){
-                return DeviceOperator.of(new DeviceCacheInfo(),redisUtils);
+                //设备不存在,即缓存操作不存在
+                return null;
             }
             if(pump==null){
-                pump=new BusPumpEntity();
-                pump.setAlias(device.getAlias());
-                pump.setTenantId(device.getTenantId());
-                pump.setDeviceId(device.getDeviceId());
+                deviceOperator.setAlias(device.getAlias());
+                deviceOperator.setTenantId(device.getTenantId());
+                deviceOperator.setDeviceId(device.getDeviceId());
             }
-            DeviceOperator operator = DeviceOperator.of(DeviceCacheInfo.of(pump, device), redisUtils);
             //数据同步到缓存中
-            operator.sync();
-            return operator;
+            return deviceOperator;
         }
-        return DeviceOperator.of(JSONUtil.toBean(JSONUtil.toJsonStr(result), DeviceCacheInfo.class),redisUtils);
+        return deviceOperator;
     }
 
     @Override
-    public void register(DeviceCacheInfo basicInfo) {
-        if(StrUtil.isNotEmpty(basicInfo.getDeviceId())){
-            DeviceOperator.of(basicInfo, redisUtils).sync();
+    public void register(DeviceCacheInfo deviceInfo) {
+        if(StrUtil.isNotEmpty(deviceInfo.getDeviceId())){
+            ClusterDeviceOperator deviceOperator = new ClusterDeviceOperator( configStorageManager.getStorage(deviceInfo.getKey()));
+            deviceOperator.set(deviceInfo);
         }
     }
 
     @Override
     public void unRegister(String deviceId) {
-        DeviceCacheInfo deviceCacheInfo = DeviceCacheInfo.of(deviceId);
-        DeviceOperator.of(deviceCacheInfo, redisUtils).clear();
+        String key=getId()+deviceId;
+        ClusterDeviceOperator deviceOperator = new ClusterDeviceOperator(configStorageManager.getStorage(key));
+        deviceOperator.clear();
     }
 
 

+ 0 - 164
coffee-system/src/main/java/com/coffee/bus/registry/device/DeviceOperator.java

@@ -1,164 +0,0 @@
-package com.coffee.bus.registry.device;
-
-import cn.hutool.core.util.StrUtil;
-import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
-import com.coffee.common.redis.RedisUtils;
-import lombok.AllArgsConstructor;
-
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author lifang
- * @version 1.0.0
- * @ClassName DeviceOperator.java
- * @Description 设备注册操作
- * @createTime 2022年04月01日 17:14:00
- */
-@AllArgsConstructor
-public class DeviceOperator {
-    private transient final DeviceCacheInfo deviceCacheInfo;
-
-    private transient final RedisUtils redisUtils;
-
-    /**
-     * 该操作是否有效,即判断该操作是否存在设备id
-     */
-    private final boolean validate;
-
-    public static DeviceOperator of(DeviceCacheInfo deviceCacheInfo, RedisUtils redisUtils){
-        return new DeviceOperator(deviceCacheInfo,redisUtils,!StrUtil.isNullOrUndefined(deviceCacheInfo.getDeviceId()));
-    }
-    /**
-     * 清除缓存
-     */
-    public DeviceOperator clear(){
-        if(!validate)return this;
-        redisUtils.del(deviceCacheInfo.getKey());
-        return this;
-    }
-    /**
-     * 将自身同步至redis
-     * @param
-     */
-    public DeviceOperator sync(){
-        if(!validate)return this;
-        redisUtils.hmset(deviceCacheInfo.getKey(),getMap());
-        return this;
-    }
-
-    public DeviceOperator updateAlias(String alias){
-        if(!validate)return this;
-        deviceCacheInfo.setAlias(alias);
-        redisUtils.hset(deviceCacheInfo.getKey(),"alias",alias);
-        return this;
-    }
-
-    public DeviceOperator updateEnable(Integer enable){
-        if(!validate)return this;
-        deviceCacheInfo.setEnable(enable);
-        redisUtils.hset(deviceCacheInfo.getKey(),"enable",enable);
-        return this;
-    }
-
-    public DeviceOperator updateTenantId(String tenantId){
-        if(!validate)return this;
-        deviceCacheInfo.setTenantId(tenantId);
-        redisUtils.hset(deviceCacheInfo.getKey(),"tenantId",tenantId);
-        return this;
-    }
-
-
-    public DeviceOperator updateRunId(String runId){
-        if(!validate)return this;
-        deviceCacheInfo.setId(runId);
-        redisUtils.hset(deviceCacheInfo.getKey(),"id",runId);
-        return this;
-    }
-
-    public DeviceOperator updateStartTime(Date startTime){
-        if(!validate)return this;
-        deviceCacheInfo.setStartTime(startTime);
-        redisUtils.hset(deviceCacheInfo.getKey(),"startTime",startTime);
-        return this;
-    }
-
-
-    public String getDeviceId(){
-        return deviceCacheInfo.getDeviceId();
-    }
-
-    public String getAlias(){
-        return deviceCacheInfo.getAlias();
-    }
-
-    public String getTenantId(){
-        return deviceCacheInfo.getTenantId();
-    }
-
-    public String getPatientCode(){
-        return deviceCacheInfo.getPatientCode();
-    }
-
-    /**
-     * 获取正在运行设备绑定id
-     * @return
-     */
-    public String getRunId(){
-        return deviceCacheInfo.getId();
-    }
-
-    public Date getStartTime(){
-        return deviceCacheInfo.getStartTime();
-    }
-
-    /**
-     * 判断设备在系统中是否存在
-     * @return
-     */
-    public boolean isExist(){
-        return deviceCacheInfo !=null&& !StrUtil.isEmpty(deviceCacheInfo.getDeviceId()) ;
-    }
-
-
-    /**
-     * 判断设备在医院中是否可以使用
-     * @return
-     */
-    public boolean canUse(){
-        return isExist()&& deviceCacheInfo.getEnable()!=null&& deviceCacheInfo.getEnable()==1 ;
-    }
-
-    /**
-     * 该设备
-     * @return
-     */
-    public boolean isFirst(){
-        return StrUtil.isEmpty(deviceCacheInfo.getId());
-    }
-
-    public Map<String,Object> getMap(){
-        Map<String, Object> result = new HashMap<>();
-        if(null!= deviceCacheInfo.getId()){
-            result.put("id", deviceCacheInfo.getId());
-        }
-        if(null!= deviceCacheInfo.getDeviceId()){
-            result.put("deviceId", deviceCacheInfo.getDeviceId());
-        }
-        if(StrUtil.isNotEmpty(deviceCacheInfo.getAlias())){
-            result.put("alias", deviceCacheInfo.getAlias());
-        }
-        if(null!= deviceCacheInfo.getEnable()){
-            result.put("enable", deviceCacheInfo.getEnable());
-        }
-        if(StrUtil.isNotEmpty(deviceCacheInfo.getTenantId())){
-            result.put("tenantId", deviceCacheInfo.getTenantId());
-        }
-        if(null!= deviceCacheInfo.getStartTime()){
-            result.put("startTime", deviceCacheInfo.getStartTime());
-        }
-
-        return result;
-    }
-}

+ 2 - 1
coffee-system/src/main/java/com/coffee/bus/registry/device/DeviceRegistry.java

@@ -2,6 +2,7 @@ package com.coffee.bus.registry.device;
 
 import com.coffee.bus.registry.Registry;
 import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
+import com.coffee.bus.registry.device.bean.DeviceOperator;
 
 /**
  * @author lifang
@@ -16,7 +17,7 @@ public interface DeviceRegistry extends Registry {
      * @param deviceId
      * @return
      */
-    DeviceOperator getDevice(String deviceId);
+    DeviceOperator<DeviceCacheInfo> getDevice(String deviceId);
 
     /**
      * 注册设备信息

+ 0 - 172
coffee-system/src/main/java/com/coffee/bus/registry/device/RedisDeviceOperator.java

@@ -1,172 +0,0 @@
-package com.coffee.bus.registry.device;
-
-import cn.hutool.core.map.MapUtil;
-import cn.hutool.core.util.StrUtil;
-import cn.hutool.json.JSONUtil;
-import com.coffee.bus.enums.NetPumpStatusEnum;
-import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
-import com.coffee.bus.registry.device.bean.DeviceOperator;
-import com.coffee.common.redis.RedisUtils;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author lifang
- * @version 1.0.0
- * @ClassName RedisDeviceOperator.java
- * @Description TODO
- * @createTime 2022年04月06日 15:27:00
- */
-public class RedisDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
-    private transient RedisUtils redisUtils;
-    private DeviceCacheInfo cacheInfo;
-    @Override
-    public void clear() {
-        if(cacheInfo!=null){
-            redisUtils.del(cacheInfo.getKey());
-        }
-
-    }
-
-    @Override
-    public void sync() {
-        if(cacheInfo!=null){
-            redisUtils.hmset(cacheInfo.getKey(),getMap());
-        }
-
-    }
-
-    @Override
-    public void setAll(DeviceCacheInfo all) {
-
-        this.cacheInfo=all;
-    }
-
-    @Override
-    public DeviceCacheInfo getAll() {
-        return cacheInfo;
-    }
-
-    @Override
-    public void setDeviceId(String deviceId) {
-
-    }
-
-    @Override
-    public String getDeviceId() {
-        return null;
-    }
-
-    @Override
-    public void setAlias(String alias) {
-
-    }
-
-    @Override
-    public String getAlias() {
-        return null;
-    }
-
-    @Override
-    public void setEnable(String enable) {
-
-    }
-
-    @Override
-    public boolean getEnable() {
-        return false;
-    }
-
-    @Override
-    public void setTenantId(String tenantId) {
-
-    }
-
-    @Override
-    public String getTenantId() {
-        return null;
-    }
-
-    @Override
-    public void setUsingId(String usingId) {
-
-    }
-
-    @Override
-    public String getUsingId() {
-        return null;
-    }
-
-    @Override
-    public void setStartTime(Date startTime) {
-
-    }
-
-    @Override
-    public Date getStartTime() {
-        return null;
-    }
-
-    @Override
-    public void setPatientCode(String patientCode) {
-
-    }
-
-    @Override
-    public String getPatientCode() {
-        return null;
-    }
-
-    @Override
-    public NetPumpStatusEnum getStatus() {
-        return null;
-    }
-
-    @Override
-    public void setStatus(NetPumpStatusEnum status) {
-
-    }
-
-    @Override
-    public void setMaster(boolean master) {
-
-    }
-
-    @Override
-    public boolean getMaster() {
-        return false;
-    }
-
-    private Map<String,Object> getMap(){
-        Map<String, Object> result = new HashMap<>();
-        if(null!= cacheInfo.getDeviceId()){
-            result.put("deviceId", cacheInfo.getDeviceId());
-        }
-        if(StrUtil.isNotEmpty(cacheInfo.getAlias())){
-            result.put("alias", cacheInfo.getAlias());
-        }
-        if(null!= cacheInfo.getEnable()){
-            result.put("enable", cacheInfo.getEnable());
-        }
-        if(StrUtil.isNotEmpty(cacheInfo.getTenantId())){
-            result.put("tenantId", cacheInfo.getTenantId());
-        }
-        if(StrUtil.isNotEmpty(cacheInfo.getPatientCode())){
-            result.put("patientCode", cacheInfo.getPatientCode());
-        }
-        if(null!= cacheInfo.getId()){
-            result.put("usingId", cacheInfo.getId());
-        }
-        if(null!= cacheInfo.getStartTime()){
-            result.put("startTime", cacheInfo.getStartTime());
-        }
-        if(null!= cacheInfo.getStatus()){
-            result.put("status", cacheInfo.getStatus());
-        }
-        if(null!= cacheInfo.getMaster()){
-            result.put("master", cacheInfo.getMaster());
-        }
-        return result;
-    }
-}

+ 10 - 36
coffee-system/src/main/java/com/coffee/bus/registry/device/bean/DeviceCacheInfo.java

@@ -5,6 +5,7 @@ import com.coffee.bus.entity.BusPumpEntity;
 import com.coffee.bus.enums.NetPumpStatusEnum;
 import com.coffee.bus.registry.CacheInfo;
 import com.coffee.bus.registry.RegistryConstant;
+import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 //import com.coffee.common.cache.*;
@@ -18,7 +19,7 @@ import java.util.Date;
  * @createTime 2022年04月01日 17:06:00
  */
 @Data
-@NoArgsConstructor
+@Builder
 public class DeviceCacheInfo implements CacheInfo {
     @Override
     public String getKey() {
@@ -37,7 +38,7 @@ public class DeviceCacheInfo implements CacheInfo {
     /**
      * 是否启用
      */
-    private Integer enable;
+    private Boolean enable;
 
     /**
      * 设备所属医院
@@ -51,7 +52,7 @@ public class DeviceCacheInfo implements CacheInfo {
      * 运行泵主键id
      * @see com.coffee.bus.entity.BusPumpEntity
      */
-    private String id;
+    private String usingId;
 
     private Date startTime;
 
@@ -63,43 +64,16 @@ public class DeviceCacheInfo implements CacheInfo {
 
     private String patientCode;
 
+    /**
+     * 泵当前标记位(分包标识)
+     */
+    private Integer mark;
     /**
      * 当前泵是否为主泵
      */
-    private Integer master;
+    private Boolean master;
     /**********泵正在运行状态**********/
 
-
-
-
-    public DeviceCacheInfo(String deviceId, String alias, Integer enable, String tenantId) {
-        this.deviceId = deviceId;
-        this.alias = alias;
-        this.enable = enable;
-        this.tenantId = tenantId;
-    }
-
-    public DeviceCacheInfo( String deviceId, String alias, Integer enable, String tenantId, String id, Date startTime) {
-        this.deviceId = deviceId;
-        this.alias = alias;
-        this.enable = enable;
-        this.tenantId = tenantId;
-        this.id = id;
-        this.startTime = startTime;
-    }
-
-    public static DeviceCacheInfo of(String deviceId, String alias, Integer enable, String tenantId) {
-        return new DeviceCacheInfo(deviceId,alias,enable,tenantId);
-    }
-
-    public static DeviceCacheInfo of(String deviceId) {
-        return new DeviceCacheInfo(deviceId,null,null,null);
-    }
-
-    public static DeviceCacheInfo of( BusPumpEntity deviceUsing, BusDeviceEntity device) {
-
-        return new DeviceCacheInfo(deviceUsing.getDeviceId(), deviceUsing.getAlias(),device.getEnable(),deviceUsing.getTenantId(),deviceUsing.getId(),deviceUsing.getStartTime());
+    private DeviceCacheInfo() {
     }
-
-
 }

+ 24 - 16
coffee-system/src/main/java/com/coffee/bus/registry/device/bean/DeviceOperator.java

@@ -1,7 +1,10 @@
 package com.coffee.bus.registry.device.bean;
 
 import com.coffee.bus.enums.NetPumpStatusEnum;
+import com.coffee.common.cache.value.Value;
+
 import java.util.Date;
+import java.util.Map;
 
 /**
  * @author lifang
@@ -11,27 +14,17 @@ import java.util.Date;
  * @createTime 2022年04月06日 15:16:00
  */
 public interface DeviceOperator<T> {
-    /**
-     * 清除操作缓存
-     */
-    void clear();
 
-    /**
-     * 同步缓存数据
-     */
-    void sync();
-
-    /**
-     * 设置缓存数据
-     * @param all
-     */
-    void setAll(T all);
 
     /**
      * 获取所有缓存数据
      * @return
      */
-    T getAll();
+    T get();
+
+    void set(T  all);
+
+    Map<String, Value> getAll();
 
     /**
      * 设置设备号
@@ -61,7 +54,7 @@ public interface DeviceOperator<T> {
      * 设置设备是否启用
      * @param enable
      */
-    void setEnable(String enable);
+    void setEnable(Boolean enable);
 
     /**
      * 获取设备是否启用
@@ -141,4 +134,19 @@ public interface DeviceOperator<T> {
      * @return
      */
     boolean getMaster();
+
+    /**
+     * 设置当前设备标记位
+     * @param mark
+     */
+    void setMark(String mark);
+
+    /**
+     * 获取当前设备标记位
+     * @return
+     */
+    Integer getMark();
+
+
+    void clear();
 }

+ 6 - 3
coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceService.java

@@ -1,10 +1,10 @@
 package com.coffee.bus.service;
 
 import cn.hutool.core.util.StrUtil;
-import com.coffee.bus.registry.device.DeviceOperator;
 import com.coffee.bus.registry.device.DeviceRegistry;
 import com.coffee.bus.entity.BusDeviceEntity;
 import com.coffee.bus.mapper.BusDeviceMapper;
+import com.coffee.bus.registry.device.bean.DeviceOperator;
 import com.coffee.common.crud.BaseService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -49,11 +49,14 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
     public void postUpdate(BusDeviceEntity entity) {
         DeviceOperator deviceOperator = deviceRegistry
                 .getDevice(entity.getDeviceId());
+        if(deviceOperator==null){
+            return;
+        }
         if(StrUtil.isNotEmpty(entity.getAlias())){
-            deviceOperator.updateAlias(entity.getAlias());
+            deviceOperator.setAlias(entity.getAlias());
         }
         if(entity.getEnable()!=null){
-            deviceOperator.updateEnable(entity.getEnable());
+            deviceOperator.setEnable(entity.getEnable()==1);
         }
     }
 

+ 26 - 22
coffee-system/src/main/java/com/coffee/bus/websocket/listener/DeviceInfoListener.java

@@ -8,12 +8,13 @@ import cn.hutool.extra.spring.SpringUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
-import com.coffee.bus.registry.device.DeviceOperator;
 import com.coffee.bus.registry.device.DeviceRegistry;
 import com.coffee.bus.entity.BusPumpEntity;
 import com.coffee.bus.enums.NetPumpStatusEnum;
 import com.coffee.bus.listener.event.bean.DeviceAlarmEvent;
 import com.coffee.bus.listener.event.bean.DeviceInfoEvent;
+import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
+import com.coffee.bus.registry.device.bean.DeviceOperator;
 import com.coffee.bus.registry.patient.PatientOperator;
 import com.coffee.bus.registry.patient.PatientRegistry;
 import com.coffee.bus.service.LocalBusPumpService;
@@ -67,31 +68,30 @@ public class DeviceInfoListener {
     @Async
     @Transactional(rollbackFor = Exception.class)
     public void deviceInfoDetail(DeviceInfoEvent infoEvent){
+        //若数据发送过快,为防止冲突,请在此加锁处理 todo
         /****************处理泵数据****************/
         BusPumpEntity device = infoEvent.getContent();
         //1、判断该设备是否已和医院绑定并开启使用
         String deviceId = device.getDeviceId();
-        DeviceOperator deviceOperator = deviceRegistry.getDevice(deviceId);
+        DeviceOperator<DeviceCacheInfo> deviceOperator = deviceRegistry.getDevice(deviceId);
         PatientOperator patientOperator =null;
-        if (!deviceOperator.canUse()) {
+        if (deviceOperator==null||!Boolean.TRUE.equals(deviceOperator.getEnable())) {
             log.warn("设备[{}]暂不可用,数据已丢弃",deviceId);
             return ;
         }
         log.info("接收到设备数据:[{}]",infoEvent.getContent().toString());
         device.setMonitorType(1);
-        //首次运行需要与病人、医院进行绑定
-        if(deviceOperator.isFirst()){
+        //不存在运行id,即首次运行,需要与病人、医院进行绑定
+        String usingId = deviceOperator.getUsingId();
+        if(StrUtil.isEmpty(usingId)){
             initDevice(device,deviceOperator);
+            //todo
             patientOperator = patientRegistry.getPatient(device.getTenantId(), device.getPatientCode());
             initPatient(device,patientOperator);
-            //二次确认
-            deviceOperator = deviceRegistry.getDevice(deviceId);
-            if(device.getId().equals(deviceOperator.getRunId())){
-                deviceUsingService.save(device);
-            }
+            deviceUsingService.save(device);
         }
         else {
-            device.setId(deviceOperator.getRunId());
+            device.setId(usingId);
             deviceUsingService.updateById(device);
         }
         /****************处理泵数据****************/
@@ -150,12 +150,20 @@ public class DeviceInfoListener {
 
         /****************处理泵与患者关系****************/
 
-        if(!deviceOperator.isFirst()){
-            //非首次注册,则推送设备消息
-            String topic = WebSocketConstant.getDeviceInfoDetailTopic(null, device.getId(), device.getTenantId());
-            redisTemplate.convertAndSend(topic, device);
-        }
-//        SpringUtil.publishEvent(new DeviceAlarmEvent(this,device));
+        //则推送设备上报消息
+        String topic = WebSocketConstant.getDeviceInfoDetailTopic(null, device.getId(), device.getTenantId());
+        redisTemplate.convertAndSend(topic, device);
+
+        //更新设备缓存信息
+        DeviceCacheInfo cacheInfo = DeviceCacheInfo.builder()
+                .usingId(device.getId())
+                .status(device.getRunState())
+                .startTime(device.getStartTime())
+                .master(device.getMaster() == 1)
+                .patientCode(device.getPatientCode())
+                .mark(device.getMark())
+                .build();
+        deviceOperator.set(cacheInfo);
     }
 
     /**
@@ -175,6 +183,7 @@ public class DeviceInfoListener {
      * @param alarmEvent
      */
     @EventListener
+    @Async
     public void deviceAlarm(DeviceAlarmEvent alarmEvent){
         BusPumpEntity pump = alarmEvent.getContent();
         //获取医院配置,对医院功能配置进行过滤筛选
@@ -204,19 +213,14 @@ public class DeviceInfoListener {
     private void initDevice(BusPumpEntity pump, DeviceOperator deviceOperator){
         //处理无泵状态(即泵首次与医院进行绑定)
         Date now = new Date();
-
         //设置注册时间和泵的开始时间
         pump.setId(String.valueOf(IdWorker.getId()));
         pump.setRegisterTime(now);
         pump.setStartTime(now);
         pump.setAlias(deviceOperator.getAlias());
-
         //设备绑定医院
         pump.setTenantId(deviceOperator.getTenantId());
 
-        //缓存存储泵的开始时间和注册时间 todo 缓存更新放在最后进行
-        deviceOperator.updateStartTime(now);
-        deviceOperator.updateRunId(pump.getId());
     }
 
     @Scheduled(cron = "0/50 * * * * ?")