def post(self): """Insert new items.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) content = data.get('content', '') mobiles = data.get('mobiles', None) logging.info("[UWEB] Announcement request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: mobiles_ = u'' if mobiles is not None: mobiles_ = ','.join(mobiles) for mobile in mobiles: SMSHelper.send(mobile, content) announcement = dict(cid=self.current_user.cid, content=content, mobiles=mobiles_) record_announcement(self.db, announcement) self.write_ret(status) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception( "[UWEB] record share failed, Exception: %s", e.args) self.write_ret(status)
def post(self): """Turn on buletooth.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) tids = data.get('tids', None) self.check_tid(tid) logging.info("[BLUETOOTH] kqly request: %s, uid: %s, tids: %s", data, self.current_user.uid, tids) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body:%s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: tids = str_to_list(tids) tids = tids if tids else [self.current_user.tid, ] tids = [str(t) for t in tids] kqly(self.db, self.redis, tids) self.write_ret(status) except Exception as e: logging.exception("[BLUETOOTH] Kqly failed. uid: %s, tid: %s, Exception: %s. ", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): status = WXErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) mannual_status = data.get('mannual_status', None) terminal = self.db.get("SELECT mannual_status, defend_status, service_status FROM T_TERMINAL_INFO " "WHERE tid = %s ", tid) if terminal: service_status = terminal['service_status'] if int(service_status) == 0: status = WXErrorCode.OUTSERVICE self.write_ret(status=status, message=WXErrorCode.ERROR_MESSAGE[status]) return try: self.db.execute("UPDATE T_TERMINAL_INFO SET mannual_status = %s" "WHERE tid = %s", mannual_status, tid) except MySQLdb.Error as e: logging.exception("[WEIXIN] execute update sql terminal:%s mannual_stauts failed ", tid, e.args) status = WXErrorCode.SERVER_BUSY self.write_ret(status=status, message=WXErrorCode.ERROR_MESSAGE[status]) except Exception as e: logging.exception("[WEIXIN] update terminal:%s mannual_stauts failed", tid) status = WXErrorCode.FAILED self.write_ret(status=status, message=WXErrorCode.ERROR_MESSAGE[status])
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) tids = data.get('tids', None) # check tid whether exist in request and update current_user self.check_tid(tid) logging.info("[UWEB] Defend request: %s, uid: %s, tid: %s, tids: %s", data, self.current_user.uid, self.current_user.tid, tids) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body:%s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: res = [] tids = str_to_list(tids) tids = tids if tids else [self.current_user.tid, ] tids = [str(tid) for tid in tids] for tid in tids: r = DotDict(tid=tid, status=ErrorCode.SUCCESS) try: terminal = QueryHelper.get_available_terminal(tid, self.db) if not terminal: r.status = ErrorCode.LOGIN_AGAIN res.append(r) logging.error("[UWEB] The terminal with tid: %s does not exist, redirect to login.html", tid) continue update_mannual_status(self.db, self.redis, tid, data.mannual_status) logging.info("[UWEB] uid:%s, tid:%s set mannual status to %s successfully", self.current_user.uid, tid, data.mannual_status) except Exception as e: r.status = ErrorCode.FAILED logging.exception("[UWEB] uid:%s, tid:%s set mannual status to %s failed. Exception: %s", self.current_user.uid, tid, data.mannual_status, e.args) finally: res.append(r) # NOTE: wspush if status == ErrorCode.SUCCESS: for tid in tids: WSPushHelper.pushS7(tid, self.db, self.redis) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] uid:%s, tid:%s set mannual status to %s failed. Exception: %s", self.current_user.uid, self.current_user.tid, data.mannual_status, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def onMessage(self, message, metadata): # for convenience, make a dot dict version of the message dot_message = DotDict(message) # double check that this is our target message if 'admin#reports#activity' not in dot_message.get('details.kind','')\ or 'id' not in message.get('details','') \ or 'etag' not in message.get('details',''): return (message, metadata) message["source"] = "gsuite" message["tags"].append("gsuite") # clean up ipaddress field if 'ipaddress' in message['details']: message['details']['sourceipaddress'] = message['details'][ 'ipaddress'] del message['details']['ipaddress'] # set the actual time if dot_message.get("details.id.time", None): message['utctimestamp'] = toUTC( message['details']['id']['time']).isoformat() # set the user_name if dot_message.get("details.actor.email", None): message["details"]["user"] = dot_message.get( "details.actor.email", "") # set summary message["summary"] = chevron.render( "{{details.user}} {{details.events.0.name}} from IP {{details.sourceipaddress}}", message) # set category message['category'] = "authentication" #success/failure if 'fail' in message["summary"]: message["details"]["success"] = False if 'success' in message["summary"]: message["details"]["success"] = True #suspicious? suspicious = {"boolvalue": True, "name": "is_suspicious"} for e in dot_message.get("details.events", []): for p in e.get("parameters", []): if dict_match(suspicious, p): message["details"]["suspicious"] = True return (message, metadata)
def determine_threshold_trigger(alert_params, events): """Given a threshold alert's params, and a set of events determine if it should fire and if so, resolve it's summary, event snippets, etc. """ # mostCommon the events by the dotted aggregation key counts = mostCommon(events, alert_params["aggregation_key"]) # determine if these events trigger an alert # according to the parameters logger.debug(counts) for i in counts: if i[1] >= alert_params["threshold"]: alert = alert_params alert["triggered"] = True # set the summary via chevron/mustache template # with the alert plus metadata metadata = {"metadata": {"value": i[0], "count": i[1]}} alert = merge(alert, metadata) # limit events to those matching the aggregation_key value # so the alert only gets events that match the count mostCommon results alert["events"] = [] for event in events: dotted_event = DotDict(event) if i[0] == dotted_event.get(alert_params["aggregation_key"]): alert["events"].append(dotted_event) alert["summary"] = chevron.render(alert["summary"], alert) # walk the alert events for any requested event snippets for event in alert["events"][:alert_params["event_sample_count"]]: alert["summary"] += " " + chevron.render( alert_params["event_snippet"], event) yield alert
def put(self): """Update the parameters of terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) # check tid whether exist in request and update current_user self.check_tid(tid) logging.info("[UWEB] Terminal request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: terminal = QueryHelper.get_available_terminal( self.current_user.tid, self.db) if not terminal: status = ErrorCode.LOGIN_AGAIN logging.error("[UWEB] The terminal with tid: %s does not exist," " redirect to login.html", self.current_user.tid) self.write_ret(status) return user = QueryHelper.get_user_by_uid(self.current_user.uid, self.db) if not user: status = ErrorCode.LOGIN_AGAIN logging.error("[UWEB] The user with uid: %s does not exist," " redirect to login.html", self.current_user.uid) self.write_ret(status) return # sql injection if data.has_key('corp_cnum') and not check_cnum(data.corp_cnum): status = ErrorCode.ILLEGAL_CNUM self.write_ret(status) return # NOTE: deprecated if data.has_key('white_list'): white_list = ":".join(data.white_list) if not check_sql_injection(white_list): status = ErrorCode.ILLEGAL_WHITELIST self.write_ret(status) return self.update_terminal_db(data) # NOTE: wspush to client if status == ErrorCode.SUCCESS: WSPushHelper.pushS7(tid, self.db, self.redis) self.write_ret(status) except Exception as e: logging.exception("[UWEB] uid:%s, tid:%s update terminal info failed. Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def get_tinyurl(url): """Get a tiny url for wap url. """ try: # Baidu dwz #h = httplib2.Http() #msg={'url':url} #url_create = 'http://dwz.cn/create.php' #response, content = h.request(url_create, "POST", urlencode(msg), headers={'Content-Type': 'application/x-www-form-urlencoded'}) #res = DotDict(json_decode(content)) #logging.info("[TINY_URL] response: %s", res) #if res.status == 0: # return res.tinyurl #else: # return None # google h = httplib2.Http() url_create = 'https://www.googleapis.com/urlshortener/v1/url' msg = json_encode({'longUrl': url}) response, content = h.request(url_create, "POST", msg, headers = {'Content-Type': 'application/json'}) res = DotDict(json_decode(content)) logging.info("[TINY_URL] response: %s", res) return res.get('id', None) except Exception as e: logging.exception("Get tiny url failed. Exception: %s", e.args) return None
def post(self): """Clear the cookie and set defend.""" try: data = DotDict(json_decode(self.request.body)) devid = data.get("devid", "") logging.info("[UWEB] logout request: %s, uid: %s", data, self.current_user.uid) except: self.write_ret(ErrorCode.ILLEGAL_DATA_FORMAT) logging.error("[UWEB] illegal format, body:%s", self.request.body) else: # 1: if there are tids, set defend for tid in data.tids: update_mannual_status(self.db, self.redis, tid, UWEB.DEFEND_STATUS.YES) # 2: remove devid from android_push_list android_push_list_key = get_android_push_list_key( self.current_user.uid) android_push_list = self.redis.getvalue(android_push_list_key) android_push_list = android_push_list if android_push_list else [] if devid in android_push_list: android_push_list.remove(devid) self.redis.set(android_push_list_key, android_push_list) logging.info("[UWEB] uid:%s, android_push_lst: %s", self.current_user.uid, android_push_list) finally: # 3: clear cookie self.clear_cookie(self.app_name) self.write_ret(ErrorCode.SUCCESS)
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get("tid", None) self.check_tid(tid) logging.info("[UWEB] terminal request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write(status) return try: tid = data["tid"] items = self.db.query("SELECT * FROM T_ALERT_SETTING WHERE tid=%s", tid) if len(items) >= 7: status = ErrorCode.FAILED self.write_ret(status) logging.error("[UWEB] terminal %s set too many event periods", tid) else: start_time = data["start_time"] end_time = data["end_time"] week = data["week"] self.db.execute("INSERT INTO T_ALERT_SETTING" " VALUES(NULL, %s, %s, %s, %s)", tid, start_time, end_time, week) logging.info("[UWEB] terminal add event period success: %s", data) self.write_ret(status) except Exception as e: logging.exception("[UWEB] tid:%s insert event period. Exception:%s", tid, e.args) status = ErrorCode.FAILED self.write_ret(status)
def put(self): """Modify some settings about mileage notification. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Mileage notification request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) tid = data.tid except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: distance_notification = data.get('distance_notification', None) day_notification = data.get('day_notification', None) assist_mobile = data.get('assist_mobile', None) if distance_notification is not None: self.db.execute("UPDATE T_MILEAGE_NOTIFICATION" " SET distance_notification = %s," " notify_count = 0," " left_days = 1," " set_time = %s" " WHERE tid = %s", distance_notification, int(time.time()), tid) if day_notification is not None: self.db.execute("UPDATE T_DAY_NOTIFICATION" " SET day_notification = %s," " notify_count = 0," " left_days = 1," " set_time = %s" " WHERE tid = %s", day_notification, int(time.time()), tid) if assist_mobile is not None: self.db.execute("UPDATE T_TERMINAL_INFO" " SET assist_mobile = %s" " WHERE tid = %s", assist_mobile, tid) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Get mileage notification. Exception: %s", self.current_user.cid, self.current_user.oid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def put(self): """Modify profile of current user. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid',None) # check tid whether exist in request and update current_user self.check_tid(tid) logging.info("[UWEB] User profile request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: if data.has_key('name') and not check_name(data.name): status = ErrorCode.ILLEGAL_NAME self.write_ret(status) return name = data.get('name', None) if name is not None: sql_cmd = "UPDATE T_USER SET name = %s WHERE uid = %s" self.db.execute(sql_cmd, name, self.current_user.uid) cnum = data.get('cnum', None) if cnum is not None: self.db.execute("UPDATE T_CAR" " SET cnum = %s" " WHERE tid = %s", safe_unicode(cnum), self.current_user.tid) terminal_info_key = get_terminal_info_key(self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info['alias'] = cnum if cnum else self.current_user.sim self.redis.setvalue(terminal_info_key, terminal_info) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Update profile failed. uid:%s, tid:%s, Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Get a GPS location or cellid location. workflow: if gps: try to get a gps location elif cellid: get a latest cellid and get a cellid location """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid',None) # check tid whether exist in request and update current_user self.check_tid(tid, finish=True) logging.info("[UWEB] realtime request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Realtime failed. Exception: %s", e.args) self.write_ret(status) self.finish() return current_query = DotDict() current_query.timestamp = int(time()) terminal = QueryHelper.get_available_terminal(self.current_user.tid, self.db) if not terminal: status = ErrorCode.LOGIN_AGAIN logging.error("[UWEB] The terminal with tid: %s does not exist, redirect to login.html", self.current_user.tid) self.write_ret(status) self.finish() return current_query.locate_flag = data.locate_flag def _on_finish(realtime): realtime['cellid_status'] = 1 self.set_header(*self.JSON_HEADER) self.write(json_encode(realtime)) self.finish() def __callback(db): self.db = db self.request_realtime(current_query, callback=_on_finish) #NOTE: deprecated. self.keep_waking(self.current_user.sim, self.current_user.tid) self.queue.put((10, __callback))
def determine_deadman_trigger(alert_params, events): """Given a deadman alert's params and a set of events (or lack thereof) determine if it should fire and resolve summary/snippets, etc Largely the same as a threshold alert, except this accounts for a lack of events (altogether missing, or below a count) as the trigger """ counts = mostCommon(events, alert_params["aggregation_key"]) if not events: # deadman alerts are built to notice # when expected events are missing # but it means we have no events to pass on # make a meta event for the fact that events are missing events = [] meta_event = { "utctimestamp": utcnow().isoformat(), "severity": "INFO", "summary": "Expected event not found", "category": "deadman", "source": "deadman", "tags": ["deadman"], "plugins": [], "details": {}, } events.append(meta_event) if not counts: # make up a metadata count counts = [(alert_params["aggregation_key"], 0)] for i in counts: # lack of events, or event count below the threshold is a trigger if i[1] <= alert_params["threshold"]: alert = alert_params alert["triggered"] = True # set the summary via chevron/mustache template # with the alert plus metadata metadata = {"metadata": {"value": i[0], "count": i[1]}} alert = merge(alert, metadata) # limit events to those matching the aggregation_key value # so the alert only gets events that match the count mostCommon results alert["events"] = [] for event in events: dotted_event = DotDict(event) if i[0] == dotted_event.get(alert_params["aggregation_key"]): alert["events"].append(dotted_event) alert["summary"] = chevron.render(alert["summary"], alert) # walk the alert events for any requested event snippets for event in alert["events"][:alert_params["event_sample_count"]]: alert["summary"] += " " + chevron.render( alert_params["event_snippet"], event) yield alert
def post(self): """Insert new items.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) umobile = data.umobile umobile = data.platform tmobile = data.get('tmobile','') tid = data.get('tid','') platform = data.get('platform','') logging.info("[UWEB] share log request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: record_share(self.db, locals()) self.write_ret(status) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[UWEB] Record share failed, Exception: %s", e.args) self.write_ret(status)
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = data.get('mobile', None) avatar = base64.urlsafe_b64decode(str(data.avatar)) logging.info("[avatar] Request: %s, avatar: %s", mobile, avatar) except Exception as e: logging.error("[avatar] Illegal format, body: %s, avatar: %s.", self.request.body, avatar) status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: terminal = self.db.get("SELECT tid FROM T_TERMINAL_INFO WHERE mobile = %s", mobile) tid = terminal.tid avatar_name = tid + '.png' avatar_path = self.application.settings['avatar_path'] + avatar_name avatar_full_path = self.application.settings['server_path'] + avatar_path img = open(avatar_full_path, 'w') img.write(avatar) img.close() avatar_time = self.update_avatar_time(tid) logging.info("[avatar] avatar_time: %s, tid: %s, user: %s", avatar_time, tid, self.current_user.uid) if status == 0: self.write_ret(status, dict_=dict(avatar_path=avatar_path, avatar_time=avatar_time)) else: self.write_ret(status, message=ErrorCode.ERROR_MESSAGE[status], dict_=dict(avatar_path=avatar_path, avatar_time=avatar_time)) except Exception as e: logging.exception("[avatar] Post avatar failed, user: %s. Exception: %s", self.current_user.uid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Query single-events through start_time, end_time and tid. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) start_time = data.start_time end_time = data.end_time tid = data.tid # For paging page_size = int(data.get("pagesize", UWEB.LIMIT.PAGE_SIZE)) page_number = int(data.pagenum) pagecnt = int(data.pagecnt) logging.info("[UWEB] Single list request: %s, cid: %s", data, self.current_user.cid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. Exception: %s", e.args) self.write_ret(status) return try: # NOTE: we need return the event count to GUI at first time query if pagecnt == -1: res = QueryHelper.get_single_event(tid, start_time, end_time, self.db) count = res.count d, m = divmod(count, page_size) pagecnt = (d + 1) if m else d res = QueryHelper.get_single_event_paged( tid, start_time, end_time, page_number * page_size, page_size, self.db ) self.write_ret(status, dict_=DotDict(res=res, pagecnt=pagecnt)) except Exception as e: logging.exception("[UWEB] cid: %s single list failed. Exception: %s", self.current_user.cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Insert new items.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = data.get('mobile', '') logging.info("[UWEB] feedback request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: #if data.has_key('contact') and not check_sql_injection(data.contact): # status = ErrorCode.ILLEGAL_NAME # logging.info("[UWEB] feedback failed, Message: %s", ErrorCode.ERROR_MESSAGE[status]) # self.write_ret(status) # return #if data.has_key('email') and not check_sql_injection(data.email): # status = ErrorCode.ILLEGAL_EMAIL # logging.info("[UWEB] feedback failed, Message: %s", ErrorCode.ERROR_MESSAGE[status]) # self.write_ret(status) # return self.db.execute("INSERT INTO T_FEEDBACK(contact, mobile," " email, content, timestamp, category)" " VALUES(%s, %s, %s, %s, %s, %s)", data.contact, mobile, data.email, safe_unicode(data.content), int(time.time()), data.category) self.write_ret(status) except Exception as e: status = ErrorCode.FEEDBACK_FAILED logging.exception("[UWEB] add feedback failed, Exception: %s, content:\n%s", e.args, data.content) self.write_ret(status)
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) page_size = int(data.get('pagesize', UWEB.LIMIT.PAGE_SIZE)) page_number = int(data.pagenum) page_count = int(data.pagecnt) start_time = data.start_time end_time = data.end_time logging.info("[UWEB] announcement request: %s, uid: %s", data, self.current_user.uid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.info("[UWEB] Invalid data format: %s, Exception: %s", data, e.args) self.write_ret(status) return try: if page_count == -1: res = QueryHelper.get_announcement( self.current_user.cid, start_time, end_time, self.db) d, m = divmod(res.count, page_size) page_count = (d + 1) if m else d res = QueryHelper.get_announcement_paged( self.current_user.cid, start_time, end_time, page_number * page_size, page_size, self.db) self.write_ret(status=status, dict_=DotDict(res=res, pagecnt=page_count)) except Exception as e: logging.exception("[UWEB] Get announcement list failed.") status = ErrorCode.SUCCESS self.write_ret(status=status)
def post(self): """Retrive various attendance. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = data.get('mobile', None) mobiles = str_to_list(mobile) if not mobiles: terminals = self.db.query("SELECT tmobile FROM V_TERMINAL" " where cid = %s", self.current_user.cid) mobiles = [str(terminal['tmobile']) for terminal in terminals] logging.info("[UWEB] attendance request: %s, cid: %s", data, self.current_user.cid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. Exception: %s", e.args) self.write_ret(status) return try: page_size = int(data.get('pagesize', UWEB.LIMIT.PAGE_SIZE)) page_number = int(data.pagenum) page_count = int(data.pagecnt) start_time = data.start_time end_time = data.end_time # we need return the event count to GUI at first time query if page_count == -1: sql = ("SELECT COUNT(*) as count FROM V_ATTENDANCE" " WHERE mobile IN %s " " AND (timestamp BETWEEN %s AND %s)") sql = sql % (tuple(mobiles + DUMMY_IDS_STR), start_time, end_time) res = self.db.get(sql) event_count = res.count d, m = divmod(event_count, page_size) page_count = (d + 1) if m else d sql = ("SELECT mobile as tid, mobile, clatitude, clongitude," " timestamp, name, type, speed, degree," " locate_error" " FROM V_ATTENDANCE" " WHERE mobile IN %s" " AND (timestamp BETWEEN %s AND %s)" " ORDER BY timestamp DESC" " LIMIT %s, %s") sql = sql % (tuple(mobiles + DUMMY_IDS_STR), start_time, end_time, page_number * page_size, page_size) res = self.db.query(sql) # change the type form decimal to float. for r in res: r['alias'] = QueryHelper.get_alias_by_tid(r['tid'], self.redis, self.db) r['name'] = r['name'] if r['name'] is not None else u'' r['degree'] = float(r['degree']) r['speed'] = float(r['speed']) r['comment'] = '' self.write_ret(status, dict_=DotDict(res=res, pagecnt=page_count)) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[UWEB] cid:%s get attendance info failed. Exception: %s", self.current_user.cid, e.args) self.write_ret(status)
def post(self): """Retrive various event. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = data.mobile sn = data.sn category = int(data.category) location = data.location gps = int(data.gps) gsm = int(data.gsm) pbat = int(data.pbat) attendance_time = data.get('attendance_time', '') logging.info("[UWEB] upload request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. Exception: %s, data: %s", e.args, self.request.body) self.write_ret(status) return try: terminal = self.db.get("SELECT id, service_status, mobile" " FROM T_TERMINAL_INFO" " WHERE mobile = %s" " AND sn = %s" " AND service_status = %s" " AND biz_type = %s LIMIT 1", mobile, sn, UWEB.SERVICE_STATUS.ON, UWEB.BIZ_TYPE.YDWQ) if not terminal: status = ErrorCode.ACCOUNT_NOT_MATCH logging.info('mobile: %s, sn: %s not match, drop it.', mobile, sn) self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal['tid'] # NOTE: location may be a dict or list if type(location) != list: locations = [location,] else: locations = location if category == UWEB.UPLOAD_CATEGORY.HEARTBEAT: pass elif category == UWEB.UPLOAD_CATEGORY.LOCATION: for location in locations: location = DotDict(dev_id=tid, lat=location['clatitude'], lon=location['clongitude'], alt=0, cLat=location['clatitude'], cLon=location['clongitude'], gps_time=location['timestamp'], name=location.get('name', ''), category=1, type=int(location['type']), speed=location['speed'], degree=location['degree'], cellid='', locate_error=int(location['locate_error'])) insert_location(location, self.db, self.redis) elif category == UWEB.UPLOAD_CATEGORY.ATTENDANCE: location = locations[0] if len(locations) >= 1 else None if location: location = DotDict(dev_id=tid, lat=location['clatitude'], lon=location['clongitude'], alt=0, cLat=location['clatitude'], cLon=location['clongitude'], gps_time=location['timestamp'], name=location.get('name', ''), category=1, type=int(location['type']), speed=location['speed'], degree=location['degree'], cellid='', locate_error=int(location['locate_error'])) lid = insert_location(location, self.db, self.redis) a_info=dict(mobile=mobile, comment=u'', timestamp=attendance_time if attendance_time else location['gps_time'], lid=lid) record_attendance(self.db, a_info) else: logging.error("[UWEB] Invalid attendance data, location is missed.") else: #TODO: handle power-event location = locations[0] if len(locations) >= 1 else None if location: location = DotDict(dev_id=tid, lat=location['clatitude'], lon=location['clongitude'], alt=0, cLat=location['clatitude'], cLon=location['clongitude'], gps_time=location['timestamp'], name=location.get('name', ''), category=1, type=int(location['type']), speed=location['speed'], degree=location['degree'], cellid='', locate_error=int(location['locate_error'])) lid = insert_location(location, self.db, self.redis) #a_info=dict(mobile=mobile, # comment=u'', # timestamp=attendance_time if attendance_time else location['gps_time'], # lid=lid) #record_attendance(self.db, a_info) t_info = DotDict(gps=gps, gsm=gsm, login=1, pbat=pbat, tid=tid) update_terminal_info_ydwq(self.db, self.redis, t_info) update_terminal_status(self.redis, tid) self.write_ret(status) except Exception as e: logging.exception("[UWEB] tmobile:%s upload failed. Exception: %s", mobile, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Get mass point through tid in some period.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.tid start_time = data.start_time end_time = data.end_time cellid_flag = data.get('cellid_flag', 0) logging.info("[UWEB] Mass point request: %s, uid: %s, tid: %s", data, self.current_user.uid, tid) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[UWEB] Mass point request failed. uid: %s, tid: %s. Exception: %s.", self.current_user.uid, tid, e.args ) self.write_ret(status) self.finish() return 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() self.queue.put((10, _on_finish))
def post(self): """Turn on tracing.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) tids = data.get('tids', None) flag = int(data.get('flag', 1)) # check tid whether exist in request and update current_user self.check_tid(tid) logging.info("[UWEB] track LQ request: %s, " " uid: %s, tid: %s, tids: %s, flag: %s", data, self.current_user.uid, tid, tids, flag) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: tids = str_to_list(tids) tids = tids if tids else [self.current_user.tid, ] tids = [str(tid) for tid in tids] if int(flag) == 1: for tid in tids: # NOTE: just send lqgz temporary terminal = QueryHelper.get_terminal_by_tid(tid, self.db) lqgz_key = get_lqgz_key(tid) lqgz_value = self.redis.getvalue(lqgz_key) lqgz_interval_key = get_lqgz_interval_key(tid) if not lqgz_value: interval = 30 # in minute biz_type = QueryHelper.get_biz_type_by_tmobile( terminal.mobile, self.db) if biz_type != UWEB.BIZ_TYPE.YDWS: self.write_ret(status) return sms = SMSCode.SMS_LQGZ % interval SMSHelper.send_to_terminal(terminal.mobile, sms) self.redis.setvalue( lqgz_key, True, SMS.LQGZ_SMS_INTERVAL) self.redis.setvalue( lqgz_interval_key, True, SMS.LQGZ_INTERVAL * 2) # END track_key = get_track_key(tid) track = self.redis.get(track_key) logging.info("[UWEB] Get track: %s from redis", track) if track and int(track) == 1: # turn on track already logging.info( "[UWEB] Terminal: %s turn on track already.", tid) else: self.db.execute("UPDATE T_TERMINAL_INFO SET track = %s" " WHERE tid = %s", flag, tid) self.redis.setvalue(track_key, 1, UWEB.TRACK_INTERVAL) sessionID_key = get_terminal_sessionID_key(tid) self.redis.delete(sessionID_key) self.write_ret(status) except Exception as e: logging.exception("[UWEB] uid: %s, tid: %s send lqgz failed. Exception: %s. ", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tids = data.get("tids") op_type = data.get("op_type") logging.info( "[UWEB] ACC request: %s, uid: %s, tid: %s, tids: %s", data, self.current_user.uid, self.current_user.tid, tids, ) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: res = [] for tid in tids: r = DotDict(tid=tid, status=ErrorCode.SUCCESS, message=ErrorCode.ERROR_MESSAGE[status]) try: t = QueryHelper.get_terminal_by_tid(tid, self.db) if str(t["dev_type"]) != "D": r["status"] = ErrorCode.ACC_NOT_ALLOWED logging.info( "[UWEB] Acc is not allowed. uid: %s, tid: %s, dev_type: %s", self.current_user.uid, tid, t["dev_type"], ) else: acc_status_info_key = get_acc_status_info_key(tid) acc_status_info = self.redis.getvalue(acc_status_info_key) if acc_status_info: r["status"] = ErrorCode.ACC_TOO_FREQUENCY logging.info("[UWEB] Acc is too frequency. uid: %s, tid: %s", self.current_user.uid, tid) else: acc_status_info = dict( client_id=self.client_id, op_type=op_type, timestamp=int(time.time()), op_status=0, # failed t2_status=0, # wait for T2 acc_message=u"", ) self.redis.setvalue(acc_status_info_key, acc_status_info, UWEB.ACC_STATUS_EXPIRY) except Exception as e: r["status"] = ErrorCode.FAILED logging.info( "[UWEB] Set acc status failed, uid:%s, tid:%s, op_type:%s.", self.current_user.uid, tid, op_type ) finally: r["message"] = ErrorCode.ERROR_MESSAGE[r["status"]] res.append(r) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] Set acc status failed, uid:%s, Exception: %s.", self.current_user.uid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): try: data = DotDict(json_decode(self.request.body)) track_lst = data.get('track_lst', []) current_time = int(time.time()) lastinfo_time = data.get('lastinfo_time') #NOTE: first time, lastinfo_time = -1, set the lsstinfo_time as current_time if lastinfo_time == -1: lastinfo_time = current_time except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.info("[UWEB] lastfinfo for corp failed, message: %s, Exception: %s, request: \n%s", ErrorCode.ERROR_MESSAGE[status], e.args, self.request.body) self.write_ret(status) return try: status = ErrorCode.SUCCESS corp = self.db.get("SELECT cid, name, mobile FROM T_CORP WHERE cid = %s", self.current_user.cid) if self.current_user.oid == UWEB.DUMMY_OID: groups = self.db.query("SELECT id gid, name FROM T_GROUP WHERE corp_id = %s", self.current_user.cid) else: 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] groups = self.db.query("SELECT id gid, name FROM T_GROUP WHERE id IN %s", tuple(DUMMY_IDS + gids)) res = DotDict(name=corp.name if corp else '', cid=corp.cid if corp else '', online=0, offline=0, groups=[], lastinfo_time=current_time) for group in groups: group['trackers'] = {} terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO" " WHERE group_id = %s" " AND (service_status = %s" " OR service_status = %s)", group.gid, UWEB.SERVICE_STATUS.ON, UWEB.SERVICE_STATUS.TO_BE_ACTIVATED) tids = [str(terminal.tid) for terminal in terminals] for tid in tids: group['trackers'][tid] = {} # 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 if terminal['login'] == GATEWAY.TERMINAL_LOGIN.ONLINE: res['online'] +=1 else: res['offline'] +=1 # 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 #1: build the basic_info basic_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, icon_type=terminal['icon_type'] if terminal.get('icon_type', None) is not None else 0, fob_list=terminal['fob_list'] if terminal['fob_list'] else []) #2: build track_info track_info = [] for item in track_lst: if tid == item['track_tid']: track_key = get_track_key(tid) self.redis.setvalue(track_key, 1, UWEB.TRACK_INTERVAL) endtime = int(basic_info['timestamp'])-1 if basic_info['timestamp'] else int(current_time)-1 points_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, int(item['track_time'])+1, endtime) #points_track = get_locations_with_clatlon(points_track, self.db) for point in points_track: if point['clatitude'] and point['clongitude']: t = dict(latitude=point['latitude'], longitude=point['longitude'], clatitude=point['clatitude'], clongitude=point['clongitude'], type=point['type'], timestamp=point['timestamp']) track_info.append(t) break #3: build trace_info trace_info = [] points_trace = self.db.query("SELECT distinct 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", tid, int(current_time)-60*5, basic_info['timestamp']) #points_trace = get_locations_with_clatlon(points_trace, self.db) points_trace = points_trace[:5] len_trace = 0 if points_trace: for point in points_trace: if len_trace >= 5: break if point['clatitude'] and point['clongitude']: trace_info.append(point['clatitude']) trace_info.append(point['clongitude']) len_trace += 1 else: continue #4: build alert_info alarm_info_key = get_alarm_info_key(tid) alarm_info_all = self.redis.getvalue(alarm_info_key) alarm_info_all = alarm_info_all if alarm_info_all else [] alarm_info = [] if alarm_info_all: for alarm in alarm_info_all: #NOTE: here, check alarm's keeptime when kept in reids, not timestamp alarm occurs if alarm.get('keeptime', None) is None: alarm['keeptime'] = alarm['timestamp'] if alarm['keeptime'] >= lastinfo_time/1000: alarm_info.append(alarm) if alarm_info: # NOTE: here, do not remove alarm_info, it will automagically disappear after 1 day #self.redis.delete(alarm_info_key) #logging.info("[UWEB] lastinfo_time: %s, alarm_info_key: %s, alarm_info: %s", lastinfo_time, alarm_info_key, alarm_info) pass for alarm in alarm_info: alarm['alias'] = terminal['alias'] group['trackers'][tid]['basic_info'] = basic_info group['trackers'][tid]['track_info'] = track_info group['trackers'][tid]['trace_info'] = trace_info group['trackers'][tid]['alarm_info'] = alarm_info res.groups.append(group) self.write_ret(status, dict_=DotDict(res=res, res_type=3)) except Exception as e: logging.exception("[UWEB] cid: %s get corp lastinfo failed. Exception: %s", self.current_user.cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
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 post(self): """Retrive various event. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) tids = data.get('tids', None) tids = str_to_list(tids) if not tids: tids = [tid, ] if tid else [] tids = [str(t) for t in tids] if not tid: tid = tids[0] if len(tids) > 0 else '' # check tid whether exist in request and update current_user self.check_tid(tid) logging.info("[UWEB] Event request: %s, uid: %s, tid: %s, tids: %s", data, self.current_user.uid, self.current_user.tid, tids) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body: %s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: terminal = QueryHelper.get_available_terminal(self.current_user.tid, self.db) if not terminal: status = ErrorCode.LOGIN_AGAIN logging.error( "[UWEB] The terminal with tid: %s does not exist, redirect to login.html", self.current_user.tid) self.write_ret(status) return page_size = int(data.get('pagesize', UWEB.LIMIT.PAGE_SIZE)) category = data.get('category', None) categories = data.get('categories', None) page_number = int(data.pagenum) page_count = int(data.pagecnt) start_time = data.start_time end_time = data.end_time # NOTE: all catgories all_categories = [2, # powerlow 3, # illegalshake 4, # illegalmove 5, # sos 6, # hearbeat lost 7, # region enter 8, # retion out 9, # power off 10, # stop 11, # speed_limit ] if category is not None: if int(category) == -1: categories = all_categories else: categories = [int(category), ] elif categories: if int(categories[0]) == -1: categories = all_categories else: categories = [int(c) for c in categories] # NOTE: for individual: the interval between start_time and end_time is one week; # for enterprise: no need to check time interval if self.current_user.cid != UWEB.DUMMY_CID: hide_lst = [5] # enterprise: no sos else: # individual: no stop hide_lst = [5, 10, 11] if (int(end_time) - int(start_time)) > UWEB.QUERY_INTERVAL: self.write_ret(ErrorCode.QUERY_INTERVAL_EXCESS) return # we need return the event count to GUI at first time query if page_count == -1: sql = ("SELECT COUNT(*) as count FROM V_EVENT" " WHERE tid IN %s " + " AND category IN %s" + " AND (timestamp BETWEEN %s AND %s)")\ % (tuple(tids + DUMMY_IDS_STR), tuple(list(set(categories) - set(hide_lst)) + DUMMY_IDS), start_time, end_time) res = self.db.get(sql) event_count = res.count d, m = divmod(event_count, page_size) page_count = (d + 1) if m else d sql = ("SELECT tid, latitude, longitude, clatitude, clongitude," " timestamp, name, type, speed, degree," " category, pbat, terminal_type, fobid, rid, locate_error" " FROM V_EVENT" " WHERE tid IN %s" " AND (timestamp BETWEEN %s AND %s)" " AND category IN %s" " ORDER BY timestamp DESC" " LIMIT %s, %s") %\ (tuple(tids + DUMMY_IDS_STR), start_time, end_time, tuple(list(set(categories) - set(hide_lst)) + DUMMY_IDS), page_number * page_size, page_size) events = self.db.query(sql) alias_dict = {} for tid in tids: terminal_info_key = get_terminal_info_key(tid) terminal_info = self.redis.getvalue(terminal_info_key) alias_dict[tid] = terminal_info['alias'] if terminal_info[ 'alias'] else terminal_info['mobile'] # Adjust the fields in events. for event in events: event['alias'] = alias_dict[event['tid']] event['pbat'] = event['pbat'] if event[ 'pbat'] is not None else 0 event['fobid'] = event['fobid'] if event[ 'fobid'] is not None else u'' event['name'] = event['name'] if event[ 'name'] is not None else u'' event['degree'] = float(event['degree']) event['speed'] = float(event['speed']) event['comment'] = '' if event['category'] == EVENTER.CATEGORY.POWERLOW: if event['terminal_type'] == '1': if int(event['pbat']) == 100: event['comment'] = ErrorCode.ERROR_MESSAGE[ ErrorCode.TRACKER_POWER_FULL] elif int(event['pbat']) <= 5: event['comment'] = ErrorCode.ERROR_MESSAGE[ ErrorCode.TRACKER_POWER_OFF] else: event['comment'] = ( ErrorCode.ERROR_MESSAGE[ErrorCode.TRACKER_POWER_LOW]) % event['pbat'] else: event['comment'] = ErrorCode.ERROR_MESSAGE[ ErrorCode.FOB_POWER_LOW] % event['fobid'] if event['category'] in [EVENTER.CATEGORY.REGION_ENTER, EVENTER.CATEGORY.REGION_OUT]: region = self.db.get("SELECT name AS region_name" " FROM T_REGION" " WHERE id = %s", event.rid) region_name = safe_unicode( region.region_name) if region else u'' event['comment'] = u'围栏名:' + region_name self.write_ret(status, dict_=DotDict(events=events, pagecnt=page_count)) except Exception as e: logging.exception("[UWEB] uid:%s, tid:%s get alarm info failed. Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Get track through tid in some period.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) # check tid whether exist in request and update current_user self.check_tid(tid) logging.info("[UWEB] track request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception( "[UWEB] Invalid data format. Exception: %s", e.args) self.write_ret(status) self.finish() return try: start_time = data.start_time end_time = data.end_time cellid_flag = data.get('cellid_flag', 0) network_type = data.get('network_type', 1) except Exception as e: logging.exception("[UWEB] uid: %s, tid: %s get track failed. Exception: %s. ", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status) self.finish() return 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() self.queue.put((10, _on_finish))
def post(self): """Acquire push account through uid @uid: user uid """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.tid s_type = data.s_type if data.get("category", None): category = int(data.get("category")) else: category = 1 logging.info("[UWEB] Test wspush request: %s.", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body: %s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: # user = self.db.get("select owner_mobile") user = QueryHelper.get_user_by_tid(tid, self.db) uid = user.owner_mobile if s_type == "S3": WSPushHelper.pushS3(tid, self.db, self.redis) elif s_type == "S4": WSPushHelper.pushS4(tid, self.db, self.redis) elif s_type == "S5": body = dict( tid=tid, category=category, pbat=100, type=1, timestamp=int(time.time()), longitude=419004000, latitude=143676000, clongitude=419004000, clatitude=143676000, name="test name", speed=111, degree=203, gsm=0, locate_error=100, gps=25, alias="111", region_id=11, ) WSPushHelper.pushS5(tid, body, self.db, self.redis) elif s_type == "S6": WSPushHelper.pushS6(tid, self.db, self.redis) elif s_type == "S7": WSPushHelper.pushS7(tid, self.db, self.redis) elif s_type == "S8": acc_message = 1 WSPushHelper.pushS8(tid, acc_message, self.db, self.redis) self.write_ret(status=status) except Exception as e: logging.exception("[UWEB] WSPushHandler get push account failed, Exception:%s", e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
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
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tmobile = data.tmobile flag = data.get('flag', 0) logging.info("[UWEB] unbind request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) IOLoop.instance().add_callback(self.finish) return try: #NOTE:delete is not permited status = ErrorCode.DELETE_NOT_PERMITED self.write_ret(status) self.finish() return terminal = self.db.get("SELECT id, tid, owner_mobile, login" " FROM T_TERMINAL_INFO" " WHERE mobile = %s" " AND service_status = %s", tmobile, UWEB.SERVICE_STATUS.ON) if not terminal: status = ErrorCode.TERMINAL_NOT_EXISTED logging.error("The terminal with tmobile: %s does not exist!", tmobile) self.write_ret(status) IOLoop.instance().add_callback(self.finish) return key = get_del_data_key(terminal.tid) self.redis.set(key, flag) if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE: status = self.send_jb_sms(tmobile, terminal.owner_mobile, terminal.tid) self.write_ret(status) IOLoop.instance().add_callback(self.finish) return def _on_finish(response): status = ErrorCode.SUCCESS response = json_decode(response) if response['success'] == ErrorCode.SUCCESS: logging.info("[UWEB] uid:%s, tid: %s, tmobile:%s GPRS" " unbind successfully", self.current_user.uid, terminal.tid, tmobile) else: status = response['success'] # unbind failed. clear sessionID for relogin, then unbind it again sessionID_key = get_terminal_sessionID_key(terminal.tid) self.redis.delete(sessionID_key) logging.error("[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind failed, " " message: %s, send JB sms...", self.current_user.uid, terminal.tid, tmobile, ErrorCode.ERROR_MESSAGE[status]) status = self.send_jb_sms(tmobile, terminal.owner_mobile, terminal.tid) self.write_ret(status) IOLoop.instance().add_callback(self.finish) seq = str(int(time.time()*1000))[-4:] args = DotDict(seq=seq, tid=terminal.tid) GFSenderHelper.async_forward(GFSenderHelper.URLS.UNBIND, args, _on_finish) except Exception as e: logging.exception("[UWEB] tmobile:%s unbind failed. Exception: %s", data.tmobile, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status) IOLoop.instance().add_callback(self.finish)
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)
def post(self): """Get mass point through tid in some period.""" status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.tid start_time = data.start_time end_time = data.end_time start_day = datetime.datetime.fromtimestamp(start_time) end_day = datetime.datetime.fromtimestamp(end_time) # get how many days the end_time and start_time cover days = abs(end_day - start_day).days + 1 if ((start_day.hour * 60 * 60 + start_day.minute * 60 + start_day.second) > (end_day.hour * 60 * 60 + end_day.minute * 60 + end_day.second)): days += 1 cellid_flag = data.get('cellid_flag', 0) logging.info("[UWEB] Mass point day request: %s, uid: %s, tid: %s", data, self.current_user.uid, tid) 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() return 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() self.queue.put((10, _on_finish))