예제 #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"));
        }
예제 #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "state/{region}/{campus}/{building}/{floor}/{room}")] HttpRequest req,
            [Blob("shared", Connection = "AzureWebJobsStorage")] CloudBlobContainer container,
            string region,
            string campus,
            string building,
            string floor,
            string room,
            ILogger log)
        {
            bool prerequisites = true;
            var  errors        = new StringBuilder();

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

            if (string.IsNullOrEmpty(room))
            {
                prerequisites = false;
                errors.Append("Required query parameter {room} 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");
            }

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();


            var atlasFeaturesFileName = $"{region}_{campus}_{building}_featuremap.json".ToLower();
            var featureMapref         = container.GetBlockBlobReference(atlasFeaturesFileName);

            List <Feature> features;


            if (prerequisites)
            {
                var rdi = new Dictionary <string, Dictionary <string, RoomDataItem> >();
                try
                {
                    features = await blobDataService.ReadBlobData <List <Feature> >(container, atlasFeaturesFileName);

                    var    jo       = JObject.Parse(requestBody);
                    string stateSet = null;
                    string value    = null;

                    if (jo != null)
                    {
                        var jp = jo.First as JProperty;
                        if (jp != null)
                        {
                            stateSet = jp.Name;
                            value    = jp.Value.ToString();
                        }
                    }

                    var stateSetCfg = buildingConfig.StateSets.FirstOrDefault(ss => ss.StateSetName.ToLower() == stateSet.ToLower());

                    if (stateSetCfg != null)
                    {
                        var feature = features.FirstOrDefault(f => f.Properties.Name.ToLower().Replace("-", "") == room.ToLower().Replace("-", ""));
                        if (feature != null)
                        {
                            var mapsService = new MapsService(buildingConfig.SubscriptionKey, buildingConfig.DatasetId, stateSetCfg.StateSetId.ToString());
                            await mapsService.UpdateTagState(stateSetCfg.StateSetName, feature.Id, value);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.LogError(ex.Message);
                }

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