예제 #1
0
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log, ExecutionContext context)
        {
            config = new ServiceConfig();

            if (File.Exists(String.Format($"{context.FunctionAppDirectory}/secrets.json")))
            {
                //secrets.json exists use it and environment variables
                var builder = new ConfigurationBuilder()
                              .SetBasePath(context.FunctionAppDirectory)
                              .AddJsonFile("secrets.json", optional: true, reloadOnChange: true)
                              .AddEnvironmentVariables("FUNC_");

                IConfigurationRoot root = builder.Build();
                ConfigurationBinder.Bind(root, config);
            }
            else if (File.Exists(String.Format("{0}/{1}", context.FunctionAppDirectory, "local.settings.json")))
            {
                //use for local testing...do not use in production
                //remember to add the storage connection string
                var builder = new ConfigurationBuilder()
                              .SetBasePath(context.FunctionAppDirectory)
                              .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                              .AddEnvironmentVariables("FUNC_");

                IConfigurationRoot root = builder.Build();
                ConfigurationBinder.Bind(root, config);

                config.StorageConnectionString = root.GetConnectionString("StorageConnectionString");
            }
            else
            {
                //no secrets or local.settings.json files...use only environment variables
                var builder = new ConfigurationBuilder()
                              .AddEnvironmentVariables("FUNC_");


                IConfigurationRoot root = builder.Build();
                ConfigurationBinder.Bind(root, config);
            }

            string luss = req.Query["luss"];

            try
            {
                EdgeGatewayConfiguration edgeConfig = ProvisionAsync(luss).GetAwaiter().GetResult();
                return((ActionResult) new OkObjectResult(edgeConfig));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }
        }
예제 #2
0
        private static async Task <EdgeGatewayConfiguration> ProvisionAsync(string luss)
        {
            LussEntity entity = await LussEntity.LoadAsync(luss, config.LussStorageTableName, config.StorageConnectionString);

            if (entity == null || entity.Success.HasValue || entity.Expires < DateTime.Now)
            {
                return(null);
            }


            //get the security token to call Piraeus mgmt api
            IEnumerable <Claim> claims = GetClaims(entity);
            string accessToken         = GetPiraeusAccessToken();


            //create the CAPL policies
            string publishPolicyId        = null;
            string subscribePolicyId      = null;
            AuthorizationPolicy pubPolicy = CreateCaplPolicy(entity, true, out publishPolicyId);
            AuthorizationPolicy subPolicy = CreateCaplPolicy(entity, false, out subscribePolicyId);

            //add the CAPL policies to Piraeus
            AddCaplPolicy(pubPolicy, accessToken);
            AddCaplPolicy(subPolicy, accessToken);


            //create the pi-system metadata
            string        inputUriString  = GetEventUriString(entity, true);
            string        outputUriString = GetEventUriString(entity, false);
            EventMetadata inputMetadata   = GetEventMetadata(inputUriString, publishPolicyId, subscribePolicyId, (ushort)entity.UnitId, true);
            EventMetadata outputMetadata  = GetEventMetadata(outputUriString, subscribePolicyId, publishPolicyId, (ushort)entity.UnitId, false);

            //add the pi-systems to Piraeus
            AddEventMetadata(inputMetadata, accessToken);
            AddEventMetadata(outputMetadata, accessToken);

            //update the RTU Map
            RtuMap map = RtuMap.LoadFromConnectionStringAsync(config.RtuMapStorageContainerName, config.RtuMapFilename, config.StorageConnectionString).GetAwaiter().GetResult();

            if (map == null)
            {
                map = new RtuMap();
            }
            else
            {
                if (map.HasItem((ushort)entity.UnitId))
                {
                    map.Remove((ushort)entity.UnitId);
                }
            }

            map.Add((ushort)entity.UnitId, inputUriString, outputUriString);
            map.UpdateMapAsync(config.RtuMapStorageContainerName, config.RtuMapFilename, config.StorageConnectionString).GetAwaiter();

            //update the LUSS entity
            entity.Access = DateTime.UtcNow;
            if (!entity.Success.HasValue)
            {
                entity.Success = true;
            }

            await entity.UpdateAsync();

            string edgeSecurityToken = GetEdgeSecurityToken(entity);

            //create the cofiguration to return
            EdgeGatewayConfiguration edgeConfig = new EdgeGatewayConfiguration()
            {
                Hostname          = entity.Hostname,
                ModBusContainer   = entity.ModbusContainer,
                ModBusPort        = entity.ModbusPort,
                ModBusPath        = entity.ModbusPath,
                RtuInputPiSystem  = inputUriString,
                RtuOutputPiSsytem = outputUriString,
                SecurityToken     = edgeSecurityToken,
                UnitId            = entity.UnitId
            };

            return(edgeConfig);
        }