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 = self.db.get("SELECT id FROM T_TERMINAL_INFO" " WHERE tid = %s" " AND service_status = %s", self.current_user.tid, UWEB.SERVICE_STATUS.ON) if terminal: lq_sms_key = get_lq_sms_key(self.current_user.tid) sms = SMSCode.SMS_LQ % SMS.LQ.WEB biz_type = QueryHelper.get_biz_type_by_tmobile(self.current_user.sim, self.db) if biz_type != UWEB.BIZ_TYPE.YDWS: pass else: SMSHelper.send_to_terminal(self.current_user.sim, sms) self.redis.setvalue(lq_sms_key, True, SMS.LQ_INTERVAL) lq_interval_key = get_lq_interval_key(self.current_user.tid) self.redis.setvalue(lq_interval_key, int(time.time()), (SMS.LQ.WEB*60 - 160)) logging.info("[UWEB] wake up, send %s to Sim: %s", sms, self.current_user.sim) else: status = ErrorCode.LOGIN_AGAIN self.write_ret(status) except Exception as e: logging.exception("[UWEB] uid: %s wake up tid: %s failed. Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def send_cq_sms(db, redis, tid, mobile): """Send cq sms to terminal.. """ sms_cq = SMSCode.SMS_CQ if len(mobile) != 11: logging.info("[PUBLIC] Mobile is valid, ignore it. mobile: %s", mobile) return biz_type = QueryHelper.get_biz_type_by_tmobile(mobile, db) if biz_type != UWEB.BIZ_TYPE.YDWS: logging.info( "[PUBLIC] Biz_type is no need cq, ignore it. mobile: %s, biz_type: %s", mobile, biz_type) return SMSHelper.send_to_terminal(mobile, sms_cq) logging.info("[PUBLIC] Send cq sms to terminal. mobile: %s", mobile)
def send_jb_sms(self, tmobile, umobile, tid): """ #NOTE: deprecated. It should never be invoked. """ unbind_sms = SMSCode.SMS_UNBIND biz_type = QueryHelper.get_biz_type_by_tmobile(tmobile, self.db) if biz_type != UWEB.BIZ_TYPE.YDWS: ret = DotDict(status=ErrorCode.SUCCESS) else: ret = SMSHelper.send_to_terminal(tmobile, unbind_sms) ret = json_decode(ret) status = ret["status"] if status == ErrorCode.SUCCESS: self.db.execute( "UPDATE T_TERMINAL_INFO" " SET service_status = %s" " WHERE mobile = %s", UWEB.SERVICE_STATUS.TO_BE_UNBIND, tmobile, ) terminals = self.db.query( "SELECT id FROM T_TERMINAL_INFO" " WHERE owner_mobile = %s" " AND service_status = %s", umobile, UWEB.SERVICE_STATUS.ON, ) # clear user if len(terminals) == 0: self.db.execute("DELETE FROM T_USER" " WHERE mobile = %s", umobile) lastinfo_key = get_lastinfo_key(umobile) lastinfo_time_key = get_lastinfo_time_key(umobile) ios_id_key = get_ios_id_key(umobile) ios_badge_key = get_ios_badge_key(umobile) keys = [lastinfo_key, lastinfo_time_key, ios_id_key, ios_badge_key] self.redis.delete(*keys) logging.info("[UWEB] Delete User: %s", umobile) logging.info("[UWEB] umobile: %s, tid: %s, tmobile: %s SMS unbind successfully.", umobile, tid, tmobile) else: logging.error( "[UWEB] umobile: %s, tid: %s, tmobile: %s SMS unbind failed. Message: %s", umobile, tid, tmobile, ErrorCode.ERROR_MESSAGE[status], ) return status
def send_lq_sms(self, sim, tid, interval): """Send LQ Message to terminal. lq_sms_key: when send lq sms to terminal, keep it in redis for 3 minutes. in 3 minutes, do not send lq sms twice. lq_interval_key: when send lq sms to terminal, keep it in redis for interval. in the period of interval, terminal is been awaken. when the period of interval is past, lq_sms should be send again """ lq_sms_key = get_lq_sms_key(tid) lq_interval_key = get_lq_interval_key(tid) self.redis.setvalue(lq_interval_key, int(time.time()), (interval * 60 - 160)) if not self.redis.getvalue(lq_sms_key): sms = SMSCode.SMS_LQ % interval biz_type = QueryHelper.get_biz_type_by_tmobile(sim, self.db) if biz_type != UWEB.BIZ_TYPE.YDWS: pass else: SMSHelper.send_to_terminal(sim, sms) logging.info("[UWEB] send %s to Sim: %s", sms, sim) self.redis.setvalue(lq_sms_key, True, SMS.LQ_INTERVAL)
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)) 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, tmobile, pmobile, is_clear): """Delete a terminal. @param: tmobile // terminal's mobile @param: pmobile // owner_mobile @param: is_clear: 清除// 1: 清除历史数据; 0: 不清楚历史数据 """ status = ErrorCode.SUCCESS try: terminal = self.db.get("SELECT id, login, mobile, tid" " FROM T_TERMINAL_INFO" " WHERE mobile = %s", tmobile) tid = terminal.tid #NOTE: record whether clear history in redis key = get_del_data_key(tid) self.redis.set(key, is_clear) biz_type = QueryHelper.get_biz_type_by_tmobile(tmobile, self.db) if int(biz_type) == UWEB.BIZ_TYPE.YDWS: if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE: # offline if terminal.mobile == tid: delete_terminal_new(tid, self.db, self.redis) logging.info('[ADMIN] Delete terminal. umobile:%s, tid: %s, tmobile:%s.', pmobile, tid, tmobile) else: delete_terminal_new(tid, self.db, self.redis) unbind_sms = SMSCode.SMS_UNBIND ret = SMSHelper.send_to_terminal(tmobile, unbind_sms) ret = DotDict(json_decode(ret)) logging.info('[ADMIN] Send JB sms. umobile:%s, tid: %s, tmobile:%s.', pmobile, tid, tmobile) self.write_ret(status) return else: delete_terminal_new(tid, self.db, self.redis) self.write_ret(status) return # unbind terminal seq = str(int(time.time()*1000))[-4:] args = DotDict(seq=seq, tid=terminal.tid) response = GFSenderHelper.forward(GFSenderHelper.URLS.UNBIND, args) response = json_decode(response) if response['success'] == ErrorCode.SUCCESS: logging.info("[ADMIN] Umobile: %s, tid: %s, tmobile:%s" " GPRS unbind successfully", pmobile, 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("[ADMIN] Umobile:%s, tid: %s, tmobile:%s" " GPRS unbind failed, message: %s, send JB sms...", pmobile, terminal.tid, tmobile, ErrorCode.ERROR_MESSAGE[status]) unbind_sms = SMSCode.SMS_UNBIND biz_type = QueryHelper.get_biz_type_by_tmobile(tmobile, self.db) if biz_type != UWEB.BIZ_TYPE.YDWS: ret = DotDict(status=ErrorCode.SUCCESS) else: ret = SMSHelper.send_to_terminal(tmobile, unbind_sms) ret = DotDict(json_decode(ret)) status = ret.status if ret.status == ErrorCode.SUCCESS: self.db.execute("UPDATE T_TERMINAL_INFO" " SET service_status = %s" " WHERE id = %s", UWEB.SERVICE_STATUS.TO_BE_UNBIND, terminal.id) logging.info("[ADMIN] umobile: %s, tid: %s, tmobile: %s" " SMS unbind successfully.", pmobile, terminal.tid, tmobile) else: logging.error("[ADMIN] umobile: %s, tid: %s, tmobile: %s" " SMS unbind failed. Message: %s", pmobile, terminal.tid, tmobile, ErrorCode.ERROR_MESSAGE[status]) except Exception as e: status = ErrorCode.FAILED logging.exception("[ADMIN] Delete service failed." " tmobile: %s, owner mobile: %s, Exception: %s", tmobile, pmobile, e.args) self.write_ret(status)
def post(self, ecmobile): """Delete the corp. """ status = ErrorCode.SUCCESS try: ec = self.get_ecbusiness_info(ecmobile) groups = self.db.query( "SELECT id FROM T_GROUP WHERE corp_id = %s", ec.cid) groups = [group.id for group in groups] terminals = self.db.query("SELECT id, tid, mobile, owner_mobile" " FROM T_TERMINAL_INFO WHERE group_id IN %s", tuple(groups + DUMMY_IDS)) for terminal in terminals: # unbind terminal seq = str(int(time.time() * 1000))[-4:] args = DotDict(seq=seq, tid=terminal.tid) response = GFSenderHelper.forward( GFSenderHelper.URLS.UNBIND, args) response = json_decode(response) if response['success'] == ErrorCode.SUCCESS: logging.info("[ADMIN] umobile: %s, tid: %s, tmobile:%s" " GPRS unbind successfully", terminal.owner_mobile, terminal.tid, terminal.mobile) 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("[ADMIN] umobile:%s, tid: %s, tmobile:%s" " GPRS unbind failed, message: %s, send JB sms...", terminal.owner_mobile, terminal.tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[status]) 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)) status = ret.status if ret.status == ErrorCode.SUCCESS: self.db.execute("UPDATE T_TERMINAL_INFO" " SET service_status = %s" " WHERE id = %s", UWEB.SERVICE_STATUS.TO_BE_UNBIND, terminal.id) logging.info("[ADMIN] umobile: %s, tid: %s, tmobile: %s" " SMS unbind successfully.", terminal.owner_mobile, terminal.tid, terminal.mobile) else: logging.error("[ADMIN] umobile: %s, tid: %s, tmobile: %s" " SMS unbind failed. Message: %s", terminal.owner_mobile, terminal.tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[status]) self.db.execute("DELETE FROM T_CORP WHERE cid = %s", ec.cid) except Exception as e: status = ErrorCode.FAILED logging.exception("Delete service failed. ecmobile: %s, Exception: %s.", ecmobile, e.args) 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)