示例#1
0
文件: test_mt.py 项目: jcsy521/ydws
def test():
    status = ErrorCode.SUCCESS
    
    seq = str(int(time.time()*1000))[-4:]
    args = DotDict(seq=seq,
                   tid='jiabind')
    response = GFSenderHelper.forward(GFSenderHelper.URLS.UNBIND, args)
    response = json_decode(response)
    print 'response', 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']
    print 'status', status
示例#2
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)
示例#3
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)
示例#4
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)
示例#5
0
文件: car.py 项目: jcsy521/ydws
 def get(self, tid):
     status = ErrorCode.SUCCESS
     logging.info("[UWEB] switch car request: %s", tid)
     try:
         terminal = self.db.get("SELECT ti.tid, ti.mobile as sim, owner_mobile,"
                               "  ti.login, ti.defend_status "
                               "  FROM T_TERMINAL_INFO as ti "
                               "  WHERE ti.tid = %s"
                               "    AND service_status = %s"
                               "  LIMIT 1",
                               tid, UWEB.SERVICE_STATUS.ON)
         if terminal: 
             self.send_lq_sms(terminal.sim, tid, SMS.LQ.WEB)
             self.bookkeep(dict(cid=self.current_user.cid,
                                oid=self.current_user.oid,
                                uid=terminal.owner_mobile if terminal.owner_mobile else self.current_user.cid,
                                tid=tid,
                                sim=terminal.sim))
             def _on_finish(response):
                 response = json_decode(response)
                 if response['success'] == ErrorCode.SUCCESS:
                     new_fobs = response['params']['FOBS']
                     if new_fobs:
                         fobs = self.db.query("SELECT fobid FROM T_FOB"
                                              "  WHERE tid = %s",
                                              tid)
                         old_fobids = [fob.fobid for fob in fobs]
                         new_fobids = new_fobs.split(':')
                         add_fobids = list(set(new_fobids) - set(old_fobids))
                         del_fobids = list(set(old_fobids) - set(new_fobids))
                         for fobid in add_fobids:
                             self.db.execute("INSERT INTO T_FOB(tid, fobid)"
                                             "  VALUES(%s, %s)",
                                             tid, fobid)
                         for fobid in del_fobids:
                             self.db.execute("DELETE FROM T_FOB"
                                             "  WHERE tid = %s"
                                             "    AND fobid = %s",
                                             tid, fobid)
                         if len(old_fobids) != len(new_fobids):
                             self.db.execute("UPDATE T_TERMINAL_INFO"
                                             "  SET keys_num = %s"
                                             "  WHERE tid = %s",
                                             len(new_fobids),
                                             tid)
                         # redis
                         terminal_info_key = get_terminal_info_key(tid)
                         terminal_info = self.redis.getvalue(terminal_info_key)
                         if terminal_info:
                             terminal_info['fob_list'] = new_fobids
                             terminal_info['keys_num'] = len(new_fobids)
                             self.redis.setvalue(terminal_info_key, terminal_info)
             # send S5 for querying fobs 3 minutes later
             seq = str(int(time.time()*1000))[-4:]
             args = DotDict(seq=seq,
                            tid=tid,
                            params=['fobs',])
             IOLoop.instance().add_timeout(int(time.time()) + 180,
                                           lambda: GFSenderHelper.async_forward(
                                                       GFSenderHelper.URLS.QUERY,
                                                       args,
                                                       _on_finish))
         else:
             status = ErrorCode.LOGIN_AGAIN
         self.write_ret(status) 
     except Exception as e:
         logging.exception("[UWEB] uid: %s switch car to tid: %s failed. Exception: %s", 
                           self.current_user.uid, tid, e.args) 
         status = ErrorCode.SERVER_BUSY
         self.write_ret(status)
