|
|
@@ -0,0 +1,162 @@
|
|
|
+package com.yunfeiyun.agmp.iots.cache;
|
|
|
+
|
|
|
+import com.yunfeiyun.agmp.common.enums.RedisCacheKey;
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
|
|
|
+import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
+import com.yunfeiyun.agmp.iots.service.BusinessCoreService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class DeviceconnCacheService {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用于存储设备连接信息,键为设备连接标识符(devconnBid),值为IotDeviceconn对象。模拟了原先在Redis中保存设备连接信息的功能。
|
|
|
+ */
|
|
|
+ private final Map<String, IotDeviceconn> deviceConnMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用于存储设备连接与MQTT连接标识之间的映射关系,键为设备连接标识符(devconnBid),值为MQTT连接标识(connectionId)。替代了原本在Redis中通过键值对保存MQTT连接ID的方式。
|
|
|
+ */
|
|
|
+ private final Map<String, String> mqttConnectIdMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用于存储MQTT连接标识与一组设备连接标识符之间的映射关系。键为MQTT连接标识(connectionId),值为包含相关设备连接标识符(devconnBid)的集合。这个Map追踪哪些设备连接与特定的MQTT连接有关联,以便于管理和删除操作。
|
|
|
+ */
|
|
|
+ private final Map<String, Set<String>> mqttDeviceConnBidListMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用于存储HTTP通用连接信息,键为设备类型代码(devtypeCode),值为与此类型相关的设备连接标识符(devconnBid)的集合。
|
|
|
+ * 此Map模拟了原系统中基于设备类型代码管理HTTP连接的方式,允许快速查找和管理属于同一类型的设备连接。
|
|
|
+ */
|
|
|
+ private final Map<String, Set<String>> httpDeviceConnBidListMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BusinessCoreService businessCoreService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将设备连接信息保存到内存中。
|
|
|
+ *
|
|
|
+ * @param iotDeviceconn 设备连接对象
|
|
|
+ */
|
|
|
+ public void setCache(IotDeviceconn iotDeviceconn) {
|
|
|
+ log.info("【设备连接缓存】保存设备连接信息缓存,设备链接信息标识:{}", iotDeviceconn.getDevconnBid());
|
|
|
+ deviceConnMap.put(iotDeviceconn.getDevconnBid(), iotDeviceconn);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从内存中删除指定的设备连接信息。
|
|
|
+ *
|
|
|
+ * @param iotDeviceconn 设备连接对象
|
|
|
+ */
|
|
|
+ public void deleteCache(IotDeviceconn iotDeviceconn) {
|
|
|
+ log.info("【设备连接缓存】删除设备连接信息缓存,设备链接信息标识:{}", iotDeviceconn.getDevconnBid());
|
|
|
+ deviceConnMap.remove(iotDeviceconn.getDevconnBid());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据设备连接标识符获取设备连接信息。如果缓存中不存在,则尝试从数据库中查找并更新缓存。
|
|
|
+ *
|
|
|
+ * @param devconnBid 设备连接标识符
|
|
|
+ * @return IotDeviceconn 设备连接对象
|
|
|
+ */
|
|
|
+ public IotDeviceconn getIotDeviceConnByDevconnBid(String devconnBid) {
|
|
|
+ log.info("【设备连接缓存】查询设备连接信息,设备连接标识为:{}", devconnBid);
|
|
|
+ IotDeviceconn iotDeviceconn = deviceConnMap.get(devconnBid);
|
|
|
+ if (iotDeviceconn == null) {
|
|
|
+ log.error("【设备连接缓存】查询设备连接信息失败,设备连接标识为:{}", devconnBid);
|
|
|
+ iotDeviceconn = businessCoreService.selectDevConnByConnId(devconnBid);
|
|
|
+ if (iotDeviceconn == null) {
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_CONN_BID);
|
|
|
+ }
|
|
|
+ setCache(iotDeviceconn);
|
|
|
+ }
|
|
|
+ return iotDeviceconn;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据IotDevice对象获取设备连接信息。
|
|
|
+ *
|
|
|
+ * @param iotDevice 设备对象
|
|
|
+ * @return IotDeviceconn 设备连接对象
|
|
|
+ */
|
|
|
+ public IotDeviceconn getIotDeviceConnByIotDevice(IotDevice iotDevice) {
|
|
|
+ return this.getIotDeviceConnByDevconnBid(iotDevice.getDevconnBid());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【mqtt】通过设备连接标识获取mqtt链接标识
|
|
|
+ */
|
|
|
+ public String getMqttConnectIdByDeviceConnBid(String devconnBid) {
|
|
|
+ log.error("【设备连接缓存】查询mqtt链接标识,设备连接标识为:{}", devconnBid);
|
|
|
+ String connectId = mqttConnectIdMap.get(devconnBid);
|
|
|
+ if (connectId == null) {
|
|
|
+ log.error("【设备连接缓存】查询mqtt链接标识失败,设备连接标识为:{}", devconnBid);
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_CONN_BID);
|
|
|
+ }
|
|
|
+ return connectId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【mqtt】关联设备连接标识和mqtt链接标识
|
|
|
+ */
|
|
|
+ public void setMqttConnectionIdByConnBid(String devconnBid, String connectionId) {
|
|
|
+ mqttConnectIdMap.put(devconnBid, connectionId);
|
|
|
+ mqttDeviceConnBidListMap.computeIfAbsent(connectionId, k -> ConcurrentHashMap.newKeySet()).add(devconnBid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【mqtt】删除mqtt链接标识
|
|
|
+ */
|
|
|
+ public void deleteMqttConnectionId(String devconnBid) {
|
|
|
+ String connectionId = mqttConnectIdMap.remove(devconnBid);
|
|
|
+ if (connectionId != null && mqttDeviceConnBidListMap.containsKey(connectionId)) {
|
|
|
+ mqttDeviceConnBidListMap.get(connectionId).remove(devconnBid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【mqtt】判定mqtt链接标识是否还有绑定的设备连接标识
|
|
|
+ */
|
|
|
+ public boolean mqttConnectionIdHasLink(String connectionId) {
|
|
|
+ return mqttDeviceConnBidListMap.containsKey(connectionId) && !mqttDeviceConnBidListMap.get(connectionId).isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【http】保存租户的http通用连接信息
|
|
|
+ */
|
|
|
+ public void setHttpCommonConnectionByDevtypeCode(String devconnBid, String devtypeCode) {
|
|
|
+ httpDeviceConnBidListMap.computeIfAbsent(devtypeCode, k -> ConcurrentHashMap.newKeySet()).add(devconnBid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【http】删除租户的http通用连接信息
|
|
|
+ */
|
|
|
+ public void deleteHttpCommonConnectionByDevtypeCode(String devconnBid, String devtypeCode) {
|
|
|
+ if (httpDeviceConnBidListMap.containsKey(devtypeCode)) {
|
|
|
+ httpDeviceConnBidListMap.get(devtypeCode).remove(devconnBid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【http】判定http通用连接是否还有绑定的设备连接
|
|
|
+ */
|
|
|
+ public boolean httpConnectionBidHasLink(String devtypeCode) {
|
|
|
+ return httpDeviceConnBidListMap.containsKey(devtypeCode) && !httpDeviceConnBidListMap.get(devtypeCode).isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void cleanCache() {
|
|
|
+ deviceConnMap.clear();
|
|
|
+ mqttConnectIdMap.clear();
|
|
|
+ mqttDeviceConnBidListMap.clear();
|
|
|
+ httpDeviceConnBidListMap.clear();
|
|
|
+ }
|
|
|
+}
|