forked from ericohm/Wifi-sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapproximate.py
66 lines (58 loc) · 2.01 KB
/
approximate.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
import math
import xml.etree.ElementTree as ET
import pickle
import glob, os
import time
import json
#Eric Ohman 24/7 2015
#This script takes in all our points for each macAddress and returns our best estimate of where that person is
#based on the signal strengths.
iteration = "default"
try:
signals = pickle.load(open(iteration+".p",'rb'))
except (OSError,IOError,NameError):
signals = {}
#Some out commented code, I chose to go with comparing the radius to eachoter instead of absolute distances.
#def getRadius2(x):
# logdistance = 1+math.log10(2.4*10**6)-187/20+2/5-x/20
# radius = 10**logdistance
# return min(radius,200)
#This function returns the radius, longitude and latitude for each signal.
#Wifi signal strength are given in dBM and -xdBM/(-x+1)dbm = 1.12.
#Therefor we give the radius in releation to the highest one in the list.
def getRadius(macAdress):
radius = []
sig = signals[macAdress]["Entries"]
theMax = float(-100)
for i in sig:
temp = [float(i["Signal"]),i["Latitude"],i["Longitude"]]
theMax = float(max(theMax,i["Signal"]))
radius.append(temp)
for i in radius:
i[0] = 1.12**(theMax-i[0])
return radius
#Simple function that returns our best estimate for a particular macAdress based on the signal strengths and coordinates.
def centerOfMass(macAdress):
points = getRadius(macAdress)
points.sort(key =lambda x: x[0])
points = points[0:min(3,len(points))]
lon = 0
lat = 0
m = float(0.0)
for i in points:
temp = float(i[0]**2)
lat = lat+i[1]/temp
lon = lon+i[2]/temp
m = m+1/temp
longitude = round(lon/m,6)
latitude = round(lat/m,6)
return {"Longitude":longitude, "Latitude":latitude}
#We add an estimate for every macAdress in signals.
for i in signals:
signals[i]["Estimate"] = centerOfMass(i)
#Save the signals so we can upload it via the app.js script.
pickle.dump(signals,open(iteration+".p","wb"))
jsonString = json.dumps(signals)
text_file = open("wifi_signals.txt", "w")
text_file.write(jsonString)
text_file.close()