Example #1
0
    def post(self):
        try:
            data = DotDict(json_decode(self.request.body))
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            logging.info("[UWEB] lastfinfo failed, message: %s, request: \n%s", 
                         ErrorCode.ERROR_MESSAGE[status], self.request.body)
            self.write_ret(status)
            return 

        try:
            cars_info = OrderedDict() 
            usable = 0 # nothing is modified, the cars_info is no use, use the data last time
            status = ErrorCode.SUCCESS
            
            terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO"
                                      "  WHERE service_status = %s"
                                      "    AND owner_mobile = %s"
                                      "    AND login_permit = 1"
                                      "    ORDER BY LOGIN DESC",
                                      UWEB.SERVICE_STATUS.ON, self.current_user.uid)
            tids = [terminal.tid for terminal in terminals]

            # 1 inquery data     
            for tid in tids:
                # 1: get terminal info 
                terminal = QueryHelper.get_terminal_info(tid, self.db, self.redis) 
                if terminal['login'] == GATEWAY.TERMINAL_LOGIN.SLEEP:
                    terminal['login'] = GATEWAY.TERMINAL_LOGIN.ONLINE

                # 2: get location 
                location = QueryHelper.get_location_info(tid, self.db, self.redis)
                if location and not (location.clatitude or location.clongitude):
                    location_key = get_location_key(str(tid))
                    locations = [location,] 
                    locations = get_locations_with_clatlon(locations, self.db) 
                    location = locations[0]
                    self.redis.setvalue(location_key, location, EVENTER.LOCATION_EXPIRY)

                if location and location['name'] is None:
                    location['name'] = ''

                if location and location['type'] == 1: # cellid 
                    location['locate_error'] = 500  # mile

                car_dct = {}
                car_info=dict(defend_status=terminal['defend_status'] if terminal['defend_status'] is not None else 1,
                              mannual_status=terminal['mannual_status'] if terminal['mannual_status'] is not None else 1,
                              fob_status=terminal['fob_status'] if terminal['fob_status'] is not None else 0,
                              timestamp=location['timestamp'] if location else 0,
                              speed=location.speed if location else 0,
                              # NOTE: degree's type is Decimal, float() it before json_encode
                              degree=float(location.degree) if location else 0.00,
                              locate_error=location.get('locate_error', 20) if location else 20,
                              name=location.name if location else '',
                              type=location.type if location else 1,
                              latitude=location['latitude'] if location else 0,
                              longitude=location['longitude'] if location else 0, 
                              clatitude=location['clatitude'] if location else 0,
                              clongitude=location['clongitude'] if location else 0, 
                              login=terminal['login'] if terminal['login'] is not None else 0,
                              bt_name=terminal.get('bt_name', '') if terminal else '',
                              bt_mac=terminal.get('bt_mac', '') if terminal else '',
                              gps=terminal['gps'] if terminal['gps'] is not None else 0,
                              gsm=terminal['gsm'] if terminal['gsm'] is not None else 0,
                              pbat=terminal['pbat'] if terminal['pbat'] is not None else 0,
                              mobile=terminal['mobile'],
                              owner_mobile=terminal['owner_mobile'],
                              alias=terminal['alias'],
                              #keys_num=terminal['keys_num'] if terminal['keys_num'] is not None else 0,
                              keys_num=0,
                              fob_list=terminal['fob_list'] if terminal['fob_list'] else [])

                car_dct[tid]=car_info
                cars_info.update(car_dct)
            
            lastinfo_key = get_lastinfo_key(self.current_user.uid)

            # BIG NOTE: here, compare lastinfo and cars_info as str
            lastinfo = self.redis.get(lastinfo_key)

            lastinfo_time_key = get_lastinfo_time_key(self.current_user.uid)
            lastinfo_time = self.redis.getvalue(lastinfo_time_key)

            if lastinfo == str(cars_info):  
                pass
            else:
                lastinfo_time = int(time.time())
                self.redis.setvalue(lastinfo_key, cars_info) 
                self.redis.setvalue(lastinfo_time_key, lastinfo_time)

            track_tid = data.get('track_tid', None)  # use cache
            track_info = []
            query_time = data.get('time', None) 
            track_time = data.get('track_time', query_time) 
            
            # 2 check whether provide usable data   
            if data.get('cache', None):  # use cache
                if query_time is not None: # use time
                    if int(query_time) < lastinfo_time:
                        usable = 1
                        if track_tid:
                            if track_tid not in tids:
                                logging.error("The terminal with tid: %s does not exist", track_tid)
                            else:
                                track_key = get_track_key(track_tid)
                                self.redis.setvalue(track_key, 1, UWEB.TRACK_INTERVAL)
                                if int(query_time) == -1:
                                    pass
                                elif lastinfo_time - query_time > 1: # every 30 second, ternimal generate a location 
                                    car_info = cars_info[track_tid]
                                    endtime = int(car_info['timestamp'])-1 if car_info['timestamp'] else int(lastinfo_time)-1 
                                    track  = self.db.query("SELECT id, latitude, longitude," 
                                                           "    clatitude, clongitude"
                                                           "  FROM T_LOCATION"
                                                           "  WHERE tid = %s"
                                                           "    AND NOT (latitude = 0 OR longitude = 0)"
                                                           "    AND (timestamp BETWEEN %s AND %s)"
                                                           "    AND type = 0"
                                                           "    ORDER BY timestamp",
                                                           track_tid, int(track_time)+1, endtime)
                                    track = get_locations_with_clatlon(track, self.db)
                                    for item in track:
                                        if item['clatitude'] and item['clongitude']:
                                            track_info.append(item['clatitude'])
                                            track_info.append(item['clongitude'])
                    else: 
                        cars_info = {}
                        usable = 0
                else: # no time
                    if lastinfo == cars_info: 
                        cars_info = {}
                        usable = 0
                    else: 
                        usable = 1
            else: 
                usable = 1

            self.write_ret(status, 
                           dict_=DotDict(cars_info=cars_info,
                                         track_info=track_info,
                                         usable=usable,
                                         lastinfo_time=lastinfo_time)) 
        except Exception as e:
            logging.exception("[UWEB] uid: %s, data: %s get lastinfo failed. Exception: %s", 
                              self.current_user.uid, data, e.args) 
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)
Example #2
0
from libgravatar.const import const
from utils.ordereddict import OrderedDict

