Beispiel #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "statesim")] HttpRequest req,
            [Blob("shared", Connection = "AzureWebJobsStorage")] CloudBlobContainer container,
            ILogger log)
        {
            var atlasConfigFile = Environment.GetEnvironmentVariable("AtlasConfigFile") ?? "atlasConfig.json";
            var blobDataService = new BlobDataService();
            var atlasConfig     = await blobDataService.ReadBlobData <BuildingConfig[]>(container, atlasConfigFile);

            foreach (var buildingConfig in atlasConfig)
            {
                var  buidingFileBase       = buildingConfig.BuildingId.ToLower().Replace("/", "_");
                var  buildingStateFileName = $"{buidingFileBase}_state.json";
                var  buildingStateRef      = container.GetBlockBlobReference(buildingStateFileName);
                bool buildInitialState     = !await buildingStateRef.ExistsAsync();

                var buildingStateSet = new Dictionary <string, UnitStateSet>();

                if (buildInitialState)
                {
                    Random         rnd = new Random();
                    List <Feature> features;

                    var  atlasFeaturesFileName = $"{buidingFileBase}_featuremap.json".ToLower();
                    var  featureMapRef         = container.GetBlockBlobReference(atlasFeaturesFileName);
                    bool useAtlas = !await featureMapRef.ExistsAsync();

                    if (useAtlas)
                    {
                        features = await MapsService.FetchFeaturesFromAtlas(buildingConfig.DatasetId, buildingConfig.SubscriptionKey);

                        await featureMapRef.UploadTextAsync(JsonConvert.SerializeObject(features));
                    }
                    else
                    {
                        features = await blobDataService.ReadBlobData <List <Feature> >(container, atlasFeaturesFileName);
                    }
                    foreach (var feature in features)
                    {
                        var name = $"{buildingConfig.BuildingId}/{feature.Properties.LevelId}/{feature.Properties.Name}";
                        if (!buildingStateSet.ContainsKey(name))
                        {
                            buildingStateSet.Add(name, new UnitStateSet {
                                unitName = feature.Id, states = GetDefaultStates(rnd, buildingConfig.StateSets)
                            });
                        }
                    }
                    await buildingStateRef.UploadTextAsync(JsonConvert.SerializeObject(buildingStateSet));
                }
                else
                {
                    buildingStateSet = await blobDataService.ReadBlobData <Dictionary <string, UnitStateSet> >(container, buildingStateFileName);
                }

                foreach (var stateSetCfg in buildingConfig.StateSets)
                {
                    if (stateSetCfg != null)
                    {
                        var mapsService = new MapsService(buildingConfig.SubscriptionKey, buildingConfig.DatasetId, stateSetCfg.StateSetId.ToString());
                        foreach (var buildingState in buildingStateSet)
                        {
                            if (buildingState.Value.states.ContainsKey(stateSetCfg.StateSetName))
                            {
                                await mapsService.UpdateTagState(
                                    stateSetCfg.StateSetName,
                                    buildingState.Value.unitName,
                                    buildingState.Value.states[stateSetCfg.StateSetName]);
                            }
                        }
                    }
                }
            }

            return(new OkObjectResult("Ok"));
        }
Beispiel #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "roomdata/{region}/{campus}/{building}")] HttpRequest req,
            [Blob("shared", Connection = "AzureWebJobsStorage")] CloudBlobContainer container,
            string region,
            string campus,
            string building,
            ILogger log)
        {
            bool prerequisites = true;
            var  errors        = new StringBuilder();

            var atlasConfigFile = Environment.GetEnvironmentVariable("AtlasConfigFile") ?? "atlasConfig.json";

            bool rebuild = false;

            Boolean.TryParse(req.Query["rebuild"], out rebuild);

            if (string.IsNullOrEmpty(building))
            {
                prerequisites = false;
                errors.Append("Required query {building} was not defined");
            }

            var blobDataService = new BlobDataService();
            var atlasConfig     = await blobDataService.ReadBlobData <BuildingConfig[]>(container, atlasConfigFile);

            var buildingConfig = atlasConfig.FirstOrDefault(b => b.BuildingId.ToLower() == $"{region}/{campus}/{building}".ToLower());

            if (buildingConfig == null)
            {
                prerequisites = false;
                errors.Append($"Atlas config for {building} was not found");
            }

            var  atlasFeaturesFileName = $"{region}_{campus}_{building}_FeatureMap.json".ToLower();
            var  featureMapref         = container.GetBlockBlobReference(atlasFeaturesFileName);
            bool useAtlas = rebuild || !await featureMapref.ExistsAsync();

            List <Feature> features;


            if (prerequisites)
            {
                var rdi = new Dictionary <string, Dictionary <string, RoomDataItem> >();
                try
                {
                    if (useAtlas)
                    {
                        features = await MapsService.FetchFeaturesFromAtlas(buildingConfig.DatasetId, buildingConfig.SubscriptionKey);

                        await featureMapref.UploadTextAsync(JsonConvert.SerializeObject(features));
                    }
                    else
                    {
                        features = await blobDataService.ReadBlobData <List <Feature> >(container, atlasFeaturesFileName);
                    }

                    foreach (var feature in features)
                    {
                        var levelId = $"{region}/{campus}/{building}/{GetLevelName(feature.Properties.LevelId)}".ToLower();
                        if (!rdi.ContainsKey(levelId))
                        {
                            rdi.Add(levelId, new Dictionary <string, RoomDataItem>());
                        }
                        if (!rdi[levelId].ContainsKey(feature.Properties.Name))
                        {
                            rdi[levelId].Add(feature.Properties.Name.Replace("-", ""),
                                             new RoomDataItem()
                            {
                                name    = feature.Properties.Name,
                                type    = GetFeatureType(feature.Properties.CategoryId),
                                unitId  = feature.Id,
                                polygon = feature.Geometry.Coordinates[0]
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.LogError(ex.Message);
                }

                return(new OkObjectResult(JsonConvert.SerializeObject(rdi)));
            }
            else
            {
                log.LogError(errors.ToString());
                return(new NotFoundResult());
            }
        }