def post(self): status = ErrorCode.SUCCESS try: t = self.get_argument('t', None) key = self.get_argument('key', None) tid = self.get_argument('tid', None) mannual_status = self.get_argument('mannual_status', None) logging.info("[UWEB] Defend request t:%s, key:%s, tid:%s, mannual_status: %s", t, key, tid, mannual_status) 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: m = md5() pri_key = 'f36f7c203ea2c7b6f587e223132d9b85' m.update(tid + t + pri_key) hash_ = m.hexdigest() if hash_ != key: logging.info("[UWEB] Delegation requeset key wrong.") raise tornado.web.HTTPError(401) update_mannual_status(self.db, self.redis, tid, mannual_status) self.write_ret(status,) except Exception as e: logging.exception("[UWEB] tid:%s set mannual status to %s failed. Exception: %s", tid, mannual_status, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
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): """Get last infomation of a terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = str(data.mobile) manual_status = data.manual_status token = data.token logging.info("[MANUAL] Request, data:%s", data) except Exception as e: status = ErrorCode.DATA_FORMAT_INVALID logging.exception("[REBOO] Invalid data format, body: %s, mobile: %s.", self.request.body, mobile) self.write_ret(status) return try: status = self.basic_check(token, mobile) if status != ErrorCode.SUCCESS: self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal.tid update_mannual_status(self.db, self.redis, tid, manual_status) self.write_ret(status) except Exception as e: logging.exception("[MANUAL] mobile: %s. Exception: %s", mobile, e.args) status = ErrorCode.FAILED 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 update_terminal_db(self, data): """Update database. """ # NOTE: these fields should to be modified in db terminal_keys = ['cellid_status', 'white_pop', 'trace', 'freq', 'vibchk', 'vibl', 'push_status', 'login_permit', 'alert_freq', 'stop_interval', 'biz_type', 'speed_limit'] terminal_fields = [] if data.has_key('tid'): del data['tid'] for key, value in data.iteritems(): # NOTE: These fields should be modified in database. if key in terminal_keys: if data.get(key, None) is not None: terminal_fields.append(key + ' = ' + str(value)) # NOT: These fields should be handled specially. if key == 'white_list': white_list = data[key] if len(data['white_list']) < 1: pass else: self.db.execute("DELETE FROM T_WHITELIST WHERE tid = %s", self.current_user.tid) for white in white_list[1:]: self.db.execute("INSERT INTO T_WHITELIST" " VALUES(NULL, %s, %s)" " ON DUPLICATE KEY" " UPDATE tid = VALUES(tid)," " mobile = VALUES(mobile)", self.current_user.tid, white) elif key == 'alias': self.db.execute("UPDATE T_TERMINAL_INFO" " SET alias = %s" " WHERE tid = %s", value, 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[key] = value self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'icon_type': self.db.execute("UPDATE T_TERMINAL_INFO" " SET icon_type = %s" " WHERE tid = %s", value, 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[key] = value self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'corp_cnum': self.db.execute("UPDATE T_CAR" " SET cnum = %s" " WHERE tid = %s", safe_utf8(value), self.current_user.tid) self.db.execute("UPDATE T_TERMINAL_INFO" " SET alias = %s" " WHERE tid = %s", safe_utf8(value), 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'] = value if value else self.current_user.sim self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'owner_mobile' and value is not None: umobile = value user = dict(umobile=umobile, password=u'111111') add_user(user, self.db, self.redis) t = QueryHelper.get_terminal_by_tid(self.current_user.tid, self.db) old_uids = [t.owner_mobile] # send sms self.db.execute("UPDATE T_TERMINAL_INFO" " SET owner_mobile = %s" " WHERE tid = %s", umobile, self.current_user.tid) register_sms = SMSCode.SMS_REGISTER % ( umobile, self.current_user.sim) SMSHelper.send_to_terminal(self.current_user.sim, register_sms) # update redis terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[key] = umobile self.redis.setvalue(terminal_info_key, terminal_info) # wspush to client WSPushHelper.pushS3(self.current_user.tid, self.db, self.redis) WSPushHelper.pushS3_dummy(old_uids, self.db, self.redis) elif key == "alert_freq": alert_freq_key = get_alert_freq_key(self.current_user.tid) if self.redis.exists(alert_freq_key): logging.info( "[UWEB] Termianl %s delete alert freq in redis.", self.current_user.tid) self.redis.delete(alert_freq_key) # if vibl has been changed,then update use_scene as well elif key == "vibl": use_scene = get_use_scene_by_vibl(value) self.db.execute("UPDATE T_TERMINAL_INFO SET use_scene=%s WHERE tid=%s", use_scene, self.current_user.tid) logging.info("[UWEB] Terminal %s update use_scene %s and vibl %s", self.current_user.tid, use_scene, value) logging.info( "[UWEB] Termianl %s delete session in redis.", self.current_user.tid) # NOTE: deprecated. elif key == "parking_defend" and value is not None: if value == 1: mannual_status = UWEB.DEFEND_STATUS.SMART else: mannual_status = UWEB.DEFEND_STATUS.YES update_mannual_status( self.db, self.redis, self.current_user.tid, mannual_status) elif key == 'login_permit': # wspush to client WSPushHelper.pushS3(self.current_user.tid, self.db, self.redis) else: pass # NOTE:update database. terminal_clause = ','.join(terminal_fields) if terminal_clause: self.db.execute("UPDATE T_TERMINAL_INFO" " SET " + terminal_clause + " WHERE tid = %s ", self.current_user.tid) # NOTE: clear sessionID if freq, stop_interval, vibl can be found. if data.has_key('freq') or data.has_key('stop_interval') or data.has_key('vibl'): clear_sessionID(self.redis, self.current_user.tid)
def handle_defend(info, address, connection, channel, exchange, gw_binding, db, redis): """ S18 defend status report packet 0: success, then record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id resend_key, resend_flag = get_resend_flag(redis, dev_id, head.timestamp, head.command) # old version is compatible if len(body) == 1: body.append('0') logging.info("[GW] old version is compatible, append mannual status 0") args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, command=head.command) sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID else: if resend_flag: logging.warn("[GW] Recv resend packet, head: %s, body: %s and drop it!", info.head, info.body) else: redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY) hp = AsyncParser(body, head) defend_info = hp.ret defend_info['mannual_status'] = defend_info['defend_status'] if defend_info['defend_source'] != 0: # come from sms or web if defend_info['defend_source'] == "1": _status = u"设防" if defend_info['defend_status'] == "1" else u"撤防" tname = QueryHelper.get_alias_by_tid(head.dev_id, redis, db) sms = SMSCode.SMS_DEFEND_SUCCESS % (tname, _status) user = QueryHelper.get_user_by_tid(head.dev_id, db) if user: SMSHelper.send(user.owner_mobile, sms) del defend_info['defend_status'] del defend_info['defend_source'] update_mannual_status(db, redis, head.dev_id, defend_info['mannual_status']) update_terminal_status(redis, head.dev_id, address) if args['success'] == GATEWAY.RESPONSE_STATUS.SUCCESS: acc_status_info_key = get_acc_status_info_key(dev_id) acc_status_info = redis.getvalue(acc_status_info_key) if acc_status_info and (not acc_status_info['t2_status']): # T2(query) is need args['success'] = 3 # acc_status is changed logging.info("[GW] ACC_status is changed, dev_id: %s, acc_status_info: %s", dev_id, acc_status_info) hc = AsyncRespComposer(args) request = DotDict(packet=hc.buf, address=address, dev_id=dev_id) append_gw_request(request, connection, channel, exchange, gw_binding) except: logging.exception("[GW] Handle defend status report exception.") GWException().notify()