# ==== Errors ====
#
# Errors usually come with a number and human readable text. Generally the text should be followed whenever possible,
# but a brief description of the numeric error codes are as follows:
#
#     -7  Use secure.gravatar.com
#     -8  Internal error
#     -9  Authentication error
#     -10 Method parameter missing
#     -11 Method parameter incorrect
#     -100  Misc error (see text)
#
const.ERRORS={7:'Use secure.gravatar.com',
        8:'Internal error',
        9:'Authentication error',
        10:'Method parameter missing',
        11:'Method parameter incorrect',
        100:'Misc error (see text)'}

#Rating
RATING=OrderedDict({-1:'unknown',0:'g',1:'pg',2:'r',3:'x'})
RATING.merge(RATING.reverse_dictionary())
const.rating=RATING
Example #3
0
        def _on_finish(db):
            self.db = db
            try:
                data = DotDict(json_decode(self.request.body))
                biz_type = data.get("biz_type", UWEB.BIZ_TYPE.YDWS)
                track_list = data.get("track_list", [])
                version_type = int(data.get("version_type", 0))
                logging.info("[UWEB] Lastposition request: %s", 
                             data)
            except Exception as e:
                self.write_ret(ErrorCode.ILLEGAL_DATA_FORMAT) 
                self.finish()
                return

            try:
                res = OrderedDict() 
                usable = 1
                status = ErrorCode.SUCCESS
                if data.get('tids', None):
                    terminals = []
                    for tid in data.tids:
                        terminal = QueryHelper.get_terminal_info(tid, self.db, self.redis) 
                        if terminal:
                            terminals.append(DotDict(tid=tid))
                        else:
                            logging.exception("[UWEB] tid: %s can not be found.", 
                                              tid)
                else:
                    if self.current_user.oid != UWEB.DUMMY_OID: # operator,Note: operator also has cid, so we check oid firstly.
                        groups = self.db.query("SELECT group_id "
                                               "  FROM T_GROUP_OPERATOR"
                                               "  WHERE oper_id = %s", 
                                               self.current_user.oid)
                        gids = [g.group_id for g in groups]
                        terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO"
                                                  "  WHERE (service_status = %s"
                                                  "         OR service_status = %s)"
                                                  "    AND biz_type = %s"
                                                  "    AND group_id IN %s"
                                                  "    ORDER BY LOGIN DESC",
                                                  UWEB.SERVICE_STATUS.ON, 
                                                  UWEB.SERVICE_STATUS.TO_BE_ACTIVATED, 
                                                  biz_type,
                                                  tuple(DUMMY_IDS + gids))
                    elif self.current_user.cid != UWEB.DUMMY_CID: # Corp 
                        groups = self.db.query("SELECT id gid, name "
                                               "  FROM T_GROUP"
                                               "  WHERE corp_id = %s", 
                                               self.current_user.cid)
                        gids = [g.gid for g in groups]
                        terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO"
                                                  "  WHERE (service_status = %s"
                                                  "         OR service_status = %s)"
                                                  "    AND biz_type = %s"
                                                  "    AND group_id IN %s"
                                                  "    ORDER BY LOGIN DESC",
                                                  UWEB.SERVICE_STATUS.ON, 
                                                  UWEB.SERVICE_STATUS.TO_BE_ACTIVATED, 
                                                  biz_type,
                                                  tuple(DUMMY_IDS + gids))
                    else : # individual user
                        #NOTE: only show the terminals with login_permit is 1
                        terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO"
                                                  "  WHERE (service_status = %s"
                                                  "         OR service_status = %s)"
                                                  "    AND biz_type = %s"
                                                  "    AND owner_mobile = %s"
                                                  "    AND login_permit = 1"
                                                  "    ORDER BY login DESC",
                                                  UWEB.SERVICE_STATUS.ON, 
                                                  UWEB.SERVICE_STATUS.TO_BE_ACTIVATED, 
                                                  biz_type,
                                                  self.current_user.uid)
                _now_time = time.time()
                if (_now_time - _start_time) > 5:
                    logging.info("[UWEB] Lastinfo step1 used time: %s > 5s",
                                _now_time - _start_time)

                tids = [terminal.tid for terminal in terminals]
                for tid in tids:
                    _now_time = time.time()
                    if (_now_time - _start_time) > 5:
                        logging.info("[UWEB] Lastinfo step2 used time: %s > 5s",
                                    _now_time - _start_time)

                    res[tid] = {'car_info':{},
                                'track_info':[]}

                    # 0: get group info
                    group_info = get_group_info_by_tid(self.db, tid)

                    # 1: get terminal info 
                    terminal = QueryHelper.get_terminal_info(tid, self.db, self.redis) 
                    mobile = terminal['mobile']
                    if terminal['login'] == GATEWAY.TERMINAL_LOGIN.SLEEP:
                        terminal['login'] = GATEWAY.TERMINAL_LOGIN.ONLINE

                    # 2: get location 
                    location = QueryHelper.get_location_info(tid, self.db, self.redis)
                    if location and not (location.clatitude or location.clongitude):
                        location_key = get_location_key(str(tid))
                        locations = [location,] 
                        #locations = get_locations_with_clatlon(locations, self.db) 
                        location = locations[0]
                        if location.clatitude and location.clongitude:
                            self.redis.setvalue(location_key, location, EVENTER.LOCATION_EXPIRY)

                    if location and location['name'] is None:
                        location['name'] = location['name'] if location['name'] else ''


                    if location and location['type'] == 1: # cellid
                        location['locate_error'] = 500  # mile

                    avatar_full_path, avatar_path, avatar_name, avatar_time = self.get_avatar_info(mobile)
                    service_status = QueryHelper.get_service_status_by_tmobile(self.db, mobile)

                    acc_status_info = QueryHelper.get_acc_status_info_by_tid(self.client_id, tid, self.db, self.redis)
                    acc_message = acc_status_info['acc_message']

                    car_info=dict(defend_status=terminal['defend_status'] if terminal.get('defend_status', None) is not None else 1,
                                  service_status=service_status,
                                  mannual_status=terminal['mannual_status'] if terminal.get('mannual_status', None) is not None else 1,
                                  acc_message=acc_message,
                                  fob_status=terminal['fob_status'] if terminal.get('fob_status', None) is not None else 0,
                                  timestamp=location['timestamp'] if location else 0,
                                  speed=location.speed if location else 0,
                                  # NOTE: degree's type is Decimal, float() it before json_encode
                                  degree=float(location.degree) if location else 0.00,
                                  locate_error=location.get('locate_error', 20) if location else 20,
                                  name=location.name if location else '',
                                  type=location.type if location else 1,
                                  latitude=location['latitude'] if location else 0,
                                  longitude=location['longitude'] if location else 0, 
                                  clatitude=location['clatitude'] if location else 0,
                                  clongitude=location['clongitude'] if location else 0, 
                                  login=terminal['login'] if terminal['login'] is not None else 0,
                                  bt_name=terminal.get('bt_name', '') if terminal else '',
                                  bt_mac=terminal.get('bt_mac', '') if terminal else '',
                                  dev_type=terminal['dev_type'] if terminal.get('dev_type', None) is not None else 'A',
                                  gps=terminal['gps'] if terminal['gps'] is not None else 0,
                                  gsm=terminal['gsm'] if terminal['gsm'] is not None else 0,
                                  pbat=terminal['pbat'] if terminal['pbat'] is not None else 0,
                                  mobile=terminal['mobile'],
                                  owner_mobile = terminal['owner_mobile'],
                                  alias=terminal['alias'] if terminal['alias'] else terminal['mobile'],
                                  #keys_num=terminal['keys_num'] if terminal['keys_num'] is not None else 0,
                                  keys_num=0,
                                  group_id=group_info['group_id'],
                                  group_name=group_info['group_name'],
                                  icon_type=terminal['icon_type'],
                                  fob_list=terminal['fob_list'] if terminal['fob_list'] else [],
                                  avatar_path=avatar_path,
                                  avatar_time=avatar_time)

                    res[tid]['car_info'] = car_info
                
                lastposition_key = get_lastposition_key(self.current_user.uid)
                lastposition = self.redis.get(lastposition_key)

                lastposition_time_key = get_lastposition_time_key(self.current_user.uid)
                lastposition_time = self.redis.getvalue(lastposition_time_key)

                if lastposition == str(res):  
                    pass
                else:
                    lastposition_time = int(time.time())
                    self.redis.setvalue(lastposition_key, res) 
                    self.redis.setvalue(lastposition_time_key, lastposition_time)

                query_time = data.get('lastposition_time', None)
                # 2 check whether provide usable data   
                if int(data.get('cache', 0)) == 1:  # use cache
                    if int(query_time) == lastposition_time:
                        usable = 0 
                        res = {} 
                    else: 
                        usable = 1
                        for item in track_list:
                            track_tid = item['track_tid']
                            if track_tid not in tids:
                                logging.error("The terminal with tid: %s does not exist", 
                                              track_tid)
                            else:
                                track_time = item['track_time']
                                track_key = get_track_key(track_tid)
                                self.redis.setvalue(track_key, 1, UWEB.TRACK_INTERVAL)
                                car_info = res[track_tid]['car_info']
                                endtime = int(car_info['timestamp'])-1 if car_info['timestamp'] else int(lastposition_time)-1 
                                track_info = self.get_track_info(track_tid, int(track_time)+1, endtime)
                                res[track_tid]['track_info'] = track_info
                else: 
                    usable = 1
                    for item in track_list:
                        track_tid = item['track_tid']
                        if track_tid not in tids:
                            logging.error("The terminal with tid: %s does not exist", track_tid)
                        else:
                            track_time = item['track_time']
                            track_key = get_track_key(track_tid)
                            self.redis.setvalue(track_key, 1, UWEB.TRACK_INTERVAL)
                            car_info = res[track_tid]['car_info']
                            endtime = int(car_info['timestamp'])-1 if car_info['timestamp'] else int(time.time())-1
                            track_info = self.get_track_info(track_tid, int(track_time)+1, endtime) 
                            res[track_tid]['track_info'] = track_info
               
                if int(version_type) >= 1:
                    terminals = []
                    for k, v in res.iteritems():
                        v.update({'tid':k})
                        terminals.append(v)

                    self.write_ret(status, 
                                   dict_=DotDict(terminals=terminals,
                                                 usable=usable,
                                                 lastposition_time=lastposition_time))

                else:
                    self.write_ret(status, 
                                   dict_=DotDict(res=res,
                                                 usable=usable,
                                                 lastposition_time=lastposition_time))
                _now_time = time.time()
                if (_now_time - _start_time) > 5:
                    logging.info("[UWEB] Lastinfo step3 used time: %s > 5s",
                                _now_time - _start_time)
            except Exception as e:
                logging.exception("[UWEB] uid: %s get lastposition failed. Exception: %s", 
                                  self.current_user.uid, e.args) 
                status = ErrorCode.SERVER_BUSY
                self.write_ret(status)
            IOLoop.instance().add_callback(self.finish)