From 3190cfa03ae951e46a4d56d07d1155945d4aa7ae Mon Sep 17 00:00:00 2001 From: "zhczyx@163.com" Date: Sat, 20 Dec 2025 20:55:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3KingIoserver=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=BC=A0=E8=BF=87=E6=9D=A5=E6=95=B4=E6=95=B0=E4=BD=86?= =?UTF-8?q?=E6=98=AF=E6=98=BE=E7=A4=BA=E5=B0=8F=E6=95=B0=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entitiy/kingio/KingIODataItemEntity.java | 40 +++++++++++++++++++ .../sender/service/KingIOServerService.java | 21 +++++----- 2 files changed, 51 insertions(+), 10 deletions(-) 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; } // 输出数据解读