1.引入jar包
<dependency>
<groupId>bacnet4jgroupId>
<artifactId>bacnet4jartifactId>
<version>5.0.2version>
dependency>
<dependency>
<groupId>ai.serotonin.ossgroupId>
<artifactId>sero-warpartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>ai.serotonin.ossgroupId>
<artifactId>sero-schedulerartifactId>
<version>1.1.0version>
dependency>
<dependency>
<groupId>org.mockitogroupId>
<artifactId>mockito-coreartifactId>
<version>2.7.22version>
<scope>testscope>
dependency>
2.创建工具类
package com.dpx.base.util.BACnet;
import java.util.Arrays;
import java.util.List;
import com.serotonin.bacnet4j.LocalDevice;
import com.serotonin.bacnet4j.RemoteDevice;
import com.serotonin.bacnet4j.exception.BACnetException;
import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder;
import com.serotonin.bacnet4j.transport.DefaultTransport;
import com.serotonin.bacnet4j.type.Encodable;
import com.serotonin.bacnet4j.type.enumerated.ObjectType;
import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
import com.serotonin.bacnet4j.type.enumerated.RestartReason;
import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
import com.serotonin.bacnet4j.type.primitive.UnsignedInteger;
import com.serotonin.bacnet4j.util.PropertyValues;
import com.serotonin.bacnet4j.util.ReadListener;
import com.serotonin.bacnet4j.util.RequestUtils;
public class BACnetUtil {
public static IpNetwork initIpNetwork(String ip, int port) {
IpNetwork ipNetwork = new IpNetworkBuilder()
.withLocalBindAddress(ip)
.withSubnet("255.255.255.0", 24)
.withPort(port)
.withReuseAddress(true)
.build();
return ipNetwork;
}
public static LocalDevice initLocalDevice(int deviceNumber, IpNetwork ipNetwork) {
LocalDevice localDevice = new LocalDevice(deviceNumber, new DefaultTransport(ipNetwork));
try {
localDevice.initialize();
localDevice.startRemoteDeviceDiscovery();
return localDevice;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
BACnetUtil test = new BACnetUtil();
IpNetwork ipNetwork = initIpNetwork("172.16.10.204", 47808);
LocalDevice localDevice = initLocalDevice(123, ipNetwork);
try {
RemoteDevice remoteDevice = localDevice.getRemoteDeviceBlocking(3202835);
List<ObjectIdentifier> objectList = RequestUtils.getObjectList(localDevice, remoteDevice).getValues();
for(int i=1;i<objectList.size();i++){
String name=((Encodable)RequestUtils.getProperty(localDevice, remoteDevice ,objectList.get(i), PropertyIdentifier.objectName)).toString();
System.out.println(name);
getSplitheatrecoveryfreshairconditioningunit(name,localDevice,remoteDevice,objectList.get(i));
}
Thread.sleep(3000);
localDevice.terminate();
} catch (Exception e) {
e.printStackTrace();
if (localDevice != null) {
localDevice.terminate();
}
}
}
public static void getFan(String name,LocalDevice localDevice,RemoteDevice remoteDevice,ObjectIdentifier objectIdentifier){
if("EFMAS".equals(name)){
try {
String EFMAS = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
if("inactive".equals(EFMAS)){
System.err.println("手动");
}else {
System.err.println("自动");
}
} catch (BACnetException e) {
e.printStackTrace();
}
}else if("SFRS".equals(name)){
try {
String SFRS= ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
if("inactive".equals(SFRS)){
System.err.println("手动");
}else {
System.err.println("自动");
}
} catch (BACnetException e) {
e.printStackTrace();
}
}
}
public static void getFreshairconditioningunit(String name,LocalDevice localDevice,RemoteDevice remoteDevice,ObjectIdentifier objectIdentifier){
if("SFtemp".equals(name)){
try {
String SFtemp = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
System.err.println(SFtemp);
} catch (BACnetException e) {
e.printStackTrace();
}
}else if("SFRS".equals(name)){
try {
String SFRS= ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
if("inactive".equals(SFRS)){
System.err.println("停止");
}else {
System.err.println("启动");
}
} catch (BACnetException e) {
e.printStackTrace();
}
}
}
public static void getCombinedheatrecoveryairconditioningunit(String name,LocalDevice localDevice,RemoteDevice remoteDevice,ObjectIdentifier objectIdentifier){
if("AHUHFTEMP_AV".equals(name)){
try {
String AHUHFTEMP_AV = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
System.err.println(AHUHFTEMP_AV);
} catch (BACnetException e) {
e.printStackTrace();
}
}else if("AHUSFTEMP_AV".equals(name)){
try {
String AHUSFTEMP_AV = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
System.err.println(AHUSFTEMP_AV);
} catch (BACnetException e) {
e.printStackTrace();
}
}else if("FanSFRS".equals(name)){
try {
String FanSFRS= ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
if("inactive".equals(FanSFRS)){
System.err.println("手动");
}else {
System.err.println("自动");
}
} catch (BACnetException e) {
e.printStackTrace();
}
}else if("AHUHFSHIDU_AV".equals(name)){
try {
String AHUHFSHIDU_AV = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
System.err.println(AHUHFSHIDU_AV);
} catch (BACnetException e) {
e.printStackTrace();
}
}
}
public static void getSplitheatrecoveryfreshairconditioningunit(String name,LocalDevice localDevice,RemoteDevice remoteDevice,ObjectIdentifier objectIdentifier){
if("EFtemp".equals(name)){
try {
String EFtemp = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
System.err.println(EFtemp);
} catch (BACnetException e) {
e.printStackTrace();
}
}else if("SFtemp".equals(name)){
try {
String SFtemp = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
System.err.println(SFtemp);
} catch (BACnetException e) {
e.printStackTrace();
}
}else if("RSfan".equals(name)){
try {
String RSFAN = ((Encodable) RequestUtils.getProperty(localDevice, remoteDevice ,objectIdentifier, PropertyIdentifier.presentValue)).toString();
if("inactive".equals(RSFAN)){
System.err.println("手动");
}else {
System.err.println("自动");
}
} catch (BACnetException e) {
e.printStackTrace();
}
}
}
}