Java BACnet

Build.gradle

repositories {
    maven {
        url "https://maven.mangoautomation.net/repository/ias-release/"
    }
}
compile group: 'com.serotonin', name: 'bacnet4j', version: '4.0.1'

Read and Write Values

public class Main {

    private static LocalDevice localDevice;
    private static RemoteDevice remoteDevice;

    public static void main(String[] args) {
        try {

            IpNetwork network = new IpNetworkBuilder().
                    withBroadcast("192.168.1.255", 24).
                    build();

            Transport transport = new DefaultTransport(network);
            localDevice = new LocalDevice(6, transport);


            localDevice.initialize();

            remoteDevice = localDevice.getRemoteDeviceBlocking(6);

            DiscoveryUtils.getExtendedDeviceInformation(localDevice, remoteDevice);

            /*List oids = ((SequenceOf) RequestUtils.sendReadPropertyAllowNull(localDevice, remoteDevice,
                    remoteDevice.getObjectIdentifier(), PropertyIdentifier.objectList)).getValues();*/

            List<ObjectIdentifier> objectIdentifierList = new ArrayList<ObjectIdentifier>();

            /*for (Object obj : oids) {
                if (obj.getClass().equals(ObjectIdentifier.class)) {
                    ObjectIdentifier oid = (ObjectIdentifier) obj;

                    if (oid.getInstanceNumber() == 85) {
                        if (oid.getObjectType() == ObjectType.binaryOutput) {
                            objectIdentifierList.add(oid);
                        }
                    }
                }
            }*/

            ObjectIdentifier objectIdentifier = new ObjectIdentifier(ObjectType.binaryOutput, 85);
            objectIdentifierList.add(objectIdentifier);
            readPresentValues(objectIdentifierList);
            //readPresentValue(objectIdentifier);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void readPresentValues(List<ObjectIdentifier> oids) throws BACnetException {
        PropertyReferences references = new PropertyReferences();

        for (ObjectIdentifier oid : oids) {
            references.add(oid, PropertyIdentifier.presentValue);
        }

        PropertyValues values = RequestUtils.readProperties(localDevice, remoteDevice, references, null);

        for (ObjectIdentifier oid : oids) {
            System.out.println(values.getString(oid, PropertyIdentifier.presentValue));
        }
    }

    private static void readPresentValue(ObjectIdentifier oid) throws Exception {
        ReadPropertyRequest rpr = new ReadPropertyRequest(oid,
                PropertyIdentifier.presentValue);
        ReadPropertyAck ack = (ReadPropertyAck) localDevice.send(remoteDevice, rpr);
        System.out.println("Present value: " + ack.getValue());
    }

    private static void setPresentValue(ObjectIdentifier oid, Encodable value, int priority) throws Exception {
        WritePropertyRequest wpr = new WritePropertyRequest(oid,
                PropertyIdentifier.presentValue, null, value, new UnsignedInteger(priority));
        localDevice.send(remoteDevice, wpr);
    }

    private static void getPriorityArray(ObjectIdentifier oid) throws Exception {
        ReadPropertyRequest rpr = new ReadPropertyRequest(oid,
                PropertyIdentifier.priorityArray);
        ReadPropertyAck ack = (ReadPropertyAck) localDevice.send(remoteDevice, rpr);
        System.out.println("Priority array: " + ack.getValue());
    }
}

References
https://github.com/infiniteautomation/BACnet4J
https://www.csimn.com/CSI_pages/BACnet101.html
https://github.com/openmucextensions/bacnet/wiki

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.