Commit c6f368e9f2922fd1a09233cb518323a8d5064c3e
1 parent
ad076126
kppv runs
Showing
1 changed file
with
60 additions
and
11 deletions
Show diff stats
python/main_kppv.py
... | ... | @@ -14,6 +14,17 @@ import numpy as np; |
14 | 14 | import pandas as pd; |
15 | 15 | ############################################################################### |
16 | 16 | |
17 | +############################################################################### | |
18 | +#VARIABLES MESUREES | |
19 | +############################################################################### | |
20 | +ping_mesured=0; | |
21 | +rssi_mesured=0; | |
22 | +result=0; | |
23 | +puissance_read = 0; | |
24 | +temp_read = 0; | |
25 | +trame_read = []; | |
26 | +############################################################################### | |
27 | + | |
17 | 28 | ############################################################################## |
18 | 29 | #DEFINITION DE LA LECTURE DE PORT |
19 | 30 | ############################################################################## |
... | ... | @@ -58,27 +69,51 @@ dataset_rssi = np.concatenate((dataset_rssi_attack, dataset_rssi_norm), axis=0); |
58 | 69 | #KPPV PING |
59 | 70 | ############################################################################### |
60 | 71 | def prediction_ping(ping): |
61 | - dataset_ping_temp = []; | |
62 | 72 | cpt = 0; |
63 | 73 | k = 5; |
74 | + dataset_ping_temp = np.zeros((X_ping,1)); | |
64 | 75 | #Remplissage des distances euclidiennes |
65 | - for i in range(0, X_ping-1): | |
66 | - dataset_ping_temp[i][0] = ((dataset_ping[i][1]-ping)**2)**0.5; | |
76 | + for i in range(0, X_ping): | |
77 | + dataset_ping_temp[i,0] = ((dataset_ping[i,1]-ping)**2)**0.5; | |
67 | 78 | #Concatenation avec distances euclidiennes |
68 | 79 | dataset_ping_temp = np.c_[dataset_ping, dataset_ping_temp]; |
69 | 80 | #Tri |
70 | - np.sort(dataset_ping_temp, axis=2); | |
81 | + dataset_ping_temp = dataset_ping_temp[dataset_ping_temp[:,2].argsort()] | |
71 | 82 | #Checking des k premiers voisins |
83 | + print(dataset_ping_temp); | |
72 | 84 | for j in range(0, k-1): |
73 | - if(dataset_ping_temp[i][0]==1): | |
74 | - cpt = cpt+1; | |
85 | + if(dataset_ping_temp[j,0]==1): | |
86 | + cpt = cpt+1; | |
75 | 87 | if(cpt >= (k/2)+1): |
76 | 88 | return 1; #Attaque |
77 | 89 | else: |
78 | 90 | return 0; #RAS |
79 | - | |
80 | - | |
81 | -t = prediction_ping(1000); | |
91 | +############################################################################### | |
92 | + | |
93 | +############################################################################### | |
94 | +#KPPV RSSI | |
95 | +############################################################################### | |
96 | +def prediction_rssi(rssi): | |
97 | + cpt = 0; | |
98 | + k = 5; | |
99 | + dataset_rssi_temp = np.zeros((X_rssi,1)); | |
100 | + #Remplissage des distances euclidiennes | |
101 | + for i in range(0, X_rssi): | |
102 | + dataset_rssi_temp[i,0] = ((dataset_rssi[i,1]-rssi)**2)**0.5; | |
103 | + #Concatenation avec distances euclidiennes | |
104 | + dataset_rssi_temp = np.c_[dataset_rssi, dataset_rssi_temp]; | |
105 | + #Tri | |
106 | + dataset_rssi_temp = dataset_rssi_temp[dataset_rssi_temp[:,2].argsort()] | |
107 | + #Checking des k premiers voisins | |
108 | + for j in range(0, k-1): | |
109 | + if(dataset_rssi_temp[j,0]==1): | |
110 | + cpt = cpt+1; | |
111 | + if(cpt >= (k/2)+1): | |
112 | + return 1; #Attaque | |
113 | + else: | |
114 | + return 0; #RAS | |
115 | +############################################################################### | |
116 | + | |
82 | 117 | ############################################################################### |
83 | 118 | #AFFICHAGE |
84 | 119 | ############################################################################### |
... | ... | @@ -89,7 +124,10 @@ def Recorder(): |
89 | 124 | global puissance_read; |
90 | 125 | global trame_read; |
91 | 126 | global cpt_trames; |
92 | - global cpt_trames_temp; | |
127 | + global cpt_trames_temp; | |
128 | + cpt_trames = 0; | |
129 | + cpt_trames_temp = 0; | |
130 | + | |
93 | 131 | while(1): |
94 | 132 | #RECUPERATION DES DONNEES |
95 | 133 | x = ser.readline(); |
... | ... | @@ -114,7 +152,10 @@ def Recorder(): |
114 | 152 | #T2 |
115 | 153 | temp = t2; |
116 | 154 | |
117 | - if(cpt_trames_temp+1 == cpt_trames): | |
155 | + if(cpt_trames_temp + 1 == cpt_trames): | |
156 | + #BOOLEEN | |
157 | + state_ping = prediction_ping(ping_mesured); | |
158 | + state_rssi = prediction_rssi(puissance_read); | |
118 | 159 | #AFFICHAGE |
119 | 160 | print("\n###############################\n"); |
120 | 161 | print("TRAME : "+ "".join(map(chr, trame_read))+"\n"); |
... | ... | @@ -122,6 +163,14 @@ def Recorder(): |
122 | 163 | print("TEMPERATURE : "+str(temp_read)+" °C\n"); |
123 | 164 | print("PUISSANCE : "+str(puissance_read)+" dBm\n"); |
124 | 165 | print("PING : "+str(ping_mesured)+" ms\n"); |
166 | + if(state_ping == 0): | |
167 | + print("PING NORMAL\n"); | |
168 | + elif(state_ping == 1): | |
169 | + print("PING ATTAQUE\n"); | |
170 | + if(state_rssi == 0): | |
171 | + print("RSSI NORMAL\n"); | |
172 | + elif(state_rssi == 1): | |
173 | + print("RSSI ATTAQUE\n"); | |
125 | 174 | print("###############################\n"); |
126 | 175 | |
127 | 176 | cpt_trames_temp = cpt_trames; | ... | ... |