Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active December 11, 2015 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpdonp/4569832 to your computer and use it in GitHub Desktop.
Save donpdonp/4569832 to your computer and use it in GitHub Desktop.
icecondor location processing (neuronbot)
function(msg) {
var location = msg
if(msg.type == "location"){
var location_key = 'icecondor:'+location.username
var previous = db.get(location_key)
var last_location
if(previous) { last_location = JSON.parse(previous)}
var words = msg["username"]+": "+msg["provider"]
if(msg["accuracy"]){
words = words + "(acc "+msg["accuracy"].toFixed(1)+"m) "
}
if(msg["velocity"] > 0){
words = words + " moving "+msg["velocity"].toFixed(1)+"m/s. "
}
var necessary_speed = 0
var historical = false
if(last_location) {
var current_date = new Date(location.date)
var last_date = new Date(last_location.date)
if (current_date < last_date){
historical = true
words = words+"(historical) "
} else {
db.set(location_key, JSON.stringify(location))
}
} else {
db.set(location_key, JSON.stringify(location))
}
var radius = 75
if(location["accuracy"] < radius && !historical) {
if(last_location) {
var location_moved = location_distance(last_location, location)
words += "moved "+location_moved.toFixed(1)+"m. "
}
var moved = true;
var moved_meters
var venue_key = 'icecondor:'+msg["username"]+':venue'
var last_venue = JSON.parse(db.get(venue_key))
if(last_venue){
moved_meters = venue_distance(last_venue, location)
if(moved_meters < radius*4){
moved = false
words += dwelling()
}
words += "("+moved_meters.toFixed(1)+"m. away) "
} else {
words += "(no previous) "
}
if(moved){
words += " "+intersection_words(location)
words += update_venue(location, radius)
}
}
words += location.date+" "
if(msg["battery_level"]){
words = words + "batt:"+msg["battery_level"]+"% "
}
if(msg["ac_power"]){
words = words + "(charging)"
}
var say = {target:"#icecondor", message: words}
return say
}
function intersection_words(ll){
var iurl = "http://api.geonames.org/findNearestIntersectionOSMJSON?lat="+
ll["latitude"]+"&lng="+ll["longitude"]+"&username=donpdonp"
var intr = JSON.parse(http.get(iurl))
if(intr.intersection){
var intname = dir_shorten(intr.intersection.street1)+" & "+dir_shorten(intr.intersection.street2)
db.set('icecondor:'+msg["username"]+':intersection', intname)
return intname;
}
}
function dir_shorten(street){
street = street.replace("Southeast","SE")
street = street.replace("Northeast","NE")
street = street.replace("Southwest","SW")
street = street.replace("Northwest","NW")
street = street.replace("Boulevard","Blvd")
street = street.replace("Avenue","Ave")
street = street.replace("Street","St")
return street
}
function foursq_venues(location, radius){
var id = db.get('4sq_client_id')
var secret = db.get('4sq_client_secret')
var url = "https://api.foursquare.com/v2/venues/search?ll="+
location.latitude+","+location.longitude+
"&intent=browse&radius="+radius+
"&client_id="+id+"&client_secret="+secret+
"&v=20130101"
resp = JSON.parse(http.get(url))
if(resp.meta.code == 200){
return resp["response"]["venues"]
}
}
function set_location(location){
}
function update_venue(msg, radius){
var words = ""
var venues = foursq_venues(msg, radius)
if(venues){
if(venues.length > 0) {
var venue = venues[0] // closest
var venue_key = 'icecondor:'+msg["username"]+':venue'
var venue_date_key = 'icecondor:'+msg["username"]+':vdate'
var last_venue = JSON.parse(db.get(venue_key))
words = words + ". 4sq venue: "+venue["name"]+" "
if(last_venue == null) {
db.set(venue_key, JSON.stringify(venue))
db.set(venue_date_key, (new Date).toString())
} else {
var old_date = new Date(db.get(venue_date_key))
var duration_mins = ((new Date()) - old_date)/1000/60
var notice = db.get('icecondor:'+msg["username"]+':notice', "true")
if(last_venue.id != venue.id){
db.set(venue_key, JSON.stringify(venue))
db.set('icecondor:'+msg["username"]+':vdate', (new Date).toString())
words = words+" "+venue["location"]["address"]+" (at previous venue for "+duration_words(duration_mins)+")"
if(notice == "true"){
var left_msg = msg["username"]+" has left "+last_venue.name+" after "+duration_words(duration_mins)+"."
bot.say("#icecondor",left_msg)
bot.emit({type:'beacon',sender:'icecondor.js',message:left_msg})
db.set('icecondor:'+msg["username"]+':notice', "false")
var email = db.get('icecondor:'+msg["username"]+':email')
if(email){ bot.emit({type:'email',to:email,subject:left_msg,body:left_msg,message:''}) }
}
} else {
}
}
} else {
words = words + " 4sq: no venues inside "+radius.toFixed(0)+"m"
}
}
return words
}
function duration_words(mins){
var words = ""
var hours = parseInt(mins/60)
if(hours > 0) { words += hours+"hr" }
var mins = parseInt(mins % 60)
words += mins+"min"
return words
}
function venue_distance(venue, location){
return gju.pointDistance({coordinates:[venue.location.lat,
venue.location.lng]},
{coordinates:[location.latitude,
location.longitude]})
}
function location_distance(locationa, locationb){
return gju.pointDistance({coordinates:[locationa.latitude,
locationa.longitude]},
{coordinates:[locationb.latitude,
locationb.longitude]})
}
function dwelling(){
var words = ""
var venue_key = 'icecondor:'+msg["username"]+':venue'
var venue = JSON.parse(db.get(venue_key))
var notice = db.get('icecondor:'+msg["username"]+':notice', "true")
var venue_date_key = 'icecondor:'+msg["username"]+':vdate'
var old_date = new Date(db.get(venue_date_key))
var duration_mins = ((new Date()) - old_date)/1000/60
var dwell_mins = 10
if(notice != "true" && duration_mins >= dwell_mins){
var category_words = venue.categories.map(function(v){return v.name}).join("/")
var say = ""+msg["username"]+" is near a "+category_words+" called "+venue["name"]+" (for "+duration_words(duration_mins)+") "
bot.say("#icecondor", say)
bot.emit({type:'beacon',sender:'icecondor.js',message:say})
db.set('icecondor:'+msg["username"]+':notice', "true")
var email = db.get('icecondor:'+msg["username"]+':email')
if(email){ bot.emit({type:'email',to:email,subject:say,body:say,message:say}) }
} else {
words += "near: "+venue.name+" "
}
words += "(dwelling "+duration_words(duration_mins)+") "
return words
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment