def get(self): """Get the latest usable location. #NOTE: deprecated """ try: tid = self.get_argument('tid',None) # check tid whether exist in request and update current_user self.check_tid(tid) 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 is noexist, redirect to login.html", self.current_user.tid) self.write_ret(status) return ret = self.get_realtime(self.current_user.uid, self.current_user.sim) self.set_header(*self.JSON_HEADER) self.write(json_encode(ret)) except Exception as e: logging.exception("Failed to get location: %s, Sim: %s", e.args, self.current_user.sim) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def get(self): status = ErrorCode.SUCCESS try: tid = self.get_argument('tid', None) # check tid whether exist in request and update current_user self.check_tid(tid) terminal = QueryHelper.get_available_terminal(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 else: terminal_info_key = get_terminal_info_key(self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) mannual_status = terminal_info['mannual_status'] self.write_ret(status, dict_=DotDict(defend_status=mannual_status, mannual_status=mannual_status, fob_status=terminal.fob_status)) except Exception as e: logging.exception("[UWEB] uid:%s tid:%s get defend status failed. Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status) return
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(self): """Get terminal info. """ status = ErrorCode.SUCCESS try: tid = self.get_argument('tid', None) # check tid whether exist in request and update current_user self.check_tid(tid) car_sets = DotDict() 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_mobile(terminal.owner_mobile, self.db) if not user: logging.error("[UWEB] The user with uid: %s does not exist," " redirect to login.html", self.current_user.uid) self.clear_cookie(self.app_name) self.write_ret(ErrorCode.LOGIN_AGAIN) return # NOTE: deprecated. if terminal['mannual_status'] == 1: terminal['parking_defend'] = 0 else: terminal['parking_defend'] = 1 # NOTE: deprecated. whitelist = QueryHelper.get_white_list_by_tid( self.current_user.tid, self.db) car_info = QueryHelper.get_car_by_tid( self.current_user.tid, self.db) car = dict(corp_cnum=car_info.get('cnum', '')) # add tow dict: terminal, car. add two value: whitelist_1, # whitelist_2 white_list = [terminal.owner_mobile] for item in whitelist: white_list.append(item['mobile']) car_sets.update(terminal) car_sets.update(car) car_sets.update(DotDict(white_list=white_list)) self.write_ret(status, dict_=dict(car_sets=car_sets)) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[UWEB] uid: %s tid: %s get terminal failed. Exception: %s", self.current_user.uid, self.current_user.tid, e.args) self.write_ret(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 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 post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] batch delete request: %s, corp_id: %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: status = ErrorCode.SUCCESS tids = list(data.tids) flag = data.flag res = [] for tid in tids: r = DotDict(tid=tid, status=ErrorCode.SUCCESS) terminal = QueryHelper.get_available_terminal(tid, self.db) if not terminal: r.status = ErrorCode.SUCCESS res.append(r) logging.error( "[UWEB] The terminal with tid: %s does not exist!", tid) continue key = get_del_data_key(tid) self.redis.set(key, flag) biz_type = QueryHelper.get_biz_type_by_tmobile( terminal.mobile, self.db) if int(biz_type) == UWEB.BIZ_TYPE.YDWQ: delete_terminal(tid, self.db, self.redis) res.append(r) continue elif int(biz_type) == UWEB.BIZ_TYPE.YDWS: if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE: if terminal.mobile == tid: delete_terminal(tid, self.db, self.redis) else: r.status = self.send_jb_sms( terminal.mobile, terminal.owner_mobile, tid) res.append(r) continue # NOT: unbind. TODO: It should be re-factor some day. seq = str(int(time.time() * 1000))[-4:] args = DotDict(seq=seq, tid=tid) response = GFSenderHelper.forward( GFSenderHelper.URLS.UNBIND, args) 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, tid, terminal.mobile) res.append(r) else: # unbind failed. clear sessionID for relogin, then unbind # it again sessionID_key = get_terminal_sessionID_key(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, tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[response['success']]) unbind_sms = SMSCode.SMS_UNBIND biz_type = QueryHelper.get_biz_type_by_tmobile( terminal.mobile, self.db) if biz_type != UWEB.BIZ_TYPE.YDWS: ret = DotDict(status=ErrorCode.SUCCESS) else: ret = SMSHelper.send_to_terminal( terminal.mobile, unbind_sms) ret = DotDict(json_decode(ret)) if ret.status == ErrorCode.SUCCESS: res.append(r) self.db.execute("UPDATE T_TERMINAL_INFO" " SET service_status = %s" " WHERE id = %s", UWEB.SERVICE_STATUS.TO_BE_UNBIND, terminal.id) logging.info("[UWEB] uid: %s, tid: %s, tmobile: %s SMS unbind successfully.", self.current_user.uid, tid, terminal.mobile) else: r.status = ErrorCode.FAILED res.append(r) logging.error("[UWEB] uid: %s, tid: %s, tmobile: %s SMS unbind failed. Message: %s", self.current_user.uid, tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[status]) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] cid: %s batch delete failed. Exception: %s", self.current_user.cid, e.args) status = ErrorCode.ILLEGAL_FILE 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 delete(self): """Delete a terminal. """ try: status = ErrorCode.SUCCESS tid = self.get_argument('tid', None) flag = self.get_argument('flag', 0) logging.info("[UWEB] Corp delete terminal request. tid: %s, flag: %s, cid: %s", tid, flag, self.current_user.cid) terminal = QueryHelper.get_available_terminal(tid, self.db) if not terminal: logging.error("[UWEB] The terminal with tid: %s does not exist!", tid) status = ErrorCode.TERMINAL_NOT_EXISTED self.write_ret(status) return t_info = QueryHelper.get_terminal_basic_info(tid, self.db) key = get_del_data_key(tid) self.redis.set(key, flag) biz_type = QueryHelper.get_biz_type_by_tmobile( terminal.mobile, self.db) if int(biz_type) == UWEB.BIZ_TYPE.YDWS: if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE: if terminal.mobile == tid: delete_terminal(tid, self.db, self.redis) else: status = self.send_jb_sms( terminal.mobile, terminal.owner_mobile, tid) if status == ErrorCode.SUCCESS: WSPushHelper.pushS3(tid, self.db, self.redis, t_info) self.write_ret(status) return else: delete_terminal(tid, self.db, self.redis) if status == ErrorCode.SUCCESS: WSPushHelper.pushS3(tid, self.db, self.redis, t_info) self.write_ret(status) return # unbind terminal seq = str(int(time.time() * 1000))[-4:] args = DotDict(seq=seq, tid=tid) response = GFSenderHelper.forward(GFSenderHelper.URLS.UNBIND, args) response = json_decode(response) logging.info( "[UWEB] UNBind terminal: %s, response: %s", tid, response) if response['success'] == ErrorCode.SUCCESS: logging.info("[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind successfully", self.current_user.uid, tid, terminal.mobile) else: status = response['success'] # unbind failed. clear sessionID for relogin, then unbind it # again clear_sessionID(self.redis, tid) logging.error('[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind failed, message: %s, send JB sms...', self.current_user.uid, tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[status]) status = self.send_jb_sms( terminal.mobile, terminal.owner_mobile, tid) if status == ErrorCode.SUCCESS: WSPushHelper.pushS3(tid, self.db, self.redis, t_info) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Delete terminal failed. cid: %s, Exception: %s", self.current_user.cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def get(self): """Get terminal info. """ status = ErrorCode.SUCCESS try: tid = self.get_argument('tid', None) # check tid whether exist in request and update current_user self.check_tid(tid) # part 1: terminal tracker = DotDict() # 1: terminal # NOTE: static_val, move_val are deprecated terminal = QueryHelper.get_available_terminal( self.current_user.tid, self.db) if not terminal: status = ErrorCode.LOGIN_AGAIN logging.error("The terminal with tid: %s does not exist, redirect to login.html", self.current_user.tid) self.write_ret(status) return # 撤防,智能设防 if terminal['mannual_status'] != UWEB.DEFEND_STATUS.YES: terminal['parking_defend'] = 1 else: # 强力设防 terminal['parking_defend'] = 0 # 2: sos is deprecatd user = QueryHelper.get_user_by_uid(self.current_user.uid, self.db) if not user: status = ErrorCode.LOGIN_AGAIN logging.error("The user with uid: %s does not exist, redirect to login.html", self.current_user.uid) self.write_ret(status) return sos = dict(mobile='') tracker.update(sos) tracker.update(dict(push_status=terminal.push_status)) tracker.update(dict(sos_pop=terminal.white_pop)) tracker.update(dict(vibl=terminal.vibl)) tracker.update(dict(static_val=terminal.static_val)) tracker.update(dict(parking_defend=terminal.parking_defend)) tracker.update(dict(owner_mobile=terminal.owner_mobile)) tracker.update(dict(speed_limit=terminal.speed_limit)) # part 2: profile profile = DotDict() car = QueryHelper.get_car_by_tid(self.current_user.tid, self.db) profile.update(dict(name=user.name, mobile=user.mobile, email=user.email, cnum=car.cnum)) # part 3: sms option sms_options = QueryHelper.get_sms_option(user.mobile, self.db) # part 5: corp info corp = DotDict() corp = QueryHelper.get_corp_by_cid(self.current_user.cid, self.db) self.write_ret(status, dict_=dict(tracker=tracker, sms_options=sms_options, profile=profile, corp=corp)) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[UWEB] Get appsetting failed. uid: %s tid: %s, Exception: %s", self.current_user.uid, self.current_user.tid, e.args) self.write_ret(status)