Access and Use System Properties (-D defined properties) in a Script
This article shows you how to get the values of system properties which are defined on the command line using -Dpropertyname=value. There are other ways to set system parameters for MIX or TransSECS applications, and the code examples shown below will also work for these methods. In MIX you can add a parameter to the the mix.properties file, and for TransSECS deployments (and also for MIX applications running TransSECS code) you can add a parameter to ErgoTechConfiguration.properties.
System Properties
In java system properties can be defined on the command line using -Dpropertyname=value. This allows you to change a parameter value before running MIX or a TransSECS deployment. For example, for a specific deploymentyou may want to change the ip address of a plc by adding a parameter such as -Dipaddress=192.168.1.10 to the command line for MIX.
System properties are read as Strings. So if you have a variable in your application which is a numeric value you can do a numeric conversion on the String. Be sure to use a numeric value such as “0” as the property default in case the value is not defined on the commandline.
Access the Command Line Property Value
The general idea to access these properties is to use System.getProperty(“propertyname”,“optional default”)
var System = Java.type("java.lang.System"); ipAddress=System.getProperty("ipaddress","192.168.0.2"); //use a default value if the parameter "ipaddress" is not defined //now the ipAddress value can be used to set the ip address of a device server DeviceServer->setAddress(ipAddress); //note: setAddress method may be setHostname in some device servers
If the parameter is not defined you can check for null instead of using a default value
var System = Java.type("java.lang.System"); ipAddress=System.getProperty("ipaddress"); //ipAddress will be null if "ipaddress" is not defined if (ipAddress!=null) { DeviceServer->setAddress(ipAddress); }