Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)