Exemplo n.º 1
0
def update_positions(ride_positions):
    """
    Handler for fleet backends to call when taxi positions changes. Sends the signals and stores the data in memcache.
    @param ride_positions: A list of C{TaxiRidePosition}
    """
    logging.info("fleet manager: positions update %s" % [rp.ride_uuid for rp in ride_positions])
    fleet_signals.positions_update_signal.send(sender="fleet_manager", positions=ride_positions)

    for rp in ride_positions:
        key = _get_key(rp.ride_uuid)
        logging.info("getting from memcache: %s, %s" % (key, FM_MEMCACHE_NAMESPACE))
        cached_rp = memcache.get(key, namespace=FM_MEMCACHE_NAMESPACE)
        if cached_rp:
            logging.info("memcache[%s] -> %s" % (key, pickle.loads(cached_rp).__dict__))

        pickled_rp = pickle.dumps(rp)
        if cached_rp != pickled_rp: # this is a new position
            logging.info("new position received: %s[%s:%s]" % (rp.ride_uuid, rp.lat, rp.lon))
            memcache.set(key, pickled_rp, namespace=FM_MEMCACHE_NAMESPACE)

            pickmeapp_ride, shared_ride = rides_from_uuid(rp.ride_uuid)
            if pickmeapp_ride or shared_ride:
                e = RideEvent(pickmeapp_ride=pickmeapp_ride, shared_ride=shared_ride,
                              status=FleetManagerRideStatus.POSITION_CHANGED, raw_status=POSITION_CHANGED, lat=rp.lat,
                              lon=rp.lon, taxi_id=rp.taxi_id, timestamp=rp.timestamp)
                e.save()
                logging.info("new ride event created: %s" % e)
            if shared_ride:
                for point in shared_ride.points.all():
                    distance = distance_between_points(point.lat, point.lon, rp.lat, rp.lon) * 1000
                    if distance <= DISTANCE_CONSIDERED_AS_VISITED_IN_METERS:
                        point.update(visited=True)
                        logging.info("%s %s visited" % (point.get_type_display(), point.id))
        else:
            logging.info("old position received: %s[%s:%s]" % (rp.ride_uuid, rp.lat, rp.lon))
Exemplo n.º 2
0
def send_message(ride, message):
    logging.info(u"send_message '%s' to ride: %s" % (message, ride.id))
    try:
        station = ride.station
        assert station, "ride [%s] is not assigned to a station" % ride.id
        assert station.fleet_station_id, "station %s has no fleet_station_id" % ride.station.name
        assert ride.dn_fleet_manager_id, "ride [%s] is not associated with a fleet manager" % ride.id
        assert ride.taxi_number, "ride [%s] is not associated with a taxi number" % ride.id

        ride_fm = FleetManager.by_id(ride.dn_fleet_manager_id)
        result = ride_fm.send_message(message, ride.station.fleet_station_id,
                                      ride.taxi_number)

        # log a RideEvent about his message
        rp = get_ride_position(ride)
        e = RideEvent(pickmeapp_ride=None,
                      shared_ride=ride,
                      status=FleetManagerRideStatus.MESSAGE_SENT,
                      raw_status=message,
                      lat=rp.lat if rp else None,
                      lon=rp.lon if rp else None,
                      taxi_id=ride.taxi_number,
                      timestamp=default_tz_now())
        e.save()

        return result
    except Exception, e:
        logging.error(traceback.format_exc())
        return False
Exemplo n.º 3
0
def update_ride(fmr):
    """
    Update our db with ride data from a fleet manager.
    @param fmr: A C{FleetMangerRide} containing updated data about the ride.
    """
    logging.info("fleet manager: ride update %s" % fmr)
    fleet_signals.fmr_update_signal.send(sender="fleet_manager", fmr=fmr)

    pickmeapp_ride, shared_ride = rides_from_uuid(fmr.id)
    if not (pickmeapp_ride or shared_ride):
        logging.error("fleet manager: fmr update to non-existing ride")
        return

    e = RideEvent(pickmeapp_ride=pickmeapp_ride,
                  shared_ride=shared_ride,
                  status=fmr.status,
                  raw_status=fmr.raw_status,
                  lat=fmr.lat,
                  lon=fmr.lon,
                  taxi_id=fmr.taxi_id,
                  timestamp=fmr.timestamp)
    e.save()

    # TODO_WB: update wb_ride when we trust ISR data
    wb_ride = pickmeapp_ride or shared_ride
    logging.info(
        "fleet manager: ride [%s] not updated. fmr_status=%s raw_status=%s)" %
        (wb_ride.id, FleetManagerRideStatus.get_name(
            fmr.status), fmr.raw_status))
