def update_terminal_info(db, redis, t_info): """Update terminal's info in db and redis. :arg db: database instance :arg redis: redis instance :arg terminal: dict, e.g. { 'mobile':'', ... } NOTE: Only those properties which are different from platform is needed to change. """ tid = t_info['dev_id'] terminal_info_key = get_terminal_info_key(tid) terminal_info = QueryHelper.get_terminal_info(tid, db, redis) # 1: db set_clause_dct = [] # gps, gsm, pbat, changed by position report keys = ['mobile', 'defend_status', 'login', 'keys_num', 'fob_status', 'mannual_status', 'softversion', 'bt_mac', 'bt_name', 'dev_type'] for key in keys: value = t_info.get(key, None) t_value = terminal_info.get(key, '') if value is not None and value != t_value: set_clause_dct.append(key + " = " + "'" + str(t_info[key]) + "'") if 'login_time' in t_info: set_clause_dct.append('login_time' + " = " + str(t_info['login_time'])) login_time_key = get_login_time_key(tid) redis.setvalue(login_time_key, t_info['login_time']) set_clause = ','.join(set_clause_dct) if set_clause: sql_cmd = ("UPDATE T_TERMINAL_INFO " " SET " + set_clause + " WHERE tid = %s") db.execute(sql_cmd, tid) # 2: redis for key in terminal_info: value = t_info.get(key, None) if value is not None: terminal_info[key] = value redis.setvalue(terminal_info_key, terminal_info) # NOTE:wspush to client. terminal basic info WSPushHelper.pushS6(tid, db, redis) return terminal_info
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)