|
|
@@ -1,17 +1,17 @@
|
|
|
package com.yunfeiyun.agmp.iot.common.service;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.yunfeiyun.agmp.common.enums.RedisCacheKey;
|
|
|
import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
|
|
|
-import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
|
|
|
-import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
|
|
|
+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 org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
@Service
|
|
|
public class DeviceconnCacheService {
|
|
|
@@ -25,13 +25,49 @@ public class DeviceconnCacheService {
|
|
|
redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_CONN)+iotDeviceconn.getDevconnBid());
|
|
|
}
|
|
|
public IotDeviceconn getIotDeviceConnByDevconnBid(String devconnBid){
|
|
|
+ // 首先查询出当前设备的连接信息
|
|
|
IotDeviceconn iotDeviceconn = (IotDeviceconn) redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_CONN)+devconnBid);
|
|
|
if(null == iotDeviceconn){
|
|
|
throw new IotBizException(IotErrorCode.INVALID_DEVICE_CONN_BID);
|
|
|
}
|
|
|
return iotDeviceconn;
|
|
|
}
|
|
|
+
|
|
|
+ public IotDeviceconn getIotDeviceConnByIotDevice(IotDevice iotDevice){
|
|
|
+ return this.getIotDeviceConnByDevconnBid(iotDevice.getDevconnBid());
|
|
|
+ }
|
|
|
private String getKey(RedisCacheKey redisCacheKey){
|
|
|
return redisCacheKey.getPrefix() + ":" + redisCacheKey.getModuleType() + ":" + redisCacheKey.getName()+":";
|
|
|
}
|
|
|
+
|
|
|
+ public String getMqttConnectIdByDeviceConnBid(String devconnBid){
|
|
|
+ String connectId = (String) redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID)+devconnBid);
|
|
|
+ if(null == connectId){
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_CONN_BID);
|
|
|
+ }
|
|
|
+ return connectId;
|
|
|
+ }
|
|
|
+ public void setMqttConnectionIdByConnBid(String devconnBid,String connectionId){
|
|
|
+ redisTemplate.opsForList().leftPush(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+connectionId,devconnBid);
|
|
|
+ redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID)+devconnBid,connectionId);
|
|
|
+ }
|
|
|
+ public void deleteMqttConnectionId(String devconnBid){
|
|
|
+ redisTemplate.opsForList().remove(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+getMqttConnectIdByDeviceConnBid(devconnBid),1,devconnBid);
|
|
|
+ redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID)+devconnBid);
|
|
|
+ }
|
|
|
+ public boolean mqttConnectionIdHasLink(String connectionId){
|
|
|
+ return redisTemplate.hasKey(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+connectionId);
|
|
|
+ }
|
|
|
+ public void cleanCache(){
|
|
|
+ cleanCache(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST);
|
|
|
+ cleanCache(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID);
|
|
|
+ cleanCache(RedisCacheKey.IOT_DEVICE_CONN);
|
|
|
+ }
|
|
|
+ public void cleanCache(RedisCacheKey redisCacheKey){
|
|
|
+ Set<String> keySet = redisTemplate.keys(getKey(redisCacheKey)+"*");
|
|
|
+ for(String key : keySet){
|
|
|
+ redisTemplate.delete(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|