GDPR Cookie Consent by Free Privacy Policy Generator
post thumb
Asset Control
by Matthias Hanitzsch-Moonlight/ on 11 Mar 2018

A Basic Java API for Asset Control - Static Data

Details how to use the Basic Java API to work with static data.

Note: This article is part of a series. You may want to read the previous article regarding how to Connect to AC before continuing with the material below.

ADO creation

All static data lives in the context of an ADO. So I will need to create an ADO instance first. Initially, this is in memory only. The respective ADO may or may not exist in AC.

The API provides the class AcService to help - among other things - with the creation of ADOs both in memory and in AC. Please see the following code that demonstrates how to get an instance representing an ADO that exists in the system:

  AcConnection conn = AcConnection.getDefaultConnection();
  AcService ac = new AcService(conn);

  Ado ado = ac.createAdo("C0.ISS.1");
  assertThat(ado.existsInAc(), is(true));

If I want to create a new ADO in the system, I have to specify its ID, longname and template, and call createInAc(). When I am done, I can delete it again:

  Ado newAdo = ac.createAdo("C0.TFN.001", "TEST ADO", "C0_IN_T001_LSA");
  assertThat(newAdo.existsInAc(), is(false));
  assertThat(newAdo.existsInAc(), is(true));
  assertThat(newAdo.existsInAc(), is(false));

Loading static data

Now I can inspect some of the static data on my ADO:

  Ado ado = ac.createAdo("C0.ISS.1");

  Value value1 = ado.load("C0_SRC25");
  assertThat(value1.toString(), is("ISS_STK"));
  Value value2 = ado.load("C0_I0276");
  assertThat(value2.toLong(), is(1L));
  Value value3 = ado.load("C0_I0015");
  assertThat(value3.toDouble(), is(0.0));

Please keep in mind that the Basic Java API for AC only has limited support for data types (integers, float and strings). Calling toString() on an attribute values of types like date, time and compound falls back onto the underlying implementation in the Asset Control Java API and may or may not produce the desired result.

If you would like an extended version of the Java API, please get in touch via email or on LinkedIn.

Updating static data

With regards to data, reading must be silver and writing must be gold. I will show you how to do this next:

  ado.set("C0_I0015", 1.5);;
  assertThat(ado.load("C0_I0015").toDouble(), is(1.5));

At this point I should mention an important distinction:

In memory vs. stored

The method set(attr, value) only sets the static value in memory. The stored attribute value in AC will not have changed yet.

Values that have been set with set(attr, value) can be retrieved with get(attr) immediately. The method load(attr) will always return the value stored in AC as well as update the in-memory copy of it.

You may want to read the following code carefully to see what I mean:

  // Value is 3.0 in AC
  assertThat(ado.load("C0_I0015").toDouble(), is(3.0));
  // Value is set to 99.0 in memory
  ado.set("C0_I0015", 99.0);
  assertThat(ado.get("C0_I0015").toDouble(), is(99.0));
  // However, value is still 3.0 in AC
  assertThat(ado.load("C0_I0015").toDouble(), is(3.0));
  // Need to set again as load has overwritten the previous 99.0
  ado.set("C0_I0015", 99.0);
  // Now we store back into AC;
  // Both get and load report the same value
  assertThat(ado.get("C0_I0015").toDouble(), is(99.0));
  assertThat(ado.load("C0_I0015").toDouble(), is(99.0));

What may first seem confusing allows you to collect a number of set() operations before storing the ADO back into AC (while performing the store() operation only once).

At the same time, there are situations where you want to quickly change a static value, both in memory and in AC at the same time. This is what setAndStore(attr, value) is for:

  ado.setAndStore("C0_I0015", 3.0);
  assertThat(ado.load("C0_I0015").toDouble(), is(3.0));

ADO Builder

Another convenient way of creating, populating and storing an ADO is using the builder pattern:

  Ado ado = ac.createAdo("C0.TFN.0001")
              .withLongname("TEST ADO")
              .withAttrValue("C0_SRC25", ValueFactory.createValue("ISS_STK"))
              .withAttrValue("C0_I0015", ValueFactory.createValue(4.5))

Thank you for reading.

In the next article I show you how to work with Timeseries Data.

comments powered by Disqus