diff options
Diffstat (limited to 'src/main/java/org/traccar/helper')
-rw-r--r-- | src/main/java/org/traccar/helper/Log.java | 51 | ||||
-rw-r--r-- | src/main/java/org/traccar/helper/ObdDecoder.java | 78 | ||||
-rw-r--r-- | src/main/java/org/traccar/helper/model/AttributeUtil.java | 16 |
3 files changed, 60 insertions, 85 deletions
diff --git a/src/main/java/org/traccar/helper/Log.java b/src/main/java/org/traccar/helper/Log.java index 9aaf1cfd3..d01d70559 100644 --- a/src/main/java/org/traccar/helper/Log.java +++ b/src/main/java/org/traccar/helper/Log.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2024 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.traccar.helper; import org.traccar.config.Config; import org.traccar.config.Keys; +import org.traccar.model.Pair; import java.io.BufferedWriter; import java.io.File; @@ -30,9 +31,9 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.FileStore; import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Path; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Comparator; import java.util.Date; import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; @@ -40,6 +41,7 @@ import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import java.util.stream.Stream; public final class Log { @@ -124,21 +126,13 @@ public final class Log { } private static String formatLevel(Level level) { - switch (level.getName()) { - case "FINEST": - return "TRACE"; - case "FINER": - case "FINE": - case "CONFIG": - return "DEBUG"; - case "INFO": - return "INFO"; - case "WARNING": - return "WARN"; - case "SEVERE": - default: - return "ERROR"; - } + return switch (level.getName()) { + case "FINEST" -> "TRACE"; + case "FINER", "FINE", "CONFIG" -> "DEBUG"; + case "INFO" -> "INFO"; + case "WARNING" -> "WARN"; + default -> "ERROR"; + }; } @Override @@ -150,7 +144,7 @@ public final class Log { } if (record.getThrown() != null) { - if (message.length() > 0) { + if (!message.isEmpty()) { message.append(" - "); } if (fullStackTraces) { @@ -278,17 +272,22 @@ public final class Log { } public static long[] getStorageSpace() { - long usable = 0; - long total = 0; - for (Path root : FileSystems.getDefault().getRootDirectories()) { + var stores = new ArrayList<Pair<Long, Long>>(); + for (FileStore store : FileSystems.getDefault().getFileStores()) { try { - FileStore store = Files.getFileStore(root); - usable += store.getUsableSpace(); - total += store.getTotalSpace(); + long usableSpace = store.getUsableSpace(); + long totalSpace = store.getTotalSpace(); + if (totalSpace > 1_000_000_000) { + stores.add(new Pair<>(usableSpace, totalSpace)); + } } catch (IOException ignored) { } } - return new long[]{usable, total}; + return stores.stream() + .sorted(Comparator.comparingDouble(p -> p.first() / (double) p.second())) + .flatMap(p -> Stream.of(p.first(), p.second())) + .mapToLong(Long::longValue) + .toArray(); } } diff --git a/src/main/java/org/traccar/helper/ObdDecoder.java b/src/main/java/org/traccar/helper/ObdDecoder.java index 3cbae334a..091747225 100644 --- a/src/main/java/org/traccar/helper/ObdDecoder.java +++ b/src/main/java/org/traccar/helper/ObdDecoder.java @@ -30,17 +30,13 @@ public final class ObdDecoder { private static final int MODE_CODES = 0x03; public static Map.Entry<String, Object> decode(int mode, String value) { - switch (mode) { - case MODE_CURRENT: - case MODE_FREEZE_FRAME: - return decodeData( - Integer.parseInt(value.substring(0, 2), 16), - Long.parseLong(value.substring(2), 16), true); - case MODE_CODES: - return decodeCodes(value); - default: - return null; - } + return switch (mode) { + case MODE_CURRENT, MODE_FREEZE_FRAME -> decodeData( + Integer.parseInt(value.substring(0, 2), 16), + Long.parseLong(value.substring(2), 16), true); + case MODE_CODES -> decodeCodes(value); + default -> null; + }; } private static Map.Entry<String, Object> createEntry(String key, Object value) { @@ -53,7 +49,7 @@ public final class ObdDecoder { int numValue = Integer.parseInt(value.substring(i * 4, (i + 1) * 4), 16); codes.append(' ').append(decodeCode(numValue)); } - if (codes.length() > 0) { + if (!codes.isEmpty()) { return createEntry(Position.KEY_DTCS, codes.toString().trim()); } else { return null; @@ -61,49 +57,29 @@ public final class ObdDecoder { } public static String decodeCode(int value) { - char prefix; - switch (value >> 14) { - case 1: - prefix = 'C'; - break; - case 2: - prefix = 'B'; - break; - case 3: - prefix = 'U'; - break; - default: - prefix = 'P'; - break; - } + char prefix = switch (value >> 14) { + case 1 -> 'C'; + case 2 -> 'B'; + case 3 -> 'U'; + default -> 'P'; + }; return String.format("%c%04X", prefix, value & 0x3FFF); } public static Map.Entry<String, Object> decodeData(int pid, long value, boolean convert) { - switch (pid) { - case 0x04: - return createEntry(Position.KEY_ENGINE_LOAD, convert ? value * 100 / 255 : value); - case 0x05: - return createEntry(Position.KEY_COOLANT_TEMP, convert ? value - 40 : value); - case 0x0B: - return createEntry("mapIntake", value); - case 0x0C: - return createEntry(Position.KEY_RPM, convert ? value / 4 : value); - case 0x0D: - return createEntry(Position.KEY_OBD_SPEED, value); - case 0x0F: - return createEntry("intakeTemp", convert ? value - 40 : value); - case 0x11: - return createEntry(Position.KEY_THROTTLE, convert ? value * 100 / 255 : value); - case 0x21: - return createEntry("milDistance", value); - case 0x2F: - return createEntry(Position.KEY_FUEL_LEVEL, convert ? value * 100 / 255 : value); - case 0x31: - return createEntry("clearedDistance", value); - default: - return null; - } + return switch (pid) { + case 0x04 -> createEntry(Position.KEY_ENGINE_LOAD, convert ? value * 100 / 255 : value); + case 0x05 -> createEntry(Position.KEY_COOLANT_TEMP, convert ? value - 40 : value); + case 0x0B -> createEntry("mapIntake", value); + case 0x0C -> createEntry(Position.KEY_RPM, convert ? value / 4 : value); + case 0x0D -> createEntry(Position.KEY_OBD_SPEED, value); + case 0x0F -> createEntry("intakeTemp", convert ? value - 40 : value); + case 0x11 -> createEntry(Position.KEY_THROTTLE, convert ? value * 100 / 255 : value); + case 0x21 -> createEntry("milDistance", value); + case 0x2F -> createEntry(Position.KEY_FUEL_LEVEL, convert ? value * 100 / 255 : value); + case 0x31 -> createEntry("clearedDistance", value); + default -> null; + }; } } diff --git a/src/main/java/org/traccar/helper/model/AttributeUtil.java b/src/main/java/org/traccar/helper/model/AttributeUtil.java index 2630f64f0..0e3d91766 100644 --- a/src/main/java/org/traccar/helper/model/AttributeUtil.java +++ b/src/main/java/org/traccar/helper/model/AttributeUtil.java @@ -70,20 +70,20 @@ public final class AttributeUtil { if (result != null) { Class<T> valueClass = key.getValueClass(); if (valueClass.equals(Boolean.class)) { - return (T) (result instanceof String - ? Boolean.parseBoolean((String) result) + return (T) (result instanceof String stringResult + ? Boolean.parseBoolean(stringResult) : result); } else if (valueClass.equals(Integer.class)) { - return (T) (Object) (result instanceof String - ? Integer.parseInt((String) result) + return (T) (Object) (result instanceof String stringResult + ? Integer.parseInt(stringResult) : ((Number) result).intValue()); } else if (valueClass.equals(Long.class)) { - return (T) (Object) (result instanceof String - ? Long.parseLong((String) result) + return (T) (Object) (result instanceof String stringResult + ? Long.parseLong(stringResult) : ((Number) result).longValue()); } else if (valueClass.equals(Double.class)) { - return (T) (Object) (result instanceof String - ? Double.parseDouble((String) result) + return (T) (Object) (result instanceof String stringResult + ? Double.parseDouble(stringResult) : ((Number) result).doubleValue()); } else { return (T) result; |