示例#6
0
文件: unbind.py 项目: jcsy521/ydws
    def post(self):
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            tmobile = data.tmobile
            flag = data.get('flag', 0)
            logging.info("[UWEB] unbind request: %s", data)
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            self.write_ret(status)
            IOLoop.instance().add_callback(self.finish)
            return 

        try:
            #NOTE:delete is not permited
            status = ErrorCode.DELETE_NOT_PERMITED 
            self.write_ret(status)
            self.finish()
            return

            terminal = self.db.get("SELECT id, tid, owner_mobile, login"
                                   "  FROM T_TERMINAL_INFO"
                                   "  WHERE mobile = %s"
                                   "    AND service_status = %s",
                                   tmobile, 
                                   UWEB.SERVICE_STATUS.ON)
            if not terminal:
                status = ErrorCode.TERMINAL_NOT_EXISTED
                logging.error("The terminal with tmobile: %s does not exist!", 
                              tmobile)
                self.write_ret(status)
                IOLoop.instance().add_callback(self.finish)
                return

            key = get_del_data_key(terminal.tid)
            self.redis.set(key, flag)
            if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE:
                status = self.send_jb_sms(tmobile, terminal.owner_mobile, terminal.tid)
                self.write_ret(status)
                IOLoop.instance().add_callback(self.finish)
                return

            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)

            seq = str(int(time.time()*1000))[-4:]
            args = DotDict(seq=seq,
                           tid=terminal.tid)
            GFSenderHelper.async_forward(GFSenderHelper.URLS.UNBIND, args, _on_finish)
        except Exception as e:
            logging.exception("[UWEB] tmobile:%s unbind failed. Exception: %s", 
                              data.tmobile, e.args)
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)
            IOLoop.instance().add_callback(self.finish)
示例#7
0
文件: terminal.py 项目: jcsy521/ydws
    def delete(self):
        """Delete a terminal.
        """
        try:
            status = ErrorCode.SUCCESS
            tid = self.get_argument('tid', None)
            flag = self.get_argument('flag', 0)
            logging.info("[UWEB] Corp delete terminal request. tid: %s, flag: %s, cid: %s",
                         tid, flag, self.current_user.cid)

            terminal = QueryHelper.get_available_terminal(tid, self.db)
            if not terminal:
                logging.error("[UWEB] The terminal with tid: %s does not exist!", 
                              tid)
                status = ErrorCode.TERMINAL_NOT_EXISTED
                self.write_ret(status)
                return

            t_info = QueryHelper.get_terminal_basic_info(tid, self.db)
            key = get_del_data_key(tid)
            self.redis.set(key, flag)
            biz_type = QueryHelper.get_biz_type_by_tmobile(
                terminal.mobile, self.db)
            if int(biz_type) == UWEB.BIZ_TYPE.YDWS:
                if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE:
                    if terminal.mobile == tid:
                        delete_terminal(tid, self.db, self.redis)
                    else:
                        status = self.send_jb_sms(
                            terminal.mobile, terminal.owner_mobile, tid)

                    if status == ErrorCode.SUCCESS:
                        WSPushHelper.pushS3(tid, self.db, self.redis, t_info)
                    self.write_ret(status)
                    return
            else:
                delete_terminal(tid, self.db, self.redis)
                if status == ErrorCode.SUCCESS:
                    WSPushHelper.pushS3(tid, self.db, self.redis, t_info)
                self.write_ret(status)
                return

            # unbind terminal
            seq = str(int(time.time() * 1000))[-4:]
            args = DotDict(seq=seq,
                           tid=tid)
            response = GFSenderHelper.forward(GFSenderHelper.URLS.UNBIND, args)
            response = json_decode(response)
            logging.info(
                "[UWEB] UNBind terminal: %s, response: %s", tid, response)
            if response['success'] == ErrorCode.SUCCESS:
                logging.info("[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind successfully",
                             self.current_user.uid, tid, terminal.mobile)
            else:
                status = response['success']
                # unbind failed. clear sessionID for relogin, then unbind it
                # again
                clear_sessionID(self.redis, tid)
                logging.error('[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind failed, message: %s, send JB sms...',
                              self.current_user.uid, tid, terminal.mobile,
                              ErrorCode.ERROR_MESSAGE[status])
                status = self.send_jb_sms(
                    terminal.mobile, terminal.owner_mobile, tid)

            if status == ErrorCode.SUCCESS:
                WSPushHelper.pushS3(tid, self.db, self.redis, t_info)
            self.write_ret(status)
        except Exception as e:
            logging.exception("[UWEB] Delete terminal failed. cid: %s, Exception: %s",
                              self.current_user.cid, e.args)
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)