// Get or Set objectId pool for the tenant devices
        private async Task GetOrSetObjectIdsPoolAsync()
        {
            _logger.LogInformation("Setting up devices objectId pool...");

            var poolFilePath = _configuration["ObjectIdsPoolFilePath"];

            if (await TryLoadExistingPoolFromFileAsync(poolFilePath).ConfigureAwait(false))
            {
                return;
            }

            _logger.LogInformation("ObjectId pool doesn't exist. Creating a new one...");

            _objectIdsPool = new ObjectIdsPool {
                Tenants = new List <Tenant>()
            };

            foreach (var tenant in _tenantDeviceClients)
            {
                var tnt = new Tenant
                {
                    Id      = tenant.Key.Id,
                    Devices = new List <TenantDevice>()
                };

                foreach (var deviceClient in tenant.Value)
                {
                    tnt.Devices.Add(new TenantDevice
                    {
                        DeviceId = deviceClient.DeviceId,
                        ObjectId = Guid.NewGuid()
                    });
                }

                _objectIdsPool.Tenants.Add(tnt);
            }

            using var writeStream = new FileStream(poolFilePath, FileMode.Create, FileAccess.Write);
            await JsonSerializer.SerializeAsync(writeStream, _objectIdsPool).ConfigureAwait(false);

            _logger.LogInformation("Device objectId pool successfully set up.");
        }
        private async Task <bool> TryLoadExistingPoolFromFileAsync(string poolFilePath)
        {
            if (!File.Exists(poolFilePath))
            {
                return(false);
            }

            _logger.LogInformation("ObjectId pool exists. Reading...");

            using var readStream = new FileStream(poolFilePath, FileMode.Open);
            _objectIdsPool       = await JsonSerializer.DeserializeAsync <ObjectIdsPool>(readStream).ConfigureAwait(false);

            if (_tenantDeviceClients.Count != _objectIdsPool.Tenants.Count)
            {
                _objectIdsPool = null;
                return(false);
            }

            foreach (var tenant in _tenantDeviceClients)
            {
                var poolTenant = _objectIdsPool.Tenants.FirstOrDefault(t => t.Id == tenant.Key.Id);
                if (poolTenant == null)
                {
                    _objectIdsPool = null;
                    return(false);
                }

                if (poolTenant.Devices.Count != tenant.Value.Count)
                {
                    _objectIdsPool = null;
                    return(false);
                }
            }

            return(true);
        }