@@ -450,4 +598,22 @@ public class KingIOServerService { } throw new IllegalArgumentException("Invalid date format: " + timestamp); } + + /** + * 判断字符串是否为指定的日期格式:yyyy-M-d H:m:s + * 例如:2025-8-28 15:36:28 + */ + public static boolean isDateString(String dateString) { + if (dateString == null || dateString.trim().isEmpty()) { + return false; + } + + try { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s"); + LocalDateTime.parse(dateString.trim(), formatter); + return true; + } catch (DateTimeParseException e) { + return false; + } + } } diff --git a/src/main/java/com/techsor/datacenter/sender/service/TrendLogService.java b/src/main/java/com/techsor/datacenter/sender/service/TrendLogService.java new file mode 100644 index 0000000..8cf280a --- /dev/null +++ b/src/main/java/com/techsor/datacenter/sender/service/TrendLogService.java @@ -0,0 +1,251 @@ +package com.techsor.datacenter.sender.service; + +import com.alibaba.fastjson2.JSON; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.techsor.datacenter.sender.components.CommonOpt; +import com.techsor.datacenter.sender.config.DataSourceContextHolder; +import com.techsor.datacenter.sender.dao.KingIOServerDAO; +import com.techsor.datacenter.sender.dao.TypeDAO; +import com.techsor.datacenter.sender.entitiy.DeviceEntity; +import com.techsor.datacenter.sender.entitiy.kingio.*; +import com.techsor.datacenter.sender.utils.TimeUtils; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.text.DecimalFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +/** + * 服务类,用于处理TrendLog的数据。 + *
+ * 此服务类负责接收和解析TrendLog的数据,并根据数据处理逻辑生成相应的数据消息。 + */ +@Slf4j +@Component("TrendLogService") +public class TrendLogService { + + @Resource + TypeDAO typeDAO; + @Resource + KingIOServerDAO kingIOServerDAO; + @Autowired + private CommonOpt commonOpt; + + private final ObjectMapper objectMapper = new ObjectMapper(); + private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#0.00"); + /** + * 将JSON字符串解析为KingIODataModel对象。 + * + * @param json King IO Server发送的数据的JSON字符串 + * @return 解析后的KingIODataModel对象 + * @throws IOException 当解析JSON时发生错误 + */ + public TrendLog1RawEntity parseJson(String json) throws IOException { + return objectMapper.readValue(json, TrendLog1RawEntity.class); + } + /** + * 开始处理King IO Server发来的数据。 + *
+ * 该方法首先调用parseJson方法解析数据,然后根据解析后的数据对象进行进一步处理。 + * + * @param data King IO Server发送的数据的JSON字符串 + * @return 处理后的数据消息,如果无法处理则返回空字符串 + * @throws Exception 当解析JSON或处理数据过程中发生错误 + */ + public String start(String data) throws Exception { + TrendLog1RawEntity dataModel = parseJson(data); + return processData(dataModel); + } + /** + * 根据解析后的KingIODataModel对象处理数据。 + *
+ * 此方法遍历数据对象中的数据项,根据数据项的类型和内容生成相应的数据消息字符串。
+ *
+ * @param data 解析后的KingIODataModel对象
+ * @return 生成的数据消息字符串,如果无法处理则返回空字符串
+ * @throws ParseException 当处理数据时发生错误
+ */
+ public String processData(TrendLog1RawEntity data) throws ParseException {
+ log.info("TrendLog DataSize to process:{}",data.getLog_buffer().size());
+ List
+ * 此方法根据设备的类型ID和数据项的内容生成特定格式的DBM消息字符串。
+ *
+ * @param deviceItem 设备实体对象
+ * @return 生成的DBM消息字符串
+ */
+ private String generateDBMStr(DeviceEntity deviceItem,String value){
+ String finalStr = "";
+ String tempKey ="";
+ finalStr = String.format("{\"%s"+tempKey+"\":%s",deviceItem.getDeviceSN(),value+"}");
+ return finalStr;
+ }
+
+ /**
+ * Transform boolan obj to int. If it's int, return itself.
+ * @param value
+ * @return
+ */
+ private Integer BooleanObjToInt(Object value){
+ Integer tempValue;
+
+ if (value instanceof Boolean) {
+ tempValue = (Boolean) value ? 1 : 0; // 布尔型处理
+ } else if (value instanceof Integer) {
+ tempValue = (Integer) value; // 整型处理
+ } else if (value instanceof Double) {
+ tempValue = Integer.valueOf(((Double) value).intValue());
+ }
+ else {
+ throw new IllegalArgumentException("Unsupported value type: " + value.getClass());
+ }
+
+ return tempValue;
+ }
+
+ public static Date parseDate(String timestamp) {
+ String[] patterns = {
+ "yyyy-MM-dd HH:mm:ss.SSS", // 3 位毫秒
+ "yyyy-MM-dd HH:mm:ss.SSSS" // 4 位毫秒
+ };
+
+ for (String pattern : patterns) {
+ try {
+ SimpleDateFormat tempDateFormat = new SimpleDateFormat(pattern);
+ return tempDateFormat.parse(timestamp);
+ } catch (Exception ignored) { }
+ }
+ throw new IllegalArgumentException("Invalid date format: " + timestamp);
+ }
+
+ /**
+ * 判断字符串是否为指定的日期格式:yyyy-M-d H:m:s
+ * 例如:2025-8-28 15:36:28
+ */
+ public static boolean isDateString(String dateString) {
+ if (dateString == null || dateString.trim().isEmpty()) {
+ return false;
+ }
+
+ try {
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s");
+ LocalDateTime.parse(dateString.trim(), formatter);
+ return true;
+ } catch (DateTimeParseException e) {
+ return false;
+ }
+ }
+}
diff --git a/src/main/java/com/techsor/datacenter/sender/service/impl/DataProcessServiceImpl.java b/src/main/java/com/techsor/datacenter/sender/service/impl/DataProcessServiceImpl.java
index 3939de2..57449e2 100644
--- a/src/main/java/com/techsor/datacenter/sender/service/impl/DataProcessServiceImpl.java
+++ b/src/main/java/com/techsor/datacenter/sender/service/impl/DataProcessServiceImpl.java
@@ -32,7 +32,10 @@ import com.techsor.datacenter.sender.entitiy.bastatus.BaStatusEntity;
import com.techsor.datacenter.sender.entitiy.bastatus.BaStatusThirdReqEntity;
import com.techsor.datacenter.sender.entitiy.company.CompanyEntity;
import com.techsor.datacenter.sender.entitiy.delta.DeltaNumEntity;
+import com.techsor.datacenter.sender.entitiy.kingio.KingIODataItemEntity;
import com.techsor.datacenter.sender.entitiy.kingio.KingIODbmEntity;
+import com.techsor.datacenter.sender.entitiy.kingio.TrendLog1RawEntity;
+import com.techsor.datacenter.sender.entitiy.kingio.TrendLog1RawEntity.LogBuffer;
import com.techsor.datacenter.sender.entitiy.st150.St150DataItem;
import com.techsor.datacenter.sender.entitiy.st150.St150DbmEntity;
import com.techsor.datacenter.sender.service.AlarmDataPushLambda;
@@ -66,9 +69,13 @@ import org.springframework.util.ObjectUtils;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
+import lombok.val;
+
import java.lang.reflect.Type;
import java.math.BigDecimal;
+import java.text.DecimalFormat;
import java.text.MessageFormat;
+import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
@@ -89,6 +96,7 @@ import static org.apache.commons.text.StringEscapeUtils.unescapeJson;
public class DataProcessServiceImpl implements IDataProcessService {
private static Logger log= LoggerFactory.getLogger(DataProcessServiceImpl.class);
+ private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
// @Autowired
// @Qualifier("sysMqttClient")
@@ -1678,6 +1686,8 @@ public class DataProcessServiceImpl implements IDataProcessService {
}
}
+
+
/**
* Use to process processSt150Data data.
*/
@@ -1717,9 +1727,24 @@ public class DataProcessServiceImpl implements IDataProcessService {
}catch (Exception e){
log.error(e.getMessage(),e);
}
+ }
+ }
+
+ public static Date parseDate(String timestamp) {
+ String[] patterns = {
+ "yyyy-MM-dd HH:mm:ss.SSS", // 3 位毫秒
+ "yyyy-MM-dd HH:mm:ss.SSSS" // 4 位毫秒
+ };
+ for (String pattern : patterns) {
+ try {
+ SimpleDateFormat tempDateFormat = new SimpleDateFormat(pattern);
+ return tempDateFormat.parse(timestamp);
+ } catch (Exception ignored) { }
}
+ throw new IllegalArgumentException("Invalid date format: " + timestamp);
}
+
}
diff --git a/src/main/java/com/techsor/datacenter/sender/utils/TimeUtils.java b/src/main/java/com/techsor/datacenter/sender/utils/TimeUtils.java
index ac31a49..f5273d4 100644
--- a/src/main/java/com/techsor/datacenter/sender/utils/TimeUtils.java
+++ b/src/main/java/com/techsor/datacenter/sender/utils/TimeUtils.java
@@ -31,17 +31,17 @@ public class TimeUtils {
return formatDateTime;
}
- public static String kingiOServerTimeToTs(String time){
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
- LocalDateTime localDateTime = LocalDateTime.parse(time, formatter);
- ZoneId japanZone = ZoneId.of("Asia/Tokyo");
- ZonedDateTime zonedDateTime = localDateTime.atZone(japanZone);
- long epochMilli = zonedDateTime.toInstant().toEpochMilli();
-
- // 打印结果
-// System.out.println("毫秒时间戳: " + epochMilli);
- return String.valueOf(epochMilli);
- }
+ public static String kingiOServerTimeToTs(String time) {
+ // 支持 .SSS, .SS, .S, 甚至无毫秒
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS][.SS][.S]");
+
+ LocalDateTime localDateTime = LocalDateTime.parse(time, formatter);
+ ZoneId japanZone = ZoneId.of("Asia/Tokyo");
+ ZonedDateTime zonedDateTime = localDateTime.atZone(japanZone);
+ long epochMilli = zonedDateTime.toInstant().toEpochMilli();
+
+ return String.valueOf(epochMilli);
+ }
public static String st150TimeToTs(String timeStr){
// 定义时间格式