Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
#9 Read single properyt service.
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Apr 20, 2021
1 parent 1ab9a19 commit e73ef1f
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package no.entra.bacnet.internal.property;

import no.entra.bacnet.internal.properties.PropertyIdentifier;
import no.entra.bacnet.objects.ObjectId;

import java.util.Objects;

public class ReadSinglePropertyResult {
private ObjectId objectId;
private PropertyIdentifier propertyIdentifier;
private Object value;
private Integer arrayIndexNumber = null;

public ReadSinglePropertyResult() {
}

public ReadSinglePropertyResult(ObjectId objectId, PropertyIdentifier propertyIdentifier, Object value) {
this.objectId = objectId;
this.propertyIdentifier = propertyIdentifier;
this.value = value;
}

public ObjectId getObjectId() {
return objectId;
}

public void setObjectId(ObjectId objectId) {
this.objectId = objectId;
}

public PropertyIdentifier getPropertyIdentifier() {
return propertyIdentifier;
}

public void setPropertyIdentifier(PropertyIdentifier propertyIdentifier) {
this.propertyIdentifier = propertyIdentifier;
}

public Object getValue() {
return value;
}

public void setValue(Object value) {
this.value = value;
}

public Integer getArrayIndexNumber() {
return arrayIndexNumber;
}

public void setArrayIndexNumber(Integer arrayIndexNumber) {
this.arrayIndexNumber = arrayIndexNumber;
}

public boolean isValid() {
return objectId != null && propertyIdentifier != null && value != null;
}

@Override
public String toString() {
return "ReadSinglePropertyResult{" +
"objectId=" + objectId +
", propertyIdentifier=" + propertyIdentifier +
", value=" + value +
", arrayIndexNumber=" + arrayIndexNumber +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ReadSinglePropertyResult that = (ReadSinglePropertyResult) o;
return Objects.equals(getObjectId(), that.getObjectId()) &&
getPropertyIdentifier() == that.getPropertyIdentifier() &&
Objects.equals(getValue(), that.getValue()) &&
Objects.equals(getArrayIndexNumber(), that.getArrayIndexNumber());
}

@Override
public int hashCode() {
return Objects.hash(getObjectId(), getPropertyIdentifier(), getValue(), getArrayIndexNumber());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package no.entra.bacnet.internal.property;

import no.entra.bacnet.internal.apdu.SDContextTag;
import no.entra.bacnet.internal.objects.ObjectIdMapper;
import no.entra.bacnet.internal.octet.OctetReader;
import no.entra.bacnet.internal.parseandmap.ParserResult;
import no.entra.bacnet.internal.properties.PropertyIdentifier;
import no.entra.bacnet.objects.ObjectId;
import no.entra.bacnet.octet.Octet;
import no.entra.bacnet.services.BacnetParserException;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

public class ReadSinglePropertyResultParser {
private static final Logger log = getLogger(ReadSinglePropertyResultParser.class);

public static ParserResult<ReadSinglePropertyResult> parse(String hexString) throws BacnetParserException {
ParserResult<ReadSinglePropertyResult> parserResult = new ParserResult();
parserResult.setInitialHexString(hexString);
OctetReader propertyReader = new OctetReader(hexString);

ReadSinglePropertyResult propertyResult = new ReadSinglePropertyResult();
//1 Read ObjectId
Octet sdContextTag0 = propertyReader.next();
if (!sdContextTag0.equals(SDContextTag.TAG0LENGTH4)) {
parserResult.setUnparsedHexString(propertyReader.unprocessedHexString());
parserResult.setErrorMessage("PropertyResult must start with SD-ContextTag 0. Value is: " + sdContextTag0);
parserResult.setParsedOk(false);
throw new BacnetParserException("PropertyResult must start with SD-ContextTag 0. Value is: " + sdContextTag0, parserResult);
}
String objectIdHexString = propertyReader.next(4);
ParserResult<ObjectId> objectIdResult = ObjectIdMapper.parse(objectIdHexString);
if (!objectIdResult.isParsedOk()) {
parserResult.setUnparsedHexString(propertyReader.unprocessedHexString());
parserResult.setErrorMessage("Could not parse required parameter ObjectId from :" + objectIdHexString);
parserResult.setParsedOk(false);
throw new BacnetParserException("Could not parse required parameter ObjectId.", parserResult);
} else {
propertyResult.setObjectId(objectIdResult.getParsedObject());
}

//2 Read Property Identifier
Octet sdContextTag1 = propertyReader.next();
if (!sdContextTag1.equals(SDContextTag.TAG1LENGTH1)) {
parserResult.setUnparsedHexString(propertyReader.unprocessedHexString());
parserResult.setErrorMessage("PropertyResult must have SD-ContextTag 1. Value is: " + sdContextTag1);
parserResult.setParsedOk(false);
throw new BacnetParserException("PropertyResult must have with SD-ContextTag 1. Value is: " + sdContextTag1, parserResult);
}
final Octet propertyIdentifierOctet = propertyReader.next();
PropertyIdentifier propertyIdentifier = PropertyIdentifier.fromOctet(propertyIdentifierOctet);
propertyResult.setPropertyIdentifier(propertyIdentifier);
parserResult.setParsedObject(propertyResult);
return parserResult;

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import no.entra.bacnet.internal.parseandmap.ParserResult;
import no.entra.bacnet.internal.properties.ReadObjectPropertiesResult;
import no.entra.bacnet.internal.properties.ReadObjectPropertiesResultParser;
import no.entra.bacnet.internal.property.ReadSinglePropertyResult;
import no.entra.bacnet.internal.property.ReadSinglePropertyResultParser;
import no.entra.bacnet.objects.ObjectId;
import no.entra.bacnet.properties.ReadPropertyMultipleResponse;
import no.entra.bacnet.properties.ReadPropertyMultipleService;
import no.entra.bacnet.property.ReadPropertyService;
import no.entra.bacnet.services.*;
import org.slf4j.Logger;

Expand Down Expand Up @@ -80,6 +83,10 @@ protected static ParserResult<Service> parseServiceExpectingReply(ConfirmedServi
try {
switch (serviceChoice) {

case ReadProperty:
ParserResult<ReadSinglePropertyResult> readSinglePropertyResult = ReadSinglePropertyResultParser.parse(hexString);
ReadPropertyService readPropertyService = new ReadPropertyService();
break;
case ReadPropertyMultiple:
ParserResult<ReadObjectPropertiesResult> readPropertyMultipleResult = ReadObjectPropertiesResultParser.parse(hexString);
if (readPropertyMultipleResult.isParsedOk()) {
Expand All @@ -99,6 +106,8 @@ protected static ParserResult<Service> parseServiceExpectingReply(ConfirmedServi
readPropertyMultipleService.setReadPropertyMultipleResponse(response);
parserResult.setParsedObject(readPropertyMultipleService);
parserResult.setParsedOk(true);
} else {
log.trace("Failed to parse ReadProperty or ReadPropertyMultiple. Result is {}. ServiceChoice: {}", readPropertyMultipleResult, serviceChoice);
}
break;
default:
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/no/entra/bacnet/property/ReadPropertyService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package no.entra.bacnet.property;

import no.entra.bacnet.BacnetRequest;
import no.entra.bacnet.services.Service;

public class ReadPropertyService extends BacnetRequest implements Service {
@Override
public String buildHexString() {
return null;
}

@Override
public boolean expectReply() {
return false;
}
}
4 changes: 4 additions & 0 deletions src/main/java/no/entra/bacnet/services/ServiceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package no.entra.bacnet.services;

public enum ServiceType {
}
28 changes: 28 additions & 0 deletions src/main/java/no/entra/bacnet/services/ServicesSupported.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package no.entra.bacnet.services;

import java.util.ArrayList;
import java.util.List;

public class ServicesSupported {

private final List<ServiceType> supportedServices;

public ServicesSupported() {
supportedServices = new ArrayList<>();
}

public ServicesSupported(List<ServiceType> supportedServices) {
this.supportedServices = supportedServices;
}
public void addSupportedService(ServiceType serviceType) {
supportedServices.add(serviceType);
}
public void removeSupportedService(ServiceType serviceType) {
if (isServiceSupported(serviceType)) {
supportedServices.remove(serviceType);
}
}
public boolean isServiceSupported(ServiceType serviceType) {
return supportedServices.contains(serviceType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package no.entra.bacnet.internal.property;

import no.entra.bacnet.device.DeviceId;
import no.entra.bacnet.internal.parseandmap.ParserResult;
import no.entra.bacnet.internal.properties.PropertyIdentifier;
import no.entra.bacnet.services.BacnetParserException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class ReadSinglePropertyResultParserTest {

@BeforeEach
void setUp() {
}

@Test
void parseSupportedServices() throws BacnetParserException {
String hexString = "0c0200000819613e850707000bc000f8003f";
ParserResult<ReadSinglePropertyResult> parserResult = ReadSinglePropertyResultParser.parse(hexString);
assertNotNull(parserResult);
assertEquals(new DeviceId(8),parserResult.getParsedObject().getObjectId());
assertEquals(PropertyIdentifier.ProtocolServicesSupported, parserResult.getParsedObject().getPropertyIdentifier());
}
}

0 comments on commit e73ef1f

Please sign in to comment.