use client id to avoid self-flocking
This commit is contained in:
parent
f1ccf681be
commit
3bdd4be474
17
model.py
17
model.py
@ -2,7 +2,7 @@
|
||||
Object-Document Map for bike model
|
||||
"""
|
||||
from mongoengine import Document, DateTimeField, GeoPointField, \
|
||||
FloatField, connection
|
||||
FloatField, StringField, connection
|
||||
import bson
|
||||
import datetime
|
||||
from LatLon23 import LatLon, Latitude, Longitude
|
||||
@ -54,6 +54,7 @@ class Bike(Document):
|
||||
speed = FloatField(default=0)
|
||||
bearing = FloatField(default=0)
|
||||
last_update = DateTimeField(default=datetime.datetime.utcnow)
|
||||
client_id = StringField(required=True)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Document, self).__init__(*args, **kwargs)
|
||||
@ -105,7 +106,7 @@ class Bike(Document):
|
||||
min_age = min_step / self.speed
|
||||
else:
|
||||
min_age = 60
|
||||
|
||||
|
||||
trip_len = distance.geodesic(self.get_point(), self.get_dest()).meters
|
||||
|
||||
local_radius = trip_len * point_altruism
|
||||
@ -121,7 +122,9 @@ class Bike(Document):
|
||||
'coordinates': self.point},
|
||||
'$maxDistance': local_radius}},
|
||||
'last_update': {
|
||||
'$gte': datetime.datetime.now() - datetime.timedelta(seconds=min_age)},
|
||||
'$gte': (datetime.datetime.now()
|
||||
- datetime.timedelta(seconds=min_age))},
|
||||
'client_id': {'$ne': self.client_id},
|
||||
'_id': {'$ne': bson.objectid.ObjectId(self.id)}})]
|
||||
|
||||
local_ids = set([bike['_id'] for bike in local])
|
||||
@ -134,14 +137,18 @@ class Bike(Document):
|
||||
{'$geometry': {'type': 'Point',
|
||||
'coordinates': self.destination},
|
||||
'$maxDistance': destination_radius}},
|
||||
'client_id': {'$ne': self.client_id},
|
||||
'_id': {'$ne': bson.objectid.ObjectId(self.id)},
|
||||
'last_update': {
|
||||
'$gte': datetime.datetime.now() - datetime.timedelta(seconds=min_age)}
|
||||
})]
|
||||
'$gte': (datetime.datetime.now()
|
||||
- datetime.timedelta(seconds=min_age))}
|
||||
})]
|
||||
remote_ids = set([bike['_id'] for bike in remote])
|
||||
|
||||
# intersect them!
|
||||
flock_ids = local_ids.intersection(remote_ids)
|
||||
|
||||
# return bikes, not bike ids
|
||||
return (bike for bike in local if bike['_id'] in flock_ids)
|
||||
|
||||
def flock_data(self):
|
||||
|
||||
17
server.py
17
server.py
@ -11,15 +11,17 @@ db = MongoEngine(app)
|
||||
# TODO: at registration give current location
|
||||
@app.route("/register/")
|
||||
def register():
|
||||
dest_lat = request.args.get('dest_lat', None)
|
||||
dest_lon = request.args.get('dest_lon', None)
|
||||
dest_lat = request.args.get('dest_lat', None)
|
||||
client_id = request.args.get('client_id', None)
|
||||
|
||||
assert dest_lat is not None and dest_lon is not None
|
||||
assert dest_lat is not None and dest_lon is not None and client_id is not None
|
||||
|
||||
dest = (float(dest_lon), float(dest_lat))
|
||||
|
||||
bike = Bike(point=(0, 0),
|
||||
destination=dest)
|
||||
destination=dest,
|
||||
client_id=client_id)
|
||||
|
||||
bike.save()
|
||||
|
||||
@ -36,12 +38,13 @@ def update(bike_id):
|
||||
lon = float(request.args.get('lon', None))
|
||||
speed = float(request.args.get('speed', None))
|
||||
bearing = float(request.args.get('bearing', None))
|
||||
client_id = request.args.get('client_id', None)
|
||||
|
||||
local_altruism = float(request.args.get('local_altruism', 0.1))
|
||||
dest_altruism = float(request.args.get('dest_altruism', 0.2))
|
||||
# TODO: maybe client tunes these?
|
||||
# local_altruism = float(request.args.get('local_altruism', 0.1))
|
||||
# dest_altruism = float(request.args.get('dest_altruism', 0.2))
|
||||
|
||||
|
||||
bike.update(lat, lon, speed, bearing)
|
||||
bike.update(lat, lon, speed, bearing, client_id)
|
||||
|
||||
return jsonify(last_update=bike.last_update.isoformat(),
|
||||
**bike.flock_data())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user