예제 #1
0
파일: public.py 프로젝트: jcsy521/ydws
def insert_location(location, db, redis):
    """Insert whole-data into T_LOCATION.
    """
    location = DotDict(location)
    # NOTE: if locate_error is bigger then 500, set it 500
    if int(location.locate_error) > 500:
        location.locate_error = 500
    lid = db.execute("INSERT INTO T_LOCATION(tid, latitude, longitude, altitude,"
                     "    clatitude, clongitude, timestamp, name, category, type,"
                     "    speed, degree, cellid, locate_error)"
                     "  VALUES (%s, %s, %s, %s, %s, %s, %s,"
                     "          %s, %s, %s, %s, %s, %s, %s)",
                     location.dev_id, location.lat, location.lon,
                     location.alt, location.cLat, location.cLon,
                     location.gps_time, location.name,
                     location.category, location.type,
                     location.speed, location.degree,
                     location.cellid, location.locate_error)
    if location.lat and location.lon:
        track_key = get_track_key(location.dev_id)
        track = redis.get(track_key)
        # if track is on, just put PVT into redis
        # maybe put cellid into redis later.
        # if track and (int(track) == 1) and (location.get("Tid", None) != EVENTER.TRIGGERID.PVT):
        #    return lid
        # NOTE: if location's type is gps, put it into redis
        if track and (int(track) == 1) and (location.type != 0):
            return lid

        location_key = get_location_key(location.dev_id)
        last_location = redis.getvalue(location_key)
        if (last_location and (location.gps_time > last_location['timestamp'])) or\
                not last_location:

            logging.info("[PUBLIC] Keep location in redis. tid: %s, location: %s",
                         location.dev_id, location)
            mem_location = {'id': lid,
                            'latitude': location.lat,
                            'longitude': location.lon,
                            'type': location.type,
                            'clatitude': location.cLat,
                            'clongitude': location.cLon,
                            'timestamp': location.gps_time,
                            'name': location.name,
                            'degree': location.degree,
                            'speed': location.speed,
                            'locate_error': location.locate_error}
            location_key = get_location_key(location.dev_id)
            redis.setvalue(location_key, mem_location, EVENTER.LOCATION_EXPIRY)

            if int(location.type) == 0:  # gps
                logging.info("[PUBLIC] Keep gps_location in gps_redis. tid: %s, location: %s",
                             location.dev_id, location)
                location_key = get_gps_location_key(location.dev_id)
                redis.setvalue(
                    location_key, mem_location, EVENTER.LOCATION_EXPIRY)

    return lid