-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indoor_DeterministicBurstNoise_Server.py
234 lines (206 loc) · 5.79 KB
/
Indoor_DeterministicBurstNoise_Server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#Program untuk mengolah data yang baru didapat langsung dari RSSI_AP*.txt
#Data hasil pengolahan offline akan disimpan dalam sebuah file .txt, nanti progarm tinggal baca dari situ dulu
#program akan menerima bacaan dari alat dan kemudian memberikan balasan berupa persentase kemungkinan posisi yg ada
#Perbedaan program ini dengan yg Indoor_Deterministic_Server adalah disini sudah diterapkan rotasi dan
#bentuk file yang dibaca sudah berbeda dimana kita RSSI di 6 posisi akan ditulis di 4 arah berbeda (y[0])
import os.path
import paho.mqtt.client as mqtt
import numpy as np
import math
import itertools as itertools
AverageData_filename = "AverageData.txt"
Result_filename = "result.txt"
#Ref Data 6 posisi, 8 AP
totalPosition = 6
totalAP = 8
totalRotation = 4
RefData = [[[0 for x in range(totalPosition)] for z in range(totalRotation)] for y in range(totalAP)]
#Realtime data. 6 AP
RealtimeData = [0 for x in range(totalAP)]
#RealTimeRotation
#untuk menyimpan hasil perhitungan euclidian
SquaredDeltaSignalResult = [[0 for x in range(totalPosition)]for y in range(totalAP)]
#untuk menyimpan hasil euclidian
ResultEuclidian = [0 for x in range(0,20)]
#untuk menyimpan hasil posisi
ResultPosition = [0 for x in range(0,20)]
#arah kita
y = [0]
y[0] = 1
topic_AP = "otto/RSSI/#"
topic_AP1 = "otto/RSSI/AP1"
topic_AP2 = "otto/RSSI/AP2"
topic_AP3 = "otto/RSSI/AP3"
topic_AP4 = "otto/RSSI/AP4"
topic_AP5 = "otto/RSSI/AP5"
topic_AP6 = "otto/RSSI/AP6"
topic_AP7 = "otto/RSSI/AP7"
topic_AP8 = "otto/RSSI/AP8"
topicResult = "otto/result"
topic_Y = "otto/coor/y"
AP1_Data = [0]
AP2_Data = [0]
AP3_Data = [0]
AP4_Data = [0]
AP5_Data = [0]
AP6_Data = [0]
AP7_Data = [0]
AP8_Data = [0]
AP1_Data[0] = False
AP2_Data[0] = False
AP3_Data[0] = False
AP4_Data[0] = False
AP5_Data[0] = False
AP6_Data[0] = False
AP7_Data[0] = False
AP8_Data[0] = False
def on_connect(client, userdata, flags, rc):
print("Connected with result code" + str(rc))
client.subscribe([(topic_AP1,0),(topic_AP2,0),(topic_AP3,0), \
(topic_AP4,0),(topic_AP5,0),(topic_AP6,0),(topic_AP7,0),(topic_AP8,0),(topic_Y,0)])
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
def receive_Y_coor(client, userdata, message):
y[0] = int(message.payload)-1
def ReadExistingData():
readFile = open(AverageData_filename, "r")
i=0
k=0
comment = False
for line in readFile:
j=0
#remove \n from string
a = line.rstrip()
#split by \t
res = a.split("\t")
for x in res:
if(x=='#' or x==""):
comment = True
break
else:
RefData[i][k][j] = float(x)
j = j+1
if(comment==False):
i = i+1
if(i==7):
i=1
k+=1
else:
comment=False
def resetAP_Condition():
AP1_Data[0] = False
AP2_Data[0] = False
AP3_Data[0] = False
AP4_Data[0] = False
AP5_Data[0] = False
AP6_Data[0] = False
AP7_Data[0] = False
AP8_Data[0] = False
def euclidianDistance(num1, num2):
return (num1-num2)**2
def calculateAverageData(Data, j):
jumlahData= len(Data)
total = 0
for x in Data:
if(float(x)==0):
x=100.0
total += float(x)
avg = total/jumlahData
RealtimeData[j] = avg
return
def calculateAllPossibleCombination():
AP = [0,1,2,3,4,5]
combinar = 3
k=0
for item in itertools.combinations(AP,combinar):
currentExpectedPosition = 0
current_diftotal = 0
minimum_diftotal = 1000
for i in xrange(0,totalPosition):
for j in item:
current_diftotal += SquaredDeltaSignalResult[i][y[0]][j]
current_diftotal = math.sqrt(current_diftotal)
if(current_diftotal<minimum_diftotal):
minimum_diftotal = current_diftotal
currentExpectedPosition = i+1
ResultEuclidian[k] = minimum_diftotal
ResultPosition[k] = currentExpectedPosition
k+=1
def determinedProbabilityOfPosition():
textFile = open(Result_filename, "a")
textFile.write('\n')
textFile.write("Rotation: " + str(y[0]))
for i in ResultPosition:
textFile.write(str(i) + ",")
textFile.close()
for x in xrange(1,7):
p = ResultPosition.count(x)
result = p*100/20
messageResult = str(result) + "/" + str(x)
client.publish(topicResult, messageResult)
textFile = open(Result_filename, "a")
textFile.write('\n' + str(x) + ": " + '\t' + str(result) + "%")
textFile.close()
def calculatePosition():
#KONDISI INI DIGANTI BILA INGIN RUBAH DARI 6 JADI 8 AP!!
if(AP1_Data[0] == True and AP2_Data[0] == True and AP3_Data[0] == True and AP4_Data[0] == True and AP5_Data[0] == True and AP6_Data[0] == True):
textFile = open(Result_filename, "a")
textFile.write("\nBegin new session \n")
textFile.close()
i=0
for x in range(totalPosition):
j=0
total_dif=0
for x in range(totalAP):
dif = euclidianDistance(RefData[i][y[0]][j], RealtimeData[j])
SquaredDeltaSignalResult[i][j] = dif
j+=1
i+=1
calculateAllPossibleCombination()
determinedProbabilityOfPosition()
resetAP_Condition()
def GetData(client, userdata, message):
j=100
if message.topic == topic_AP1:
j=0
AP1_Data[0] = True
elif message.topic == topic_AP2:
j=1
AP2_Data[0] = True
elif message.topic == topic_AP3:
j=2
AP3_Data[0] = True
elif message.topic == topic_AP4:
j=3
AP4_Data[0] = True
elif message.topic == topic_AP5:
j=4
AP5_Data[0] = True
elif message.topic == topic_AP6:
j=5
AP6_Data[0] = True
else:
return 0
A = message.payload.rstrip()
Data = A.split("\t")
if(j!=100):
calculateAverageData(Data, j)
calculatePosition()
return 1
def writeTimestamp():
import time
import datetime
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
textFile = open(Result_filename, "a")
textFile.write("\ntimestamp: " + st + '\n')
textFile.close()
ReadExistingData()
writeTimestamp()
client = mqtt.Client()
client.message_callback_add(topic_AP, GetData)
client.message_callback_add(topic_Y, receive_Y_coor)
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)
client.loop_forever()