示例#1
0
    def set_device_status(device, device_status):
        '''
        :param device: Device 类型
        :param device_status:
        :return:
        '''
        if not isinstance(device, Device):
            log.error("当前设置设备状态传入参数错误: device = {} type = {}".format(
                device, type(device)))
            return False

        if device_status not in Device.STATUS_VALUES:
            log.error(
                "当前设置设备状态传入参数错误: device_status = {}".format(device_status))
            return False

        # 先更新数据库,确保数据更新成功
        update_info = {
            Device.state: device_status,
            Device.state_version: device.state_version + 1
        }
        rowcount = Device.query.filter_by(
            id=device.id,
            state_version=device.state_version).update(update_info)
        if rowcount <= 0:
            log.error(
                "更新设备状态失败,版本信息已经被修改: id = {} state_version = {} state = {}".
                format(device.id, device.state_version, device_status))
            return False

        log.info("设备状态写入数据库完成: rowcount = {} ".format(rowcount))
        device_status_key = RedisClient.get_device_status_key(
            device.device_code)

        # 存储状态到redis中 状态只保存一天,防止数据被删除 缓存一直存在
        redis_device_client.setex(device_status_key,
                                  DEFAULT_EXPIRED_DEVICE_STATUS, device_status)
        log.info(
            "设备状态设置成功: device_id = {} device_code = {} state = {} state_version = {}"
            .format(device.id, device.device_code, device_status,
                    device.state_version + 1))
        return True
示例#2
0
    def get_device_status(device):
        '''
        获取设备使用状态
        :param device: basestring or Device 类型
        :return:
        '''

        if isinstance(device, basestring):
            device_code = device
        elif isinstance(device, Device):
            # 如果传入的是设备信息直接返回设备使用状态即可, 缓存和数据库中的设备信息是保持严格一致的,只要写入则同时写入缓存和数据库
            return device.state
        else:
            log.error("当前参数数据类型不正确: device = {} type = {}".format(
                device, type(device)))
            return None

        # 先判断是否在缓存中
        device_status_key = RedisClient.get_device_status_key(device_code)

        device_status = redis_device_client.get(device_status_key)
        if device_status is not None:
            return device_status

        # 没有从缓存中找到设备状态 则去数据库中找
        device = DeviceService.get_device_by_code(device_code)
        if device is None:
            log.error("当前设备码没有从缓存中找到,也不存在于数据库中: device_code = {}".format(
                device_code))
            return None

        # 存储状态到redis中 状态只保存一天,防止数据被删除 缓存一直存在
        redis_device_client.setex(device_status_key,
                                  DEFAULT_EXPIRED_DEVICE_STATUS, device.state)

        log.info(
            "当前设备状态从数据库中加载, 缓存到redis中: device_code = {}".format(device_code))
        return device.state
示例#3
0
    def delete_device(device_id):

        device = DeviceService.get_device_by_id(device_id)

        if device is None:
            log.warn("当前需要删除的设备不存在: device_id = {}".format(device_id))
            return False

        # 当前设备在线,且设备正在被用户使用,则不能够删除
        if DeviceService.get_device_alive_status(device) == Device.ALIVE_ONLINE and \
                        DeviceService.get_device_status(device) != DeviceStatus.STATUE_FREE:
            log.warn("当前设备不处于空闲状态,不能删除: device_id = {}".format(device.id))
            return False

        # 删除设备状态缓存信息
        device_status_key = RedisClient.get_device_status_key(
            device.device_code)
        redis_device_client.delete(device_status_key)

        # 删除设备更新状态缓存
        device_update_status_key = RedisClient.get_device_update_status_key(
            device.device_code)
        redis_device_client.delete(device_update_status_key)

        # 删除设备心跳缓存
        device_heart_key = RedisClient.get_device_heart_key(device.device_code)
        redis_device_client.delete(device_heart_key)

        # 删除设备上的游戏
        DeviceGameService.delete_deploy_device_game(device.id)

        if not device.delete():
            log.warn("设备信息删除失败: {}".format(
                json.dumps(device.to_dict(), ensure_ascii=False)))
            return False
        return True