diff --git a/src/main/java/com/techsor/datacenter/sender/entitiy/kingio/KingIODataItemEntity.java b/src/main/java/com/techsor/datacenter/sender/entitiy/kingio/KingIODataItemEntity.java index 88025bf..4b6fb4c 100644 --- a/src/main/java/com/techsor/datacenter/sender/entitiy/kingio/KingIODataItemEntity.java +++ b/src/main/java/com/techsor/datacenter/sender/entitiy/kingio/KingIODataItemEntity.java @@ -19,4 +19,44 @@ public class KingIODataItemEntity { this.timestamp = formattedTimestamp; this.quality = s; } + + /** + * 自定义的 getter 方法,根据原始 value 的类型返回预处理后的值。 + * 注意:这里返回 Float 而不是 float,因为 Object 不能持有原始类型。 + */ + public Object getValue() { + if (this.value instanceof Number) { // 检查是否为数字类型 + Number num = (Number) this.value; + // 检查是否为整数类型 (Integer, Long, Short, Byte) + if (this.value instanceof Integer || + this.value instanceof Long || + this.value instanceof Short || + this.value instanceof Byte) { + // 返回 Integer (intValue() 返回 int,自动装箱为 Integer) + // 如果 value 本身就是 Integer,直接返回即可 + if (this.value instanceof Integer) { + return this.value; + } else { + // 如果是 Long, Short, Byte,转换为 Integer + return num.intValue(); + } + } else { + // 其他数字类型 (Double, Float 等) 视为小数 + // 你要求返回 float,所以即使是 Double 也转换为 float + // 注意:从 Double 转换为 Float 可能会有精度损失 + if (this.value instanceof Float) { + return this.value; // 如果 value 本身就是 Float,直接返回 + } else { + return num.floatValue(); // 返回 Float (如从 Double 转换) + } + } + } else if (this.value instanceof String) { + return this.value; // 如果是 String,直接返回 + } else if (this.value instanceof Boolean) { + return this.value; // 如果是 Boolean,直接返回 + } + // 如果是其他类型(如 Character 等),也可以按需处理 + // 或者保持原样返回 + return this.value; + } } diff --git a/src/main/java/com/techsor/datacenter/sender/service/KingIOServerService.java b/src/main/java/com/techsor/datacenter/sender/service/KingIOServerService.java index 1203cc5..bfe9bc1 100644 --- a/src/main/java/com/techsor/datacenter/sender/service/KingIOServerService.java +++ b/src/main/java/com/techsor/datacenter/sender/service/KingIOServerService.java @@ -113,18 +113,19 @@ public class KingIOServerService { } - //Process the number value. keep 4 ecimal places + // Process the number value. Keep 2 decimal places for floats, otherwise keep as integer. if (value instanceof Number) { - DecimalFormat df = new DecimalFormat("#.####"); // 保留四位小数的格式 - String formatted = df.format((Number)value); - value = Double.parseDouble(formatted); - }else if (value instanceof Boolean){ - if((Boolean)value==true){ - value = 1; - }else{ - value = 0; + double doubleVal = ((Number) value).doubleValue(); + if (doubleVal % 1 == 0) { // Check if the numeric value is a whole number (e.g., 1.0, 5.0) + value = (int) doubleVal; // Represent whole numbers as Integer + } else { + // Format non-whole numbers to 2 decimal places + DecimalFormat df = new DecimalFormat("#.##"); + String formatted = df.format(doubleVal); + value = Double.parseDouble(formatted); // Result is Double } - + } else if (value instanceof Boolean) { + value = (Boolean) value ? 1 : 0; } // 输出数据解读