| | |
| | | import com.zy.asrs.service.DeviceDataLogService; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import com.zy.core.enums.SlaveType; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.nio.file.StandardOpenOption; |
| | | import java.nio.file.SimpleFileVisitor; |
| | | import java.nio.file.StandardOpenOption; |
| | | import java.nio.file.attribute.BasicFileAttributes; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.Set; |
| | | import java.util.List; |
| | | import java.util.ArrayList; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | import java.util.stream.Stream; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | @Slf4j |
| | | @Component |
| | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); |
| | | Map<String, Map<String, List<DeviceDataLog>>> group = new HashMap<>(); |
| | | for (DeviceDataLog logItem : list) { |
| | | String typeName = logItem.getType(); |
| | | String datePart = sdf.format(logItem.getCreateTime() == null ? new Date() : logItem.getCreateTime()); |
| | | String prefix = typeName + "_" + String.valueOf(logItem.getDeviceNo()) + "_" + datePart + "_"; |
| | | String prefix = buildFilePrefix(logItem, datePart); |
| | | if (prefix == null) { |
| | | continue; |
| | | } |
| | | group.computeIfAbsent(datePart, k -> new HashMap<>()) |
| | | .computeIfAbsent(prefix, k -> new ArrayList<>()) |
| | | .add(logItem); |
| | |
| | | Path dayDir = baseDir.resolve(dateEntry.getKey()); |
| | | Files.createDirectories(dayDir); |
| | | for (Map.Entry<String, List<DeviceDataLog>> entry : dateEntry.getValue().entrySet()) { |
| | | String prefix = entry.getKey(); |
| | | List<DeviceDataLog> logs = entry.getValue(); |
| | | if (logs == null || logs.isEmpty()) { |
| | | continue; |
| | | } |
| | | DeviceDataLog firstLog = logs.get(0); |
| | | Path deviceDir = resolveDeviceDir(dayDir, firstLog); |
| | | if (deviceDir == null) { |
| | | continue; |
| | | } |
| | | Files.createDirectories(deviceDir); |
| | | String prefix = buildFilePrefix(firstLog, dateEntry.getKey()); |
| | | if (prefix == null) { |
| | | continue; |
| | | } |
| | | logs.sort(Comparator.comparing(DeviceDataLog::getCreateTime, Comparator.nullsLast(Date::compareTo))); |
| | | int index = findStartIndex(dayDir, prefix); |
| | | Path current = dayDir.resolve(prefix + index + ".log"); |
| | | int index = findStartIndex(deviceDir, prefix); |
| | | Path current = deviceDir.resolve(prefix + index + ".log"); |
| | | if (!Files.exists(current)) { |
| | | Files.createFile(current); |
| | | } |
| | |
| | | byte[] line = (json + System.lineSeparator()).getBytes(StandardCharsets.UTF_8); |
| | | if (size + line.length > max) { |
| | | index++; |
| | | current = dayDir.resolve(prefix + index + ".log"); |
| | | current = deviceDir.resolve(prefix + index + ".log"); |
| | | if (!Files.exists(current)) { |
| | | Files.createFile(current); |
| | | } |
| | |
| | | } catch (Exception e) { |
| | | log.error("设备日志文件存储失败", e); |
| | | } |
| | | } |
| | | |
| | | private String buildFilePrefix(DeviceDataLog logItem, String datePart) { |
| | | if (logItem == null || logItem.getType() == null || logItem.getDeviceNo() == null) { |
| | | return null; |
| | | } |
| | | if (String.valueOf(SlaveType.Devp).equals(logItem.getType())) { |
| | | if (logItem.getStationId() == null) { |
| | | log.warn("跳过缺少站点号的输送设备日志, deviceNo={}, createTime={}", logItem.getDeviceNo(), logItem.getCreateTime()); |
| | | return null; |
| | | } |
| | | return logItem.getType() + "_" + logItem.getDeviceNo() + "_station_" + logItem.getStationId() + "_" + datePart + "_"; |
| | | } |
| | | return logItem.getType() + "_" + logItem.getDeviceNo() + "_" + datePart + "_"; |
| | | } |
| | | |
| | | private Path resolveDeviceDir(Path dayDir, DeviceDataLog logItem) { |
| | | if (dayDir == null || logItem == null || logItem.getType() == null || logItem.getDeviceNo() == null) { |
| | | return null; |
| | | } |
| | | return dayDir.resolve(logItem.getType()).resolve(String.valueOf(logItem.getDeviceNo())); |
| | | } |
| | | |
| | | private int findStartIndex(Path baseDir, String prefix) throws Exception { |
| | |
| | | if (val > maxIdx) { |
| | | maxIdx = val; |
| | | } |
| | | } catch (NumberFormatException ignored) {} |
| | | } catch (NumberFormatException ignored) { |
| | | } |
| | | } |
| | | } |
| | | int candidate = maxIdx == 0 ? 1 : maxIdx; |
| | |
| | | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| | | try { |
| | | Files.deleteIfExists(file); |
| | | } catch (Exception ignored) {} |
| | | } catch (Exception ignored) { |
| | | } |
| | | return FileVisitResult.CONTINUE; |
| | | } |
| | | |
| | |
| | | public FileVisitResult postVisitDirectory(Path dir, java.io.IOException exc) { |
| | | try { |
| | | Files.deleteIfExists(dir); |
| | | } catch (Exception ignored) {} |
| | | } catch (Exception ignored) { |
| | | } |
| | | return FileVisitResult.CONTINUE; |
| | | } |
| | | }); |