Example #1
0
File: car.py Project: 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)
Example #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)