def put(self): """Modify a existing group. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) cid = self.current_user.cid tid = self.current_user.tid gid = data.gid name = data.name logging.info("[UWEB] Modify group request: %s, cid: %s", data, self.current_user.cid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body:%s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: group = self.get_group_by_cid(cid, name) if group: status = ErrorCode.GROUP_EXIST self.write_ret(status) return self.db.execute("UPDATE T_GROUP" " SET name = %s" " WHERE id = %s", name, gid) # NOTE: wspush to client if status == ErrorCode.SUCCESS: WSPushHelper.pushS3(tid, self.db, self.redis) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Modify group failed. cid: %s, Exception: %s", self.current_user.cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Create a new group. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] add group request: %s, cid: %s", data, self.current_user.cid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body:%s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: cid = data.cid name = data.name group = self.get_group_by_cid(cid, name) if group: status = ErrorCode.GROUP_EXIST self.write_ret(status) return group_info = dict(cid=cid, name=name, type=UWEB.GROUP_TYPE.NEW) gid = add_group(group_info, self.db, self.redis) # NOTE: wspush to client tid = self.current_user.tid if status == ErrorCode.SUCCESS: WSPushHelper.pushS3(tid, self.db, self.redis) self.write_ret(status, dict_=dict(gid=gid, cid=cid, name=name)) except Exception as e: logging.exception("[UWEB] Create group failed. uid: %s, Exception: %s", self.current_user.uid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Transfer some terminals to a existing group. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Transfer group request: %s, cid: %s", data, self.current_user.cid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body:%s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: tids = [str(tid) for tid in data.tids] gid = data.gid sql_cmd = ("UPDATE T_TERMINAL_INFO" " SET group_id = %s" " WHERE tid IN %s") % ( gid, tuple(tids + DUMMY_IDS_STR), ) self.db.execute(sql_cmd) # NOTE: wspush to client tid = self.current_user.tid if status == ErrorCode.SUCCESS: WSPushHelper.pushS3(tid, self.db, self.redis) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Transfer group failed. cid: %s, Exception: %s", self.current_user.cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def delete_terminal_new(tid, db, redis, del_user=True): """Delete terminal from platform and clear the associated info. """ terminal = db.get("SELECT mobile, owner_mobile, group_id FROM T_TERMINAL_INFO" " WHERE tid = %s", tid) if not terminal: logging.info("Terminal: %s already does not exist, do nothing.", tid) return else: t_info = QueryHelper.get_terminal_basic_info(tid, db) # NOTE: record the del action. corp = QueryHelper.get_corp_by_gid(terminal.group_id, db) bind_info = dict(tid=tid, tmobile=terminal.mobile, umobile=terminal.owner_mobile, group_id=terminal.group_id, cid=corp.get('cid', '') if corp else '', del_time=int(time.time())) record_del_action(bind_info, db) WSPushHelper.pushS3(tid, db, redis, t_info) clear_data(tid, db, redis) logging.info("[PUBLIC] Delete Terminal: %s, tmobile: %s, umobile: %s", tid, terminal.mobile, terminal.owner_mobile)
def delete(self): """Delete a group. """ try: status = ErrorCode.SUCCESS delete_ids = map(int, str_to_list(self.get_argument("ids", None))) for delete_id in delete_ids: terminals = self.db.query( "SELECT * FROM T_TERMINAL_INFO" " WHERE group_id = %s" " AND (service_status = %s " " OR service_status = %s)", delete_id, UWEB.SERVICE_STATUS.ON, UWEB.SERVICE_STATUS.TO_BE_ACTIVATED, ) if not terminals: logging.info( "[UWEB] group delete request: %s, uid: %s, tid: %s", delete_ids, self.current_user.uid, self.current_user.tid, ) self.db.execute("DELETE FROM T_GROUP WHERE id = %s", delete_id) else: status = ErrorCode.GROUP_HAS_TERMINAL # NOTE: wspush to client tid = self.current_user.tid if status == ErrorCode.SUCCESS: WSPushHelper.pushS3(tid, self.db, self.redis) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Delete group failed. cid: %s, Exception: %s", self.current_user.cid, 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 post(self): """Acquire push account through uid @uid: user uid """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.tid s_type = data.s_type if data.get("category", None): category = int(data.get("category")) else: category = 1 logging.info("[UWEB] Test wspush request: %s.", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body: %s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: # user = self.db.get("select owner_mobile") user = QueryHelper.get_user_by_tid(tid, self.db) uid = user.owner_mobile if s_type == "S3": WSPushHelper.pushS3(tid, self.db, self.redis) elif s_type == "S4": WSPushHelper.pushS4(tid, self.db, self.redis) elif s_type == "S5": body = dict( tid=tid, category=category, pbat=100, type=1, timestamp=int(time.time()), longitude=419004000, latitude=143676000, clongitude=419004000, clatitude=143676000, name="test name", speed=111, degree=203, gsm=0, locate_error=100, gps=25, alias="111", region_id=11, ) WSPushHelper.pushS5(tid, body, self.db, self.redis) elif s_type == "S6": WSPushHelper.pushS6(tid, self.db, self.redis) elif s_type == "S7": WSPushHelper.pushS7(tid, self.db, self.redis) elif s_type == "S8": acc_message = 1 WSPushHelper.pushS8(tid, acc_message, self.db, self.redis) self.write_ret(status=status) except Exception as e: logging.exception("[UWEB] WSPushHandler get push account failed, Exception:%s", e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
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)
def add_terminal(terminal, db, redis): """"Add a terminal. @param: terminal, {'tid':'', 'tmobile':'', 'owner_mobile':'', 'group_id':'', 'dev_type':'', 'imsi':'', 'imei':'', 'factory_name':'', 'softversion':'', 'keys_num':'', 'bt_name':'', 'bt_mac':'', 'login':'', 'mannual_status':'', 'alias':'', 'icon_type':'', 'login_permit':'', 'push_status':'', 'vibl':'', 'use_scene':'', 'biz_type':'', 'activation_code':'', 'service_status':'', 'begintime':'', 'endtime':'', 'offline_time':'' 'speed_limit':'' 'stop_interval':'' 'distance_current':'' # car 'cnum':'', } @param: db @param: redis """ if terminal.get('tid', ''): tid = terminal['tid'] else: tid = terminal['tmobile'] #NOTE: If tid has exist, delete it. This should never happen. t = db.get("SELECT id, service_status, mobile, tid, owner_mobile, group_id" " FROM T_TERMINAL_INFO WHERE tid = %s", tid) if t: db.execute("DELETE FROM T_TERMINAL_INFO WHERE tid = %s", tid) logging.info("[PUBLIC] Delete the existed but unvalid terminal. terminal: %s.", t) #NOTE: If mobile has exist, delete it. This should not appears often. t = db.get("SELECT id, service_status, mobile, tid, owner_mobile, group_id" " FROM T_TERMINAL_INFO WHERE mobile = %s", terminal.get('tmobile')) if t and int(t['service_status']) == 2: db.execute("DELETE FROM T_TERMINAL_INFO WHERE mobile = %s", terminal.get('tmobile')) logging.info("[PUBLIC] Delete the existed but unvalid terminal. terminal: %s.", t) # add terminal 28 items. db.execute("INSERT INTO T_TERMINAL_INFO(tid, mobile, owner_mobile," " group_id, dev_type, imsi, imei, factory_name, softversion," " keys_num, bt_name, bt_mac, login, mannual_status, alias," " icon_type, login_permit, push_status, vibl, use_scene," " biz_type, activation_code, service_status, begintime," " endtime, offline_time, speed_limit, stop_interval," " distance_current)" " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s," " %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s," " %s, %s, %s, %s, %s)", tid, terminal.get('tmobile'), terminal.get('owner_mobile'), terminal.get('group_id', -1), terminal.get('dev_type', 'A'), terminal.get('imsi', ''), terminal.get('imei', ''), terminal.get('factory_name', ''), terminal.get('softversion', ''), terminal.get('keys_num', 0), terminal.get('bt_name', ''), terminal.get('bt_mac', ''), terminal.get('login', 0), terminal.get('mannual_status', 1), terminal.get('alias', ''), terminal.get('icon_type', 0), terminal.get('login_permit', 1), terminal.get('push_status', 1), terminal.get('vibl', 1), terminal.get('use_scene', 3), terminal.get('biz_type', 0), terminal.get('activation_code', ''), terminal.get('service_status', 1), terminal.get('begintime'), terminal.get('endtime'), terminal.get('offline_time'), terminal.get('speed_limit', 120), terminal.get('stop_interval', 0), terminal.get('distance_current', 0)) # add car tnum --> cnum car = db.get("SELECT id FROM T_CAR WHERE tid= %s", tid) if car: db.execute("DELETE FROM T_CAR WHERE tid = %s", tid) db.execute("INSERT INTO T_CAR(tid, cnum, type, color, brand)" " VALUES(%s, %s, %s, %s, %s)", tid, terminal.get('cnum', ''), terminal.get('ctype', 1), terminal.get('ccolor', 0), terminal.get('cbrand', '')) # NOTE: wspush to client WSPushHelper.pushS3(tid, db, redis) logging.info("[PUBLIC] Add terminal, tid: %s, terminal: %s.", tid, terminal)