|
@@ -6,7 +6,9 @@ import com.shkpr.iot.common.core.constants.TemplateMetadata;
|
|
|
import com.shkpr.iot.common.core.domain.po.DataValue;
|
|
|
import com.shkpr.iot.common.core.domain.po.PointRule;
|
|
|
import com.shkpr.iot.common.core.domain.po.Template;
|
|
|
+import com.shkpr.iot.common.core.enums.ValueTypeEnum;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.math.NumberUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -90,11 +92,11 @@ public class JsonUtil {
|
|
|
PointRule rule = rules.stream().filter(it -> {
|
|
|
String[] split = it.getFlagIndex().split(",");
|
|
|
return split.length == 2 &&
|
|
|
- Objects.equals(objectMapper.convertValue(node.get(split[0]),String.class), split[1]);
|
|
|
+ Objects.equals(objectMapper.convertValue(node.get(split[0]), String.class), split[1]);
|
|
|
}).findFirst().orElse(null);
|
|
|
if (rule == null) continue;
|
|
|
//获取值
|
|
|
- Object value = objectMapper.convertValue(node.get(rule.getValueIndex()), Object.class);
|
|
|
+ Object value = getValue(node.get(rule.getValueIndex()), rule.getValueType());
|
|
|
//存入值对象
|
|
|
dataValues.add(new DataValue(equipmentCode, rule.getKey(), value, dateTime));
|
|
|
}
|
|
@@ -106,4 +108,38 @@ public class JsonUtil {
|
|
|
}
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取值
|
|
|
+ * @param node 节点
|
|
|
+ * @param type 类型
|
|
|
+ * @return 值
|
|
|
+ */
|
|
|
+ private static Object getValue(JsonNode node, ValueTypeEnum type) {
|
|
|
+ if (node == null || type == null || node.isNull()) return null;
|
|
|
+ //按字符串解析json
|
|
|
+ String s = String.valueOf(objectMapper.convertValue(node, Object.class)).trim();
|
|
|
+ //根据类型解析值
|
|
|
+ Object value;
|
|
|
+ switch (type) {
|
|
|
+ case BOOLEAN:
|
|
|
+ if (NumberUtils.isParsable(s)) value = Integer.parseInt(s) == 1;
|
|
|
+ else value = Boolean.valueOf(s);
|
|
|
+ break;
|
|
|
+ case UNSIGNED_INT:
|
|
|
+ case SIGNED_INT:
|
|
|
+ value = Integer.parseInt(s);
|
|
|
+ break;
|
|
|
+ case SINGLE_FLOAT:
|
|
|
+ value = Float.parseFloat(s);
|
|
|
+ break;
|
|
|
+ case DOUBLE_FLOAT:
|
|
|
+ value = Double.parseDouble(s);
|
|
|
+ break;
|
|
|
+ case ASCII:
|
|
|
+ default:
|
|
|
+ value = s;
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
}
|