RRD4J List Data Experiment (openHAB,Java,B4J)

Exploring openHAB & openHABian.
rwblinn
Site Admin
Posts: 224
Joined: 07 Oct 2017, 12:16

RRD4J List Data Experiment (openHAB,Java,B4J)

Postby rwblinn » 02 Feb 2018, 15:48

openHAB2 RRD4J List Data Experiment (openHAB,Java,B4J)
Version 20180202

Objectives
To list the content, by using a Java or B4J console application, of a rrd archive (nHA_Energy_PowerConsumption.rrd) containing one openHAB item (Number nHA_Energy_PowerConsumption).

Image

Software
  • Raspberry Pi Stretch Linux 4.9.59-v7+ ARM
  • openHAB 2.2.0
  • Java JDK8
  • B4J v6.01
  • Eclipse Oxygen.2
  • rrd4j Archive 2.1.1

Download
Download Java and B4J project source with data.
Note: the rrd4j Jar archive is not included (lookup the GitHub repository).

openHAB Configuration
File: nHA_Energy_PowerConsumption.rrd
Folder Raspberry Pi holding the rrd archives: /var/lib/openhab2/persistence/rrd4j
Item: nHA_Energy_PowerConsumption

Java Solution
See comments in the Java Code.

Code: Select all

package rrd4jlistdata;
import static org.rrd4j.ConsolFun.*;
import org.rrd4j.core.FetchData;
import org.rrd4j.core.FetchRequest;
import org.rrd4j.core.RrdDb;
import org.rrd4j.core.Util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class rrd4jlistdataex {

   // Set the start and end date for fetching data
   // Note that the first month JAN = 0, FEB = 1 ...
    static final long START = Util.getTimestamp(2018, 0, 30);
    static final long END = Util.getTimestamp(2018, 0, 31);
    static final int MAX_STEP = 300;
   
   public rrd4jlistdataex() {
      // TODO Auto-generated constructor stub
   }

   /**
    * Test read-only access on a rrd4j archive
    * @param args
    */
   public static void main(String[] args) {
      // Set the start and end date
        long start = START;
        long end = END;
        // Format of the timestamp
        SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm");
        jdf.setTimeZone(TimeZone.getTimeZone("GMT+2"));

      try {
         // Set the path to the data file - using the data folder of this project.
         String rrdPath = "data/nHA_Energy_PowerConsumption.rrd";
         // Create a new rrdDb object and open in readonly mode
         RrdDb rrdDb = new RrdDb(rrdPath, true);
         System.out.println("File reopen in read-only mode");
         // Get some database information
            Date dtlu = new Date(rrdDb.getLastUpdateTime()*1000L);
         System.out.println("== Last update time was: " + jdf.format(dtlu));
         // System.out.println("== Last info was: " + rrdDb.getInfo());

         // fetch data using AVERAGE as the consolidation function with the finest resolution (300 sec, default).
            Date dts = new Date(start*1000L);
            Date dte = new Date(end*1000L);
         System.out.println("== Create Fetch Request:" + jdf.format(dts) + " - " + jdf.format(dte));
         FetchRequest request = rrdDb.createFetchRequest(AVERAGE, start, end);
         System.out.println("== Dump Request ...");
         System.out.println(request.dump());
         System.out.println("== Fetch data ...");
         FetchData fetchData = request.fetchData();
         System.out.println("== Data fetched.");

         // Get the first and last timestamp
            Date dtf = new Date(fetchData.getFirstTimestamp()*1000L);
           System.out.println("First Timestamp=" + jdf.format(dtf));
            Date dtl = new Date(fetchData.getLastTimestamp()*1000L);
           System.out.println("Last Timestamp=" + jdf.format(dtl));
           System.out.println("Data points=" + fetchData.getRowCount());
         
         // Option 1 list the data from large string = rrdfetch-like output form
         // System.out.println(fetchData.toString());

         // Option 2 to fetch timestamp & value.
         // The value has column id 0 as the dataset has only 1 field (beside the timestamp).
         // If a group with multiple items is used, then the dataset has multiple fields.
           long[] ts = fetchData.getTimestamps();
         double[] data = fetchData.getValues(0);
            for (int i = 0; i < fetchData.getRowCount(); ++i) {
                Date dt = new Date(ts[i]*1000L);
               System.out.println(jdf.format(dt) + "=" + data[i]);
            }
      }
      catch (IOException ex){
         System.out.println(ex);
      }
   }
}


Example Output (snippet)

Code: Select all

File reopen in read-only mode
== Last update time was: 2018-Feb-02 13:05
== Create Fetch Request:2018-Jan-30 01:00 - 2018-Jan-31 01:00
== Dump Request ...
fetch "data/nHA_Energy_PowerConsumption.rrd" AVERAGE --start 1517266800 --end 1517353200
== Fetch data ...
== Data fetched.
First Timestamp=2018-Jan-30 01:00
Last Timestamp=2018-Jan-31 01:02
Data points=104
2018-Jan-30 01:00=296.37184615384615
2018-Jan-30 01:14=390.5595
2018-Jan-30 01:28=418.85985714285727
2018-Jan-30 01:42=394.07492857142853
...


B4J Solution
The B4J Non-UI (console application) solution uses the JavaObject library to create objects and use methods from the rrd4j Jar Archive.
A dedicated class is created to fetch the data and return as a list of maps (timestamp:value). The class is open for further enhancements.
Lookup the B4J source in openhabrrd4jlistdata.zip.

ToDo
  • Experiment completed.

Version
  • 20180202

Return to “Experiments”



Who is online