Skip to content

Commit

Permalink
Addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Aug 1, 2023
1 parent 0eaef9e commit 90675be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void onDetectedObject(DetectedObject detection) {
for (InfrastructureInstance instance : managedInstances.values()) {
if (instance.containsSensor(detection.getSensorId())) {
try {
instance.sendInteraction(encodeObjectDetection(detection));
instance.sendInteraction(encodeMsg(detection));
// Assuming each sensor would only ever be registered to a single infrastructure
// instance
break;
Expand All @@ -195,37 +195,17 @@ public void onDetectedObject(DetectedObject detection) {
}

/**
* Helper method to serialize time sychronization message to JSON and encode into bytes
* for eventual transmission via a Datagramsocket.
* Helper method to serialize message into JSON and encode as bytes.
*
* @param message time synchronization message to encode
* @return resulting bytes encoded from JSON string.
* @param message java object containing message information
* @return bytes encoded from JSON string representation of object.
*/
private byte[] encodeTimeMessage(InfrastructureTimeMessage message) {
return asJson(message).getBytes();
}

/**
* Helper method to serialize java Object into JSON via the {@link com.google.gson.Gson} object.
*
* @param obj to serialze into JSON string.
* @return resulting JSON string.
*/
private String asJson(Object obj) {
private byte[] encodeMsg(Object message) {
Gson gson = new Gson();
return gson.toJson(obj);
}
/**
* Helper method to serialize object detection message to JSON and encode into bytes
* for eventual transmission via a Datagramsocket.
*
* @param message object detection message to encode
* @return resulting bytes encoded from JSON string.
*/
private byte[] encodeObjectDetection(DetectedObject detection) {
return asJson(detection).getBytes();
return gson.toJson(message).getBytes();
}


/**
* This function is used to send out encoded timestep update to all registered
* instances the manager has on the managed instances map
Expand All @@ -240,7 +220,7 @@ public void onTimeStepUpdate(InfrastructureTimeMessage message) throws IOExcepti
}

for (InfrastructureInstance currentInstance : managedInstances.values()) {
currentInstance.sendTimeSyncMsg(encodeTimeMessage(message));
currentInstance.sendTimeSyncMsg(encodeMsg(message));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public static DetectionType fromLabel(String label) {
throw new IllegalArgumentException("Unknown DetectionType name " + label);
}

/**
* Returns string label associated with enum.
* @return
*/
public String getLabel(){
return label;
}
Expand Down

0 comments on commit 90675be

Please sign in to comment.