예제 #1
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
예제 #2
0
    def push_to_ios(category, tid, t_alias, location, ios_id, ios_badge, region_id=None):
        """Push info fo IOS by the means of ANPS
        @param: category,
                tid, 
                t_alias,
                location,
                ios_id,
                ios_bagde
        """

        try:
            h = httplib2.Http(timeout=20)
            
            # 1: format alert 
            CATEGORY = {2:u'电量告警',
                        3:u'震动告警',
                        4:u'移动告警',
                        5:u'SOS',
                        6:u'通讯异常',
                        7:u'进入围栏',
                        8:u'离开围栏',
                        9:u'断电告警' }
            if not CATEGORY.get(category, None):
                logging.info("Invalid category, drop it. category: %s", category)
                return

            alert = u"您的定位器 “%s” 产生了%s" % (t_alias, CATEGORY[category])

            # 2: format body 
            ret = DotDict(tid=tid,
                          category=category,
                          longitude=location.lon,
                          latitude=location.lat,
                          clongitude=location.cLon,
                          clatitude=location.cLat,
                          name=location.name if location.name else '',
                          timestamp=location.timestamp,
                          speed=float(location.speed),
                          degree=float(location.degree),
                          type=location.type,
                          alias=t_alias,
                          pbat=location.pbat,
                          comment=location.comment, 
                          region_id=region_id if region_id else -1)

            keys = ['tid',
                    'category', 
                    'latitude', 
                    'longitude', 
                    'clatitude', 
                    'clongitude', 
                    'name', 
                    'timestamp', 
                    'speed', 
                    'degree', 
                    'type', 
                    'alias', 
                    'pbat', 
                    'comment',
                    'region_id']

            def get_body_str(ret):
                body_str = u'' 
                for key in keys:
                    tmp = '#%s' % ret.get(key,'')
                    body_str = safe_utf8(body_str) + safe_utf8(tmp) 
                body_str = body_str[1:]
                return body_str

            body_str = get_body_str(ret)
            #NOTE: here, the maxsize is 180, it bigger than it, set ret.name ''
            if len(body_str) > UWEB.IOS_MAX_SIZE:
                logging.info("Push body is bigger than: %s, set name ''", UWEB.IOS_MAX_SIZE)
                ret.name=''
                body_str = get_body_str(ret)

            headers = {"Content-type": "application/x-www-form-urlencoded; charset=utf-8"}
            url = ConfHelper.OPENFIRE_CONF.ios_push_url 
            data = DotDict(uid=ios_id,
                           alert=safe_utf8(alert),
                           badge=ios_badge,
                           body=safe_utf8(body_str))

            response, content = h.request(url, 'POST', body=urlencode(data), headers=headers)
            if response['status'] == '200':
                ret = json_decode(content)
                if ret['status'] == 0:
                    logging.info("Push to IOS success! Message: %s", ret['message'])
                else:
                    logging.error("Push to IOS failed! Message: %s", ret['message'])
            else:
                logging.error("Push to IOS failed! response: %s", response)

        except Exception as e:
            logging.exception("Push to IOS failed. Exception: %s", e.args)