Пример #1
0
        private static async Task <string> UpdateTableAsync(LocalConfig config, DeviceConfig dconfig)
        {
            var             luss   = LussGenerator.Create();
            ContainerEntity entity = new ContainerEntity(luss, config.Hostname, dconfig.Container.ModuleId, dconfig.VirtualRtuId, dconfig.DeviceId, dconfig.Container.Slaves, dconfig.Container.LoggingLevel, dconfig.Container.InstrumentationKey, TimeSpan.FromMinutes(dconfig.Expiry), config.TableName, dconfig.StorageConnectionString);
            await entity.UpdateAsync();

            return(luss);
        }
Пример #2
0
        public async Task <ModuleConfig> ProvisionAsync()
        {
            ModuleConfig configuration = null;

            try
            {
                ContainerEntity entity =
                    await ContainerEntity.LoadAsync(luss, config.TableName, config.StorageConnectionString);

                if (entity.Access.HasValue)
                {
                    throw new SecurityException("Field gateway LUSS has previously been accessed.");
                }

                entity.Access = DateTime.UtcNow;
                await entity.UpdateAsync();

                if (entity.Created > DateTime.UtcNow)
                {
                    throw new SecurityException(
                              $"Field gateway LUSS has expired with {entity.Created.ToString()} > {DateTime.Now.ToString()}");
                }

                //add resources to Piraeus
                UpdatePiraeus(entity, config.ApiToken);

                //update the rtu map
                await UpdateRtuMapAsync(entity, config.StorageConnectionString, config.RtuMapContainer,
                                        config.RtuMapFilename);

                //create the identity for the field gateway
                List <Claim> claims = new List <Claim>();
                claims.Add(new Claim(string.Format($"http://{entity.Hostname.ToLowerInvariant()}/name"),
                                     entity.DeviceId.ToLowerInvariant()));
                claims.Add(new Claim(string.Format($"http://{entity.Hostname.ToLowerInvariant()}/role"),
                                     entity.ModuleId.ToLowerInvariant()));
                string issuer   = string.Format($"http://{entity.Hostname.ToLowerInvariant()}/");
                string audience = issuer;

                string securityToken = GetSecurityToken(issuer, audience, claims);

                configuration = new ModuleConfig(entity.Hostname, entity.VirtualRtuId, entity.DeviceId, entity.ModuleId,
                                                 entity.Slaves, securityToken, entity.LoggingLevel, entity.InstrumentationKey);
            }
            catch (Exception ex)
            {
                log?.LogInformation($"Failed to provision field gateway with luss '{luss}'.");
                log?.LogError(ex.Message);
                throw ex;
            }

            return(configuration);
        }