示例#1
0
def block_test():
    db = DBConnection().db
    redis = MyRedis()

    #terminals = db.query("select tid from T_TERMINAL_INFO where group_id= '438';")
    #terminals = db.query("select tid from T_TERMINAL_INFO where group_id= '438'")
    terminals = db.query("select id, tid,  test from T_TERMINAL_INFO where mobile = '14778748087' ")
    
    for terminal in terminals: 
        tid = terminal.tid
        #db.execute("UPDATE T_TERMINAL_INFO SET trace_para='60:0' WHERE tid = %s", 
        #           tid)
        #print 'tid: %s test is closed.' % tid
        sessionID_key = get_terminal_sessionID_key(tid)
        old_sessionid = redis.get(sessionID_key)
        if old_sessionid:
            redis.delete(sessionID_key)
            print "Termianl %s delete session in redis." % tid
示例#2
0
def block_test():
    db = DBConnection().db
    redis = MyRedis()

    #terminals = db.query("SELECT tid, mobile, group_id, cid FROM V_TERMINAL WHERE group_id != '-1'")
    #terminals = db.query("SELECT tid, tmobile, group_id, cid FROM V_TERMINAL WHERE cid = '13600335550'")
    #terminals = db.query("SELECT tid, mobile, group_id FROM T_TERMINAL_INFO WHERE stop_interval != 0 limit 1000")
    print 'len ', len(terminals)
    for terminal in terminals: 
        tid = terminal.tid
        db.execute("UPDATE T_TERMINAL_INFO SET stop_interval=0 WHERE tid = %s", 
                   tid)
        print 'tid: %s stop_interval is closed.' % tid
        sessionID_key = get_terminal_sessionID_key(tid)
        old_sessionid = redis.get(sessionID_key)
        if old_sessionid:
            redis.delete(sessionID_key)
            print "Termianl %s delete session in redis." % tid
示例#3
0
文件: unbind.py 项目: jcsy521/ydws
 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)
示例#4
0
文件: testsms.py 项目: jcsy521/ydws
    def put(self):   
        """Modify the charge of test-sms.
        """
        status = ErrorCode.SUCCESS
        try:
            data = json_decode(self.request.body)
            mobile = data.get('mobile')
            test = data.get('test')
            logging.info("[ADMIN] Modify test, data: %s", data)
        except Exception as e: 
            status = ErrorCode.ILLEGAL_DATA_FORMAT 
            logging.exception("[ADMIN] Invalid data format. Exception: %s",
                              e.args) 
            self.write_ret(status) 
            return

        try:
            res = self.db.get("SELECT tid, mobile, test"
                              " FROM T_TERMINAL_INFO"
                              "  WHERE mobile = %s",
                              mobile)
            if not res:
                status = ErrorCode.MOBILE_NOT_ORDERED
                message = ErrorCode.ERROR_MESSAGE[status] % mobile
                self.write_ret(status=status, 
                               message=message)
            else: 
                self.db.execute("UPDATE T_TERMINAL_INFO SET test=%s WHERE tid = %s",
                                test, res['tid'])
                sessionID_key = get_terminal_sessionID_key(res['tid']) 
                old_sessionid = self.redis.get(sessionID_key) 
                if old_sessionid: 
                    self.redis.delete(sessionID_key)
                logging.info("[ADMIN] Tid: %s set test to %s.",
                             res['tid'], test)

                self.write_ret(status=status)
        except Exception as e:
            logging.exception("[ADMIN] Modify test sms failed. mobile: %s.", 
                              mobile)
            self.render('errors/error.html',
                        message=ErrorCode.FAILED)
示例#5
0
def block_test():
    db = DBConnection().db
    redis = MyRedis()

    #terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where static_val =0 and move_val=0")
    terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where tid = '392240008A' ")
    #terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where id in (12923, 21792 )")
    #terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where static_val =60")
    for terminal in terminals: 
        tid = terminal.tid
        #db.execute("UPDATE T_TERMINAL_INFO SET static_val=120,move_val=0 WHERE tid = %s", 
        #           tid)
        #db.execute("UPDATE T_TERMINAL_INFO SET static_val=0, move_val=60 WHERE tid = %s", 
        #           tid)
        print 'tid: %s parking is modified.' % tid
        sessionID_key = get_terminal_sessionID_key(tid)
        old_sessionid = redis.get(sessionID_key)
        if old_sessionid:
            redis.delete(sessionID_key)
            print "Termianl %s delete session in redis." % tid
