数据同步服务-语音
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.3 KiB

1 week ago
package com.threecloud.dataserviceyy.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Files;
import java.util.Date;
/**
* 同步时间工具类
* 管理设备上次同步时间的本地文件存储
*/
public class SyncTimeUtil {
private static final Logger logger = LoggerFactory.getLogger(SyncTimeUtil.class);
/**
* 从本地文件读取上次同步时间
*
* @param basePath 基础路径
* @param deviceId 设备ID
* @return 上次同步时间不存在返回null
*/
public static Date readLastSyncTime(String basePath, String deviceId) {
String markerPath = FilePathUtil.buildSyncMarkerPath(basePath, deviceId);
File markerFile = new File(markerPath);
if (!markerFile.exists()) {
logger.debug("同步标记文件不存在: deviceId={}", deviceId);
return null;
}
try {
byte[] bytes = Files.readAllBytes(markerFile.toPath());
long millis = Long.parseLong(new String(bytes).trim());
Date syncTime = new Date(millis);
logger.debug("读取到上次同步时间: deviceId={}, time={}", deviceId, syncTime);
return syncTime;
} catch (Exception e) {
logger.warn("读取同步时间失败: deviceId={}, error={}", deviceId, e.getMessage());
return null;
}
}
/**
* 保存同步时间到本地文件
*
* @param basePath 基础路径
* @param deviceId 设备ID
* @param time 同步时间
*/
public static void writeLastSyncTime(String basePath, String deviceId, Date time) {
String markerDir = FilePathUtil.buildSyncMarkerDir(basePath);
File dir = new File(markerDir);
if (!dir.exists()) {
dir.mkdirs();
}
String markerPath = FilePathUtil.buildSyncMarkerPath(basePath, deviceId);
File markerFile = new File(markerPath);
try {
Files.write(markerFile.toPath(), String.valueOf(time.getTime()).getBytes());
logger.debug("保存同步时间成功: deviceId={}, time={}", deviceId, time);
} catch (Exception e) {
logger.warn("保存同步时间失败: deviceId={}, error={}", deviceId, e.getMessage());
}
}
}