Hi Synyster,
Thanks for the offer to help, I am a beginner with Niagara Java.
I have got this far and am able to get the info shown at the bottom of the post in my Application Director with the code below but I don't know what method I need to set the value of my Numeric Writable.
Here is my code...
public void onExecute() throws Exception
{
// execute code (set executeOnChange flag on inputs)
BOrd ordinSes = BOrd.make((String) getOrd()); //get the ORD from a string input pin
System.out.println("Ord = " + ordinSes);
double newVal = getNewValue(); get the value from a double input pin
System.out.println("New Value = " + newVal);
OrdTarget outValue = ordinSes.resolve();
BComponent bc = (BComponent)outValue.getComponent();
System.out.println("Ord Target = " + outValue);
BControlPoint bcp = (BControlPoint)outValue.get();
System.out.println("BControlPoint = " + bc);
Slot s = bc.getSlot("out");
System.out.println("slot = "+s);
BValue curVal = bcp.getOutStatusValue().getValueValue();
System.out.println("Current Value = "+ curVal);
BStatus curStatus = bcp.getOutStatusValue().getStatus();
System.out.println("Current Status = "+ curStatus);
Type type1 = bc.getType();
System.out.println("Type = "+ type1);
//bcp.setStatus(curStatus);
//bcp.set(BDouble.make(getValue()));
}
The lines I have commented out are my attempt to set the value to the Numeric Writable.
This is the output of the Application Director...
Ord = station:|h:22351
New Value = 11.0
Ord Target = station:|h:22351 -> 22.1 {ok} @ def
BControlPoint = 22.1 {ok} @ def
slot = out: baja:StatusNumeric
Current Value = 22.10
Current Status = {ok} @ def
Type = control:NumericWritable
What I intend to do is check if the vale has changed and if it has update my numeric writable, if not I will wait for the next update to the JSON inputs.
I also intend to check the status and if my target Numeric Writable is in Fault or Alarm set an output pin on my block to indicate this.
Again,
Thanks for any help you can provide me.
Added note: I have compiled my own basic modules and I would like to get this code into a module so if I need to do this in a different way please give me some simple pointers. Thanks.
Try this:
private BValue lastValue = null; // To store the last value
private boolean statusIndicator = false; // To track fault or alarm status
public void onExecute() throws Exception {
try {
// Retrieve the ORD from input pin
String ordInput = (String) getOrd(); // Replace with the actual method
BOrd ordinSes = BOrd.make(ordInput);
System.out.println("ORD: " + ordinSes);
// Retrieve the new value from input pin
double newVal = getNewValue(); // Replace with the actual method
System.out.println("New Value: " + newVal);
// Resolve the ORD to an OrdTarget
OrdTarget outValue = ordinSes.resolve();
BControlPoint bcp = (BControlPoint) outValue.get();
// Check if the value has changed
BValue curVal = bcp.getOutStatusValue().getValue();
if (lastValue == null || !lastValue.equals(curVal)) {
System.out.println("Value has changed. Updating Numeric Writable.");
// Update the Numeric Writable with the new value
bcp.set(BDouble.make(newVal));
// Update lastValue to the current value
lastValue = curVal;
} else {
System.out.println("Value has not changed. Waiting for the next update.");
}
// Check the status of the Numeric Writable
BStatus curStatus = bcp.getOutStatusValue().getStatus();
System.out.println("Current Status: " + curStatus);
// Update the status indicator if the target is in Fault or Alarm
if (curStatus.isFault() || curStatus.isAlarm()) {
statusIndicator = true;
setOutputPin("statusIndicator", true); // Update your block's output pin
System.out.println("Target is in Fault or Alarm. Status indicator set.");
} else {
statusIndicator = false;
setOutputPin("statusIndicator", false); // Clear the status indicator
System.out.println("Target is normal. Status indicator cleared.");
}
} catch (Exception e) {
System.err.println("Error during execution: " + e.getMessage());
e.printStackTrace();
}
}