-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat(Samples): Add Telemetry Sample #12407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b95d44
feat(Samples): Add Telemetry Sample
01c3815
Address PR feedback
55ce677
override telemetry APIs to take in a string payload instead of object…
8049719
fix return types for Send telemetry
76b42fc
Change snippet title for publish telemetry
4bed254
Address PR feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...twins/Azure.DigitalTwins.Core/samples/DigitalTwinsClientSample/PublishTelemetrySamples.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Net; | ||
| using System.Threading.Tasks; | ||
| using Azure.DigitalTwins.Core; | ||
| using Azure.DigitalTwins.Core.Samples; | ||
| using static Azure.DigitalTwins.Core.Samples.SampleLogger; | ||
| using static Azure.DigitalTwins.Core.Samples.UniqueIdHelper; | ||
|
|
||
| namespace Azure.DigitalTwins.Samples | ||
| { | ||
| internal class PublishTelemetrySamples | ||
| { | ||
| private DigitalTwinsClient DigitalTwinsClient { get; } | ||
|
|
||
| public PublishTelemetrySamples(DigitalTwinsClient dtClient) | ||
| { | ||
| DigitalTwinsClient = dtClient; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a temporary component model, twin model and digital twin instance. | ||
| /// Publish a telemetry message and a component telemetry message to the digital twin instance. | ||
| /// </summary> | ||
| public async Task RunSamplesAsync() | ||
| { | ||
| PrintHeader("PUBLISH TELEMETRY MESSAGE SAMPLE"); | ||
|
|
||
| // For the purpose of this example we will create temporary models using a random model Ids. | ||
| // We will also create temporary twin instances to publish the telemetry to. | ||
|
|
||
| string componentModelId = await GetUniqueModelIdAsync(SamplesConstants.TemporaryComponentModelPrefix, DigitalTwinsClient).ConfigureAwait(false); | ||
| string modelId = await GetUniqueModelIdAsync(SamplesConstants.TemporaryModelPrefix, DigitalTwinsClient).ConfigureAwait(false); | ||
| string twinId = await GetUniqueTwinIdAsync(SamplesConstants.TemporaryTwinPrefix, DigitalTwinsClient).ConfigureAwait(false); | ||
|
|
||
| string newComponentModelPayload = SamplesConstants.TemporaryComponentModelPayload | ||
| .Replace(SamplesConstants.ComponentId, componentModelId); | ||
|
|
||
| string newModelPayload = SamplesConstants.TemporaryModelPayload | ||
| .Replace(SamplesConstants.ModelId, modelId) | ||
| .Replace(SamplesConstants.ComponentId, componentModelId); | ||
|
|
||
| // Then we create the models. | ||
| await DigitalTwinsClient | ||
| .CreateModelsAsync(new[] { newComponentModelPayload, newModelPayload }) | ||
| .ConfigureAwait(false); | ||
|
|
||
| Console.WriteLine($"Successfully created models with Ids: {componentModelId}, {modelId}"); | ||
|
|
||
| // Create digital twin with Component payload | ||
| string twinPayload = SamplesConstants.TemporaryTwinPayload | ||
| .Replace(SamplesConstants.ModelId, modelId) | ||
| .Replace(SamplesConstants.ComponentId, componentModelId); | ||
|
|
||
| await DigitalTwinsClient.CreateDigitalTwinAsync(twinId, twinPayload).ConfigureAwait(false); | ||
| Console.WriteLine($"Created digital twin {twinId}."); | ||
|
|
||
| try | ||
| { | ||
| #region Snippet:DigitalTwinSamplePublishTelemetry | ||
|
|
||
| Response publishTelemetryResponse = await DigitalTwinsClient.PublishTelemetryAsync(twinId, "{\"Telemetry1\": 5}"); | ||
| Console.WriteLine($"Successfully published telemetry message, status: {publishTelemetryResponse.Status}"); | ||
|
|
||
| #endregion Snippet:DigitalTwinSamplePublishTelemetry | ||
|
|
||
| #region Snippet:DigitalTwinSamplePublishComponentTelemetry | ||
|
|
||
| Response publishTelemetryToComponentResponse = await DigitalTwinsClient.PublishComponentTelemetryAsync(twinId, "Component1", "{\"ComponentTelementry1\": 9}"); | ||
barustum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
barustum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Console.WriteLine($"Successfully published component telemetry message, status: {publishTelemetryToComponentResponse.Status}"); | ||
|
|
||
| #endregion Snippet:DigitalTwinSamplePublishComponentTelemetry | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| FatalError($"Failed to publish a telemetry message due to {ex.Message}"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // Delete the twin. | ||
| await DigitalTwinsClient.DeleteDigitalTwinAsync(twinId).ConfigureAwait(false); | ||
barustum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Delete the models. | ||
| await DigitalTwinsClient.DeleteModelAsync(modelId).ConfigureAwait(false); | ||
| await DigitalTwinsClient.DeleteModelAsync(componentModelId).ConfigureAwait(false); | ||
| } | ||
| catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotFound) | ||
| { | ||
| // Digital twin or models do not exist | ||
| } | ||
| catch (RequestFailedException ex) | ||
| { | ||
| FatalError($"Failed to delete due to {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.