|
|
@@ -0,0 +1,111 @@
|
|
|
+package com.yunfeiyun.agmp.iotm.cache;
|
|
|
+
|
|
|
+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.TosDevicetype;
|
|
|
+import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
+import com.yunfeiyun.agmp.iotm.web.service.ITosDevicetypeService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 统一维护type(lv2)的缓存,增删改查
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class TypeCacheService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ITosDevicetypeService tosDevicetypeService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将设备类型信息存入缓存中
|
|
|
+ *
|
|
|
+ * @param tosDevicetype 设备类型对象
|
|
|
+ */
|
|
|
+ public void setCache(TosDevicetype tosDevicetype) {
|
|
|
+ redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_TYPE) + tosDevicetype.getDevtypeBid(), tosDevicetype);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从缓存中删除指定设备类型的记录
|
|
|
+ *
|
|
|
+ * @param tosDevicetype 设备类型对象
|
|
|
+ */
|
|
|
+ public void deleteCache(TosDevicetype tosDevicetype) {
|
|
|
+ redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_TYPE) + tosDevicetype.getDevtypeBid());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据设备类型编码从缓存中获取设备类型对象,如果不存在,则从数据库中查找并存入缓存
|
|
|
+ *
|
|
|
+ * @param devtypeCode 设备类型编码
|
|
|
+ * @return TosDevicetype 设备类型对象
|
|
|
+ */
|
|
|
+ public TosDevicetype getCacheObjectByDevTypeCode(String devtypeCode) {
|
|
|
+ Object tosDevicetype = redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_TYPE) + devtypeCode);
|
|
|
+ if (null == tosDevicetype) {
|
|
|
+ tosDevicetype = tosDevicetypeService.selectTosDevicetypeByDevtypeCode(devtypeCode);
|
|
|
+
|
|
|
+ if (tosDevicetype == null) {
|
|
|
+ throw new IotBizException(IotErrorCode.CACHE_NOT_FOUNT);
|
|
|
+ }
|
|
|
+ setCache((TosDevicetype) tosDevicetype);
|
|
|
+ }
|
|
|
+ return JSONObject.parseObject(JSONObject.toJSONString(tosDevicetype), TosDevicetype.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据设备类型标识符从缓存中获取设备类型对象,实际上调用的是getCacheObjectByDevTypeCode方法
|
|
|
+ *
|
|
|
+ * @param devtypeBid 设备类型标识符
|
|
|
+ * @return TosDevicetype 设备类型对象
|
|
|
+ */
|
|
|
+ public TosDevicetype getCacheObjectByDevTypeBid(String devtypeBid) {
|
|
|
+ return getCacheObjectByDevTypeCode(devtypeBid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据设备类型编码获取对应的服务名称
|
|
|
+ *
|
|
|
+ * @param devtypeCode 设备类型编码
|
|
|
+ * @return String 服务名称
|
|
|
+ */
|
|
|
+ public String getServiceNameByDevTypeCode(String devtypeCode) {
|
|
|
+ if (null == devtypeCode) {
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_TYPE);
|
|
|
+ }
|
|
|
+ IotDeviceDictEnum iotDeviceDictEnum = IotDeviceDictEnum.findEnumByCode(devtypeCode);
|
|
|
+ if (null == iotDeviceDictEnum) {
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_TYPE);
|
|
|
+ }
|
|
|
+ return iotDeviceDictEnum.getServiceName();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据设备类型标识符获取对应的服务名称,实际上调用的是getServiceNameByDevTypeCode方法
|
|
|
+ *
|
|
|
+ * @param devtypeBid 设备类型标识符
|
|
|
+ * @return String 服务名称
|
|
|
+ */
|
|
|
+ public String getServiceNameByDevTypeBid(String devtypeBid) {
|
|
|
+ return getServiceNameByDevTypeCode(devtypeBid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取Redis缓存键值
|
|
|
+ *
|
|
|
+ * @param redisCacheKey 缓存键枚举
|
|
|
+ * @return String 完整的缓存键
|
|
|
+ */
|
|
|
+ private String getKey(RedisCacheKey redisCacheKey) {
|
|
|
+ return redisCacheKey.getPrefix() + ":" + redisCacheKey.getModuleType() + ":" + redisCacheKey.getName() + ":";
|
|
|
+ }
|
|
|
+}
|