def truck_data(truck_id): database.connect() query = TruckData.select() query = query.where(TruckData.truck_id == truck_id) query = query.order_by(TruckData.timestamp.desc()) data = [d.get_default_dict() for d in query] return json.dumps(data) database.close()
def location_data(lat, lon, radius): database.connect() # NOTE: the "radius" is a square threshold, not circular query = TruckData.select() query = query.where(TruckData.latitude > lat - radius) query = query.where(TruckData.latitude < lat + radius) query = query.where(TruckData.longitude > lon - radius) query = query.where(TruckData.longitude < lon + radius) query = query.order_by(TruckData.timestamp.desc()) data = [d.get_default_dict() for d in query] return json.dumps(data) database.close()
def recent_data(): database.connect() collected = [] for truck, points in json.loads(request.data).iteritems(): for time, coords in points.iteritems(): new = TruckData() new.timestamp = float(time) new.truck_id = int(truck) new.latitude = float(coords[0]) new.longitude = float(coords[1]) new.save() collected.append(new) database.close() return "success"
def all_data(): database.connect() query = TruckData.select().order_by(TruckData.timestamp.desc()) data = [d.get_default_dict() for d in query] return json.dumps(data) database.close()