def handle_new_login(t_info, address, connection, channel, exchange, gw_binding, db, redis): """Handle the login packet with version bigger than 2.2.0 S1 t_info:{'dev_id' // tid 't_msisdn' // tmobile 'u_msisdn' // umobile 'imei' // sim's id 'imsi' // track's id 'dev_type' 'softversion' // version of terminal, like 2.3.0 'timestamp' 'psd' 'keys_num' 'sessionID' 'command' 'factory_name' } flag(t_info['psd']): 0 - boot_normally 1 - active_terminal 2 - assert_reboot 3 - network_uncovered 4 - server_no_response 5 - config_changed 6 - session_expired 7 - terminal_unactived 8 - package_send_fail 9 - simcard_error 10 - gprs_error 11 - mcu_timeout 12 - reboot_by_sms 100 - script_reload workflow: if normal login: if sn and imsi exist, but msisdn and msisdn are empty: send register sms again normal login, check [SN,PHONE,IMSI,USER] is matching or not. else: #JH delete old bind relation of tid, and send message to old user. update new bind relation of tmobile, and send message to new user. login response packet: 0 - success, then get a sessionID for terminal and record terminal's address 1 - unregister, terminal login first. 3 - illegal sim, a mismatch between [SN,PHONE,IMSI,USER] 6 - not whitelist """ args = DotDict(success=GATEWAY.LOGIN_STATUS.SUCCESS, sessionID='') tid = t_info.dev_id resend_key, resend_flag = get_resend_flag(redis, tid, t_info.timestamp, t_info.command) sms = '' t_status = None #NOTE: new softversion, new meaning, 1: active; othter: normal login flag = t_info['psd'] terminal = db.get("SELECT tid, group_id, mobile, imsi, owner_mobile, service_status," " defend_status, mannual_status, icon_type, login_permit, " " alias, vibl, use_scene, push_status, speed_limit, stop_interval," " distance_current" " FROM T_TERMINAL_INFO" " WHERE mobile = %s LIMIT 1", t_info['t_msisdn']) cnum = QueryHelper.get_cnum_by_terminal(tid, t_info['t_msisdn'], redis, db) #NOTE: normal login if flag != "1": # normal login #NOTE: no tmobile and ower_mobile if (not t_info['t_msisdn']) and (not t_info['u_msisdn']): t = db.get("SELECT tid, group_id, mobile, imsi, owner_mobile, service_status" " FROM T_TERMINAL_INFO" " WHERE service_status=1" " AND tid = %s " " AND imsi = %s LIMIT 1", t_info['dev_id'], t_info['imsi']) if t: args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM t_info['t_msisdn'] = t.mobile t_info['u_msisdn'] = t.owner_mobile register_sms = SMSCode.SMS_REGISTER % (t.owner_mobile, t.mobile) SMSHelper.send_to_terminal(t.mobile, register_sms) logging.info("[GW] A crash terminal tid:%s, imei:%s has no tmobile: %s, umobile:%s in login packet, so send %s again.", t_info['dev_id'], t_info['imei'], t_info['t_msisdn'], t_info['u_msisdn'], register_sms) else: args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM logging.info("[GW] A crash terminal:%s login without umobile and tmobile, and there is no record in db.", t_info['dev_id']) else: #NOTE: no tmobile if not t_info['t_msisdn']: # login first. tid_terminal = db.get("SELECT tid, mobile, owner_mobile, service_status" " FROM T_TERMINAL_INFO" " WHERE tid = %s LIMIT 1", t_info['dev_id']) args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM if tid_terminal: #NOTE: mobile is not not JH, send caution to owner sms_ = SMSCode.SMS_NOT_JH % tid_terminal.mobile SMSHelper.send(tid_terminal.owner_mobile, sms_) logging.warn("[GW] terminal: %s login at first time.", t_info['dev_id']) #NOTE: tmobile is exist elif terminal: alias = QueryHelper.get_alias_by_tid(terminal['tid'], redis, db) if terminal['tid'] != t_info['dev_id']: args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM sms = SMSCode.SMS_TERMINAL_HK % alias logging.warn("[GW] Terminal changed dev, mobile: %s, old_tid: %s, new_tid: %s", t_info['t_msisdn'], terminal['tid'], t_info['dev_id']) elif terminal['imsi'] != t_info['imsi']: args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM sms = SMSCode.SMS_TERMINAL_HK % alias logging.warn("[GW] Terminal imsi is wrong, tid: %s, mobile: %s, old_imsi: %s, new_imsi: %s", t_info['dev_id'], t_info['t_msisdn'], terminal['imsi'], t_info['imsi']) elif terminal['owner_mobile'] != t_info['u_msisdn']: register_sms = SMSCode.SMS_REGISTER % (terminal['owner_mobile'], terminal['mobile']) SMSHelper.send_to_terminal(terminal['mobile'], register_sms) logging.warn("[GW] Terminal owner_mobile is wrong, tid: %s, old_owner_mobile: %s, new_owner_mobile: %s, send the regist sms: %s again", t_info['dev_id'], terminal['owner_mobile'], t_info['u_msisdn'], register_sms) elif terminal['service_status'] == UWEB.SERVICE_STATUS.TO_BE_UNBIND: t_status = UWEB.SERVICE_STATUS.TO_BE_UNBIND logging.warn("[GW] Terminal is unbinded. tid: %s, mobile: %s", t_info["dev_id"], t_info['t_msisdn']) else: logging.info("[GW] Terminal normal login successfully. tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) else: args.success = GATEWAY.LOGIN_STATUS.UNREGISTER logging.error("[GW] Terminal login failed, unregister. tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) #NOTE: JH else: # JH logging.info("[GW] Terminal JH started. tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) # 0. Initialize the valus keeps same as the default value in database. group_id = -1 login_permit = 1 mannual_status = UWEB.DEFEND_STATUS.YES defend_status = UWEB.DEFEND_STATUS.YES icon_type = 0 alias = '' push_status = 1 vibl = 1 use_scene = 3 speed_limit = 120 stop_interval = 0 distance_current = 0 # send JH sms to terminal. default active time is one year. begintime = datetime.datetime.now() endtime = begintime + relativedelta(years=1) # 1. check data validation logging.info("[GW] Checking terminal mobile: %s and owner mobile: %s, Terminal: %s", t_info['t_msisdn'], t_info['u_msisdn'], t_info['dev_id']) if not (check_phone(t_info['u_msisdn']) and check_phone(t_info['t_msisdn'])): args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM lc = LoginRespComposer(args) request = DotDict(packet=lc.buf, address=address, dev_id=t_info['dev_id']) if t_info['u_msisdn']: # send JH failed caution to owner sms = SMSCode.SMS_JH_FAILED SMSHelper.send(t_info['u_msisdn'], sms) logging.error("[GW] Login failed! Invalid terminal mobile: %s or owner_mobile: %s, tid: %s", t_info['t_msisdn'], t_info['u_msisdn'], t_info['dev_id']) append_gw_request(request, connection, channel, exchange, gw_binding) # 2. delete to_be_unbind terminal if terminal and terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND: logging.info("[GW] Delete terminal which is to_be_unbind. tid: %s, mobile: %s", terminal['tid'], terminal['mobile']) delete_terminal_new(terminal['tid'], db, redis) terminal = None # 3. add user info exist = db.get("SELECT id FROM T_USER" " WHERE mobile = %s", t_info['u_msisdn']) #NOTE: Check ydcw or ajt ajt = QueryHelper.get_ajt_whitelist_by_mobile(t_info['t_msisdn'], db) if ajt: url_out = ConfHelper.UWEB_CONF.ajt_url_out else: url_out = ConfHelper.UWEB_CONF.url_out logging.info("[GW] Login url is: %s, tid: %s, mobile: %s ", url_out, t_info['dev_id'], t_info['t_msisdn']) if exist: logging.info("[GW] Owner already existed. tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) sms = SMSCode.SMS_USER_ADD_TERMINAL % (t_info['t_msisdn'], url_out) else: # get a new psd for new user logging.info("[GW] Create new owner started. tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) psd = get_psd() user_info = dict(umobile=t_info['u_msisdn'], password=psd, uname=t_info['u_msisdn']) add_user(user_info, db, redis) sms = SMSCode.SMS_JH_SUCCESS % (t_info['t_msisdn'], url_out, t_info['u_msisdn'], psd) # 4. JH existed tmobile is_refurbishment = False if terminal: if (terminal['tid'] == t_info['dev_id']) and \ (terminal['imsi'] == t_info['imsi']) and \ (terminal['owner_mobile'] == t_info['u_msisdn']): # 4.1 SCN: Refurbishment, the terminal-info has existed in platform. JH it again. is_refurbishment = True # check the login packet whether is send again if resend_flag: sms = '' logging.info("[GW] Recv resend packet, do not send sms. tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) else: sms = SMSCode.SMS_USER_ADD_TERMINAL % (t_info['t_msisdn'], url_out) logging.info("[GW] Terminal is refurbishment. tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) else: # 4.2 existed tmobile changed dev or corp terminal login first, get the old info(group_id, login_permit and so on) before change group_id = terminal.group_id login_permit = terminal.login_permit mannual_status = terminal.mannual_status defend_status = terminal.defend_status icon_type = terminal.icon_type alias = terminal.alias vibl = terminal.vibl use_scene = terminal.use_scene push_status = terminal.push_status speed_limit = terminal.speed_limit stop_interval = terminal.stop_interval distance_current = terminal.distance_current if terminal.tid == terminal.mobile: # corp terminal login first, keep corp info db.execute("UPDATE T_REGION_TERMINAL" " SET tid = %s" " WHERE tid = %s", t_info['dev_id'], t_info['t_msisdn']) logging.info("[GW] Corp terminal login first, tid: %s, mobile: %s.", t_info['dev_id'], t_info['t_msisdn']) elif terminal.tid != t_info['dev_id']: logging.info("[GW] Terminal changed dev, mobile: %s, new_tid: %s, delete old_tid: %s.", t_info['t_msisdn'], t_info['dev_id'], terminal.tid) else: # Refurbishment, change user logging.info("[GW] Terminal change user, tid: %s, mobile: %s, new_owner_mobile: %s, old_owner_mobile: %s", t_info['dev_id'], t_info['t_msisdn'], t_info['u_msisdn'], terminal.owner_mobile) #NOTE: If terminal has exist, firt remove the terminal logging.info("[GW] Terminal is deleted, tid: %s, mobile: %s.", terminal['tid'], terminal['mobile']) del_user = True if terminal.owner_mobile != t_info['u_msisdn']: # send message to old user of dev_id sms_ = SMSCode.SMS_DELETE_TERMINAL % terminal.mobile SMSHelper.send(terminal.owner_mobile, sms_) if terminal.tid == t_info['dev_id']: # clear data belongs to the terminal clear_data(terminal.tid, db, redis) logging.info("[GW] Send delete terminal message: %s to user: %s", sms_, terminal.owner_mobile) else: del_user = False delete_terminal_new(terminal.tid, db, redis, del_user=del_user) #NOTE: Normal JH. if not is_refurbishment: # 5. delete existed tid tid_terminal = db.get("SELECT tid, mobile, owner_mobile, service_status" " FROM T_TERMINAL_INFO" " WHERE tid = %s LIMIT 1", t_info['dev_id']) if tid_terminal: logging.info("[GW] Terminal is deleted, tid: %s, mobile: %s.", tid_terminal['tid'], tid_terminal['mobile']) del_user = True if tid_terminal['owner_mobile'] != t_info['u_msisdn']: if tid_terminal['service_status'] == UWEB.SERVICE_STATUS.TO_BE_UNBIND: logging.info("[GW] Terminal is to_be_unbind, tid: %s, mobile: %s, delete it.", tid_terminal['tid'], tid_terminal['mobile']) else: # send message to old user of dev_id sms_ = SMSCode.SMS_DELETE_TERMINAL % tid_terminal['mobile'] SMSHelper.send(tid_terminal['owner_mobile'], sms_) logging.info("[GW] Send delete terminal message: %s to user: %s", sms_, tid_terminal['owner_mobile']) # user changed, must clear history data of dev_id clear_data(tid_terminal['tid'], db, redis) else: del_user = False delete_terminal_new(tid_terminal['tid'], db, redis, del_user=del_user) # 6 add terminal info # check use sence ttype = get_terminal_type_by_tid(t_info['dev_id']) logging.info("[GW] Terminal's type is %s. tid: %s, mobile: %s", ttype, t_info['dev_id'], t_info['t_msisdn']) terminal_info = dict(tid=t_info['dev_id'], group_id=group_id, dev_type=t_info['dev_type'], tmobile=t_info['t_msisdn'], owner_mobile=t_info['u_msisdn'], imsi=t_info['imsi'], imei=t_info['imei'], factory_name=t_info['factory_name'], softversion=t_info['softversion'], keys_num=t_info['keys_num'], login=GATEWAY.TERMINAL_LOGIN.ONLINE, service_status=UWEB.SERVICE_STATUS.ON, mannual_status=mannual_status, push_status=push_status, icon_type=icon_type, begintime=int(time.mktime(begintime.timetuple())), endtime=4733481600, offline_time=int(time.mktime(begintime.timetuple())), cnum=cnum, login_permit=login_permit, bt_mac=t_info['bt_mac'], bt_name=t_info['bt_name'], vibl=vibl, use_scene=use_scene, biz_type=UWEB.BIZ_TYPE.YDWS, alias=alias, speed_limit=speed_limit, stop_interval=stop_interval, distance_current=distance_current) add_terminal(terminal_info, db, redis) # record the add action, enterprise or individual corp = QueryHelper.get_corp_by_gid(group_id, db) bind_info = dict(tid=t_info['dev_id'], tmobile=t_info['t_msisdn'], umobile=t_info['u_msisdn'], group_id=group_id, cid=corp.get('cid', '') if corp else '', add_time=int(time.time())) record_add_action(bind_info, db) logging.info("[GW] Terminal JH success! tid: %s, mobile: %s.", t_info['dev_id'], t_info['t_msisdn']) # subscription LE for new sim thread.start_new_thread(subscription_lbmp, (t_info,)) if args.success == GATEWAY.LOGIN_STATUS.SUCCESS: # get SessionID if resend_flag: logging.warn("[GW] Recv resend login packet and use old sessionID! packet: %s, tid: %s, mobile: %s.", t_info, t_info['dev_id'], t_info['t_msisdn']) args.sessionID = QueryHelper.get_terminal_sessionID(t_info['dev_id'], redis) if not args.sessionID: args.sessionID = get_sessionID() else: #NOTE: generate a sessionid and keep it in redis. args.sessionID = get_sessionID() terminal_sessionID_key = get_terminal_sessionID_key(t_info['dev_id']) redis.setvalue(terminal_sessionID_key, args.sessionID) redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY) # record terminal address update_terminal_status(redis, t_info["dev_id"], address) #NOTE: When termianl is normal login, update some properties to platform. info = DotDict(login=GATEWAY.TERMINAL_LOGIN.ONLINE, mobile=t_info['t_msisdn'], keys_num=t_info['keys_num'], softversion=t_info['softversion'], login_time=int(time.time()), dev_id=t_info["dev_id"], bt_mac=t_info['bt_mac'], bt_name=t_info['bt_name'], dev_type=t_info['dev_type']) update_terminal_info(db, redis, info) logging.info("[GW] Terminal login success! tid: %s, mobile: %s", t_info['dev_id'], t_info['t_msisdn']) #NOTE: wspush to cient if flag != "1": # normal login WSPushHelper.pushS4(t_info["dev_id"], db, redis) else: # JH pass lc = LoginRespComposer(args) request = DotDict(packet=lc.buf, address=address, dev_id=t_info["dev_id"]) append_gw_request(request, connection, channel, exchange, gw_binding) if sms and t_info['u_msisdn']: logging.info("[GW] Send sms to owner. mobile: %s, content: %s", t_info['u_msisdn'], sms) SMSHelper.send(t_info['u_msisdn'], sms) if t_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND: seq = str(int(time.time()*1000))[-4:] args_ = DotDict(seq=seq, tid=t_info["dev_id"]) ubc = UNBindComposer(args_) request = DotDict(packet=ubc.buf, address=address, dev_id=t_info["dev_id"]) append_gw_request(request, connection, channel, exchange, gw_binding) logging.warn("[GW] Terminal is unbinded, tid: %s, send unbind packet.", t_info["dev_id"])
def post(self): status = ErrorCode.SUCCESS try: data = json_decode(self.request.body) logging.info("[LOG] message request: %s", data) sms_type = data.get('sms_type') tmobile = data.get('tmobile') content = '' if check_phone(tmobile) is None: status = ErrorCode.ILLEGAL_MOBILE self.write_ret(status) else: if sms_type == 'JH': umobile = data.get('umobile') if check_phone(umobile): content = ':SIM' + ' ' + umobile + ':' + tmobile SMSHelper.send_to_terminal(tmobile, content) self.write_ret(status) else: status = ErrorCode.ILLEGAL_MOBILE self.write_ret(status) elif sms_type == 'JB': content = ':' + sms_type is_clear = data.get('is_clear') ret = SMSHelper.send_to_terminal(tmobile, content) ret = json_decode(ret) terminal = self.acbdb.get("SELECT id, tid, owner_mobile, login" " FROM T_TERMINAL_INFO" " WHERE mobile = %s" " AND service_status = 1", tmobile) if not terminal: status = ErrorCode.TERMINAL_NOT_EXISTED logging.error("The terminal with tmobile: %s does not exist!", tmobile) self.write_ret(status) return umobile = terminal.owner_mobile if ret['status'] == 0: self.acbdb.execute("UPDATE T_TERMINAL_INFO" " SET service_status = 2" " WHERE mobile = %s", tmobile) # terminals = self.acbdb.query("SELECT id FROM T_TERMINAL_INFO" # " WHERE owner_mobile = %s" # " AND service_status = 1", # umobile) # clear user # if len(terminals) == 0: # self.acbdb.execute("DELETE FROM T_USER" # " WHERE mobile = %s", # umobile) if is_clear == 1: clear_data(terminal['tid'], self.acbdb, self.redis) self.write_ret(status) elif sms_type == 'CQ': content = ':' + sms_type SMSHelper.send_to_terminal(tmobile, content) self.write_ret(status) elif sms_type == 'REBOOT': content = ':' + sms_type # SMSHelper.send_to_terminal(tmobile,content) SMSHelper.send_update_to_terminal(tmobile, content) self.write_ret(status) elif sms_type == 'TEST': content = u'尊敬的顾客,您好:这是一条测试短信,收到本条短信,说明短信提示服务正常,本短信不需要回复,如有问题,请和客服人员联系。感谢使用我们的产品,您的移动卫士。' SMSHelper.send(tmobile, content) self.write_ret(status) elif sms_type == 'KQLY': content = ':%s 30' % sms_type SMSHelper.send_to_terminal(tmobile, content) self.write_ret(status) elif sms_type == 'LQGZ': content = ':%s 30' % sms_type SMSHelper.send_to_terminal(tmobile, content) self.write_ret(status) elif sms_type == 'DW': content = ':' + sms_type SMSHelper.send_to_terminal(tmobile, content) self.write_ret(status) elif sms_type == 'UPDATE': content = ':' + sms_type SMSHelper.send_update_to_terminal(tmobile, content) self.write_ret(status) elif sms_type == 'DEL': terminal = self.acbdb.get( 'SELECT tid FROM T_TERMINAL_INFO WHERE mobile=%s', tmobile) if terminal: delete_terminal( terminal.tid, self.acbdb, self.redis, del_user=False) self.write_ret(status) elif sms_type == 'DOMAIN': ip = data.get('domain') content = ':DOMAIN ' + ip info = self.acbdb.get( 'SELECT * FROM T_TERMINAL_INFO WHERE mobile=%s', tmobile) if info: self.acbdb.execute("UPDATE T_TERMINAL_INFO" " SET domain=%s WHERE mobile=%s", ip, tmobile) SMSHelper.send_to_terminal(tmobile, content) self.write_ret(status) else: status = ErrorCode.TERMINAL_NOT_EXISTED self.write_ret(status) except Exception, e: logging.exception("acb-->sms post exception : %s", e) self.render('errors/error.html', message=ErrorCode.ERROR_MESSAGE[ErrorCode.FAILED])