示例#6
0
 def check_track_status(self):
     logging.info("[CELERY] checkertask check track status started.")
     try:
         terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO"
                                   "  WHERE track = 1"
                                   "    AND service_status = 1")
         for terminal in terminals:
             track_key = get_track_key(terminal.tid)
             track = self.redis.get(track_key)
             logging.info("[CK] track: %s, tid: %s", track, terminal.tid)
             if not track:
                 self.db.execute("UPDATE T_TERMINAL_INFO"
                                 "  SET track = 0"
                                 "  WHERE tid = %s LIMIT 1",
                                 terminal.tid)
                 sessionID_key = get_terminal_sessionID_key(terminal.tid)
                 self.redis.delete(sessionID_key)
                 logging.info("[CK] Turn off track of terminal: %s", terminal.tid)
     except Exception as e:
         logging.exception("[CELERY] Check track status exception.")
示例#7
0
文件: no_test.py 项目: jcsy521/ydws
def block_test():
    db = DBConnection().db
    redis = MyRedis()

    #terminals = db.query("select * from T_TERMINAL_INFO where mobile = 14778741722 " )
    terminals = db.query("select * from T_TERMINAL_INFO where mobile in (14778473468 ) " )
    for terminal in terminals: 
        tid = terminal.tid
        t = db.query("SELECT tid from T_TERMINAL_INFO where test !=0 and tid=%s", tid)  
        if not t:
            continue

        #print 'clear', terminal 
        db.execute("UPDATE T_TERMINAL_INFO SET test=0 WHERE tid = %s", 
                   tid)
        print 'tid: %s test is closed.' % tid
        sessionID_key = get_terminal_sessionID_key(tid)
        old_sessionid = redis.get(sessionID_key)
        if old_sessionid:
            redis.delete(sessionID_key)
            print "Termianl %s delete session in redis." % tid
示例#8
0
def batch_import(file_path):
    db = DBConnection().db
    redis = MyRedis()

    online_style = xlwt.easyxf('font: colour_index green, bold off; align: wrap on, vert centre, horiz center;')
    offline_style = xlwt.easyxf('font: colour_index brown, bold off; align: wrap on, vert centre, horiz center;')

    wt = xlwt.Workbook()   
    ws = wt.add_sheet(u'jia') 

    wb = xlrd.open_workbook(file_path)
    sheet = wb.sheets()[0]
    lst = ""
    num = 0
    for i in range(0,sheet.nrows):
        row = sheet.row_values(i)
        print 'row', row
        mobile = unicode(row[0])
        mobile = mobile[0:11] 
        #mobile = unicode(row[0])[0:-2]
        print 'mobile', repr(mobile)

        sql_t = 'select tid, test, login, service_status from T_TERMINAL_INFO where mobile =' + mobile
        terminal = db.get(sql_t)
        print 'terminal', terminal
        tid = terminal.get('tid','') if terminal else ''
        sql = 'UPDATE T_TERMINAL_INFO set test=0 where mobile =' + mobile
        print 'sql', sql
        #db.execute('UPDATE T_TERMINAL_INFO set test=0 where mobile = %s', int(mobile))
        #db.execute(sql)
        sessionID_key = get_terminal_sessionID_key(tid)
        print 'sessionID_key', sessionID_key
        old_sessionid = redis.get(sessionID_key)
        if old_sessionid:
            redis.delete(sessionID_key)
            logging.info("[UWEB] Termianl %s delete session in redis.", tid)
        #if i == 3:
        #    break
    
    print num 
示例#9
0
文件: setting.py 项目: jcsy521/ydws
    def put(self):
        """Modify the settings.
        """
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            logging.info("[UWEB] Setting modify request: %s", self.request.body) 
            tmobile = data.tmobile 
            key = data.key
            value = data.value 
            logging.info("[UWEB] Setting modify request: %s", data) 
        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:
            clear_session = False
            terminal = QueryHelper.get_terminal_by_tmobile(tmobile, self.db)
            if not terminal:
                status = ErrorCode.TERMINAL_NOT_EXISTED 
            else:
                tid = terminal.tid
                sql_cmd = "UPDATE T_TERMINAL_INFO SET %(key)s = %(value)s WHERE tid='%(tid)s' LIMIT 1 " % locals()
                if key in ['tracking_interval']: 
                    clear_session = True

                if clear_session:
                    sessionID_key = get_terminal_sessionID_key(tid)
                    self.redis.delete(sessionID_key)
                self.db.execute(sql_cmd)

            self.write_ret(status)
        except Exception as e:
            logging.exception("Modify setting failed. Exception: %s", 
                              e.args)
            self.render('errors/error.html', 
                        message=ErrorCode.FAILED)
