Last active
August 29, 2015 13:57
-
-
Save matbor/9381893 to your computer and use it in GitHub Desktop.
This will reverse Geo the gps data sent by owntracks and then send it thru mqttwarn to whatever target you specify, https://github.com/jpmens/mqttwarn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#add this first part to the top of the functions file | |
#for reversegeo | |
import time | |
#import logging | |
from urllib import urlencode | |
from urllib2 import urlopen | |
#this filter will reverse geo the address for you | |
def OwnTracksConvertWithAdd(data): | |
if type(data) == dict: | |
tst = data.get('tst', int(time.time())) | |
data['tst'] = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(tst))) | |
# Remove these elements to eliminate warnings | |
for k in ['_type', 'desc']: | |
data.pop(k, None) | |
whereisthis = getaddress(data) | |
data['whereisthis'] = whereisthis | |
return "{username} {device} {tst} was last seen at {whereisthis}".format(**data) | |
class ReverseGeo(object): | |
""" | |
Reverse geocoder using the MapQuest Open Platform Web Service. | |
""" | |
def __init__(self): | |
"""Initialize reverse geocoder; no API key is required by the | |
Nomatim-based platform""" | |
self.url = "http://open.mapquestapi.com/nominatim/v1/reverse?format=json&addressdetails=1&%s" | |
def parse_json(self, data): | |
try: | |
data = json.loads(data) | |
except: | |
data = {} | |
return data | |
def reverse(self, lat, lon): | |
params = { 'lat': lat, 'lon' : lon } | |
url = self.url % urlencode(params) | |
data = urlopen(url) | |
response = data.read() | |
return self.parse_json(response) | |
def getaddress(data): | |
if type(data) == dict and 'lat' in data: | |
nominatim = ReverseGeo() | |
lookitup = nominatim.reverse(data['lat'], data['lon']) | |
# logging.debug(lookitup) | |
fulladdress = lookitup['display_name'] | |
return str(fulladdress) | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;add this to the bottom of the mqttwarn.ini file, don't forget to add some more targets. | |
[owntracks-location] | |
topic = owntracks/+/+ | |
targets = log:info | |
datamap = OwnTracksTopic2Data() | |
format = OwnTracksConvertWithAdd() | |
@summerboy12 will re-add, was just assuming people where adding it to sample function file.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seriously cool mate - works like a charm!! One little buglette in your notes, you are missing the 'import time' at the top of the functions file. Apart from that, worked first time. Cheers.