From 5533ee1239b4fa12f0ee0041a3f9b6e6088dbe64 Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Sun, 24 May 2015 07:10:22 +0200 Subject: [PATCH] updates --- 20150430.haproxy-ubuntumirror/haproxy.cfg | 31 ++++++++ geolocate/geolocate.py | 95 ++++++----------------- geolocate/weather.py | 23 ++++++ 3 files changed, 79 insertions(+), 70 deletions(-) create mode 100644 20150430.haproxy-ubuntumirror/haproxy.cfg create mode 100755 geolocate/weather.py diff --git a/20150430.haproxy-ubuntumirror/haproxy.cfg b/20150430.haproxy-ubuntumirror/haproxy.cfg new file mode 100644 index 0000000..4912ef3 --- /dev/null +++ b/20150430.haproxy-ubuntumirror/haproxy.cfg @@ -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 diff --git a/geolocate/geolocate.py b/geolocate/geolocate.py index 4438b86..ad79dc0 100755 --- a/geolocate/geolocate.py +++ b/geolocate/geolocate.py @@ -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") diff --git a/geolocate/weather.py b/geolocate/weather.py new file mode 100755 index 0000000..6945224 --- /dev/null +++ b/geolocate/weather.py @@ -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()