From 931e9fd256e9804e198997f95c68ebb89a0c9373 Mon Sep 17 00:00:00 2001 From: baardl Date: Mon, 9 Dec 2019 15:02:49 +0100 Subject: [PATCH] #2 Parsing Observation! --- .../no/entra/bacnet/json/BacNetParser.java | 4 +- .../no/entra/bacnet/json/Bacnet2Json.java | 48 +++++++++++++++++-- .../no/entra/bacnet/json/Observation.java | 25 +++++++++- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/main/java/no/entra/bacnet/json/BacNetParser.java b/src/main/java/no/entra/bacnet/json/BacNetParser.java index 0c16c27..9288493 100644 --- a/src/main/java/no/entra/bacnet/json/BacNetParser.java +++ b/src/main/java/no/entra/bacnet/json/BacNetParser.java @@ -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); @@ -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; diff --git a/src/main/java/no/entra/bacnet/json/Bacnet2Json.java b/src/main/java/no/entra/bacnet/json/Bacnet2Json.java index b520338..74d47c1 100644 --- a/src/main/java/no/entra/bacnet/json/Bacnet2Json.java +++ b/src/main/java/no/entra/bacnet/json/Bacnet2Json.java @@ -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; @@ -14,6 +15,8 @@ import java.util.HashMap; import java.util.Map; +import static no.entra.bacnet.json.Observation.*; + public class Bacnet2Json { @@ -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); } } } @@ -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; } @@ -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) { diff --git a/src/main/java/no/entra/bacnet/json/Observation.java b/src/main/java/no/entra/bacnet/json/Observation.java index b27db36..5bcf6af 100644 --- a/src/main/java/no/entra/bacnet/json/Observation.java +++ b/src/main/java/no/entra/bacnet/json/Observation.java @@ -1,5 +1,8 @@ package no.entra.bacnet.json; +import org.json.JSONObject; + +import java.io.Serializable; import java.time.LocalDateTime; /** @@ -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; @@ -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(); + } }