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

Commit

Permalink
#2 Parsing Observation!
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Dec 9, 2019
1 parent b37ab6b commit 931e9fd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/java/no/entra/bacnet/json/BacNetParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private ReadAccessResult buildReadAccessResult(String apduHexString) {
}


public Observation buildObservation(String hexString) {
public static Observation buildObservation(String hexString) {
OctetReader apduReader = new OctetReader(hexString);
Octet pduTypeKey = apduReader.next();
PduType pduType = PduType.fromOctet(pduTypeKey);
Expand Down Expand Up @@ -92,7 +92,7 @@ public Observation buildObservation(String hexString) {
return observation;
}

String findListResultHexString(String hexString) {
static String findListResultHexString(String hexString) {
int listStartPos = hexString.indexOf(OBJECT_IDENTIFIER);
int listEndPos = hexString.indexOf(LIST_END_HEX);
String listResulHexString = null;
Expand Down
48 changes: 43 additions & 5 deletions src/main/java/no/entra/bacnet/json/Bacnet2Json.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.entra.bacnet.json;

import no.entra.bacnet.json.bvlc.Bvlc;
import no.entra.bacnet.json.bvlc.BvlcParser;
import no.entra.bacnet.json.bvlc.BvlcResult;
import no.entra.bacnet.json.npdu.Npdu;
Expand All @@ -14,6 +15,8 @@
import java.util.HashMap;
import java.util.Map;

import static no.entra.bacnet.json.Observation.*;

public class Bacnet2Json {


Expand All @@ -27,13 +30,14 @@ public static String hexStringToJson(String hexString) {
JSONObject bacnetJson = new JSONObject();
BvlcResult bvlcResult = BvlcParser.parse(hexString);
if (bvlcResult != null) {
Bvlc bvlc = bvlcResult.getBvlc();
NpduResult npduResult = NpduParser.parse(bvlcResult.getUnprocessedHexString());
if (npduResult != null) {
Npdu npdu = npduResult.getNpdu();
bacnetJson = addNetworkInfo(npdu);
Service service = ServiceParser.fromApduHexString(npduResult.getUnprocessedHexString());
if (service != null) {
bacnetJson = addServiceInfo(bacnetJson, service);
bacnetJson = addServiceInfo(bacnetJson, bvlc, npdu, service);
}
}
}
Expand All @@ -44,7 +48,7 @@ public static String hexStringToJson(String hexString) {
return bacnetMessage;
}

static JSONObject addServiceInfo(JSONObject bacnetJson, Service service) {
static JSONObject addServiceInfo(JSONObject bacnetJson, Bvlc bvlc, Npdu npdu, Service service) {
if (service == null) {
return null;
}
Expand All @@ -55,10 +59,44 @@ static JSONObject addServiceInfo(JSONObject bacnetJson, Service service) {
observationMap.put(OBSERVED_AT, LocalDateTime.now().toString());
JSONObject observationJson = new JSONObject(observationMap);
PduType pduType = service.getPduType();
if (pduType == PduType.ConfirmedRequest || pduType == PduType.UnconfirmedRequest ) {
bacnetJson.put(CONFIGURATION_REQUEST, observationJson);

switch (pduType) {
case ComplexAck:
Observation observation = BacNetParser.buildObservation(service.getUnprocessedHexString());
observationJson = buildObservationJson(bvlc, npdu, observation);
bacnetJson.put(OBSERVATION, observationJson);
break;
case ConfirmedRequest:
bacnetJson.put(CONFIGURATION_REQUEST, "{}");
break;
case UnconfirmedRequest:
bacnetJson.put(CONFIGURATION_REQUEST, "{}");
break;
default:
//do nothing
}
return bacnetJson.put(OBSERVATION, observationJson);

return bacnetJson;
}

static JSONObject buildObservationJson(Bvlc bvlc, Npdu npdu, Observation observation) {
if (observation == null) {
return null;
}

JSONObject json = new JSONObject();
json.put(ID, observation.getId());
if (npdu != null && npdu.isSourceAvailable()) {
String source = npdu.getSourceNetworkAddress().toString();
json.put(SOURCE, source);
}
json.put(VALUE, observation.getValue());
json.put(UNIT, observation.getUnit());
json.put(NAME, observation.getName());
json.put(DESCRIPTION, observation.getDescription());
json.put(OBSERVED_AT, observation.getObservedAt());

return json;
}

static JSONObject addNetworkInfo(Npdu npdu) {
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/no/entra/bacnet/json/Observation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package no.entra.bacnet.json;

import org.json.JSONObject;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
Expand All @@ -13,7 +16,14 @@
* "description": any string",
* "observedAt"
*/
public class Observation {
public class Observation implements Serializable {
public static final String ID = "id";
public static final String SOURCE = "source";
public static final String VALUE = "value";
public static final String UNIT = "unit";
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
public static final String OBSERVED_AT = "observedAt";
private String id;
private Source source;
private Object value;
Expand Down Expand Up @@ -116,4 +126,17 @@ public String toString() {
", observedAt=" + observedAt +
'}';
}

public String toJson() {
JSONObject json = new JSONObject();
json.put(ID, id);
json.put(SOURCE, source);
json.put(VALUE,value);
json.put(UNIT, unit);
json.put(NAME, name);
json.put(DESCRIPTION, description);
json.put(OBSERVED_AT, observedAt);

return json.toString();
}
}

0 comments on commit 931e9fd

Please sign in to comment.