def update_game(): log.info("开始后台更新游戏...") if DeviceGameService.update_game_all_by_http(): log.info("游戏更新成功!") else: log.error("游戏更新失败!") log.info("后台更新游戏完成...")
def device_game_add(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") username = request.json.get('username') password = request.json.get('password') name = request.json.get('game') version = request.json.get('version') if not isinstance(name, basestring) or not isinstance(version, basestring): log.error( "参数错误: username = {} password = {} game = {} version = {}".format( username, password, name, version)) return fail(HTTP_OK, u"参数错误") # 先判断能否登录 is_success, msg = AdminService.verify_authentication(username, password) if not is_success: return fail(HTTP_OK, msg) # 开始更新游戏信息 if not DeviceGameService.add_device_game(name, version): return fail(HTTP_OK, u"游戏更新失败,请重试!") return success(u'游戏更新成功!')
def update_game(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") game = request.json.get('game') version = request.json.get('version') md5 = request.json.get('md5') if not isinstance(game, basestring) or \ not isinstance(version, basestring) or \ not isinstance(md5, basestring): log.error("参数错误: game = {} version = {} md5 = {}".format( game, version, md5)) return fail(HTTP_OK, u"参数错误") # 更新游戏列表 if not GameListService.update_game_list(game, version): return fail(HTTP_OK, u"更新游戏列表失败!") game_manage = GameVersionManageService.get_game_info(game, version) if game_manage is None: game_manage, is_success = GameVersionManageService.create( game, version, md5) if not is_success: return fail(HTTP_OK, u"游戏更新失败,请重试!") else: if not GameVersionManageService.update_game_info(game_manage, md5): return fail(HTTP_OK, u"游戏更新失败,请重试!") # 开始更新游戏信息 if not DeviceGameService.add_device_game(game, version): return fail(HTTP_OK, u"游戏更新失败,请重试!") return success(u'游戏更新成功!')
def modify_game_update_time(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") time_str = request.json.get('time') if time_str is None: log.error("修改时间参数错误: time = None") return fail(HTTP_OK, u"参数错误") # 判断时间格式是否正确 if not is_valid_time(time_str): log.error("时间格式错误: time = {}".format(time_str)) return fail(HTTP_OK, u"时间格式错误: %H:%M:%S") # 设置时间 DeviceGameService.set_game_update_time(time_str, redis_cache_client) return success(u"时间更新成功!")
def device_need_update(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") device_id = request.json.get('device_id') if device_id is None: log.warn("参数错误...") return fail(HTTP_OK, u"参数错误!") return success(DeviceGameService.is_device_need_update(device_id))
def device_game_update(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") device_id = request.json.get('device_id') is_all = request.json.get('all') if device_id is None and is_all is None: log.warn("参数错误...") return fail(HTTP_OK, u"参数错误, device_id or all 必须有一个!") log.info("当前设备更新参数为: device_id = {} is_all = {}".format(device_id, is_all)) if is_all: if DeviceGameService.update_all(): log.info("所有设备游戏更新状态设置成功!") return success(u"设备游戏更新状态设置成功!") return fail(HTTP_OK, u"游戏更新设置失败,请重试!") if DeviceGameService.update(device_id): log.info("设备游戏更新状态设置成功: device_id = {}".format(device_id)) return success(u"设备游戏更新状态设置成功!") return fail(HTTP_OK, u"设备设置游戏更新失败!")
def delete_game(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") game = request.json.get('game') if not isinstance(game, basestring): log.error("参数错误: game = {}".format(game)) return fail(HTTP_OK, u"参数错误") # game_manage = GameVersionManageService.get_game_info(game, version) # if game_manage is None: # game_manage, is_success = GameVersionManageService.create(game, version, md5) # if not is_success: # return fail(HTTP_OK, u"游戏更新失败,请重试!") # else: # if not GameVersionManageService.update_game_info(game_manage, md5): # return fail(HTTP_OK, u"游戏更新失败,请重试!") # # # 开始更新游戏信息 # if not DeviceGameService.add_device_game(game, version): # return fail(HTTP_OK, u"游戏更新失败,请重试!") # # return success(u'游戏更新成功!') # 从游戏列表中删除游戏 GameListService.delete_game_list(game) # 开始删除游戏信息 DeviceGameService.delete_device_game(game) # 开始删除游戏列表信息 if not GameVersionManageService.delete_game(game): return fail(HTTP_OK, u"游戏删除失败,请重试!") return success(u'游戏删除成功!')
def update_game_thread(): log.info("开始启动定时更新游戏线程...") pre_update_time = DEFAULT_GAME_UPDATE_TIME update_job_id = "update_game" parse_time(pre_update_time) # 启动定时调度器框架 scheduler = BackgroundScheduler(logger=log, timezone="Asia/Shanghai") scheduler.add_job(update_game, trigger="cron", id=update_job_id, **parse_time(pre_update_time)) scheduler.start() SLEEP_TIME = 30 while True: while True: try: # 获取当前游戏更新时间 time_str = DeviceGameService.get_game_update_time(cache_client) if time_str == pre_update_time: log.info("当前定时更新游戏时间: {}".format(time_str)) break log.info("当前游戏更新时间发生变更: pre = {} cur = {}".format( pre_update_time, time_str)) pre_update_time = time_str scheduler.reschedule_job(update_job_id, trigger="cron", **parse_time(time_str)) except Exception as e: log.error("游戏更新调度周期异常:") log.exception(e) break time.sleep(SLEEP_TIME)
def device_game_list(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") device_code = request.json.get('device_code') device = DeviceService.get_device_by_code(device_code) if device is None: log.error("当前设备号没有获取到任何设备信息: {}".format(device_code)) return fail(HTTP_OK, u"参数错误!!") # if not isinstance(page, int) or \ # not isinstance(size, int): # log.error("获取游戏列表参数错误: page = {} size = {} device_code = {}".format( # page, size, device_code)) # return fail(HTTP_OK, u"参数错误!!") # # if page <= 0 or size <= 0: # log.error("获取游戏列表参数错误, 不能小于0: page = {} size = {} device_code = {}".format( # page, size, device_code)) # return fail(HTTP_OK, u"参数错误!!") return DeviceGameService.get_device_game_list(device.id)
def device_game_list(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") page = request.json.get('page') size = request.json.get('size') device_id = request.json.get('device_id') if not isinstance(page, int) or \ not isinstance(size, int) or \ not isinstance(device_id, int): log.error("获取游戏列表参数错误: page = {} size = {} device_id = {}".format( page, size, device_id)) return fail(HTTP_OK, u"参数错误!!") if page <= 0 or size <= 0: log.error( "获取游戏列表参数错误, 不能小于0: page = {} size = {} device_id = {}".format( page, size, device_id)) return fail(HTTP_OK, u"参数错误!!") return DeviceGameService.get_device_game_list(device_id, page, size)
def deploy_device(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") # json = { # 'province': 'xxx', # 'city': 'xxxx', # 'area': 'xxxx', # 'location': 'xxx', # 'device_code': 'xxxx', # } province = request.json.get('province', None) city = request.json.get('city', None) area = request.json.get('area', None) location = request.json.get('location', None) if province is None or city is None \ or area is None or location is None \ or province == '' or city == '' \ or area == '' or location == '': log.warn( "地址信息传入错误: province = {} city = {} area = {} location = {}".format( province, city, area, location)) return fail(HTTP_OK, u"地址信息传入错误!") # 获得设备编号信息 device_code = request.json.get('device_code', None) if device_code is None: log.warn("没有设备编号信息无法部署...") return fail(HTTP_OK, u"没有设备编号信息无法部署!") charge_id = request.json.get('charge_id', None) if charge_id is None: log.warn("当前部署没有传入费率模板ID: device_code = {}".format(device_code)) return fail(HTTP_OK, u"没有传入费率模板ID") charge = Charge.get(charge_id) if charge is None: log.warn("当前费率模板不存在: charge_id = {} device_code = {}".format( charge_id, device_code)) return fail(HTTP_OK, u"当前费率模板不存在!") # 先获得地址信息 通过地址四个属性进行查找 address = Address.find_address(province, city, area, location) if address is None: # 如果地址信息不存在则创建地址但是设备数要先初始化为0 address, is_success = Address.create(province, city, area, location, device_num=0) if address is None: log.warn( "部署设备时地址信息创建失败了: province = {} city = {} area = {} location = {} device_code = {}" .format(province, city, area, location, device_code)) return fail(HTTP_OK, u"新建地址信息失败了!") # 先判断设备是否已经存在设备列表中 device = DeviceService.get_device_by_code(device_code) if device is None: # 如果没有找到设备信息则新建设备信息 device, is_success = DeviceService.create(device_code, address.id, charge_id) if device is None: log.warn("新建设备信息失败了!!") return fail(HTTP_OK, u"新建设备信息失败了!") if not address.add_device_num(1): log.warn("新增设备数目存储失败!!") return fail(HTTP_OK, u"新增设备数目存储失败!") # 部署游戏信息 DeviceGameService.deploy_device_game(device) # 这里添加部署记录然后返回 deploy, is_success = Deploy.create(device.id, province, city, area, location) if deploy is None: log.warn("添加部署记录失败!") return fail(HTTP_OK, u"添加部署记录失败!") log.info( "添加部署记录成功: province = {} city = {} area = {} location = {} device_code = {}" .format(province, city, area, location, device_code)) return success(deploy.to_dict()) # 添加部署记录 先判断当前部署的位置是否和设备当前所处的位置是一样的 if device.address_id == address.id: log.info( "当前设备部署的位置没有发生任何变化,不需要记录: device.id = {} address.id = {}".format( device.id, address.id)) return success(u"当前设备部署位置没有发生任何变化,不需要增加部署记录") # 先获得之前部署的位置 address_old = Address.get(device.address_id) if address_old.device_num > 0: if not address_old.add_device_num(-1): return fail(HTTP_OK, u"减少设备数目存储失败!") # 然后更改部署的位置 device.address_id = address.id if not address.add_device_num(1): log.warn("新增设备数目存储失败!!") return fail(HTTP_OK, u"新增设备数目存储失败!") # 增加部署记录 deploy, is_success = Deploy.create(device.id, province, city, area, location) if deploy is None: log.warn("添加部署记录失败!") return fail(HTTP_OK, u"添加部署记录失败!") log.info( "添加部署记录成功: province = {} city = {} area = {} location = {} device_code = {}" .format(province, city, area, location, device_code)) return success(deploy.to_dict())
def modify_device_game_state(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") device_code = request.json.get('device_code') update_state = request.json.get('update_state') if update_state not in (DeviceUpdateStatus.UPDATE_ING, DeviceUpdateStatus.UPDATE_FINISH): log.error("当前状态不允许: update_state = {}".format(update_state)) return fail(HTTP_OK, u"参数错误!") device = DeviceService.get_device_by_code(device_code) if device is None: log.error("当前设备号没有获取到任何设备信息: {}".format(device_code)) return fail(HTTP_OK, u"参数错误,获取设备信息失败!!") # 获取当前游戏更新状态 current_update_state = DeviceService.get_update_state(device) # 获取当前设备状态 current_status = DeviceService.get_device_status(device) # 如果当前发送过来的状态是ing 则设备当前游戏更新状态必须是ing,否则状态错误 if update_state == DeviceUpdateStatus.UPDATE_ING: if current_update_state == DeviceUpdateStatus.UPDATE_CHECK: return fail(HTTP_OK, u"当前游戏更新状态错误, 自检中不接收ing状态") # 锁定设备 if current_status != DeviceStatus.STATUE_FREE and \ current_status != DeviceStatus.STATUE_LOCK: return fail(HTTP_OK, u"当前设备不为空闲或者锁定状态,无法更新!") # 如果当前设备为空闲状态 则锁定设备 if current_status == DeviceStatus.STATUE_FREE: if not DeviceService.set_device_status(device, DeviceStatus.STATUE_LOCK): log.error("锁定设备失败,设置设备状态信息失败: device_id = {}".format( device.id)) return fail(HTTP_OK, u'锁定设备失败,设置设备状态信息失败!!') if DeviceService.set_update_state(device, update_state): return success(u"设备游戏更新状态设置成功!") return fail(HTTP_OK, u"更新状态设置失败") # 判断当前属于什么状态 if current_update_state == DeviceUpdateStatus.UPDATE_FINISH: return success(u'当前属于游戏更新完成状态,不需要设置') # 如果当前设备被锁定了 则解锁 if current_status == DeviceStatus.STATUE_LOCK: if not DeviceService.set_device_status(device, DeviceStatus.STATUE_FREE): log.error("解锁设备异常: device_id = {}".format(device.id)) return fail(HTTP_OK, u'设备解锁失败,多进程写入设备状态异常!') # 如果当前属于更新中状态 if current_update_state == DeviceUpdateStatus.UPDATE_ING: # 设置游戏更新完成。。 DeviceGameService.update_device_game(device_id=device.id) if DeviceService.set_update_state(device, update_state, last_update_time=datetime.now()): return success(u"设备游戏更新状态设置成功!") return fail(HTTP_OK, u"更新状态设置失败") if DeviceService.set_update_state(device, update_state): return success(u"设备游戏更新状态设置成功!") return fail(HTTP_OK, u"更新状态设置失败")
def confirm_game_update_time(): if DeviceGameService.get_game_update_time(redis_cache_client) is None: DeviceGameService.set_game_update_time(DEFAULT_GAME_UPDATE_TIME, redis_cache_client)
def get_game_update_time(): return success(DeviceGameService.get_game_update_time(redis_cache_client))
def backdoor_game_update(): if DeviceGameService.update_all(): log.info("所有设备游戏更新状态设置成功!") return success(u"设备游戏更新状态设置成功!") return fail(HTTP_OK, u"游戏更新设置失败,请重试!")