예제 #1
0
파일: packettask.py 프로젝트: jcsy521/ydws
    def handle_position_info(self, location):
        """Handle the info of position, include PVT(T11), CALL(T3)
        """
        location = DotDict(location)

        current_time = int(time.time())
        #NOTE: Now, it is seldom appears
        if location.Tid == EVENTER.TRIGGERID.CALL:
            if location['gps_time'] > (current_time + 24*60*60):
                logging.info("[EVENTER] The location's (gps_time - current_time) is more than 24 hours, so drop it:%s", location)
                return
            location = lbmphelper.handle_location(location, self.redis,
                                                  cellid=True, db=self.db) 
            ## check regions
            #for region in regions:
            #    region_location = self.check_region_event(location, region)
            #    if region_location and region_location['t'] == EVENTER.INFO_TYPE.REPORT:
            #        self.handle_report_info(region_location)

            location['category'] = EVENTER.CATEGORY.REALTIME
            update_terminal_dynamic_info(self.db, self.redis, location)

            if location.get('lat') and location.get('lon'):
                self.realtime_location_hook(location)

        # NOTE: For pvt(T11)
        elif location.Tid == EVENTER.TRIGGERID.PVT:
            #NOTE: get speed_limit  
            # make pvts sortd according gsp_time
            pvts = sorted(location['pvts'], key=lambda x:x['gps_time'])
            for pvt in pvts:
            #for pvt in location['pvts']:
                # The 'future time' is drop 
                if pvt['gps_time'] > (current_time + 24*60*60):
                    logging.info("[EVENTER] The location's (gps_time - current_time) is more than 24 hours, so drop it:%s", pvt)
                    continue

                # get available location from lbmphelper
                pvt['dev_id'] = location['dev_id']
                pvt['Tid'] = location['Tid']
                pvt['valid'] = GATEWAY.LOCATION_STATUS.SUCCESS
                pvt['type'] = 0 

                #NOTE: handle stop 
                self.handle_stop(pvt)

                #NOTE: handle region 
                self.handle_region(pvt)

                #NOTE: handle single 
                self.handle_single(pvt)

                #NOTE: handle speed 
                self.handle_speed(pvt)

                #NOTE: the time of keep last_pvt is import. First check single, then keep pvt
                last_pvt_key = get_last_pvt_key(location['dev_id'])
                last_pvt = pvt 
                self.redis.setvalue(last_pvt_key, last_pvt, time=EVENTER.STOP_EXPIRY)

                # NOTE: not offset it
                #location = lbmphelper.handle_location(pvt, self.redis,
                #                                      cellid=False, db=self.db) 
                #NOTE: mileage
                pvt['category'] = EVENTER.CATEGORY.REALTIME
                if pvt.get('lat') and pvt.get('lon'): 
                    insert_location(pvt, self.db, self.redis)
                    #NOTE: handle mileage 
                    self.handle_mileage(pvt)
                self.push_to_client(pvt) 
        else:
            location.category = EVENTER.CATEGORY.UNKNOWN
            self.unknown_location_hook(location)
