package cc.blynk.utils;
import cc.blynk.server.core.model.AppName;
import cc.blynk.server.core.model.auth.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;
/**
* The Blynk Project.
* Created by Dmitriy Dumanskiy.
* Created on 04.01.16.
*/
public class FileUtils {
private final static Logger log = LogManager.getLogger(FileUtils.class);
public static void deleteQuietly(Path path) {
try {
Files.deleteIfExists(path);
} catch (Exception ignored) {
//ignore
}
}
public static boolean move(Path source, Path target) {
try {
Path targetFile = Paths.get(target.toString(), source.getFileName().toString());
Files.move(source, targetFile, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
log.debug("Failed to move file. {}" , e.getMessage());
return false;
}
return true;
}
/**
* Simply writes single reporting entry to disk (16 bytes).
* Reporting entry is value (double) and timestamp (long)
*
* @param reportingPath - path to user specific reporting file
* @param value - sensor data
* @param ts - time when entry was created
* @throws IOException
*/
public static void write(Path reportingPath, double value, long ts) throws IOException {
try (DataOutputStream dos = new DataOutputStream(
Files.newOutputStream(reportingPath, StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
dos.writeDouble(value);
dos.writeLong(ts);
dos.flush();
}
}
private static final int SIZE_OF_REPORT_ENTRY = 16;
/**
* Read bunch of last records from file.
*
* @param userDataFile - file to read
* @param count = number of records to read
* @return - byte buffer with data
* @throws IOException
*/
public static ByteBuffer read(Path userDataFile, int count) throws IOException {
try (SeekableByteChannel channel = Files.newByteChannel(userDataFile, StandardOpenOption.READ)) {
final int size = (int) Files.size(userDataFile);
final int dataSize = count * SIZE_OF_REPORT_ENTRY;
final int readDataSize = Math.min(dataSize, size);
ByteBuffer buf = ByteBuffer.allocate(readDataSize);
channel.position(Math.max(0, size - dataSize));
channel.read(buf);
return buf;
}
}
public static String getUserReportingDir(User user) {
return getUserReportingDir(user.email, user.appName);
}
public static String getUserReportingDir(String email, String appName) {
if (appName.equals(AppName.BLYNK)) {
return email;
}
return email + "_" + appName;
}
public static String csvDownloadUrl(String host, String httpPort) {
return "http://" + host + ":" + httpPort + "/";
}
}