ThingWorx Edge Java SDK > Application Details > Processing Properties in Your Application
Processing Properties in Your Application
This topic provides samples from the SteamSensor application provided with the SDK to show you how you might process data in your application. Click a section title below to display its content:
Setting Up the Steam Sensor VirtualThing (SteamThing) 
The following code from SteamThing.java simulates a Steam Sensor, by extending the FileTransferVirtualThing class:

public class SteamThing extends FileTransferVirtualThing implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(SteamThing.class);
private final Appender<ILoggingEvent> fileLogAppender;
private double _totalFlow = 0.0;
private Thread _shutdownThread = null;
private int counter = 0;
private boolean readyToSend = false;
private boolean requestedValuesFromThingworx = false;
private final static String TEMPERATURE_FIELD = "OutsideTemperature";
private final static String SENSOR_NAME_FIELD = "SensorName";
private final static String ACTIVE_TIME_FIELD = "ActivationTime";
private final static String PRESSURE_FIELD = "BarometricPressure";
private final static String FAULT_STATUS_FIELD = "CurrentFaultStatus";
private final static String INLET_VALVE_FIELD = "CurrentInletValve";
private final static String TEMPERATURE_LIMIT_FIELD = "RatedTemperatureLimit";
private final static String TOTAL_FLOW_FIELD = "TotalFlowAmount";
private final File logDirectory;
Implementing the processScanRequest 
The processScanRequest is called by the SteamSensorClient for every scan cycle to get initial settings from the platform and then scan the device for new settings. This example is from the SteamThing.java file:

// The processScanRequest is called by the SteamSensorClient every scan cycle
@Override
public void processScanRequest() throws Exception {

// Execute the code for this simulation every scan
if(readyToSend) {
try {
this.fetchInitialSettingsFromServer();
}
catch(Exception e){
LOG.info("Could not read initial setting from the server");
}

this.scanDevice();
}
}
Requesting Initial Property Values on Startup 
If property values on the ThingWorx Platform are used as part of the Agent configuration, you should request initial values from the platform on startup, using code similar to this example from the SteamThing.java file:

private void fetchInitialSettingsFromServer() {
// Some properties values on the thingworx server may be being used as part of this
// Agent's configuration. If this is the case therre initial values should be requested
// on startup
if(!this.requestedValuesFromThingworx){

this.requestedValuesFromThingworx=true;
try {
InfoTable result = getClient().readProperty(RelationshipTypes.ThingworxEntityTypes.Things, getName(), "TemperatureLimit", 10000);
Double temperatureLimit = (Double)result.getFirstRow().getValue("TemperatureLimit");
setProperty("TemperatureLimit",temperatureLimit);
}
catch(Exception e){
LOG.info("Could not read initial setting from the server");
return;
}
}
}
Checking for and Processing Faults During Scan Cycle 
The following code from SteamThing.java performs the logic for the SteamSensor for every scan cycle. Note that the processing includes checking for a fault and processing it for the TemperatureLimit property:

// Performs the logic for the steam sensor, occurs every scan cycle
public void scanDevice() throws Exception {
++counter;

if ((counter % 1) == 0) {
// Set the Temperature property value in the range of 400-440
double temperature = 400 + 40 * Math.random();
super.setProperty("Temperature", temperature);

// Get the TemperatureLimmit property value from memory
double temperatureLimit =
(Double) getProperty("TemperatureLimit").getValue().getValue();

// Set the FaultStatus property value if the TemperatureLimit x
// if ((counter % 3) == 0) {
// Add a random double value from 0.0-1.0 to the total flow
this._totalFlow += Math.random();

// Set the InletValve property value to true by default
boolean inletValveStatus = true;

// If the current second value is divisible by 15, set the
// InletValve property value to false
int seconds = DateTime.now().getSecondOfMinute();
if ((seconds % 15) == 0)
inletValveStatus = false;


super.setProperty("TotalFlow", _totalFlow);
super.setProperty("InletValve", inletValveStatus);

counter = 0;
}zand it is greater than zero
boolean faultStatus = false;

if (temperatureLimit > 0 && temperature > temperatureLimit)
faultStatus = true;

// If the sensor has a fault...
if (faultStatus) {
// Get the previous value of the fault from the property
// This is the current value because it hasn't been set yet
// This is done because we don't want to send the event every
// time it enters the fault state, only send the fault on
// the transition from non-faulted to faulted
boolean previousFaultStatus =
(Boolean) getProperty("FaultStatus").getValue().getValue();

// If the current value is not faulted, then create and queue the event
if (!previousFaultStatus) {
// Set the event information of the defined data shape for the event
ValueCollection eventInfo = new ValueCollection();
eventInfo.put(CommonPropertyNames.PROP_MESSAGE,
new StringPrimitive("Temperature at " + temperature
+ " was above limit of " + temperatureLimit));
// Queue the event
super.queueEvent("SteamSensorFault", DateTime.now(), eventInfo);
}
}

// Set the fault status property value
super.setProperty("FaultStatus", faultStatus);
if(++routeIndex==route.length)
routeIndex=0;
setPropertyValue("Location", route[routeIndex]);

}

if ((counter % 2) == 0) {
// Set the Pressure property value in the range of 18-23
double pressure = 18 + 5 * Math.random();
// Set the property values
super.setProperty("Pressure", pressure);
}
if ((counter % 3) == 0) {
// Add a random double value from 0.0-1.0 to the total flow
this._totalFlow += Math.random();

// Set the InletValve property value to true by default
boolean inletValveStatus = true;

// If the current second value is divisible by 15, set the InletValve
// property value to false
int seconds = DateTime.now().getSecondOfMinute();
if ((seconds % 15) == 0)
inletValveStatus = false;


super.setProperty("TotalFlow", _totalFlow);
super.setProperty("InletValve", inletValveStatus);

counter = 0;
}
Was this helpful?