From: rtrimana Date: Thu, 18 Oct 2018 22:07:06 +0000 (-0700) Subject: Adding a temporary cleanup for duplicate timestamps due to ON and OFF signatures... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e17cd016682e239e003693e47b9909ccedbe5f95;p=pingpong.git Adding a temporary cleanup for duplicate timestamps due to ON and OFF signatures having the same sequences (and thus the same event is detected and printed twice). --- diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/detection/SignatureDetector.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/detection/SignatureDetector.java index 64ed6ef..a0fdfe0 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/detection/SignatureDetector.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/detection/SignatureDetector.java @@ -11,6 +11,7 @@ import org.jgrapht.graph.SimpleDirectedWeightedGraph; import org.pcap4j.core.*; import java.time.Duration; +import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; @@ -67,8 +68,8 @@ public class SignatureDetector implements PacketListener, ClusterMatcher.Cluster final String onSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-onSignature-device-side.sig"; final String offSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-offSignature-device-side.sig"; // D-Link Plug PHONE signatures - //final String onSignatureFile = path + "/2018-07/dlink/onSignature-DLink-Plug-phone.sig"; - //final String offSignatureFile = path + "/2018-07/dlink/offSignature-DLink-Plug-phone.sig"; + //final String onSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-onSignature-phone-side.sig"; + //final String offSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-offSignature-phone-side.sig"; /* @@ -146,8 +147,14 @@ public class SignatureDetector implements PacketListener, ClusterMatcher.Cluster // Sort the list of detected events by timestamp to make it easier to compare it line-by-line with the trigger // times file. Collections.sort(detectedEvents, Comparator.comparing(UserAction::getTimestamp)); + + // Output the detected events - detectedEvents.forEach(outputter); + //detectedEvents.forEach(outputter); + + // TODO: Temporary clean up until we clean the pipeline + List cleanedDetectedEvents = SignatureDetector.removeDuplicates(detectedEvents); + cleanedDetectedEvents.forEach(outputter); } /** @@ -176,6 +183,31 @@ public class SignatureDetector implements PacketListener, ClusterMatcher.Cluster private final List mObservers = new ArrayList<>(); + /** + * Remove duplicates in {@code List} of {@code UserAction} objects. We need to clean this up for user actions + * that appear multiple times. + * TODO: This static method is probably just for temporary and we could get rid of this after we clean up + * TODO: the pipeline + * + * @param listUserAction A {@link List} of {@code UserAction}. + * + */ + public static List removeDuplicates(List listUserAction) { + + // Iterate and check for duplicates (check timestamps) + Set epochSecondSet = new HashSet<>(); + // Create a target list for cleaned up list + List listUserActionClean = new ArrayList<>(); + for(UserAction userAction : listUserAction) { + // Don't insert if any duplicate is found + if(!epochSecondSet.contains(userAction.getTimestamp().getEpochSecond())) { + listUserActionClean.add(userAction); + epochSecondSet.add(userAction.getTimestamp().getEpochSecond()); + } + } + return listUserActionClean; + } + public SignatureDetector(List>> searchedSignature, String routerWanIp) { // note: doesn't protect inner lists from changes :'( mSignature = Collections.unmodifiableList(searchedSignature);