Browse Source

updates

pull/1/head
Jeffrey Paul 6 years ago
parent
commit
5533ee1239
3 changed files with 79 additions and 70 deletions
  1. +31
    -0
      20150430.haproxy-ubuntumirror/haproxy.cfg
  2. +25
    -70
      geolocate/geolocate.py
  3. +23
    -0
      geolocate/weather.py

+ 31
- 0
20150430.haproxy-ubuntumirror/haproxy.cfg View File

@@ -0,0 +1,31 @@
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon

defaults
log global
mode http
option httplog
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http

listen ubuntumirror 127.0.0.1:9999
mode http
stats enable
stats uri /haproxy?stats
option httpclose
server try1 ber1.local:80 weight 256 check fall 1 inter 1s
server ubuntu-de de.archive.ubuntu.com:80 weight 1 observe layer4 backup

+ 25
- 70
geolocate/geolocate.py View File

@@ -5,85 +5,40 @@ import subprocess
import sys
import json
import re
import time
import datetime
import os

def main():
aps = nmcli2api(nmcli())
print aps
print geolocate(wifidata=aps)
ts = int(datetime.datetime.now().strftime("%s"))
loc = geolocate(wifidata=listAccessPoints())
loc['timestamp'] = ts
loc = json.dumps(loc)
path = os.path.expanduser('~/.data/location')
writeFile(os.path.join(path, 'latest.json'), loc)
writeFile(os.path.join(path, "%i.json" % ts), loc)

def writeFile(fn,content):
with open(fn,'w') as f:
f.write(content)

def geolocate(wifidata=None):
url = "https://location.services.mozilla.com/v1/geolocate?key=test"
postdata = { }
if wifidata:
postdata['wifiAccessPoints'] = wifidata
return None #requests.post(url, data=json.dumps(wifidata))
return requests.post(url, data=json.dumps(postdata)).json()

def _parse_nmcli(text):
lines = [
x for x in text.split('\n')
if re.match('^AP\[', x)
]
output = []
for line in lines:
m = re.match('AP\[(\d+)\]\.(\w+):(.*)',line)
if m:
output.append([
m.group(1),
m.group(2).lower(),
m.group(3)
])
a = {}
for item in output:
if not a.get(item[0]):
a[item[0]] = {}
a[item[0]][item[1]] = item[2]
return a.values()
def nmcli2api(aps):
"""
{
"macAddress": "01:23:45:67:89:AB",
"signalStrength": -65,
"age": 0,
"channel": 11,
"signalToNoiseRatio": 40
}
"""
output = []
for ap in aps:
x = {}
x['macAddress'] =
print ap

def nmcli():
cmd = "nmcli -t device list".split(' ')
def listAccessPoints():
bssids = []
cmd = "nmcli -t d list".split(' ')
output = subprocess.check_output(cmd)
return _parse_nmcli(output)
for line in output.split('\n'):
m = re.match('AP\[\d+\]\.BSSID\:(\S+)',line)
if m:
bssids.append(m.group(1))
if len(bssids) < 2:
return None
return [ {'macAddress': x} for x in bssids ]

sys.exit(main())

# fetched from wikipedia on 2015-04-27
# https://en.wikipedia.org/wiki/List_of_WLAN_channels
WIFI_FREQUENCIES = {
'2412': 1, '2417': 2, '2422': 3, '2427': 4, '2432': 5, '2437': 6,
'2442': 7, '2447': 8, '2452': 9, '2457': 10, '2462': 11, '2467': 12,
'2472': 13, '2484': 14, '3657.5': 131, '3660.0': 132, '3662.5': 132,
'3665.0': 133, '3667.5': 133, '3670.0': 134, '3672.5': 134, '3675.0': 135,
'3677.5': 135, '3680.0': 136, '3682.5': 136, '3685.0': 137, '3687.5': 137,
'3690.0': 138, '3692.5': 138, '5035': 7, '5040': 8, '5045': 9,
'5055': 11, '5060': 12, '5080': 16, '5170': 34, '5180': 36,
'5190': 38, '5200': 40, '5210': 42, '5220': 44, '5230': 46,
'5240': 48, '5260': 52, '5280': 56, '5300': 60, '5320': 64,
'5500': 100, '5520': 104, '5540': 108, '5560': 112, '5580': 116,
'5600': 120, '5620': 124, '5640': 128, '5660': 132, '5680': 136,
'5700': 140, '5745': 149, '5765': 153, '5785': 157, '5805': 161,
'5825': 165, '4915': 183, '4920': 184, '4925': 185, '4935': 187,
'4940': 188, '4945': 189, '4960': 192, '4980': 196,
}

def f2c(mhz):
channel = WIFI_FREQUENCIES.get(mhz)
if channel:
return channel
else:
raise ValueError("could not find channel")

+ 23
- 0
geolocate/weather.py View File

@@ -0,0 +1,23 @@
#!/usr/bin/env python

import requests
import os
import json

def getWeatherReports(lat=None, lon=None):
if lat is None or lon is None:
raise ValueError("need location to get weather")
host = "api.openweathermap.org"
path = "data/2.5/station/find?lat=%s&lon=%s" % ( lat, lon )
url = "http://%s/%s" % (host, path)
r = requests.get(url).json()
return r

def main():
reports = getWeatherReports(
lat=os.environ.get('LOCATION_LATITUDE'),
lon=os.environ.get('LOCATION_LONGITUDE')
)
print json.dumps(reports)

main()

Loading…
Cancel
Save