示例#10
0
文件: business.py 项目: jcsy521/ydws
 def post(self, tmobile, service_status):
     status = ErrorCode.SUCCESS
     try:
         terminal = self.db.get("SELECT id, tid FROM T_TERMINAL_INFO"
                                "  WHERE mobile = %s",
                                tmobile)
         self.db.execute("UPDATE T_TERMINAL_INFO"
                         "  SET service_status = %s"
                         "  WHERE id = %s",
                         service_status, terminal.id)
         # clear redis
         sessionID_key = get_terminal_sessionID_key(terminal.tid)
         address_key = get_terminal_address_key(terminal.tid)
         info_key = get_terminal_info_key(terminal.tid)
         lq_sms_key = get_lq_sms_key(terminal.tid)
         lq_interval_key = get_lq_interval_key(terminal.tid)
         keys = [sessionID_key, address_key, info_key, lq_sms_key, lq_interval_key]
         self.redis.delete(*keys)
     except Exception as e:
         status = ErrorCode.FAILED
         logging.exception("[ADMIN] Update service_status to %s failed."
                           "  tmobile: %s, Exception: %s", 
                           service_status, tmobile, e.args)
     self.write_ret(status)
示例#11
0
def block_test():
    db = DBConnection().db
    redis = MyRedis()

    #terminals = db.query("SELECT tid, mobile, group_id, cid FROM V_TERMINAL WHERE group_id != '-1'")
    terminals = db.query("SELECT tid, tmobile, group_id, cid FROM V_TERMINAL WHERE cid = '13600335550'")
    for terminal in terminals: 
        tid = terminal.tid
        ttype = get_terminal_type_by_tid(tid) 
        print 'ttype', ttype
        if ttype == 'zj200':
            t = db.query("SELECT tid from T_TERMINAL_INFO where test !=0 and tid=%s", tid)  
            if not t:
                continue

            #print 'clear'
            db.execute("UPDATE T_TERMINAL_INFO SET test=0 WHERE tid = %s", 
                       tid)
            print 'tid: %s test is closed.' % tid
            sessionID_key = get_terminal_sessionID_key(tid)
            old_sessionid = redis.get(sessionID_key)
            if old_sessionid:
                redis.delete(sessionID_key)
                print "Termianl %s delete session in redis." % tid
示例#12
0
 def test_session(self):
     tid = 'T123SIMULATOR'
     sessionID_key = get_terminal_sessionID_key(tid)
     old_sessionid = self.redis.get(sessionID_key)
     print  'session_id: %s' % old_sessionid
示例#13
0
文件: login.py 项目: jcsy521/ydws
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"])            
示例#14
0
文件: track.py 项目: jcsy521/ydws
    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)
示例#15
0
文件: batch.py 项目: jcsy521/ydws
    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)
示例#16
0
文件: business.py 项目: jcsy521/ydws
    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)
