def check_tid(self, tid, finish=False): """Check tid whether exists in request and modify the current_user. workflow: if tid is provided: update the current_user """ if tid: terminal = QueryHelper.get_terminal_by_tid(tid, self.db) self.current_user.tid=terminal.tid if terminal else tid self.current_user.sim=terminal.mobile if terminal else ''
def kqly(db, redis, tids): """Start bluetooth. """ for tid in tids: terminal = QueryHelper.get_terminal_by_tid(tid, db) kqly_key = get_kqly_key(tid) kqly_value = redis.getvalue(kqly_key) if not kqly_value: interval = 30 # in minute sms = SMSCode.SMS_KQLY % interval SMSHelper.send_to_terminal(terminal.mobile, sms) redis.setvalue(kqly_key, True, SMS.KQLY_SMS_INTERVAL)
def check_tid(self, tid, finish=False): """ check tid whether exists in request and modify the current_user. """ if tid: terminal = QueryHelper.get_terminal_by_tid(tid, self.db) #if not terminal: # status = ErrorCode.LOGIN_AGAIN # logging.error("[UWEB] The terminal with uid: %s, tid: %s does not exist, login again", self.current_user.uid, tid) # self.write_ret(status) # if finish: # self.finish() # return self.current_user.tid=terminal.tid if terminal else tid self.current_user.sim=terminal.mobile if terminal else ''
def get(self): """Get activation_code according to tid or mobile. """ status = ErrorCode.SUCCESS try: tid = self.get_argument('tid', '') mobile = self.get_argument('mobile', '') logging.info("[ACTIVATIONCODE] Activation_code query, tid: %s, mobile: %s.", tid, mobile) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[ACTIVATIONCODE] Illegal format, body: %s.", self.request.body) self.write_ret(status) return try: terminal = None if tid: terminal = QueryHelper.get_terminal_by_tid(tid, self.db) if not terminal: if mobile: terminal = QueryHelper.get_terminal_by_tmobile( mobile, self.db) activation_code = terminal.get( 'activation_code', '') if terminal else '' tid = terminal.get('tid', '') if terminal else '' mobile = terminal.get('mobile', '') if terminal else '' biz_type = terminal.get( 'biz_type', UWEB.BIZ_TYPE.YDWS) if terminal else UWEB.BIZ_TYPE.YDWS self.write_ret(status, dict_=DotDict(activation_code=activation_code, tid=tid, mobile=mobile, biz_type=biz_type)) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[ACTIVATIONCODE] Get activation_code failed, body: %s. Exception: %s", self.request.body, e.args) self.write_ret(status)
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)
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tids = data.get("tids") op_type = data.get("op_type") logging.info( "[UWEB] ACC request: %s, uid: %s, tid: %s, tids: %s", data, self.current_user.uid, self.current_user.tid, tids, ) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: res = [] for tid in tids: r = DotDict(tid=tid, status=ErrorCode.SUCCESS, message=ErrorCode.ERROR_MESSAGE[status]) try: t = QueryHelper.get_terminal_by_tid(tid, self.db) if str(t["dev_type"]) != "D": r["status"] = ErrorCode.ACC_NOT_ALLOWED logging.info( "[UWEB] Acc is not allowed. uid: %s, tid: %s, dev_type: %s", self.current_user.uid, tid, t["dev_type"], ) else: acc_status_info_key = get_acc_status_info_key(tid) acc_status_info = self.redis.getvalue(acc_status_info_key) if acc_status_info: r["status"] = ErrorCode.ACC_TOO_FREQUENCY logging.info("[UWEB] Acc is too frequency. uid: %s, tid: %s", self.current_user.uid, tid) else: acc_status_info = dict( client_id=self.client_id, op_type=op_type, timestamp=int(time.time()), op_status=0, # failed t2_status=0, # wait for T2 acc_message=u"", ) self.redis.setvalue(acc_status_info_key, acc_status_info, UWEB.ACC_STATUS_EXPIRY) except Exception as e: r["status"] = ErrorCode.FAILED logging.info( "[UWEB] Set acc status failed, uid:%s, tid:%s, op_type:%s.", self.current_user.uid, tid, op_type ) finally: r["message"] = ErrorCode.ERROR_MESSAGE[r["status"]] res.append(r) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] Set acc status failed, uid:%s, Exception: %s.", self.current_user.uid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def update_terminal_db(self, data): """Update database. """ # NOTE: these fields should to be modified in db terminal_keys = ['cellid_status', 'white_pop', 'trace', 'freq', 'vibchk', 'vibl', 'push_status', 'login_permit', 'alert_freq', 'stop_interval', 'biz_type', 'speed_limit'] terminal_fields = [] if data.has_key('tid'): del data['tid'] for key, value in data.iteritems(): # NOTE: These fields should be modified in database. if key in terminal_keys: if data.get(key, None) is not None: terminal_fields.append(key + ' = ' + str(value)) # NOT: These fields should be handled specially. if key == 'white_list': white_list = data[key] if len(data['white_list']) < 1: pass else: self.db.execute("DELETE FROM T_WHITELIST WHERE tid = %s", self.current_user.tid) for white in white_list[1:]: self.db.execute("INSERT INTO T_WHITELIST" " VALUES(NULL, %s, %s)" " ON DUPLICATE KEY" " UPDATE tid = VALUES(tid)," " mobile = VALUES(mobile)", self.current_user.tid, white) elif key == 'alias': self.db.execute("UPDATE T_TERMINAL_INFO" " SET alias = %s" " WHERE tid = %s", value, self.current_user.tid) terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[key] = value self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'icon_type': self.db.execute("UPDATE T_TERMINAL_INFO" " SET icon_type = %s" " WHERE tid = %s", value, self.current_user.tid) terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[key] = value self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'corp_cnum': self.db.execute("UPDATE T_CAR" " SET cnum = %s" " WHERE tid = %s", safe_utf8(value), self.current_user.tid) self.db.execute("UPDATE T_TERMINAL_INFO" " SET alias = %s" " WHERE tid = %s", safe_utf8(value), self.current_user.tid) terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[ 'alias'] = value if value else self.current_user.sim self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'owner_mobile' and value is not None: umobile = value user = dict(umobile=umobile, password=u'111111') add_user(user, self.db, self.redis) t = QueryHelper.get_terminal_by_tid(self.current_user.tid, self.db) old_uids = [t.owner_mobile] # send sms self.db.execute("UPDATE T_TERMINAL_INFO" " SET owner_mobile = %s" " WHERE tid = %s", umobile, self.current_user.tid) register_sms = SMSCode.SMS_REGISTER % ( umobile, self.current_user.sim) SMSHelper.send_to_terminal(self.current_user.sim, register_sms) # update redis terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[key] = umobile self.redis.setvalue(terminal_info_key, terminal_info) # wspush to client WSPushHelper.pushS3(self.current_user.tid, self.db, self.redis) WSPushHelper.pushS3_dummy(old_uids, self.db, self.redis) elif key == "alert_freq": alert_freq_key = get_alert_freq_key(self.current_user.tid) if self.redis.exists(alert_freq_key): logging.info( "[UWEB] Termianl %s delete alert freq in redis.", self.current_user.tid) self.redis.delete(alert_freq_key) # if vibl has been changed,then update use_scene as well elif key == "vibl": use_scene = get_use_scene_by_vibl(value) self.db.execute("UPDATE T_TERMINAL_INFO SET use_scene=%s WHERE tid=%s", use_scene, self.current_user.tid) logging.info("[UWEB] Terminal %s update use_scene %s and vibl %s", self.current_user.tid, use_scene, value) logging.info( "[UWEB] Termianl %s delete session in redis.", self.current_user.tid) # NOTE: deprecated. elif key == "parking_defend" and value is not None: if value == 1: mannual_status = UWEB.DEFEND_STATUS.SMART else: mannual_status = UWEB.DEFEND_STATUS.YES update_mannual_status( self.db, self.redis, self.current_user.tid, mannual_status) elif key == 'login_permit': # wspush to client WSPushHelper.pushS3(self.current_user.tid, self.db, self.redis) else: pass # NOTE:update database. terminal_clause = ','.join(terminal_fields) if terminal_clause: self.db.execute("UPDATE T_TERMINAL_INFO" " SET " + terminal_clause + " WHERE tid = %s ", self.current_user.tid) # NOTE: clear sessionID if freq, stop_interval, vibl can be found. if data.has_key('freq') or data.has_key('stop_interval') or data.has_key('vibl'): clear_sessionID(self.redis, self.current_user.tid)
def day_notify(self): logging.info("[CELERY] checkertask day_notify started.") try: #NOTE: avoid sms is sent when the server is restart. _date = datetime.datetime.fromtimestamp(int(time.time())) if _date.hour not in (9,10): return #NOTE: get all terminals which may be notified, record them into T_MILEAGE_NOTIFICATION. terminals = self.db.query("SELECT tid, day_notification, notify_count, left_days" " FROM T_DAY_NOTIFICATION" " WHERE day_notification != 0" " AND notify_count < 3") for terminal in terminals: if int(time.time()) < terminal['day_notification']: # it's not time to notify continue terminal_info = QueryHelper.get_terminal_by_tid(terminal.tid, self.db) tid = terminal['tid'] owner_mobile = terminal_info['owner_mobile'] assist_mobile = terminal_info['assist_mobile'] t = self.db.get("SELECT distance_current FROM T_TERMINAL_INFO WHERE tid = %s", terminal.tid) #distance_current = terminal_info['distance_current'] distance_current = t['distance_current'] mobile = terminal_info['mobile'] notify_count = terminal['notify_count'] left_days = terminal['left_days'] day_notification= terminal['day_notification'] if left_days == 1: # it should be notified this day logging.info("[CELERY] Send day notification." " tid: %s, mobile: %s, owner_mobile: %s, assist_mobile: %s," " day_notification: %s" " notify_count: %s, left_days: %s.", tid, mobile, owner_mobile, assist_mobile, day_notification, notify_count, left_days) self.db.execute("UPDATE T_DAY_NOTIFICATION" " SET notify_count = %s," " left_days = %s," " notify_time = %s" " WHERE tid = %s", notify_count+1, 3, int(time.time()), tid) if owner_mobile: sms = SMSCode.SMS_NOTIFY_DAY % (terminal_info['alias']) SMSHelper.send(owner_mobile, sms) if assist_mobile: user = QueryHelper.get_user_by_mobile(owner_mobile, self.db) name = safe_unicode(user['name']) sms = SMSCode.SMS_NOTIFY_ASSIST_DAY % (mobile, owner_mobile, name) SMSHelper.send(assist_mobile, sms) elif left_days in (2, 3): # do not notify, just postpone one day logging.info("[CELERY] Do not send day notification this day," " just modify the left_days." " tid: %s, mobile: %s, owner_mobile: %s, assist_mobile: %s," " day_notification: %s" " notify_count: %s, left_days: %s.", tid, mobile, owner_mobile, assist_mobile, day_notification, notify_count, left_days) self.db.execute("UPDATE T_DAY_NOTIFICATION" " SET left_days = %s" " WHERE tid = %s", left_days-1, tid) else: #NOTE: It should never occur. logging.info("[CELERY] Invalid left_days: %s, mobile: %s.", left_days, mobile) except Exception as e: logging.exception("[CELERY] Day notification failed. Exception: %s.", e.args)