def get_track(self, tid, start_time, end_time, cellid=False): """NOTE: Now, only return gps point. """ if cellid: track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name, " " type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " GROUP BY timestamp" " ORDER BY timestamp", tid, start_time, end_time) else: track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name, " " type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND category = 1" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " AND type = 0" " GROUP BY timestamp" " ORDER BY timestamp", tid, start_time, end_time) track = get_locations_with_clatlon(track, self.db) #NOTE: handle the name for t in track: if t['name'] is None: t['name'] = '' return track
def get_realtime(self, uid, sim): """Get the location of the current realtime request. workflow: if there is alive memcached, we can get location from it, else get location from db return result to user browser """ ret = DotDict(status=ErrorCode.SUCCESS, message='', location=None) location = QueryHelper.get_location_info(self.current_user.tid, self.db, self.redis) locations = [location,] locations = get_locations_with_clatlon(locations, self.db) location = locations[0] if (location and location.clatitude and location.clongitude): if not location.name: location.name = '' if location.has_key('id'): del location['id'] location['degree'] = float(location.degree) location['tid'] = self.current_user.tid ret.location = location return ret
def update_clatclon(self): # get the points without invalid clatlon within 5 second, and try to provide # valid clatlon for them logging.info("[CK] dbtask update clatclon started.") _now_time = int(time.time()) points = self.db.query("SELECT id, latitude, longitude, clatitude, clongitude, type, timestamp" " FROM T_LOCATION" " WHERE (clatitude = 0 or clongitude =0) " " AND latitude != 0 AND longitude !=0 " " AND (timestamp between %s and %s)", _now_time - 5, _now_time) logging.info("[CK] Time: %s, the number of points without valid claton: %s.", _now_time, len(points)) get_locations_with_clatlon(points, self.db)
def post(self): """Get latest location of a terminal. """ status = ErrorCode.SUCCESS res = {} try: data = DotDict(json_decode(self.request.body)) mobile = str(data.mobile) token = data.token logging.info("[REALTIME] Request, data:%s", data) except Exception as e: status = ErrorCode.DATA_FORMAT_ILLEGAL logging.exception("[REALTIME] Invalid data format, body: %s, mobile: %s.", self.request.body, mobile) self.write_ret(status) return try: status = self.basic_check(token, mobile) if status != ErrorCode.SUCCESS: self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal.tid location = QueryHelper.get_location_info(tid, self.db, self.redis) # check and make name valid if location and location['name'] is None: location['name'] = '' # check and make clatclon valid locations = [location,] locations = get_locations_with_clatlon(locations, self.db) location = locations[0] if (location and location.clatitude and location.clongitude): res = dict(lon=location.get('longitude', 0), lat=location.get('latitude', 0), clon=location.get('clongitude', 0), clat=location.get('clatitude', 0), timestamp=location.get('timestamp',0), name=location.get('name',''), type=location.get('type',0)) self.write_ret(status, dict_=dict(res=res)) except Exception as e: logging.exception("[REALTIME] sid: %s. Exception: %s", mobile, e.args) status = ErrorCode.FAILED self.write_ret(status)
def post(self): res = list() try: corp_id = 13726103889 #开发区执法局 begin_time = self.redis.getvalue('last_time') end_time = time.time() self.redis.setvalue('last_time', end_time) if (end_time - begin_time) < 14*60: # 14 分钟 logging.info("[UWEB] ZFJ request too frequency," " skip it, begin time:%s, end time:%s", begin_time, end_time) self.write({'res':res}) return logging.info("[UWEB] ZFJ request, begin time:%s, end time:%s", begin_time, end_time) if begin_time: terminals = QueryHelper.get_terminals_by_cid(corp_id,self.db) for terminal in terminals: mobile = terminal['mobile'] tid = terminal['tid'] positions = self.db.query("SELECT id, latitude, longitude," " clatitude, clongitude, timestamp" " FROM T_LOCATION" " WHERE tid=%s" " AND timestamp BETWEEN %s AND %s" " AND latitude != 0" " AND longitude != 0" " ORDER BY timestamp", tid, begin_time , end_time) _start_time = time.time() positions = get_locations_with_clatlon(positions, self.db) _now_time = time.time() if _now_time - _start_time > 3: # 3 seconds logging.info("[UWEB] Track offset used time: %s s, tid: %s, cid: %s", _now_time - _start_time, tid, corp_id) res.append({'mobile':mobile, 'positions':positions}) self.write({'res':res}) except Exception as e: logging.exception("[UWEB] zfjsyncer: get location failed. Exception: %s", e.args)
def get_track_name(self, location): """Get name of a location which comes from a track. @param: location, {'name':'', 'clatitude':'', 'clongitude':'', } """ if not location: return '' if not location['name']: if location['clatitude'] and location['clongitude']: pass else: track = get_locations_with_clatlon([location,], self.db) location = track[0] name = get_location_name(location['clatitude'], location['clongitude'], self.redis) if name: location['name'] = name self.db.execute("UPDATE T_LOCATION SET name = %s WHERE id = %s", name, location['id']) return location['name'] if location['name'] else ''
def get_track_info(self, tid, begintime, endtime): track_info = [] track = self.db.query("SELECT id, latitude, longitude," " clatitude, clongitude, type, timestamp" " 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", tid, begintime, endtime) track = get_locations_with_clatlon(track, self.db) for t in track: if t['clatitude'] and t['clongitude']: point = dict(latitude=t['latitude'], longitude=t['longitude'], clatitude=t['clatitude'], clongitude=t['clongitude'], type=t['type'], timestamp=t['timestamp']) track_info.append(point) return track_info
def handle_report_info(self, info): """These reports should be handled here: POWERLOW/POWERFULL/POWEROFF/POWERDOWN ILLEGALMOVE ILLEGALSHAKE EMERGENCY REGION_ENTER REGION_OUT STOP SPEED_LIMIT """ if not info: return # 1: get available location from lbmphelper report = lbmphelper.handle_location(info, self.redis, cellid=True, db=self.db) if not (report['cLat'] and report['cLon']): #NOTE: Get latest location last_location = QueryHelper.get_location_info(report.dev_id, self.db, self.redis) if last_location: #NOTE: Try to make the location is complete. locations = [last_location,] locations = lbmphelper.get_locations_with_clatlon(locations, self.db) last_location = locations[0] report['lat'] = last_location['latitude'] report['lon'] = last_location['longitude'] report['cLat'] = last_location['clatitude'] report['cLon'] = last_location['clongitude'] report['name'] = last_location['name'] report['type'] = last_location['type'] logging.info("[EVENTER] The report has invalid location and use last_location. report: %s", report) else: logging.info("[EVENTER] The report has invalid location and last_location is invalid. report: %s", report) current_time = int(time.time()) alarm_mobile = get_alarm_mobile(report.dev_id, self.db, self.redis) #NOTE: in pvt, timestamp is no used, so use gps_time as timestamp if not report.get('timestamp',None): report['timestamp'] = report['gps_time'] if report['timestamp'] > (current_time + 24*60*60): logging.info("[EVENTER] The report's (gps_time - current_time) is more than 24 hours, so drop it:%s", report) return #NOTE: If undefend, just save location into db if info['rName'] in [EVENTER.RNAME.ILLEGALMOVE, EVENTER.RNAME.ILLEGALSHAKE]: if str(info.get('is_notify','')) == '1': # send notify even if CF logging.info("[EVENTER] Send notify forever, go ahead. Terminal: %s, is_notify: %s", report.dev_id, info.get('is_notify','')) elif alarm_mobile: logging.info("[EVENTER] Send notify forever , go ahead. Terminal: %s, alarm_mobile: %s", report.dev_id, alarm_mobile) else: mannual_status = QueryHelper.get_mannual_status_by_tid(info['dev_id'], self.db) if int(mannual_status) == UWEB.DEFEND_STATUS.NO: report['category'] = EVENTER.CATEGORY.REALTIME insert_location(report, self.db, self.redis) update_terminal_dynamic_info(self.db, self.redis, report) logging.info("[EVENTER] %s mannual_status is undefend, drop %s report.", info['dev_id'], info['rName']) return if info['rName'] in [EVENTER.RNAME.POWERDOWN, EVENTER.RNAME.POWERLOW]: # if alert_freq_key is exists,return alert_freq_key = get_alert_freq_key(report.dev_id + info['rName']) alert_freq = QueryHelper.get_alert_freq_by_tid(info['dev_id'], self.db) if alert_freq != 0: if self.redis.exists(alert_freq_key): logging.info("[EVENTER] Don't send duplicate %s alert to terminal:%s in %s seconds", info["rName"], report.dev_id, alert_freq) return else: self.redis.setvalue(alert_freq_key, 1, time=alert_freq) #NOTE: keep alarm info alarm = dict(tid=report['dev_id'], category=report['category'], type=report['type'], timestamp=report.get('timestamp',0), latitude=report.get('lat',0), longitude=report.get('lon',0), clatitude=report.get('cLat',0), clongitude=report.get('cLon',0), name=report['name'] if report.get('name',None) is not None else '', degree=report.get('degree',0), speed=report.get('speed',0)) if info['rName'] in [EVENTER.RNAME.REGION_OUT, EVENTER.RNAME.REGION_ENTER]: region = report['region'] alarm['region_id'] = region.region_id record_alarm_info(self.db, self.redis, alarm) # 2: save into database. T_LOCATION, T_EVENT lid = insert_location(report, self.db, self.redis) update_terminal_dynamic_info(self.db, self.redis, report) self.event_hook(report.category, report.dev_id, report.get('terminal_type',1), report.get('timestamp'), lid, report.pbat, report.get('fobid'), report.get('region_id', -1)) # 3: notify the owner user = QueryHelper.get_user_by_tid(report.dev_id, self.db) if not user: logging.error("[EVENTER] Cannot find USER of terminal: %s", report.dev_id) return # send sms to owner if report.rName in [EVENTER.RNAME.STOP]: logging.info("[EVENTER] %s alert needn't to push to user. Terminal: %s", report.rName, report.dev_id) return #NOTE: notify user by sms sms_option = QueryHelper.get_sms_option_by_uid(user.owner_mobile, EVENTER.SMS_CATEGORY[report.rName].lower(), self.db) if sms_option == UWEB.SMS_OPTION.SEND: logging.info("[EVENTER] Notify report to user by sms. category: %s, tid: %s, mobile: %s", report.rName, report.dev_id, user['owner_mobile']) self.notify_report_by_sms(report, user['owner_mobile']) else: logging.info("[EVENTER] Remind option of %s is closed. Terminal: %s", report.rName, report.dev_id) if alarm_mobile: logging.info("[EVENTER] Notify report to user by sms. category: %s, tid: %s, alarm_mobile: %s", report.rName, report.dev_id, alarm_mobile) self.notify_report_by_sms(report, alarm_mobile) #NOTE: notify user by push terminal = self.db.get("SELECT push_status FROM T_TERMINAL_INFO" " WHERE tid = %s", report.dev_id) if terminal and terminal.push_status == 1: logging.info("[EVENTER] Notify report to user by push. category: %s, tid: %s, mobile: %s", report.rName, report.dev_id, user['owner_mobile']) self.notify_report_by_push(report, user['owner_mobile']) else: logging.info("[EVENTER] Push option of %s is closed. Terminal: %s", report.rName, report.dev_id) #NOTE: notify alarm_mobile if alarm_mobile: logging.info("[EVENTER] Notify report to user by push. category: %s, tid: %s, alarm_mobile: %s", report.rName, report.dev_id, alarm_mobile) self.notify_report_by_push(report, alarm_mobile)
def _on_finish(db): self.db = db if cellid_flag == 1: # gps track and cellid track track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name," " type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " GROUP BY timestamp" " ORDER BY timestamp", self.current_user.tid, start_time, end_time) else: # cellid_flag is None or 0, only gps track track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name, " " type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " AND type = 0" " GROUP BY timestamp" " ORDER BY timestamp", self.current_user.tid, start_time, end_time) # check track point count if track and len(track) > 500 and network_type == 0: logging.info("[UWEB] The %s track points length is: %s, " " and the newtork type is too low, so return error.", tid, len(track)) self.write_ret(ErrorCode.TRACK_POINTS_TOO_MUCH) self.finish() return # NOTE: if latlons are legal, but clatlons are illlegal, offset # them and update them in db. _start_time = time.time() track = get_locations_with_clatlon(track, self.db) _now_time = time.time() if _now_time - _start_time > 3: # 3 seconds logging.info("[UWEB] Track offset used time: %s s, tid: %s, cid: %s", _now_time - _start_time, self.current_user.tid, self.current_user.cid) # NOTE: filter point without valid clat and clon _track = [] for t in track: if t['clongitude'] and ['clatitude']: _track.append(t) else: logging.info("[UWEB] Invalid point: %s, drop it, cid: %s", t, self.current_user.cid) track = _track # add idle_points # track1, track2, track3,... # when the distance between two points is larger than 10 meters and the interval is less than 5 minutes, # they are regarded as idle_points idle_lst = [] idle_points = [] for i, item_start in enumerate(track): is_idle = False if i in idle_lst: continue for j in range(i + 1, len(track)): item_end = track[j] distance = get_distance( item_start.clongitude, item_start.clatitude, item_end.clongitude, item_end.clatitude) if distance >= UWEB.IDLE_DISTANCE: break else: idle_time = item_end[ 'timestamp'] - item_start['timestamp'] item_start['idle_time'] = idle_time item_start['start_time'] = item_start['timestamp'] item_start['end_time'] = item_end['timestamp'] idle_lst.append(j) is_idle = True if is_idle and item_start['idle_time'] > UWEB.IDLE_INTERVAL: idle_points.append(item_start) # modify name & degere for item in track: item['degree'] = float(item['degree']) if item.name is None: item['name'] = '' # organize and store the data to be downloaded m = hashlib.md5() m.update(self.request.body) hash_ = m.hexdigest() mem_key = self.KEY_TEMPLATE % (self.current_user.uid, hash_) res = [] if idle_lst: point_begin = dict(label=u'起点', start_time=utc_to_date( track[0]['timestamp']), end_time='', name=track[0]['name']) point_idle = [] for idle_point in idle_points: idle_label = seconds_to_label(idle_point['idle_time']) label = u'停留' + idle_label point = dict(label=label, start_time=utc_to_date( idle_point['start_time']), end_time=utc_to_date(idle_point['end_time']), name=idle_point['name']) point_idle.append(point) point_end = dict(label=u'终点', start_time=utc_to_date( track[-1]['timestamp']), end_time='', name=track[-1]['name']) res.append(point_begin) res.append(point_end) res.extend(point_idle) self.redis.setvalue(mem_key, res, time=UWEB.STATISTIC_INTERVAL) logging.info( "[UEB] Tid:%s track query, returned %s points.", self.current_user.tid, len(track)) self.write_ret(status, dict_=DotDict(track=track, idle_points=idle_points, hash_=hash_)) self.finish()
def _on_finish(db): try: status = ErrorCode.SUCCESS self.db = db res = [] stop = [] track = [] track_sample = UWEB.TRACK_SAMPLE.NO # 2014.08.01 a week. if ((start_time < LIMIT.MASS_POINT_QUERY_TIME) and ((end_time - start_time) > LIMIT.MASS_POINT_QUERY_INTERVAL)): status = ErrorCode.MASS_POINT_QUERY_EXCESS self.write_ret(status) self.finish() return for item in range(days): timestamp = start_time + 1 * 60 * 60 * 24 * (item) date = get_date_from_utc(timestamp) year, month, day = date.year, date.month, date.day start_time_, end_time_ = start_end_of_day( year=year, month=month, day=day) # NOTE: handle for the first and last point if item == 0: start_time_ = start_time elif item == days - 1: end_time_ = end_time if cellid_flag == 1: # cellid track = self.get_track( tid, start_time_, end_time_, cellid=True) else: # gps # cellid_flag is None or 0, only gps track track = self.get_track( tid, start_time_, end_time_, cellid=False) if track: last_point = track[-1] last_point = get_locations_with_clatlon( [last_point], self.db)[0] distance = self.get_track_distance(track) r = dict(timestamp=last_point.timestamp, distance=distance, latitude=last_point.latitude, longitude=last_point.longitude, clatitude=last_point.clatitude, clongitude=last_point.clongitude, name=self.get_track_name(last_point)) else: r = dict(timestamp=end_time_, distance=0, latitude=0, longitude=0, clatitude=0, clongitude=0, name=u'') res.append(r) if cellid_flag == 1: # cellid track = self.get_track( tid, start_time, end_time, cellid=True) else: # gps # cellid_flag is None or 0, only gps track track = self.get_track( tid, start_time, end_time, cellid=False) if len(track) > LIMIT.MASS_POINT_NUMBER: # > 1000 track_sample = UWEB.TRACK_SAMPLE.YES track = get_sampled_list(track, LIMIT.MASS_POINT_NUMBER) stop = self.get_stop_point(tid, start_time, end_time) res.reverse() stop.reverse() self.write_ret(status, dict_=DotDict(res=res, stop=stop, track=track, track_sample=track_sample)) self.finish() except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[UWEB] Mass-point day request failed. " " uid: %s, tid: %s. Exception: %s.", self.current_user.uid, tid, e.args) self.write_ret(status) self.finish()
def post(self): logging.info("[UWEB] Android login test") status = ErrorCode.SUCCESS cid = UWEB.DUMMY_CID oid = UWEB.DUMMY_OID uid = ConfHelper.UWEB_CONF.test_uid tid = ConfHelper.UWEB_CONF.test_tid sim = ConfHelper.UWEB_CONF.test_sim version_type = int(self.get_argument("version_type", 0)) biz_type = UWEB.BIZ_TYPE.YDWS self.bookkeep(dict(cid=cid, oid=oid, uid=uid, tid=tid, sim=sim)) user_info = QueryHelper.get_user_by_uid(uid, self.db) # NOTE: add cars_info, it's same as lastinfo cars_info = {} terminals = QueryHelper.get_terminals_by_uid(uid, biz_type, self.db) for terminal in terminals: # 1: get terminal tid = terminal.tid group_info = get_group_info_by_tid(self.db, tid) terminal_info_key = get_terminal_info_key(tid) terminal_cache = self.redis.getvalue(terminal_info_key) if terminal_cache: terminal['gps'] = terminal_cache['gps'] terminal['gsm'] = terminal_cache['gsm'] terminal['pbat'] = terminal_cache['pbat'] mobile = terminal['mobile'] terminal['keys_num'] = 0 if terminal['login'] == GATEWAY.TERMINAL_LOGIN.SLEEP: terminal['login'] = GATEWAY.TERMINAL_LOGIN.ONLINE # NOTE: if alias is null, provide cnum or sim instead terminal['alias'] = QueryHelper.get_alias_by_tid( tid, self.redis, self.db) fobs = self.db.query("SELECT fobid FROM T_FOB" " WHERE tid = %s", tid) terminal['fob_list'] = [fob.fobid for fob in fobs] terminal['sim'] = terminal['mobile'] # 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'] = '' avatar_name, avatar_path, avatar_full_path, avatar_time = self.get_avatar_info( mobile) service_status = QueryHelper.get_service_status_by_tmobile( self.db, mobile) car_dct = {} if location and location['type'] == 1: # cellid location['locate_error'] = 500 # mile car_info = dict(defend_status=terminal['defend_status'] if terminal['defend_status'] is not None else 1, service_status=service_status, 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, bt_name=terminal['bt_name'] if terminal.get('bt_name', None) is not None else '', bt_mac=terminal['bt_mac'] if terminal.get('bt_mac', None) is not None else '', dev_type=terminal['dev_type'] if terminal.get('dev_type', None) is not None else 'A', 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, 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, group_id=group_info['group_id'], group_name=group_info['group_name'], icon_type=terminal['icon_type'], avatar_path=avatar_path, avatar_time=avatar_time, fob_list=terminal['fob_list'] if terminal['fob_list'] else []) car_dct[tid] = car_info cars_info.update(car_dct) #push_info = NotifyHelper.get_push_info() push_id = uid push_key = NotifyHelper.get_push_key(push_id, self.redis) lastinfo_time_key = get_lastinfo_time_key(uid) lastinfo_time = self.redis.getvalue(lastinfo_time_key) push = dict(id='', key='') t = int(time.time()) * 1000 push_key = get_push_key(uid, t) json_data = WSPushHelper.register_wspush(uid, self.redis) data = json_data['data'] if data: id = data.get('push_id', '') key = data.get('psd', '') push['id'] = id push['key'] = key if version_type >= 1: terminals = [] for k, v in cars_info.iteritems(): v.update({'tid': k}) terminals.append(v) self.write_ret(status, dict_=DotDict(wspush=push, push_id=push_id, push_key=push_key, name=user_info.name if user_info else uid, user_type=UWEB.USER_TYPE.PERSON, lastinfo_time=lastinfo_time, terminals=terminals)) else: self.write_ret(status, dict_=DotDict(wspush=push, push_id=push_id, # app_key=push_info.app_key, push_key=push_key, name=user_info.name if user_info else uid, user_type=UWEB.USER_TYPE.PERSON, cars_info=cars_info, lastinfo_time=lastinfo_time, cars=terminals,))
def post(self): username = self.get_argument("username") password = self.get_argument("password") user_type = self.get_argument("user_type", UWEB.USER_TYPE.PERSON) biz_type = self.get_argument("biz_type", UWEB.BIZ_TYPE.YDWS) devid = self.get_argument("devid", "") versionname = self.get_argument("versionname", "") version_type = int(self.get_argument("version_type", 0)) logging.info("[UWEB] Android login request, username: %s, password: %s, user_type: %s, devid: %s", username, password, user_type, devid) # must check username and password avoid sql injection. if not (username.isalnum() and password.isalnum()): status = ErrorCode.ILLEGAL_LABEL self.write_ret(status) return # check the user, return uid, tid, sim and status cid, oid, uid, terminals, user_type, status = self.login_passwd_auth(username, password, user_type) logging.info( "[UWEB] Login auth, cid:%s, oid:%s, uid:%s, user_type:%s", cid, oid, uid, user_type) if status == ErrorCode.SUCCESS: # role: 0: person; 1: operator; 2: enterprise # method 0: web; 1: android; 2: ios # NOTE: keep the login log login_info = dict(uid=uid, role=0, method=1, versionname=versionname) record_login_user(login_info, self.db) self.bookkeep(dict(cid=cid, oid=oid, uid=uid, tid=terminals[0].tid, sim=terminals[0].sim)) user_info = QueryHelper.get_user_by_uid(uid, self.db) # NOTE: add cars_info, it's same as lastinfo cars_info = {} if user_type == UWEB.USER_TYPE.PERSON: terminals = QueryHelper.get_terminals_by_uid(uid, biz_type, self.db) elif user_type == UWEB.USER_TYPE.OPERATOR: terminals = QueryHelper.get_terminals_by_oid(oid, biz_type, self.db) elif user_type == UWEB.USER_TYPE.CORP: terminals = QueryHelper.get_terminals_by_cid(cid, biz_type, self.db) else: logging.error("[UWEB] Invalid user_type: %s", user_type) for terminal in terminals: # 1: get terminal tid = terminal.tid group_info = get_group_info_by_tid(self.db, tid) terminal_info_key = get_terminal_info_key(tid) terminal_cache = self.redis.getvalue(terminal_info_key) if terminal_cache: terminal['gps'] = terminal_cache['gps'] terminal['gsm'] = terminal_cache['gsm'] terminal['pbat'] = terminal_cache['pbat'] mobile = terminal['mobile'] terminal['keys_num'] = 0 if terminal['login'] == GATEWAY.TERMINAL_LOGIN.SLEEP: terminal['login'] = GATEWAY.TERMINAL_LOGIN.ONLINE # NOTE: if alias is null, provide cnum or sim instead terminal['alias'] = QueryHelper.get_alias_by_tid( tid, self.redis, self.db) fobs = self.db.query("SELECT fobid FROM T_FOB" " WHERE tid = %s", tid) terminal['fob_list'] = [fob.fobid for fob in fobs] terminal['sim'] = terminal['mobile'] # 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'] = '' avatar_name, avatar_path, avatar_full_path, avatar_time = self.get_avatar_info(mobile) service_status = QueryHelper.get_service_status_by_tmobile( self.db, mobile) car_dct = {} if location and location['type'] == 1: # cellid location['locate_error'] = 500 # mile car_info = dict(defend_status=terminal['defend_status'] if terminal['defend_status'] is not None else 1, service_status=service_status, 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, bt_name=terminal['bt_name'] if terminal.get('bt_name', None) is not None else '', bt_mac=terminal['bt_mac'] if terminal.get('bt_mac', None) is not None else '', dev_type=terminal['dev_type'] if terminal.get('dev_type', None) is not None else 'A', 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, 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, group_id=group_info['group_id'], group_name=group_info['group_name'], icon_type=terminal['icon_type'], avatar_path=avatar_path, avatar_time=avatar_time, fob_list=terminal['fob_list'] if terminal['fob_list'] else []) car_dct[tid] = car_info cars_info.update(car_dct) #push_info = NotifyHelper.get_push_info() push_id = devid if devid else uid push_key = NotifyHelper.get_push_key(push_id, self.redis) lastinfo_time_key = get_lastinfo_time_key(uid) lastinfo_time = self.redis.getvalue(lastinfo_time_key) # uid --> android_push_list # check push_id whether exists in a old android_push_list old_android_push_list_key = self.redis.get(push_id) if old_android_push_list_key: old_android_push_list = self.redis.getvalue( old_android_push_list_key) if not isinstance(old_android_push_list, list): self.redis.delete(old_android_push_list_key) old_android_push_list = [] if old_android_push_list and (push_id in old_android_push_list): logging.info("[UWEB] push_id:%s has existed in a old_android_push_list: %s, so remove push_id from the list.", push_id, old_android_push_list) old_android_push_list.remove(push_id) self.redis.set(old_android_push_list_key, old_android_push_list) android_push_list_key = get_android_push_list_key(uid) android_push_list = self.redis.getvalue(android_push_list_key) android_push_list = android_push_list if android_push_list else [] if push_id not in android_push_list: android_push_list.append(push_id) self.redis.set(android_push_list_key, android_push_list) self.redis.set(push_id, android_push_list_key) logging.info("[UWEB] uid: %s, android_push_lst: %s", username, android_push_list) if user_info: self.login_sms_remind(uid, user_info.mobile, terminals, login="******") else: pass # corp maybe no user_info push = dict(id='', key='') json_data = WSPushHelper.register_wspush(uid, self.redis) data = json_data['data'] if data: id = data.get('push_id', '') key = data.get('psd', '') push['id'] = id push['key'] = key if version_type >= 1: terminals = [] for k, v in cars_info.iteritems(): v.update({'tid': k}) terminals.append(v) self.write_ret(status, dict_=DotDict(wspush=push, push_id=push_id, push_key=push_key, name=user_info.name if user_info else username, user_type=user_type, terminals=terminals, lastinfo_time=lastinfo_time,)) else: self.write_ret(status, dict_=DotDict(wspush=push, push_id=push_id, # app_key=push_info.app_key, push_key=push_key, name=user_info.name if user_info else username, user_type=user_type, cars_info=cars_info, lastinfo_time=lastinfo_time, cars=terminals)) else: logging.info("[UWEB] username: %s login failed, message: %s", username, ErrorCode.ERROR_MESSAGE[status]) self.write_ret(status)
def request_realtime(self, query, callback=None): """ All realtime requests in REALTIME_VALID_INTERVAL will be considered as only one. If not, invoke gf and use handle_location of lbmphelper. """ location = QueryHelper.get_location_info(self.current_user.tid, self.db, self.redis) if location and location['name'] is None: location['name'] = '' ret = DotDict(status=ErrorCode.SUCCESS, message='', location=None) locations = [location,] locations = get_locations_with_clatlon(locations, self.db) location = locations[0] if (location and location.clatitude and location.clongitude): if location.has_key('id'): del location['id'] location['degree'] = float(location.degree) location['tid'] = self.current_user.tid ret.location = location if callback: callback(ret) return lat, lon = get_latlon_from_cellid(0,0,0,0, self.current_user.sim) clat, clon = get_clocation_from_ge([lat,],[lon,]) clat = int(clat[0]) if len(clat)>0 else 0 clon = int(clon[0]) if len(clon)>0 else 0 name = get_location_name(clat, clon, self.redis) location = DotDict(category = 1, # cellid dev_id = self.current_user.tid, lat = lat, lon = lon, cLat = clat, cLon = clon, alt = 0, gps_time = int(time.time()), type = 1, speed = 0.0, degree = 0.0, name = name, cellid = None, locate_error = 20) if clat and clon: ret.location = DotDict() ret.location.latitude = lat ret.location.longitude = lon ret.location.clongitude = clon ret.location.clatitude = clat ret.location.timestamp = int(time.time()) ret.location.name = name ret.location.speed = 0 ret.location.type = 1 ret.location.tid = self.current_user.tid ret.location.degree = 0.0 ret.location.locte_error = 20 insert_location(location, self.db, self.redis) logging.info("[UWEB] tid %s cellid query success", self.current_user.tid) else: ret.status = ErrorCode.LOCATION_CELLID_FAILED ret.message = ErrorCode.ERROR_MESSAGE[ret.status] logging.info("[UWEB] Do not find any location, and cellid query failed. tid: %s", self.current_user.tid) if callback: callback(ret)
def _on_finish(db): try: status = ErrorCode.SUCCESS self.db = db track = [] track_sample = UWEB.TRACK_SAMPLE.NO stop = [] start = {} end = {} # 2014.08.01 a week. if start_time < LIMIT.MASS_POINT_QUERY_TIME and (end_time-start_time) > LIMIT.MASS_POINT_QUERY_INTERVAL: status = ErrorCode.MASS_POINT_QUERY_EXCESS self.write_ret(status, dict_=DotDict(track=track, stop=stop, start=start, end=end)) self.finish() return if cellid_flag == 1: # cellid track = self.get_track(tid, start_time, end_time, cellid=True) else: # gps # cellid_flag is None or 0, only gps track track = self.get_track(tid, start_time, end_time, cellid=False) if track: # NOTE: if latlons are legal, but clatlons are illlegal, offset # them and update them in db. _start_time = time.time() track = get_locations_with_clatlon(track, self.db) _now_time = time.time() if _now_time - _start_time > 3: # 3 seconds logging.info("[UWEB] Track offset used time: %s s, tid: %s, cid: %s", _now_time - _start_time, tid, self.current_user.cid) # NOTE: filter point without valid clat and clon _track = [] for t in track: if t['clongitude'] and ['clatitude']: _track.append(t) else: logging.info("[UWEB] Invalid point: %s, drop it, cid: %s", t, self.current_user.cid) track = _track stop = self.get_stop_point(tid, start_time, end_time) if stop: # some handle for the stop oldest_stop = stop[0] newest_stop = stop[-1] starts = self.get_track(tid, start_time, newest_stop['start_time']) if starts: distance = self.get_track_distance(starts) lid=starts[0]['id'] location = self.db.get("SELECT * FROM T_LOCATION" " WHERE id = %s AND tid = %s LIMIT 1", lid, tid) _start_time=starts[0]["timestamp"] _end_time=starts[-1]["timestamp"] start = dict(lid=lid, tid=tid, start_time=_start_time, end_time=0, idle_time=abs(_end_time-_start_time), name=self.get_track_name(location), longitude=location['longitude'], latitude=location['latitude'], clongitude=location['clongitude'], clatitude=location['clatitude'], distance=distance) oldest_stop['distance'] = self.get_track_distance( self.get_track( tid, start['start_time'], oldest_stop['start_time'])) ends = self.get_track(tid, newest_stop['start_time'], end_time) if ends: lid=ends[-1]['id'] location = self.db.get("SELECT * FROM T_LOCATION" " WHERE id = %s", lid) _start_time=ends[-1]["timestamp"] _end_time=int(time.time()) end = dict(lid=lid, tid=tid, start_time=_start_time, end_time=_end_time, idle_time=abs(_end_time-_start_time), name=self.get_track_name(location), longitude=location['longitude'], latitude=location['latitude'], clongitude=location['clongitude'], clatitude=location['clatitude'], distance=0) #NOTE: special handle for end time end['distance'] = self.get_track_distance( self.get_track(tid, newest_stop['start_time'], end['start_time'])) #NOTE: last point is a stop if newest_stop['end_time'] == 0: #if location['speed'] < LIMIT.SPEED_LIMIT: # may be stop if location['speed'] <= LIMIT.SPEED_LIMIT: # may be stop # end_time of last one of track is useless newest_stop['end_time'] = end['start_time'] newest_stop['name'] = end['name'] newest_stop['idle_time'] = abs(newest_stop['end_time']-newest_stop['start_time']) newest_stop['distance'] = newest_stop['distance'] + self.get_track_distance( self.get_track(tid, newest_stop['start_time'], end['start_time'])) # special handle for the last point newest_stop['start_time'] = end['start_time'] end = newest_stop stop.pop() else: # it should never happpen pass # full the stop for item in stop: #NOTE: end_time is should be bigger than start_time item['idle_time'] = abs(item['end_time']-item['start_time']) if item.name is None: name = self.get_track_name(item) if start and stop: if start['start_time'] == oldest_stop['start_time']: start = {} if end and stop: if end['start_time'] == newest_stop['start_time']: end = {} if track and (not stop): start = dict(lid=track[0]['id'], tid=tid, start_time=track[0]['timestamp'], end_time=0, idle_time=0, name=self.get_track_name(track[0]), longitude=track[0]['longitude'], latitude=track[0]['latitude'], clongitude=track[0]['clongitude'], clatitude=track[0]['clatitude'], distance=0) end = dict(lid=track[-1]['id'], tid=tid, start_time=track[-1]['timestamp'], end_time=0, idle_time=0, name=self.get_track_name(track[-1]), longitude=track[-1]['longitude'], latitude=track[-1]['latitude'], clongitude=track[-1]['clongitude'], clatitude=track[-1]['clatitude'], distance=0) end['distance'] = self.get_track_distance( self.get_track(tid, start['start_time'], end['start_time'])) # special handle for the newest stop if stop and end: if stop[-1]['end_time'] > end['start_time']: stop[-1]['end_time'] = end['start_time'] stop[-1]['idle_time'] = abs(end['start_time']-stop[-1]['start_time']) # modify name & degere for item in track: item['degree'] = float(item['degree']) # if start is equal end, just provide start if start and end: if start['start_time'] == end['start_time']: end = {} if len(track) > LIMIT.MASS_POINT_NUMBER: # > 1000 track_sample = UWEB.TRACK_SAMPLE.YES track = get_sampled_list(track, LIMIT.MASS_POINT_NUMBER) logging.info("[UEB] Tid:%s mass point query, returned %s points.", tid, len(track)) # reverse the stop stop.reverse() # NOTE: replace null with '' for item in track: if item.name is None: item['name'] = '' self.write_ret(status, dict_=DotDict(track=track, track_sample=track_sample, stop=stop, start=start, end=end)) self.finish() except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[UWEB] Mass-point basic request failed. uid: %s, tid: %s. Exception: %s.", self.current_user.uid, tid, e.args ) self.write_ret(status) self.finish()
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)
def _on_finish(db): status = ErrorCode.SUCCESS self.db = db track = [] stop = [] start = {} end = {} mass_point = 0 if start_time < LIMIT.MASS_POINT_QUERY_TIME and (end_time-start_time) > LIMIT.MASS_POINT_QUERY_INTERVAL: status = ErrorCode.MASS_POINT_QUERY_EXCESS self.write_ret(status, dict_=DotDict(track=track, stop=stop, start=start, end=end, mass_point=mass_point)) self.finish() return # check track point count if (end_time-start_time) > LIMIT.MASS_POINT_INTERVAL : # mass point #if len(track) > LIMIT.MASS_POINT_NUMBER or (end_time-start_time) > LIMIT.MASS_POINT_INTERVAL : # mass point mass_point = 1 track = [] logging.info("[UWEB] mass point. tid:%s.", tid) else: # general point if cellid_flag == 1: # cellid track = self.get_track(tid, start_time, end_time, cellid=True) else: # gps # cellid_flag is None or 0, only gps track track = self.get_track(tid, start_time, end_time, cellid=False) if track: # NOTE: if latlons are legal, but clatlons are illlegal, offset # them and update them in db. _start_time = time.time() track = get_locations_with_clatlon(track, self.db) _now_time = time.time() if _now_time - _start_time > 3: # 3 seconds logging.info("[UWEB] Track offset used time: %s s, tid: %s, cid: %s", _now_time - _start_time, tid, self.current_user.cid) # NOTE: filter point without valid clat and clon _track = [] for t in track: if t['clongitude'] and ['clatitude']: _track.append(t) else: logging.info("[UWEB] Invalid point: %s, drop it, cid: %s", t, self.current_user.cid) track = _track stop = self.db.query("SELECT ts.id, ts.lid, ts.start_time," " ts.end_time, ts.distance," " tl.latitude, tl.longitude, " " tl.clatitude, tl.clongitude, " " tl.name, tl.degree, tl.speed, tl.locate_error" " FROM T_STOP AS ts, T_LOCATION AS tl" " WHERE ts.tid = %s" " AND ts.lid = tl.id " " AND ts.start_time BETWEEN %s AND %s" " AND ts.end_time !=0" " AND ts.distance !=0" " ORDER BY ts.start_time ASC", tid, start_time, end_time) if stop: # some handle for the stop oldest_stop = stop[0] newest_stop = stop[-1] starts = self.get_track(tid, start_time, newest_stop['start_time']) if starts: distance = self.get_track_distance(starts) lid=starts[0]['id'] location = self.db.get("SELECT * FROM T_LOCATION WHERE id = %s", lid) _start_time=starts[0]["timestamp"] _end_time=starts[-1]["timestamp"] start = dict(lid=lid, tid=tid, start_time=_start_time, end_time=0, idle_time=abs(_end_time-_start_time), name=self.get_track_name(location), longitude=location['longitude'], latitude=location['latitude'], clongitude=location['clongitude'], clatitude=location['clatitude'], distance=distance) oldest_stop['distance'] = self.get_track_distance( self.get_track( tid, start['start_time'], oldest_stop['start_time'])) ends = self.get_track(tid, newest_stop['start_time'], end_time) if ends: lid=ends[-1]['id'] location = self.db.get("SELECT * FROM T_LOCATION " " WHERE id = %s", lid) _start_time=ends[-1]["timestamp"] _end_time=int(time.time()) end = dict(lid=lid, tid=tid, start_time=_start_time, end_time=_end_time, idle_time=abs(_end_time-_start_time), name=self.get_track_name(location), longitude=location['longitude'], latitude=location['latitude'], clongitude=location['clongitude'], clatitude=location['clatitude'], distance=0) #NOTE: special handle for end time end['distance'] = self.get_track_distance( self.get_track(tid, newest_stop['start_time'], end['start_time'])) #NOTE: last point is a stop if newest_stop['end_time'] == 0: #if location['speed'] < LIMIT.SPEED_LIMIT: # may be stop if location['speed'] <= LIMIT.SPEED_LIMIT: # may be stop # end_time of last one of track is useless newest_stop['end_time'] = end['start_time'] newest_stop['name'] = end['name'] newest_stop['idle_time'] = abs(newest_stop['end_time']-newest_stop['start_time']) newest_stop['distance'] = newest_stop['distance'] + self.get_track_distance( self.get_track(tid, newest_stop['start_time'], end['start_time'])) # special handle for the last point newest_stop['start_time'] = end['start_time'] end = newest_stop stop.pop() else: # it should never happpen pass for item in stop: #NOTE: end_time is should be bigger than start_time item['idle_time'] = abs(item['end_time']-item['start_time']) if start and stop: if start['start_time'] == oldest_stop['start_time']: start = {} if end and stop: if end['start_time'] == newest_stop['start_time']: end = {} if track and (not stop): start = dict(lid=track[0]['id'], tid=tid, start_time=track[0]['timestamp'], end_time=0, idle_time=0, name=self.get_track_name(track[0]), longitude=track[0]['longitude'], latitude=track[0]['latitude'], clongitude=track[0]['clongitude'], clatitude=track[0]['clatitude'], distance=0) end = dict(lid=track[-1]['id'], tid=tid, start_time=track[-1]['timestamp'], end_time=0, idle_time=0, name=self.get_track_name(track[-1]), longitude=track[-1]['longitude'], latitude=track[-1]['latitude'], clongitude=track[-1]['clongitude'], clatitude=track[-1]['clatitude'], distance=0) end['distance'] = self.get_track_distance( self.get_track(tid, start['start_time'], end['start_time'])) # modify name & degere for item in track: item['degree'] = float(item['degree']) if mass_flag: mass_point = 1 track = [] logging.info("[UEB] Mass flag is valid, return empty. tid:%s.", tid) if len(track) > LIMIT.MASS_POINT_NUMBER: # mass point mass_point = 1 track = [] logging.info("[UEB] mass point. tid:%s.", tid) if mass_point == 1: if not (start or end or stop): if cellid_flag == 1: # cellid track = self.get_track(tid, start_time, end_time, cellid=True) else: # gps # cellid_flag is None or 0, only gps track track = self.get_track(tid, start_time, end_time, cellid=False) if track: start = dict(lid=track[0]['id'], tid=tid, start_time=track[0]['timestamp'], end_time=0, idle_time=0, name=self.get_track_name(track[0]), longitude=track[0]['longitude'], latitude=track[0]['latitude'], clongitude=track[0]['clongitude'], clatitude=track[0]['clatitude'], distance=0) end = dict(lid=track[-1]['id'], tid=tid, start_time=track[-1]['timestamp'], end_time=0, idle_time=0, name=self.get_track_name(track[-1]), longitude=track[-1]['longitude'], latitude=track[-1]['latitude'], clongitude=track[-1]['clongitude'], clatitude=track[-1]['clatitude'], distance=0) end['distance'] = self.get_track_distance( self.get_track(tid, start['start_time'], end['start_time'])) # if start is equal end, just provide start if start and end: if start['start_time'] == end['start_time']: end = {} # NOTE: move the distance from next point to last point lst = stop[:] if start: lst.insert(0, start) if end: lst.append(end) for k, v in enumerate(lst): if k == len(lst) - 1: break v['distance'] = lst[k+1]['distance'] logging.info("[UEB] Tid:%s mass point query, returned %s points.", tid, len(track)) self.write_ret(status, dict_=DotDict(track=track, stop=stop, start=start, end=end, mass_point=mass_point)) self.finish()