summaryrefslogtreecommitdiff
path: root/files/common/sbin/geolocate
blob: c07cce351f68b8413bbe41d4ef51975a55671fd4 (plain)
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
#!/bin/sh -ex

device=wlan0-1
tmp=/tmp/geolocate_scratch

# obtain an exclusive lock
exec 666>>/tmp/lock/geolocate
flock -w 5 -x 666;

# list neighbour wifis
iw dev $device scan >$tmp.scan

# construct HTTP request
(   echo -n '{"request_address": true, "version": "1.1.0", "wifi_towers": ['
    egrep <$tmp.scan -o -e '^BSS [0-9a-f:]{17}' -e '^[[:space:]]SSID.*' -e '^[[:space:]]signal: [0-9.-]*' \
	| tr "\n\t" " " \
	| sed 's/BSS /\n/g' \
	| tail -n +2 \
	| cut -d' ' -f1,4,7- \
	| sed 's/^\([^ ]*\) \([^ ]*\) \(.*\) $/{"mac_address":"\1", "signal_strength":"\2", "ssid":"\3"}/' \
	| tr "\n" , \
	| sed 's/,$//'
    echo ']}'
) >$tmp.json
(   echo "POST /loc/json HTTP/1.0
Content-length: $(wc -c <$tmp.json)
"
    cat $tmp.json
) >$tmp.request

# consult the ooracle
nc www.google.com 80 <$tmp.request >$tmp.response

# parse the response
tail -n1 $tmp.response \
    | tr , "\n" \
    | egrep -o -e '"latitude":[0-9.]*' -e '"longitude":[0-9.]*' -e '"street":"[^"]*' \
    | sort \
    | egrep -o '[^:"]*$' \
    | {
	read lon
	read lat
	read street

# store result
	cat <<EOF
set system.position.lon=$lon
set system.position.lat=$lat
set system.position.street="$street"
EOF
	uci batch <<EOF
set system.position.lon=$lon
set system.position.lat=$lat
set system.position.street="$street"
EOF
}

# clean up
rm $tmp*
contact: Jan Huwald // Impressum