public async Task Models_Lifecycle()
        {
            DigitalTwinsClient client = GetClient();

            string buildingModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.BuildingModelId).ConfigureAwait(false);

            string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelId).ConfigureAwait(false);

            string hvacModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.HvacModelId).ConfigureAwait(false);

            string wardModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.WardModelId).ConfigureAwait(false);

            try
            {
                string modelBuilding = TestAssetsHelper.GetBuildingModelPayload(buildingModelId, hvacModelId, floorModelId);
                string modelHvac     = TestAssetsHelper.GetHvacModelPayload(hvacModelId, floorModelId);
                string modelWard     = TestAssetsHelper.GetWardModelPayload(wardModelId);

                // CREATE models
                var modelsList = new List <string> {
                    modelBuilding, modelHvac, modelWard
                };
                await CreateAndListModelsAsync(client, modelsList).ConfigureAwait(false);

                // GET one created model
                Response <DigitalTwinsModelData> buildingModel = await client.GetModelAsync(buildingModelId).ConfigureAwait(false);

                Console.WriteLine($"Got {buildingModelId} as {buildingModel.Value.DtdlModel}");

                // LIST all models
                AsyncPageable <DigitalTwinsModelData> models = client.GetModelsAsync();
                await foreach (DigitalTwinsModelData model in models)
                {
                    Console.WriteLine($"{model.Id}");
                }

                // DECOMMISSION a model
                await client.DecommissionModelAsync(buildingModelId).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Failure in executing a step in the test case: {ex.Message}.");
            }
            finally
            {
                // Test DELETE all models.
                try
                {
                    await client.DeleteModelAsync(buildingModelId).ConfigureAwait(false);

                    await client.DeleteModelAsync(hvacModelId).ConfigureAwait(false);

                    await client.DeleteModelAsync(wardModelId).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Assert.Fail($"Test clean up failed: {ex.Message}");
                }
            }
        }
Exemple #2
0
        async public void DecomissionModel(DigitalTwinsClient client, string dtmiOfPlanetInterface)
        {
            // ------------------ DECOMISSION MODEL ---------------------
            // <DecommissionModel>
            // 'client' is a valid DigitalTwinsClient
            await client.DecommissionModelAsync(dtmiOfPlanetInterface);

            // Write some code that deletes or transitions digital twins
            //...
            // </DecommissionModel>
        }
Exemple #3
0
        /// <summary>
        /// Creates a new model with a random Id
        /// Decommission the newly created model and check for success
        /// </summary>
        public async Task RunSamplesAsync(DigitalTwinsClient client)
        {
            PrintHeader("MODEL LIFECYCLE SAMPLE");

            // For the purpose of this example we will create temporary models using random model Ids and then decommission a model.
            // We have to make sure these model Ids are unique within the DT instance.

            string newComponentModelId = await GetUniqueModelIdAsync(SamplesConstants.TemporaryComponentModelPrefix, client);

            string sampleModelId = await GetUniqueModelIdAsync(SamplesConstants.TemporaryModelPrefix, client);

            string newComponentModelPayload = SamplesConstants.TemporaryComponentModelPayload
                                              .Replace(SamplesConstants.ComponentId, newComponentModelId);

            string newModelPayload = SamplesConstants.TemporaryModelWithComponentPayload
                                     .Replace(SamplesConstants.ModelId, sampleModelId)
                                     .Replace(SamplesConstants.ComponentId, newComponentModelId);

            // Then we create the model

            try
            {
                #region Snippet:DigitalTwinsSampleCreateModels

                Response <IReadOnlyList <ModelData> > response = await client.CreateModelsAsync(new[] { newComponentModelPayload, newModelPayload });

                Console.WriteLine($"Successfully created a model with Id: {newComponentModelId}, {sampleModelId}, status: {response.GetRawResponse().Status}");

                #endregion Snippet:DigitalTwinsSampleCreateModels
            }
            catch (RequestFailedException ex) when(ex.Status == (int)HttpStatusCode.Conflict)
            {
                Console.WriteLine($"One or more models already existed.");
            }
            catch (Exception ex)
            {
                FatalError($"Failed to create models due to:\n{ex}");
            }

            // Get Model
            try
            {
                #region Snippet:DigitalTwinsSampleGetModel

                Response <ModelData> sampleModel = await client.GetModelAsync(sampleModelId);

                #endregion Snippet:DigitalTwinsSampleGetModel

                Console.WriteLine($"{sampleModel.Value.Id} has decommission status of {sampleModel.Value.Decommissioned}");
            }
            catch (Exception ex)
            {
                FatalError($"Failed to get a model due to:\n{ex}");
            }

            // Now we decommission the model

            #region Snippet:DigitalTwinsSampleDecommisionModel

            try
            {
                await client.DecommissionModelAsync(sampleModelId);

                Console.WriteLine($"Successfully decommissioned model {sampleModelId}");
            }
            catch (RequestFailedException ex)
            {
                FatalError($"Failed to decommision model {sampleModelId} due to:\n{ex}");
            }

            #endregion Snippet:DigitalTwinsSampleDecommisionModel

            // Now delete created model

            #region Snippet:DigitalTwinsSampleDeleteModel

            try
            {
                await client.DeleteModelAsync(sampleModelId);

                Console.WriteLine($"Deleted model {sampleModelId}");
            }
            catch (Exception ex)
            {
                FatalError($"Failed to delete model {sampleModelId} due to:\n{ex}");
            }

            #endregion Snippet:DigitalTwinsSampleDeleteModel
        }