// final String deviceIp = "192.168.1.246"; // .246 == phone; .199 == dlink plug?
// 2) TP-Link July 25 experiment
-// final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.pcap";
-// final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.pcap";
-// final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.timestamps";
-// final String deviceIp = "192.168.1.159";
+ final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.pcap";
+ final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.pcap";
+ final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.timestamps";
+ final String deviceIp = "192.168.1.159";
// 2b) TP-Link July 25 experiment TRUNCATED:
// Only contains "true local" events, i.e., before the behavior changes to remote-like behavior.
// Last included event is at July 25 10:38:11; file filtered to only include packets with arrival time <= 10:38:27.
- final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.truncated.pcap";
- final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.truncated.pcap";
- final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.truncated.timestamps";
- final String deviceIp = "192.168.1.159";
+// final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.truncated.pcap";
+// final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.truncated.pcap";
+// final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.truncated.timestamps";
+// final String deviceIp = "192.168.1.159";
// 3) SmartThings Plug July 25 experiment
// final String inputPcapFile = path + "/2018-07/stplug/stplug.wlan1.local.pcap";
});
});
- /*
+
// Print out all the pairs into a file for ON events
File fileOnEvents = new File(onPairsPath);
PrintWriter pwOn = null;
List<PcapPacket> tlsAppDataList = conv.getTlsApplicationDataPackets();
// Loop and print out packets
int count = 0;
+ // The direction of the first packet
+ Conversation.Direction firstDir = null;
+ // The length of the first packet
+ int firstLen = 0;
for (PcapPacket pcap : tlsAppDataList) {
boolean isPair = false;
if (count % 2 == 0) {
- pwOn.print(pcap.length() + ", ");
- //System.out.print(pcap.length() + ", ");
+ firstDir = conv.getDirection(pcap);
+ firstLen = pcap.length();
} else {// count%2 == 1
- isPair = true;
- pwOn.println(pcap.length());
- //System.out.println(pcap.length());
+ if(conv.getDirection(pcap) != firstDir) {
+ isPair = true;
+ pwOn.println(firstLen + ", " + pcap.length());
+ //System.out.println(firstDir + ", " + conv.getDirection(pcap));
+ //System.out.println(firstLen + ", " + pcap.length());
+ }
}
count++;
// If we can't create a pair then just pad it with 0
if (count == tlsAppDataList.size() && !isPair) {
- pwOn.println("0");
- //System.out.println("0");
+ pwOn.println(firstLen + ", 0");
+ }
+ }
+ } else { // Non-TLS conversations
+ List<PcapPacket> packetList = conv.getPackets();
+ // Loop and print out packets
+ int count = 0;
+ // The direction of the first packet
+ Conversation.Direction firstDir = null;
+ // The length of the first packet
+ int firstLen = 0;
+ for (PcapPacket pcap : packetList) {
+ System.out.println(pcap.length() + ", " + conv.getDirection(pcap));
+ boolean isPair = false;
+ if (count % 2 == 0) {
+ firstDir = conv.getDirection(pcap);
+ firstLen = pcap.length();
+ } else {// count%2 == 1
+ if(conv.getDirection(pcap) != firstDir) {
+ isPair = true;
+ pwOn.println(firstLen + ", " + pcap.length());
+ //System.out.println(firstDir + ", " + conv.getDirection(pcap));
+ //System.out.println(firstLen + ", " + pcap.length());
+ }
+ }
+ count++;
+ // If we can't create a pair then just pad it with 0
+ if (count == packetList.size() && !isPair) {
+ pwOn.println(firstLen + ", 0");
}
}
}
List<PcapPacket> tlsAppDataList = conv.getTlsApplicationDataPackets();
// Loop and print out packets
int count = 0;
+ // The direction of the first packet
+ Conversation.Direction firstDir = null;
+ // The length of the first packet
+ int firstLen = 0;
for (PcapPacket pcap : tlsAppDataList) {
boolean isPair = false;
if (count % 2 == 0) {
- pwOff.print(pcap.length() + ", ");
- //System.out.print(pcap.length() + ", ");
+ firstDir = conv.getDirection(pcap);
+ firstLen = pcap.length();
} else {// count%2 == 1
- isPair = true;
- pwOff.println(pcap.length());
- //System.out.println(pcap.length());
+ if(conv.getDirection(pcap) != firstDir) {
+ isPair = true;
+ pwOff.println(firstLen + ", " + pcap.length());
+ System.out.println(firstDir + ", " + conv.getDirection(pcap));
+ System.out.println(firstLen + ", " + pcap.length());
+ }
}
count++;
// If we can't create a pair then just pad it with 0
if (count == tlsAppDataList.size() && !isPair) {
- pwOff.println("0");
- //System.out.println("0");
+ pwOff.println(firstLen + ", 0");
+ }
+
+ }
+ } else { // Non-TLS conversations
+ List<PcapPacket> packetList = conv.getPackets();
+ // Loop and print out packets
+ int count = 0;
+ // The direction of the first packet
+ Conversation.Direction firstDir = null;
+ // The length of the first packet
+ int firstLen = 0;
+ for (PcapPacket pcap : packetList) {
+ System.out.println(pcap.length() + ", " + conv.getDirection(pcap));
+ boolean isPair = false;
+ if (count % 2 == 0) {
+ firstDir = conv.getDirection(pcap);
+ firstLen = pcap.length();
+ } else {// count%2 == 1
+ if(conv.getDirection(pcap) != firstDir) {
+ isPair = true;
+ pwOff.println(firstLen + ", " + pcap.length());
+ System.out.println(firstDir + ", " + conv.getDirection(pcap));
+ System.out.println(firstLen + ", " + pcap.length());
+ }
+ }
+ count++;
+ // If we can't create a pair then just pad it with 0
+ if (count == packetList.size() && !isPair) {
+ pwOff.println(firstLen + ", 0");
}
}
}
}
}
pwOff.close();
- */
+
// ================================================================================================
// <<< Some work-in-progress/explorative code that extracts a "representative" sequence >>>
--- /dev/null
+from sklearn.cluster import DBSCAN
+from sklearn import metrics
+import matplotlib.cm as cm
+import numpy as np
+import matplotlib.pyplot as plt
+
+# Create a subplot with 1 row and 2 columns
+fig, (ax2) = plt.subplots(1, 1)
+fig.set_size_inches(7, 7)
+
+
+# Read from file
+# TODO: Just change the following path and filename
+# when needed to read from a different file
+path = "/scratch/July-2018/Pairs/"
+device1 = "tplink-bulb-on"
+device2 = "tplink-bulb-off"
+filename1 = device1 + ".txt"
+filename2 = device2 + ".txt"
+
+# Number of triggers
+trig = 50
+
+# PLOTTING FOR DEVICE ON EVENT
+# Read and create an array of pairs
+with open(path + filename1, "r") as pairs:
+ pairsArr = []
+ for line in pairs:
+ # We will see a pair and we need to split it into xpoint and ypoint
+ xpoint, ypoint = line.split(", ")
+ pair = [int(xpoint), int(ypoint)]
+ pairsArr.append(pair)
+
+# Formed array of pairs
+#print(pairsArr)
+X = np.array(pairsArr);
+
+# Compute DBSCAN
+# eps = distances
+# min_samples = minimum number of members of a cluster
+db = DBSCAN(eps=30, min_samples=trig - 5).fit(X)
+core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
+core_samples_mask[db.core_sample_indices_] = True
+labels = db.labels_
+
+# Number of clusters in labels, ignoring noise if present.
+n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
+#print('Estimated number of clusters: %d' % n_clusters_)
+
+# Black removed and is used for noise instead.
+unique_labels = set(labels)
+#print("Labels: " + str(labels))
+
+colors = [plt.cm.Spectral(each)
+ for each in np.linspace(0, 1, len(unique_labels))]
+for k, col in zip(unique_labels, colors):
+ if k == -1:
+ # Black used for noise.
+ col = [0, 0, 0, 1]
+
+ class_member_mask = (labels == k)
+
+ print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
+ xy = X[class_member_mask & core_samples_mask]
+ plt.plot(xy[:, 0], xy[:, 1], 'o',
+ markeredgecolor='k', markersize=10)
+
+ xy = X[class_member_mask & ~core_samples_mask]
+ plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
+ markeredgecolor='k', markersize=6)
+
+count = 0
+for pair in pairsArr:
+ plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
+ "\nFreq: " + str(labels.tolist().count(labels[count])), fontsize=10)
+ count = count + 1
+
+#====================================================================================================
+
+# PLOTTING FOR DEVICE ON EVENT
+# Read and create an array of pairs
+with open(path + filename2, "r") as pairs:
+ pairsArr = []
+ for line in pairs:
+ # We will see a pair and we need to split it into xpoint and ypoint
+ xpoint, ypoint = line.split(", ")
+ pair = [int(xpoint), int(ypoint)]
+ pairsArr.append(pair)
+
+# Formed array of pairs
+#print(pairsArr)
+X = np.array(pairsArr);
+
+# Compute DBSCAN
+# eps = distances
+# min_samples = minimum number of members of a cluster
+db = DBSCAN(eps=10, min_samples=trig - 5).fit(X)
+core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
+core_samples_mask[db.core_sample_indices_] = True
+labels = db.labels_
+
+# Number of clusters in labels, ignoring noise if present.
+n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
+#print('Estimated number of clusters: %d' % n_clusters_)
+
+import matplotlib.pyplot as plt
+
+# Black removed and is used for noise instead.
+unique_labels = set(labels)
+#print("Labels: " + str(labels))
+
+colors = [plt.cm.Spectral(each)
+ for each in np.linspace(0, 1, len(unique_labels))]
+for k, col in zip(unique_labels, colors):
+ if k == -1:
+ # Black used for noise.
+ col = [0, 0, 0, 1]
+
+ class_member_mask = (labels == k)
+
+ print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
+ xy = X[class_member_mask & core_samples_mask]
+ plt.plot(xy[:, 0], xy[:, 1], 'o',
+ markeredgecolor='k', markersize=10)
+
+ xy = X[class_member_mask & ~core_samples_mask]
+ plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
+ markeredgecolor='k', markersize=6)
+
+count = 0
+for pair in pairsArr:
+ plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
+ "\nFreq: " + str(labels.tolist().count(labels[count])), fontsize=10)
+ count = count + 1
+
+
+
+plt.title(device1 + ' & ' + device2 + ' - Estimated number of clusters: %d' % n_clusters_)
+plt.show()
+
+
--- /dev/null
+from sklearn.cluster import DBSCAN
+from sklearn import metrics
+import matplotlib.cm as cm
+import numpy as np
+import matplotlib.pyplot as plt
+
+# Create a subplot with 1 row and 2 columns
+fig, (ax2) = plt.subplots(1, 1)
+fig.set_size_inches(7, 7)
+
+
+# Read from file
+# TODO: Just change the following path and filename
+# when needed to read from a different file
+path = "/scratch/July-2018/Pairs/"
+device = "dlink-siren-on"
+filename = device + ".txt"
+
+# Number of triggers
+trig = 50
+
+# Read and create an array of pairs
+with open(path + filename, "r") as pairs:
+ pairsArr = []
+ for line in pairs:
+ # We will see a pair and we need to split it into xpoint and ypoint
+ xpoint, ypoint = line.split(", ")
+ pair = [int(xpoint), int(ypoint)]
+ pairsArr.append(pair)
+
+# Formed array of pairs
+#print(pairsArr)
+X = np.array(pairsArr);
+
+# Compute DBSCAN
+# eps = distances
+# min_samples = minimum number of members of a cluster
+db = DBSCAN(eps=10, min_samples=trig - 5).fit(X)
+core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
+core_samples_mask[db.core_sample_indices_] = True
+labels = db.labels_
+
+# Number of clusters in labels, ignoring noise if present.
+n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
+
+print('Estimated number of clusters: %d' % n_clusters_)
+
+import matplotlib.pyplot as plt
+
+# Black removed and is used for noise instead.
+unique_labels = set(labels)
+print("Labels: " + str(labels))
+
+colors = [plt.cm.Spectral(each)
+ for each in np.linspace(0, 1, len(unique_labels))]
+for k, col in zip(unique_labels, colors):
+ if k == -1:
+ # Black used for noise.
+ col = [0, 0, 0, 1]
+
+ class_member_mask = (labels == k)
+
+ xy = X[class_member_mask & core_samples_mask]
+ plt.plot(xy[:, 0], xy[:, 1], 'o',
+ markeredgecolor='k', markersize=10)
+
+ xy = X[class_member_mask & ~core_samples_mask]
+ plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
+ markeredgecolor='k', markersize=6)
+
+count = 0
+for pair in pairsArr:
+ #if labels[count] != -1:
+ # If this is not a noise (i.e.,real data)
+ # plt.text(pair[0], pair[1], "Freq: " + str(labels.tolist().count(labels[count])), fontsize=10)
+
+ plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
+ "\nFreq: " + str(labels.tolist().count(labels[count])), fontsize=10)
+ count = count + 1
+
+
+plt.title(device + ' - Estimated number of clusters: %d' % n_clusters_)
+plt.show()
+
+
# TODO: Just change the following path and filename
# when needed to read from a different file
path = "/scratch/July-2018/Pairs/"
-filename = "alexa-off.txt"
+filename = "dlink-off.txt"
# Read and create an array of pairs
with open(path + filename, "r") as pairs:
#print(pairsArr)
X = np.array(pairsArr);
-clusters = 25
+clusters = 6
# Plot the data points based on the clusters
clusterer = KMeans(n_clusters=clusters, random_state=10)
cluster_labels = clusterer.fit_predict(X)
# 2nd Plot showing the actual clusters formed
colors = cm.nipy_spectral(cluster_labels.astype(float) / clusters)
-ax2.scatter(X[:, 0], X[:, 1], marker='o', s=100, lw=0, alpha=0.3,
+ax2.scatter(X[:, 0], X[:, 1], marker='o', s=50, lw=0, alpha=0.3,
c=colors, edgecolor='k')
# Labeling the clusters