-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathCSVLogger.java
114 lines (101 loc) · 3.43 KB
/
CSVLogger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package io.pslab.others;
import android.os.Environment;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Created by Padmal on 6/24/18.
*/
public class CSVLogger {
private File csvFile;
private String category;
public static final String CSV_DIRECTORY = "PSLab";
public static final SimpleDateFormat FILE_NAME_FORMAT = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS", Locale.ROOT);
/**
* Constructor initiate logger with a category folder
*
* @param category type of readings
*/
public CSVLogger(String category) {
this.category = category;
setupPath();
}
/**
* Create required directories and csv files to record data
*/
private void setupPath() {
File csvDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY);
if (!csvDirectory.exists()) {
try {
csvDirectory.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
File categoryDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + category);
if (!categoryDirectory.exists()) {
try {
categoryDirectory.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void prepareLogFile() {
String uniqueFileName = FILE_NAME_FORMAT.format(new Date());
csvFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + category +
File.separator + uniqueFileName + ".csv");
if (!csvFile.exists()) {
try {
csvFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Write comma seperated data lines to the file created
*
* @param data comma separated data line ends with a new line character
*/
public void writeCSVFile(CSVDataLine data) {
if (csvFile.exists()) {
try {
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter(csvFile, true)));
out.write(data.toString());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getCurrentFilePath() {
return Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + category;
}
public void deleteFile() {
csvFile.delete();
}
public void writeMetaData(String instrumentName) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String metaDataTime = sdf.format(System.currentTimeMillis());
CSVDataLine metaData = new CSVDataLine()
.add(instrumentName)
.add(metaDataTime.split(" ")[0])
.add(metaDataTime.split(" ")[1]);
writeCSVFile(metaData);
}
}