import org.rrd4j.ConsolFun;
import org.rrd4j.DsType;
import org.rrd4j.core.RrdDb;
import org.rrd4j.core.RrdDef;
import org.rrd4j.core.Sample;
class Demo implements Runnable {
private static final int MAXDEVICES = 1;
private static final int MAXCHANALS = 30;
private static final String RRDPATH = "D:\\temp\\device0"; //"/media/usb/rrdb/device";
public static void main(String[] args) throws Exception {
System.out.println("Thread: " + Thread.currentThread().getName() + ": Start the logger test...");
long start = System.currentTimeMillis();
RrdDb rrdDb;
RrdDef rrdDef;
// Initialize rrdb files
for (int i = 1; i <= MAXDEVICES; i++) {
rrdDef = new RrdDef(RRDPATH + i + ".rrd", 60);
for (int n = 1; n <= MAXCHANALS; n++) {
rrdDef.addDatasource("chan0" + n, DsType.GAUGE, 120, Double.NaN, Double.NaN);
}
rrdDef.addArchive(ConsolFun.AVERAGE, 0.5, 1, 4320); // 3 days (granularity 1 min) 1MB
rrdDef.addArchive(ConsolFun.MIN, 0.5, 1, 4320);
rrdDef.addArchive(ConsolFun.MAX, 0.5, 1, 4320);
rrdDb = new RrdDb(rrdDef);
System.out.println("Estimated file size: " + rrdDef.getEstimatedSize());
rrdDb.close();
}
long time = System.currentTimeMillis() - start;
System.out.println("Created rrdb files in " + time + "ms");
// Store Data
for (int i = 1; i <= MAXDEVICES; i++) {
final RrdDb rrdDb2 = new RrdDb(RRDPATH + i + ".rrd");
final Sample sample = rrdDb2.createSample();
Runnable task = () -> {
System.out.println(Thread.currentThread().getName() + " is running.");
try {
update(sample);
} catch (Exception ex) {
ex.printStackTrace();
}
};
Thread thread1 = new Thread(task);
thread1.start();
}
}
public static synchronized void write(long time, int channelId, double value, Sample sample) throws Exception {
sample.setTime(time);
for (int n = 1; n <= MAXCHANALS; n++) {
sample.setValue("chan0" + n, value);
}
sample.update();
}
public static void update(Sample sample) throws Exception {
double cnt = 1.0;
while (true) {
for (int n = 1; n <= MAXCHANALS; n++) {
long now = System.currentTimeMillis() / 1000;
write(now, n, cnt++, sample);
System.out.println("Date: " + now + ", value: " + cnt++);
Thread.sleep(1 * 1000);
}
}
}
@Override
public void run() {
}
}