Exemplo n.º 4
0
def update_positions(ride_positions):
    """
    Handler for fleet backends to call when taxi positions changes. Sends the signals and stores the data in memcache.
    @param ride_positions: A list of C{TaxiRidePosition}
    """
    logging.info("fleet manager: positions update %s" %
                 [rp.ride_uuid for rp in ride_positions])
    fleet_signals.positions_update_signal.send(sender="fleet_manager",
                                               positions=ride_positions)

    for rp in ride_positions:
        key = _get_key(rp.ride_uuid)
        logging.info("getting from memcache: %s, %s" %
                     (key, FM_MEMCACHE_NAMESPACE))
        cached_rp = memcache.get(key, namespace=FM_MEMCACHE_NAMESPACE)
        if cached_rp:
            logging.info("memcache[%s] -> %s" %
                         (key, pickle.loads(cached_rp).__dict__))

        pickled_rp = pickle.dumps(rp)
        if cached_rp != pickled_rp:  # this is a new position
            logging.info("new position received: %s[%s:%s]" %
                         (rp.ride_uuid, rp.lat, rp.lon))
            memcache.set(key, pickled_rp, namespace=FM_MEMCACHE_NAMESPACE)

            pickmeapp_ride, shared_ride = rides_from_uuid(rp.ride_uuid)
            if pickmeapp_ride or shared_ride:
                e = RideEvent(pickmeapp_ride=pickmeapp_ride,
                              shared_ride=shared_ride,
                              status=FleetManagerRideStatus.POSITION_CHANGED,
                              raw_status=POSITION_CHANGED,
                              lat=rp.lat,
                              lon=rp.lon,
                              taxi_id=rp.taxi_id,
                              timestamp=rp.timestamp)
                e.save()
                logging.info("new ride event created: %s" % e)
            if shared_ride:
                for point in shared_ride.points.all():
                    distance = distance_between_points(point.lat, point.lon,
                                                       rp.lat, rp.lon) * 1000
                    if distance <= DISTANCE_CONSIDERED_AS_VISITED_IN_METERS:
                        point.update(visited=True)
                        logging.info("%s %s visited" %
                                     (point.get_type_display(), point.id))
        else:
            logging.info("old position received: %s[%s:%s]" %
                         (rp.ride_uuid, rp.lat, rp.lon))
Exemplo n.º 5
0
def update_ride(fmr):
    """
    Update our db with ride data from a fleet manager.
    @param fmr: A C{FleetMangerRide} containing updated data about the ride.
    """
    logging.info("fleet manager: ride update %s" % fmr)
    fleet_signals.fmr_update_signal.send(sender="fleet_manager", fmr=fmr)

    pickmeapp_ride, shared_ride = rides_from_uuid(fmr.id)
    if not (pickmeapp_ride or shared_ride):
        logging.error("fleet manager: fmr update to non-existing ride")
        return

    e = RideEvent(pickmeapp_ride=pickmeapp_ride, shared_ride=shared_ride, status=fmr.status, raw_status=fmr.raw_status,
                  lat=fmr.lat, lon=fmr.lon, taxi_id=fmr.taxi_id, timestamp=fmr.timestamp)
    e.save()

    # TODO_WB: update wb_ride when we trust ISR data
    wb_ride = pickmeapp_ride or shared_ride
    logging.info("fleet manager: ride [%s] not updated. fmr_status=%s raw_status=%s)" %
                 (wb_ride.id, FleetManagerRideStatus.get_name(fmr.status), fmr.raw_status))
Exemplo n.º 6
0
def send_message(ride, message):
    logging.info(u"send_message '%s' to ride: %s" % (message, ride.id))
    try:
        station = ride.station
        assert station, "ride [%s] is not assigned to a station" % ride.id
        assert station.fleet_station_id, "station %s has no fleet_station_id" % ride.station.name
        assert ride.dn_fleet_manager_id, "ride [%s] is not associated with a fleet manager" % ride.id
        assert ride.taxi_number, "ride [%s] is not associated with a taxi number" % ride.id

        ride_fm = FleetManager.by_id(ride.dn_fleet_manager_id)
        result = ride_fm.send_message(message, ride.station.fleet_station_id, ride.taxi_number)

        # log a RideEvent about his message
        rp = get_ride_position(ride)
        e = RideEvent(pickmeapp_ride=None, shared_ride=ride,
                      status=FleetManagerRideStatus.MESSAGE_SENT, raw_status=message, lat=rp.lat if rp else None, lon=rp.lon if rp else None, taxi_id=ride.taxi_number, timestamp=default_tz_now())
        e.save()

        return result
    except Exception, e:
        logging.error(traceback.format_exc())
        return False