示例#17
0
文件: login.py 项目: jcsy521/ydws
def handle_old_login(t_info, address, connection, channel, exchange, gw_binding, db, redis):
    """
    S1
    Login response packet:

    0 - success, then get a sessionID for terminal and record terminal's address
    1 - illegal format of sim
    2 - expired, service stop or endtime < now
    3 - illegal sim, a mismatch between imsi and sim
    4 - psd wrong. HK
    5 - dev_id is empty
    6 - not whitelist
    """
    sms = None
    args = DotDict(success=GATEWAY.LOGIN_STATUS.SUCCESS,
                   sessionID='')
    dev_id = t_info['dev_id']

    resend_key, resend_flag = get_resend_flag(redis, dev_id, t_info.timestamp, t_info.command) 

    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"])
        append_gw_request(request, connection, channel, exchange, gw_binding)
        logging.error("[GW] Login failed! Invalid terminal mobile: %s or owner_mobile: %s, dev_id: %s",
                      t_info['t_msisdn'], t_info['u_msisdn'], t_info['dev_id'])
        return

    t_status = db.get("SELECT service_status"
                      "  FROM T_TERMINAL_INFO"
                      "  WHERE mobile = %s",
                      t_info['t_msisdn'])
    if t_status and t_status.service_status == GATEWAY.SERVICE_STATUS.OFF:
        args.success = GATEWAY.LOGIN_STATUS.EXPIRED
        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)
        logging.error("[GW] Login failed! terminal service expired! mobile: %s, dev_id: %s",
                      t_info['t_msisdn'], t_info['dev_id'])
        return


    logging.info("[GW] Checking imsi: %s and mobile: %s, Terminal: %s",
                 t_info['imsi'], t_info['t_msisdn'], t_info['dev_id'])
    tmobile = db.get("SELECT imsi FROM T_TERMINAL_INFO"
                     "  WHERE mobile = %s", t_info['t_msisdn'])
    if tmobile and tmobile.imsi and tmobile.imsi != t_info['imsi']:
        # check terminal and give a appropriate HK notification
        terminal = db.get("SELECT id FROM T_TERMINAL_INFO WHERE tid=%s", t_info['dev_id'])
        if terminal:
            alias = QueryHelper.get_alias_by_tid(t_info['dev_id'], redis, db)
        else: 
            alias = t_info['t_msisdn']
        args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM
        sms = SMSCode.SMS_TERMINAL_HK % alias 
        SMSHelper.send(t_info['u_msisdn'], sms)
        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)
        logging.error("[GW] Login failed! Illegal SIM: %s for Terminal: %s",
                      t_info['t_msisdn'], t_info['dev_id'])
        return

    terminal = db.get("SELECT id, mobile, owner_mobile, service_status"
                      "  FROM T_TERMINAL_INFO"
                      "  WHERE tid = %s", t_info['dev_id'])
    if terminal:
        if terminal.mobile != t_info['t_msisdn']:
            logging.info("[GW] Terminal: %s changed mobile, old mobile: %s, new mobile: %s",
                         t_info['dev_id'], terminal.mobile,
                         t_info['t_msisdn'])
            if (terminal.owner_mobile == t_info['u_msisdn'] or
                terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND):
                # delete old terminal!
                logging.info("[GW] Delete old tid bind relation. tid: %s, owner_mobile: %s, service_status: %s",
                             t_info['dev_id'], t_info['u_msisdn'],
                             terminal.service_status)
                delete_terminal_new(t_info['dev_id'], db, redis, del_user=False)
                exist = db.get("SELECT tid, owner_mobile, service_status FROM T_TERMINAL_INFO"
                               "  WHERE mobile = %s LIMIT 1",
                               t_info['t_msisdn'])
                if exist:
                    # cannot send unbind packet to dev_id
                    t_status = None
                    logging.info("[GW] Delete old tmobile bind relation. tid: %s, mobile: %s",
                                 exist.tid, t_info['t_msisdn'])
                    delete_terminal_new(exist.tid, db, redis, del_user=False)
                    if exist.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND:
                        logging.info("[GW] Terminal: %s of %s is to_be_unbind, delete it.",
                                     exist.tid, t_info['t_msisdn'])
                    elif exist.owner_mobile != t_info['u_msisdn']:
                        sms = SMSCode.SMS_DELETE_TERMINAL % t_info['t_msisdn']
                        SMSHelper.send(exist.owner_mobile, sms)
                terminal = None
            else:
                args.success = GATEWAY.LOGIN_STATUS.ILLEGAL_SIM
                sms = SMSCode.SMS_TID_EXIST % t_info['dev_id']
                SMSHelper.send(t_info['u_msisdn'], sms)
                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)
                logging.error("[GW] Login failed! Terminal: %s already bound by %s, new mobile: %s",
                              t_info['dev_id'], terminal.mobile, t_info['t_msisdn'])
                return

    #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] Terminal: %s, login url is: %s", t_info['t_msisdn'], url_out)

    if t_info['psd']:
        flag = 0
        # check terminal exist or not when HK
        if not terminal:
            args.success = GATEWAY.LOGIN_STATUS.UNREGISTER
            sms = SMSCode.SMS_TID_NOT_EXIST
            SMSHelper.send(t_info['u_msisdn'], sms)
            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)
            logging.error("[GW] Login failed! Terminal %s execute HK, but tid is not exist",
                          t_info['dev_id'])
            return
        # HK, change terminal mobile or owner mobile
        logging.info("[GW] Checking password. Terminal: %s",
                     t_info['dev_id'])
        owner = db.get("SELECT id FROM T_USER"
                       "  WHERE mobile = %s"
                       "    AND password = password(%s)",
                       terminal.owner_mobile, t_info['psd'])
        if not owner:
            # psd wrong
            sms = SMSCode.SMS_PSD_WRONG
            args.success = GATEWAY.LOGIN_STATUS.PSD_WRONG
            logging.error("[GW] Login failed! Password invalid. Terminal: %s",
                          t_info['dev_id'])
        else:
            if terminal:
                if terminal.mobile != t_info['t_msisdn']:
                    # terminal HK
                    logging.info("[GW] Terminal: %s HK started.", t_info['dev_id'])
                    # unbind old tmobile
                    old_bind = db.get("SELECT id, tid FROM T_TERMINAL_INFO"
                                      "  WHERE mobile = %s"
                                      "    AND id != %s",
                                      t_info['t_msisdn'], terminal.id)
                    if old_bind:
                        # clear db
                        db.execute("DELETE FROM T_TERMINAL_INFO"
                                   "  WHERE id = %s", 
                                   old_bind.id) 
                        # clear redis
                        sessionID_key = get_terminal_sessionID_key(old_bind.tid)
                        address_key = get_terminal_address_key(old_bind.tid)
                        info_key = get_terminal_info_key(old_bind.tid)
                        lq_sms_key = get_lq_sms_key(old_bind.tid)
                        lq_interval_key = get_lq_interval_key(old_bind.tid)
                        keys = [sessionID_key, address_key, info_key, lq_sms_key, lq_interval_key]
                        redis.delete(*keys)
                        logging.info("[GW] Delete old bind Terminal: %s, SIM: %s",
                                     t_info['dev_id'], t_info['t_msisdn'])

                    # update new tmobile
                    db.execute("UPDATE T_TERMINAL_INFO"
                               "  SET mobile = %s,"
                               "      imsi = %s"
                               "  WHERE id = %s",
                               t_info['t_msisdn'],
                               t_info['imsi'], terminal.id)
                    # clear redis
                    sessionID_key = get_terminal_sessionID_key(t_info['dev_id'])
                    address_key = get_terminal_address_key(t_info['dev_id'])
                    info_key = get_terminal_info_key(t_info['dev_id'])
                    lq_sms_key = get_lq_sms_key(t_info['dev_id'])
                    lq_interval_key = get_lq_interval_key(t_info['dev_id'])
                    keys = [sessionID_key, address_key, info_key, lq_sms_key, lq_interval_key]
                    redis.delete(*keys)
                    # HK sms
                    sms = SMSCode.SMS_TERMINAL_HK_SUCCESS % (terminal.mobile, t_info['t_msisdn'])
                    # subscription LE for new sim
                    thread.start_new_thread(subscription_lbmp, (t_info,)) 
                    logging.info("[GW] Terminal: %s HK success!", t_info['dev_id'])

                if terminal.owner_mobile != t_info['u_msisdn']:
                    logging.info("[GW] Owner HK started. Terminal: %s", t_info['dev_id'])
                    # owner HK
                    user = db.get("SELECT id FROM T_USER"
                                  "  WHERE mobile = %s",
                                  t_info['u_msisdn'])
                    if user:
                        logging.info("[GW] Owner already existed. Terminal: %s", t_info['dev_id'])
                        sms = SMSCode.SMS_USER_ADD_TERMINAL % (t_info['t_msisdn'],
                                                               url_out) 
                    else:
                        logging.info("[GW] Create new owner started. Terminal: %s", t_info['dev_id'])
                        psd = get_psd()
                        user_info = dict(umobile=t_info['u_msisdn'],
                                         password=psd)
                        add_user(user_info, db, redis)
                        sms = SMSCode.SMS_USER_HK_SUCCESS % (t_info['u_msisdn'],
                                                             url_out,
                                                             t_info['u_msisdn'],
                                                             psd)
                    db.execute("UPDATE T_TERMINAL_INFO"
                               "  SET owner_mobile = %s"
                               "  WHERE id = %s",
                               t_info['u_msisdn'], terminal.id)
                    logging.info("[GW] Owner of %s HK success!", t_info['dev_id'])
            else:
                logging.error("[GW] What happened? Cannot find old terminal by dev_id: %s",
                              t_info['dev_id']) 
    else:
        flag = 1 
        # login or JH
        if terminal:
            # login
            logging.info("[GW] Terminal: %s Normal login started!",
                         t_info['dev_id']) 
        else:
            # SMS JH or admin JH or change new dev JH
            logging.info("[GW] Terminal: %s, mobile: %s JH started.",
                         t_info['dev_id'], t_info['t_msisdn'])
            exist = db.get("SELECT id FROM T_USER"
                           "  WHERE mobile = %s",
                           t_info['u_msisdn'])
            if exist:
                logging.info("[GW] Owner already existed. Terminal: %s", t_info['dev_id'])
                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. Terminal: %s", t_info['dev_id'])
                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)

            admin_terminal = db.get("SELECT id, tid FROM T_TERMINAL_INFO"
                                    "  WHERE tid = %s",
                                    t_info['t_msisdn'])
            if admin_terminal:
                # admin JH
                db.execute("UPDATE T_TERMINAL_INFO"
                           "  SET tid = %s,"
                           "      dev_type = %s,"
                           "      owner_mobile = %s,"
                           "      imsi = %s,"
                           "      imei = %s,"
                           "      factory_name = %s,"
                           "      keys_num = %s,"
                           "      softversion = %s"
                           "  WHERE id = %s",
                           t_info['dev_id'],
                           t_info['dev_type'],
                           t_info['u_msisdn'],
                           t_info['imsi'],
                           t_info['imei'],
                           t_info['factory_name'],
                           t_info['keys_num'],
                           t_info['softversion'],
                           admin_terminal.id)
                db.execute("UPDATE T_CAR SET tid = %s"
                           "  WHERE tid = %s",
                           t_info['dev_id'], t_info['t_msisdn'])
                logging.info("[GW] Terminal %s by ADMIN JH success!", t_info['dev_id'])
            else:
                exist_terminal = db.get("SELECT id, tid FROM T_TERMINAL_INFO"
                                        "  WHERE mobile = %s",
                                        t_info['t_msisdn'])
                if exist_terminal:
                    # unbind old tmobile
                    db.execute("DELETE FROM T_TERMINAL_INFO"
                               "  WHERE id = %s",
                               exist_terminal.id)
                    # clear redis
                    sessionID_key = get_terminal_sessionID_key(exist_terminal.tid)
                    address_key = get_terminal_address_key(exist_terminal.tid)
                    info_key = get_terminal_info_key(exist_terminal.tid)
                    lq_sms_key = get_lq_sms_key(exist_terminal.tid)
                    lq_interval_key = get_lq_interval_key(exist_terminal.tid)
                    keys = [sessionID_key, address_key, info_key, lq_sms_key, lq_interval_key]
                    redis.delete(*keys)
                    logging.info("[GW] Terminal %s change dev, old dev: %s!",
                                 t_info['dev_id'], exist_terminal.tid)

                # send JH sms to terminal. default active time
                # is one year.
                begintime = datetime.datetime.now() 
                endtime = begintime + relativedelta(years=1)

                terminal_info = dict(tid=t_info['dev_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,
                                     group_id=-1,
                                     mannual_status=UWEB.DEFEND_STATUS.YES,
                                     begintime=int(time.mktime(begintime.timetuple())),
                                     endtime=4733481600,
                                     offline_time=int(time.mktime(begintime.timetuple())),
                                     biz_type=UWEB.BIZ_TYPE.YDWS)
                add_terminal(terminal_info, db, redis)

                # record the add action, enterprise or individual
                bind_info = dict(tid=t_info['dev_id'],
                                 tmobile=t_info['t_msisdn'],
                                 umobile=t_info['u_msisdn'],
                                 group_id=-1,
                                 cid='',
                                 add_time=int(time.time()))
                record_add_action(bind_info, db)

                logging.info("[GW] Terminal %s by SMS JH success!", t_info['dev_id'])

            # 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:
            args.sessionID = QueryHelper.get_terminal_sessionID(t_info['dev_id'], redis)
            logging.warn("[GW] Recv resend login packet: %s and use old sessionID: %s!", t_info, args.sessionID) 
            if not args.sessionID:
                args.sessionID = get_sessionID()
        else:
            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)
        # set login
        info = DotDict(login=GATEWAY.TERMINAL_LOGIN.ONLINE,
                       mobile=t_info['t_msisdn'],
                       keys_num=t_info['keys_num'],
                       login_time=int(time.time()),
                       dev_id=t_info["dev_id"])
        update_terminal_info(db, redis, info)
        logging.info("[GW] Terminal %s login success! SIM: %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']:
        SMSHelper.send(t_info['u_msisdn'], sms)

    # unbind terminal of to_be_unbind
    if t_status and t_status.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND:
        logging.info("[GW] Terminal: %s is unbinded, send unbind packet.", t_info["dev_id"])            
        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)
示例#18
0
    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)