|
@@ -1,115 +0,0 @@
|
|
|
-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.domain.IotDevice;
|
|
|
|
|
-import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
|
|
|
|
|
-import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
|
-import java.util.Set;
|
|
|
|
|
-
|
|
|
|
|
-@Service
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-public class DeviceconnCacheService {
|
|
|
|
|
- @Resource
|
|
|
|
|
- private RedisTemplate redisTemplate;
|
|
|
|
|
-
|
|
|
|
|
- public void setCache(IotDeviceconn iotDeviceconn){
|
|
|
|
|
- log.info("【设备连接缓存】保存设备连接信息缓存,设备链接信息标识:{}",iotDeviceconn.getDevconnBid());
|
|
|
|
|
- redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_CONN)+iotDeviceconn.getDevconnBid(),iotDeviceconn);
|
|
|
|
|
- }
|
|
|
|
|
- public void deleteCache(IotDeviceconn iotDeviceconn){
|
|
|
|
|
- log.info("【设备连接缓存】删除设备连接信息缓存,设备链接信息标识:{}",iotDeviceconn.getDevconnBid());
|
|
|
|
|
- redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_CONN)+iotDeviceconn.getDevconnBid());
|
|
|
|
|
- }
|
|
|
|
|
- public IotDeviceconn getIotDeviceConnByDevconnBid(String devconnBid){
|
|
|
|
|
- log.info("【设备连接缓存】查询设备连接信息,设备连接标识为:{}",devconnBid);
|
|
|
|
|
- // 首先查询出当前设备的连接信息
|
|
|
|
|
- IotDeviceconn iotDeviceconn = (IotDeviceconn) redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_CONN)+devconnBid);
|
|
|
|
|
- if(null == iotDeviceconn){
|
|
|
|
|
- log.error("【设备连接缓存】查询设备连接信息失败,设备连接标识为:{}",devconnBid);
|
|
|
|
|
- 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()+":";
|
|
|
|
|
- }
|
|
|
|
|
- /**
|
|
|
|
|
- * 【mqtt】 通过设备连接标识获取mqtt链接标识
|
|
|
|
|
- * */
|
|
|
|
|
- public String getMqttConnectIdByDeviceConnBid(String devconnBid){
|
|
|
|
|
- log.error("【设备连接缓存】查询mqtt链接标识,设备连接标识为:{}",devconnBid);
|
|
|
|
|
- String connectId = (String) redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID)+devconnBid);
|
|
|
|
|
- if(null == connectId){
|
|
|
|
|
- log.error("【设备连接缓存】查询mqtt链接标识失败,设备连接标识为:{}",devconnBid);
|
|
|
|
|
- throw new IotBizException(IotErrorCode.INVALID_DEVICE_CONN_BID);
|
|
|
|
|
- }
|
|
|
|
|
- return connectId;
|
|
|
|
|
- }
|
|
|
|
|
- /**
|
|
|
|
|
- * 【mqtt】 关联设备连接标识和mqtt链接标识
|
|
|
|
|
- * */
|
|
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
- /**
|
|
|
|
|
- * 【mqtt】删除mqtt链接标识
|
|
|
|
|
- * */
|
|
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
- /**
|
|
|
|
|
- * 【mqtt】判定mqtt链接标识是否还有绑定的设备连接标识
|
|
|
|
|
- * */
|
|
|
|
|
- public boolean mqttConnectionIdHasLink(String connectionId){
|
|
|
|
|
- return redisTemplate.hasKey(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+connectionId);
|
|
|
|
|
- }
|
|
|
|
|
- /**
|
|
|
|
|
- * 【http】保存租户的http通用连接信息
|
|
|
|
|
- * */
|
|
|
|
|
- public void setHttpCommonConnectionByDevtypeCode(String devconnBid,String devtypeCode){
|
|
|
|
|
- redisTemplate.opsForList().leftPush(getKey(RedisCacheKey.IOT_HTTP_DEVICE_CONN_BID_LIST)+devtypeCode,devconnBid);
|
|
|
|
|
- }
|
|
|
|
|
- /**
|
|
|
|
|
- * 【http】删除租户的http通用连接信息
|
|
|
|
|
- * */
|
|
|
|
|
- public void deleteMqttConnectionId(String devconnBid,String devtypeCode){
|
|
|
|
|
- redisTemplate.opsForList().remove(getKey(RedisCacheKey.IOT_HTTP_DEVICE_CONN_BID_LIST)+devtypeCode,1,devconnBid);
|
|
|
|
|
- }
|
|
|
|
|
- /**
|
|
|
|
|
- * 【http】判定http通用连接是否还有绑定的设备连接
|
|
|
|
|
- * */
|
|
|
|
|
- public boolean httpConnectionBidHasLink(String devtypeCode){
|
|
|
|
|
- return redisTemplate.hasKey(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+devtypeCode);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public void cleanCache(){
|
|
|
|
|
- // Mqtt设备连接数据标识列表
|
|
|
|
|
- cleanCache(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST);
|
|
|
|
|
- // Mqtt设备链接标识
|
|
|
|
|
- cleanCache(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID);
|
|
|
|
|
- // Mqtt设备连接信息标识
|
|
|
|
|
- cleanCache(RedisCacheKey.IOT_DEVICE_CONN);
|
|
|
|
|
- }
|
|
|
|
|
- public void cleanCache(RedisCacheKey redisCacheKey){
|
|
|
|
|
- Set<String> keySet = redisTemplate.keys(getKey(redisCacheKey)+"*");
|
|
|
|
|
- for(String key : keySet){
|
|
|
|
|
- redisTemplate.delete(key);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|