예제 #2
0
파일: lbmphelper.py 프로젝트: jcsy521/ydws
def handle_location(location, redis, cellid=False, db=None):
    """
    @param location: position/report/locationdesc/pvt
           memcached
           cellid: if True issue cellid

    @return location
    eg.
    {u'gps_time': 1407138438, 
     u'dev_id': u'T123SIMULATOR', 
     u'defend_status': u'1', 
     u'locate_error': 20, 
     u'alt': 0, 
     u'speed': 126.3, 
     u'cLat': 0, 
     u'cLon': 0, 
     u'lon': 418507200, 
     u'valid': u'0', 
     u'ns': u'N', 
     u'gps': u'20', 
     u'fobid': u'', 
     u'degree': 276.5, 
     u'softversion': u'1.0.0', 
     u'timestamp': 1407138438, 
     u'terminal_type': u'1', 
     u'sessionID': u'cyndqhy9', 
     u'pbat': u'10', 
     u'lat': 95630400, 
     u'is_notify': u'', 
     u'rName': u'POWERDOWN', 
     u'name': None, 
     u'ew': u'E', 
     u'dev_type': u'1', 
     u'command': u'T26', 
     u't': u'REPORT', 
     u'gsm': u'6', 
     u'cellid': u'460:0:10101:03633'
     u'type':0,
     u'category':9,
    }

    """
    location = DotDict(location)
    if location.valid == GATEWAY.LOCATION_STATUS.SUCCESS: # 1
        location.type = 0
        if location.get('speed') is not None and location.speed <= UWEB.SPEED_DIFF:
            pass
            #location.degree = get_last_degree(location, redis, db)
    elif location.valid == GATEWAY.LOCATION_STATUS.UNMOVE: # 4
        logging.info("[LBMPHELPER] Tid:%s gps locate flag :%s", location.dev_id, location.valid)
        #last_location = QueryHelper.get_location_info(location.dev_id, db, redis)
        last_location = QueryHelper.get_gps_location_info(location.dev_id, db, redis)
        if last_location:
            current_time = int(time.time())
            diff = current_time - last_location.timestamp
            logging.info("[LBMPHELPER] current time:%s, last locaiton time:%s, diff time:%s", current_time, last_location.timestamp, diff)
            if (current_time - last_location.timestamp) < 60 * 60 * 24 * 30: # 30 days. in seconds 
                logging.info("[LBMPHELPER] Tid:%s, current_time - last_location.timestamp  < 30 days, so use last location time:%s", 
                             location.dev_id, last_location.timestamp)
                location.gps_time = last_location.timestamp
                location.lat = last_location.latitude
                location.lon = last_location.longitude
                location.cLat = last_location.clatitude
                location.cLon = last_location.clongitude
                location.type = 0 
                location.gps = 0
            else:
                location.type = 0 
                logging.info("[LBMPHELPER] Tid:%s, current_time - last_location.timestamp >= 600s, so use location itself: %s.", location.dev_id, location)
                pass
        else:
            location.type = 0 
            logging.info("[LBMPHELPER] Tid:%s, found no location before, so use location itself: %s.", location.dev_id, location)
            pass
        
        #    location.lat = last_location.latitude
        #    if (current_time - last_location.timestamp) > 600:
        #        location.gps_time = current_time 
        #        logging.info("Tid:%s, current_time - last_location.timestamp  > 600s, so use current time:%s", location.dev_id, current_time)
        #    else:
        #        logging.info("Tid:%s, current_time - last_location.timestamp  <= 600s, so use last location time:%s", location.dev_id, last_location.timestamp)
        #        location.gps_time = last_location.timestamp
        #    location.lat = last_location.latitude
        #    location.lon = last_location.longitude
        #    location.cLat = last_location.clatitude
        #    location.cLon = last_location.clongitude
        #    location.type = 0 
        #    location.gps = 0
        #else:
        #    location.lat = 0
        #    location.lon = 0
        #    location.cLat = 0
        #    location.cLon = 0
        #    location.type = 0
        #    location.gps_time = int(time.time()) 
        #    location.degree = 0.00
        #    location.gps = 0
        #    #if cellid:
        #    #    location = issue_cellid(location, db, redis)
    elif location.valid == GATEWAY.LOCATION_STATUS.MOVE: # 6
        logging.info("[LBMPHELPER] tid:%s gps locate flag :%s", location.dev_id, location.valid)
        location.lat = 0
        location.lon = 0
        location.cLat = 0
        location.cLon = 0
        location.type = 0
        location.gps_time = int(time.time()) 
        location.degree = 0.00
        location.gps = 0
        if cellid:
            location = issue_cellid(location, db, redis)
    else: # 0,2,5
        logging.info("[LBMPHELPER] tid:%s gps locate flag :%s", location.dev_id, location.valid)
        location.lat = 0
        location.lon = 0
        location.cLat = 0
        location.cLon = 0
        location.type = 0
        location.gps_time = int(time.time()) 
        location.degree = 0.00
        #if db:
        #    location.degree = get_last_degree(location, redis, db)
        location.gps = 0
        if cellid:
            # 1: issue cellid
            location = issue_cellid(location, db, redis)
            if location.lon and location.lat:
                # 2: check the location whether is odd 
                last_location = QueryHelper.get_location_info(location.dev_id, db, redis)
                if last_location:
                    distance = get_distance(location.lon,
                                            location.lat,
                                            last_location.longitude,
                                            last_location.latitude)
                    if distance > 5000: 
                        login_time = QueryHelper.get_login_time_by_tid(location.dev_id, db, redis)
                        if last_location.timestamp < login_time:
                            logging.info("[LBMPHELPER] tid: %s distance:%s > 5000m, and last login time: %s, after last location timestamp: %s, use cellid location.",
                                         location.dev_id, distance, login_time, last_location.timestamp) 
                        else:
                            location.lat, location.lon = (last_location.latitude, last_location.longitude)
                            logging.info("[LBMPHELPER] tid:%s, distance:%s > 5000m, use last location: %s ",
                                         location.dev_id, distance, last_location)
                    elif distance < 2000:
                        location.lat, location.lon = (last_location.latitude, last_location.longitude)
                        logging.info("[LBMPHELPER] tid:%s distance:%s < 2000m, use last location:%s", location.dev_id, distance, last_location)
                    else:
                        logging.info("[LBMPHELPER] tid:%s 2000m < distance:%s < 5000m, use cellid location", location.dev_id, distance)
                else:
                    logging.info("[LBMPHELPER] tid:%s last location is none, use cellid location", location.dev_id)

    if location and location.lat and location.lon:
        clats, clons = get_clocation_from_ge([location.lat,], [location.lon,])
        location.cLat, location.cLon = clats[0], clons[0] 
        # drop some odd cellid location
        if location.type == 1 and location.cLat and location.cLon:
            if PtInPolygon(location, DM_ZJGS_POLYGON):
                location.lat = 0
                location.lon = 0
                location.cLat = 0
                location.cLon = 0

        #if (location['t'] == EVENTER.INFO_TYPE.REPORT or
        #    location['command'] == GATEWAY.T_MESSAGE_TYPE.LOCATIONDESC):
        # NOTE: change it temporarily: in platform get loction name of all
        if location.cLat and location.cLon:
            location.name = get_location_name(location.cLat, location.cLon, redis)

    if location['t'] == EVENTER.INFO_TYPE.POSITION:
        location.category = EVENTER.CATEGORY.REALTIME
    elif location['t'] == EVENTER.INFO_TYPE.REPORT:
        location.category = EVENTER.CATEGORY[location.rName]
    else:
        location.category = EVENTER.CATEGORY.UNKNOWN

    return location