===== Creating VIB ValueObjects in Scripts =====
Value Objects are the most common variable type within MIStudio and TransSECS. A ValueObject contains not only the value, but also the type, the quality and the timestamp of the value.
The primary primitive types are, Long, String and Double Value Objects. Each has its own constructor:
var LongValueObject = Java.type("com.ergotech.vib.valueobjects.LongValueObject");
var intValue = new LongValueObject(3);
var boolValue = new LongValueObject(true);
var DoubleValueObject = Java.type("com.ergotech.vib.valueobjects.DoubleValueObject");
var value = new DoubleValueObject(17.1);
var StringValueObject = Java.type("com.ergotech.vib.valueobjects.StringValueObject");
var value = new StringValueObject("one");
var doubleValue = new StringValueObject("3.1415");
All value objects understand certain methods so you can call these without knowing the type.
value.getIntValue();
value.getLongValue();
value.getDoubleValue();
value.getStringValue();
value.getBoolValue();
Note that "getFloatValue" or "getDoubleValue" called on a StringValueObject that contains a string that cannot be converted to a floating point value will throw an exception. In the example above:
var StringValueObject = Java.type("com.ergotech.vib.valueobjects.StringValueObject");
var value = new StringValueObject("one");
var doubleValue = new StringValueObject("3.1415");
var real = doubleValue.getDoubleValue() // returns 3.1415
var fail = value.getDoubleValue(); // throws an exception.
====Setting Value Object values====
To set a value to a server (DataSource) in scripting, use set[ValueObjectType]Value(number or String). Note that the Server must be a "Manipulator" which has an input.
For example:
//set a Double (float) to a named Server
BroadcastServer->setDoubleValue(42.01);
Other set methods are for Int,Long,Boolean,Float,String, etc.
BroadcastServer->setLongValue(655031340);
BroadcastServer->setStringValue("Hello");
BroadcastServer->setFloatValue(1.3);
BroadcastServer->setBoolValue(true);
You can also use variables for the data.
var pi=java.lang.Math.PI;
BroadcastServer->setDoubleValue(pi);
===Booleans===
value.getBoolValue();
''**getBoolValue()**'' will return false if the value is zero, otherwise it will return true. For //StringValueObjects// if the value is numeric, the same rule applies. If the value is ''"true"'' or ''"yes"'' regardless of the case (upper or lower) then the call will return true. If the value is ''"false"'' or ''"no"'' the call will return false. If none of these apply, the method will throw an exception.
===Value Timestamp===
value.getDate ();
Will return a Java Date Object.
value.getTimeMillis();
Will return the time in milliseconds since Jan 01 1970. This is useful for time arithmetic.
===Data Quality===
ValueObjects always have a quality associated with them. The quality is an integer value.
value.getQuality();
The list of possible qualities is described here [[ValueObject Qualities]]
You can also set the quality of a value object:
value.setQuality(2000); // set to bad quality
====Complex Types - Arrays and Tables====
One dimensional arrays are stored in //"ArrayValueObjects"//. Two dimensional arrays, such as tables, are stored in //"TwoDimensionalValueObjects"//.
**Create an ArrayValueObject**. AVOs are used for items in ComboBoxes, data for single line graphs, and passing values to a popup component.
//This creates an AVO with four StringValueObject elements
var ArrayValueObject = Java.type("com.ergotech.vib.valueobjects.ArrayValueObject");
var StringValueObject = Java.type("com.ergotech.vib.valueobjects.StringValueObject");
avo = new ArrayValueObject();
avo.addElement(new StringValueObject("one"));
avo.addElement(new StringValueObject("two"));
avo.addElement(new StringValueObject("three"));
avo.addElement(new StringValueObject("four"));
avo;
====Create a TwoDimensionalValueObject====
TwoDVOs are used most often for tables and multi-line graphs.
//an example of creating a Two Dimensional Value Object with 4 rows of 6 items
//(each row containing column name, min,max,mean,median,std dev)
//first column is the x value
var TwoDimensionalValueObject = Java.type("com.ergotech.vib.valueobjects.TwoDimensionalValueObject");
var StringValueObject = Java.type("com.ergotech.vib.valueobjects.StringValueObject");
var DoubleValueObject = Java.type("com.ergotech.vib.valueobjects.DoubleValueObject");
columns = 6;
vo = new TwoDimensionalValueObject(columns);
//create rows for 2D Value Object
row1 = new java.util.Vector(6);
row2 = new java.util.Vector(6);
row3 = new java.util.Vector(6);
row4 = new java.util.Vector(6);
row1.insertElementAt(new StringValueObject("Engine 1"),0);
row2.insertElementAt(new StringValueObject("Engine 1 Bar"),0);
row3.insertElementAt(new StringValueObject("Engine 2"),0);
row4.insertElementAt(new StringValueObject("Engine 2 Bar"),0);
for (i=1;i<6;i++) {
r = (Math.random() * 100 ) * 1;
row1.insertElementAt(new DoubleValueObject(r+0),i);
row2.insertElementAt(new DoubleValueObject(r+5),i);
row3.insertElementAt(new DoubleValueObject(r+10),i);
row4.insertElementAt(new DoubleValueObject(r+20),i);
}
//print("Row 1 " + row1);
//print("Row 2 " + row2);
//create column labels -- do this last
vo.setColumnName("Column",0);
vo.setColumnName("Min",1);
vo.setColumnName("Max",2);
vo.setColumnName("Mean",3);
vo.setColumnName("Median",4);
vo.setColumnName("Std Dev",5);
vo.addRow(row1);
vo.addRow(row2);
vo.addRow(row3);
vo.addRow(row4);
output = vo;
====JavaScript JSON====
TransSECS devices will often use JSON to represent lists. Here's an example of a recipe list.
{ "values": [ "recipe1","recipe2", "recipe3", "recipe4"] }
In most cases, this data is translated internally in TransSECS and the result is a SECS type or ValueObject.
If the need arises to work with raw JSON, JavaScript provides the ability to parse the string to a JavaScript object.
JSON.parse(json);
Examples of converting from a Javascript object to Java types are available [[https://codereview.stackexchange.com/questions/90272/java-json-parsing-with-the-nashorn-api|here]].