def update(device_id): if isinstance(device_id, Device): device = device_id else: device = Device.get(device_id) if device is None: log.error("当前设备不存在,或者参数错误: device_id = {}".format(device_id)) return False if DeviceService.get_update_state( device) != DeviceUpdateStatus.UPDATE_FINISH: log.info( "当前设备游戏更新状态不需要设置: device_id = {} update_state = {}".format( device.id, device.update_state)) return True # 判断是否有游戏需要更新 if not DeviceGameService.is_device_need_update(device.id): log.info("当前游戏都是最新版, 不需要更新: device_id = {}".format(device.id)) return True log.info("当前存在游戏需要更新,通知客户端更新: device_id = {}".format(device.id)) return DeviceService.set_update_state(device, DeviceUpdateStatus.UPDATE_WAIT)
def sync_device_alive_status(): # 先获得全体设备最后同步时间 sync_last_time = redis_device_client.get( REDIS_PRE_DEVICE_ALIVE_SYNC_LAST_TIME_KEY) if sync_last_time is not None: log.info("当前设备存活状态不需要更新到数据库, 上次同步时间: {}".format(sync_last_time)) return start_time = time.time() # 获得全部设备信息 device_list = Device.get_all() for device in device_list: # 更新设备存活状态 update_info = { Device.alive: DeviceService.get_device_alive_status(device.device_code), Device.utime: datetime.now() } Device.query.filter_by(id=device.id).update(update_info) # 存入最新同步时间 redis_device_client.setex(REDIS_PRE_DEVICE_ALIVE_SYNC_LAST_TIME_KEY, DEFAULT_EXPIRED_DEVICE_ALIVE_SYNC, get_now_time()) log.info("同步设备存活状态数目为: count = {}".format(len(device_list))) log.info("同步设备存活信息花费时间: start_time = {} use time = {} s".format( start_time, time.time() - start_time))
def to_dict(self): device = Device.get(self.device_id) if device is None: device_dict = {} else: device_dict = device.to_dict() return { 'id': self.id, 'device': device_dict, 'province': self.province, 'city': self.city, 'area': self.area, 'location': self.location, 'utime': self.utime.strftime('%Y-%m-%d %H:%M:%S'), 'ctime': self.ctime.strftime('%Y-%m-%d %H:%M:%S'), }
def delete_device_game(name): start_time = time.time() is_success = True while True: for device in Device.get_all(): log.info('device_id = {}'.format(device.id)) is_success = DeviceGameService.delete(device.id, name) if not is_success: log.error("游戏删除失败,中断删除: device_id = {}".format(device.id)) break break log.info("游戏删除耗时: game = {} {} s".format(name, time.time() - start_time)) return is_success
def add_device_game(name, version): start_time = time.time() is_success = True while True: for device in Device.get_all(): log.info('device_id = {}'.format(device.id)) game, is_success = DeviceGameService.add( device.id, name, version) if not is_success or game is None: log.error("游戏更新失败,中断更新: device_id = {}".format(device.id)) break break log.info("游戏更新耗时: game = {} {} s".format(name, time.time() - start_time)) return is_success
def update_all(): start_time = time.time() is_success = True while True: for device in Device.get_all(): log.info('设置游戏状态: device_id = {}'.format(device.id)) is_success = DeviceGameService.update(device.id) if not is_success: log.error("游戏状态设置失败,中断设置: device_id = {}".format( device.id)) break break log.info("游戏状态设置耗时: {} s".format(time.time() - start_time)) return is_success
def create(device_code, address_id, charge_id): device = Device(device_code=device_code, address_id=address_id, charge_id=charge_id) try: db.session.add(device) db.session.commit() except IntegrityError: log.error("主键重复: device_code = {} address_id = {}".format( device_code, address_id)) db.session.rollback() return None, False except Exception as e: log.error("未知插入错误: device_code = {} address_id = {}".format( device_code, address_id)) log.exception(e) return None, False return device, True
def get_device_by_id(device_id): return Device.get(device_id)