Hi all. I'm trying to build a simple proof-of-concept that retrieves a single analog value from a given device using bacnet4j. Although I have not confirmed this yet, I believe the device ID is 0 and the instance number of the value is 9.
The code first obtains and initializes a LocalDevice instance, then constructs a DeviceObjectPropertyReferences to hold the requested property. It then uses PropertyUtils to read the properties. Finally, the value is extracted as an Encodable and converted to a double.
Unfortunately, the code is not working as written - it fails with a timeout exception. I'm not sure if this is because I have the wrong broadcast address, network prefix length, or device ID, so I am trying to isolate the problem.
I am also wondering if this is the best way to read a specific value from a specific device. For example, I know the actual IP address of the device. Assuming that I have the correct device ID, is there a way to connect to the device directly (for example, one that does not require the broadcast or subnet address)?
The sample code and resulting stack trace are below. Thank you for your help.
Greg Brown
private static final int DEVICE_ID = 0;
private static final ObjectIdentifier OBJECT_IDENTIFIER = new ObjectIdentifier(ObjectType.analogValue, 9);
private static final long TIMEOUT = 5000;
public static void main(String[] args) throws Exception {
// Initialize local device
IpNetworkBuilder ipNetworkBuilder = new IpNetworkBuilder().withBroadcast(BROADCAST_ADDRESS, 24);
LocalDevice localDevice = new LocalDevice(ObjectIdentifier.UNINITIALIZED, new DefaultTransport(ipNetworkBuilder.build()));
localDevice.initialize();
// Request property
DeviceObjectPropertyReferences deviceObjectPropertyReferences = new DeviceObjectPropertyReferences();
deviceObjectPropertyReferences.add(DEVICE_ID, OBJECT_IDENTIFIER, PropertyIdentifier.presentValue);
DeviceObjectPropertyValues deviceObjectPropertyValues = PropertyUtils.readProperties(localDevice, deviceObjectPropertyReferences, null, TIMEOUT);
PropertyValues propertyValues = deviceObjectPropertyValues.getPropertyValues(DEVICE_ID);
// Extract property value
Encodable encodable = propertyValues.get(OBJECT_IDENTIFIER, PropertyIdentifier.presentValue);
double value = ((com.serotonin.bacnet4j.type.primitive.Double)encodable).doubleValue();
System.out.println(value);
}
11:17:27.638 [main] INFO com.serotonin.bacnet4j.LocalDevice - Waiting for incoming IAms
11:17:37.642 [main] INFO com.serotonin.bacnet4j.LocalDevice - Found 20 ids that are still available. Choosing 2911125
11:17:42.667 [pool-1-thread-1] INFO c.s.bacnet4j.util.PropertyUtils - Timeout while finding device 0
com.serotonin.bacnet4j.exception.BACnetTimeoutException: No response from instanceId 0
at com.serotonin.bacnet4j.util.RemoteDeviceFinder$DeviceFutureImpl.get(RemoteDeviceFinder.java:183)
at com.serotonin.bacnet4j.LocalDevice$2.get(LocalDevice.java:717)
at com.serotonin.bacnet4j.util.PropertyUtils.requestPropertiesFromDevice(PropertyUtils.java:207)
at com.serotonin.bacnet4j.util.PropertyUtils.lambda$readProperties$0(PropertyUtils.java:168)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Exception in thread "main" com.serotonin.bacnet4j.exception.PropertyValueException
at com.serotonin.bacnet4j.util.PropertyValues.get(PropertyValues.java:60)
at com.serotonin.bacnet4j.util.PropertyValues.get(PropertyValues.java:75)
at com.novapwr.agent.BACnetTest.main(BACnetTest.java:41)