|
@@ -1,6 +1,7 @@
|
|
|
package com.persagy.cache;
|
|
|
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
@@ -13,14 +14,17 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.persagy.constant.RedisConstant;
|
|
|
import com.persagy.entity.AlarmConditionState;
|
|
|
import com.persagy.entity.ZktAlarmRecordDO;
|
|
|
import com.persagy.entity.v2.AlarmCondition;
|
|
|
+import com.persagy.entity.v2.ItemCodeCondition;
|
|
|
import com.persagy.entity.v2.ObjConditionInfo;
|
|
|
import com.persagy.repository.AlarmRecordRepository;
|
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
@Component
|
|
@@ -35,17 +39,15 @@ public class AlarmRedisCache implements RedisCache {
|
|
|
|
|
|
public AlarmConditionState getAlarmDefineState(String defineId) {
|
|
|
AlarmConditionState alarmConditionState = null;
|
|
|
- String str = alarmRedisTemplate.opsForHash().get(RedisConstant.DEFINE_ID, defineId).toString();
|
|
|
- if (StringUtils.isNotBlank(str)) {
|
|
|
- try {
|
|
|
- JSONObject object = JSONObject.parseObject(str);
|
|
|
- alarmConditionState = JSON.toJavaObject(object, AlarmConditionState.class);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(" alarmDefineState json 转换 AlarmConditionState 失败", e);
|
|
|
- }
|
|
|
- }
|
|
|
try {
|
|
|
- if (Objects.isNull(alarmConditionState)) {
|
|
|
+ Object obj = alarmRedisTemplate.opsForHash().get(RedisConstant.DEFINE_ID, defineId);
|
|
|
+ if (null != obj && StringUtils.isNotBlank(obj.toString())) {
|
|
|
+ try {
|
|
|
+ alarmConditionState =JSONUtil.toBean(obj.toString(), AlarmConditionState.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(" alarmDefineState json 转换 AlarmConditionState 失败", e);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
// 默认正常报警状态
|
|
|
Optional<ZktAlarmRecordDO> recordOptional = alarmRecordRepository.findById(defineId);
|
|
|
if (recordOptional.isPresent()) {
|
|
@@ -56,6 +58,8 @@ public class AlarmRedisCache implements RedisCache {
|
|
|
alarmConditionState.setState(AlarmConditionState.State.NOT_DEAL.getType());
|
|
|
alarmConditionState.setAlarmStartTime(alarmRecordDO.getAlarmTime());
|
|
|
}
|
|
|
+ } else {
|
|
|
+ alarmConditionState = new AlarmConditionState(defineId);
|
|
|
}
|
|
|
}
|
|
|
log.debug("alarmDefineState: [{}], ( 0-正常 1-报警)", alarmConditionState.getState());
|
|
@@ -85,7 +89,13 @@ public class AlarmRedisCache implements RedisCache {
|
|
|
String str = alarmRedisTemplate.opsForHash().get(RedisConstant.METER_ID, meterId).toString();
|
|
|
if (StringUtils.isNotBlank(str)) {
|
|
|
JSONObject object = JSONObject.parseObject(str);
|
|
|
+ JSONArray conditions = object.getJSONArray("conditions");
|
|
|
+ if (null == conditions || conditions.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
ObjConditionInfo objConditionInfo = JSON.toJavaObject(object, ObjConditionInfo.class);
|
|
|
+ List<ItemCodeCondition> iccArrayy = JSON.parseArray(conditions.toString(), ItemCodeCondition.class);
|
|
|
+ objConditionInfo.setConditions(new LinkedList<>(iccArrayy));
|
|
|
return objConditionInfo;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
@@ -96,9 +106,9 @@ public class AlarmRedisCache implements RedisCache {
|
|
|
|
|
|
public AlarmCondition getAlarmCondition(String conditionId) {
|
|
|
try {
|
|
|
- String str = alarmRedisTemplate.opsForHash().get(RedisConstant.ALARMCONDITION_ID, conditionId).toString();
|
|
|
- if (StringUtils.isNotBlank(str)) {
|
|
|
- JSONObject object = JSONObject.parseObject(str);
|
|
|
+ Object obj = alarmRedisTemplate.opsForHash().get(RedisConstant.ALARMCONDITION_ID, conditionId);
|
|
|
+ if (null != obj && StringUtils.isNotBlank(obj.toString())) {
|
|
|
+ JSONObject object = JSONObject.parseObject(obj.toString());
|
|
|
AlarmCondition alarmCondition = JSON.toJavaObject(object, AlarmCondition.class);
|
|
|
return alarmCondition;
|
|
|
}
|
|
@@ -126,8 +136,10 @@ public class AlarmRedisCache implements RedisCache {
|
|
|
*/
|
|
|
public boolean lock(String key, String value, Integer timeout) {
|
|
|
boolean bool;
|
|
|
+ int i = 1;
|
|
|
while (true) {
|
|
|
Boolean b = alarmRedisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
|
|
|
+ System.out.println(key + "---key----" + b);
|
|
|
if (b) {
|
|
|
bool = b;
|
|
|
break;
|
|
@@ -138,9 +150,10 @@ public class AlarmRedisCache implements RedisCache {
|
|
|
Thread.currentThread().interrupt();
|
|
|
}
|
|
|
}
|
|
|
+ System.out.println(i);
|
|
|
}
|
|
|
return bool;
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|