Hi @S.M Greetings! Welcome to Microsoft Q&A forum. Thank you for posting the question here. There is an Azure Digital Twins Data Simulator which lets you simulate data of the digital twins. However, the simulator is preconfigured with couple of simulation types. A better alternative to achieve this is by using an SDK. I have created a sample Digital model that has three properties and generated the Digital Twins based on the model. Here is the template for my model.
{
"@context": "dtmi:dtdl:context;2",
"@id": "dtmi:com:example:Pump;1",
"@type": "Interface",
"displayName": "Pump",
"contents": [
{
"@type": "Property",
"name": "Sensor1",
"writable": true,
"schema": "double"
},
{
"@type": "Property",
"name": "Sensor2",
"writable": true,
"schema": "double"
},
{
"@type": "Property",
"name": "Sensor3",
"writable": true,
"schema": "double"
}
]
}
I created Digital Twins based on the model. Please refer the article Create a Client App that has the code samples on how to upload a model and create Digital twins. Once you have the Digital Twin created you can simulate the properties by appending a JSON patch to the Digital Twin. Please find the below code that I have used to upload model, generate twins and simulate the data.
using System;
using Azure.DigitalTwins.Core;
using Azure.Identity;
using Azure;
using System.Text.Json;
using System.Text;
namespace PumpDemo // Note: actual namespace depends on the project name.
{
internal class Program
{
static async Task Main(string[] args)
{
string adtInstanceUrl = "<Azure Digital Twin Instance URL>";
var credential = new DefaultAzureCredential();
var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential);
Console.WriteLine($"Service client created – ready to go");
Console.WriteLine();
Console.WriteLine($"Upload a model");
string dtdl = File.ReadAllText("template.json");
var models = new List<string> { dtdl };
// Upload the model to the service
try
{
await client.CreateModelsAsync(models);
Console.WriteLine("Models uploaded to the instance:");
}
catch (RequestFailedException e)
{
Console.WriteLine($"Upload model error: {e.Status}: {e.Message}");
}
// Read a list of models back from the service
AsyncPageable<DigitalTwinsModelData> modelDataList = client.GetModelsAsync();
await foreach (DigitalTwinsModelData md in modelDataList)
{
Console.WriteLine($"Model: {md.Id}");
}
//Create digital twins
await CreateDigitalTwins(client);
//simulate the properties of Digital Twin Pump-1
BasicDigitalTwin digitalTwin = await client.GetDigitalTwinAsync<BasicDigitalTwin>("Pump-1");
var jsonPatch = new Azure.JsonPatchDocument();
Random random = new Random();
int randomNumber = random.Next(1, 101);
while (true)
{
randomNumber = random.Next(1, 101);
jsonPatch.AppendReplace("/Sensor1", randomNumber);
randomNumber = random.Next(1, 101);
jsonPatch.AppendReplace("/Sensor2", randomNumber);
randomNumber = random.Next(1, 101);
jsonPatch.AppendReplace("/Sensor3", randomNumber);
await client.UpdateDigitalTwinAsync("Pump-1", jsonPatch);
Console.WriteLine("Sending updates to Digital Twin");
Thread.Sleep(10000);
}
}
static async Task CreateDigitalTwins(DigitalTwinsClient client)
{
//Create Digital Twin from the model
var twinData = new BasicDigitalTwin();
twinData.Metadata.ModelId = "dtmi:com:example:Pump;1";
twinData.Contents.Add("Sensor1", 45);
twinData.Contents.Add("Sensor2", 79);
twinData.Contents.Add("Sensor3", 28);
string prefix = "Pump-";
for (int i = 1; i <= 3; i++)
{
try
{
twinData.Id = $"{prefix}{i}";
await client.CreateOrReplaceDigitalTwinAsync<BasicDigitalTwin>(twinData.Id, twinData);
Console.WriteLine($"Created twin: {twinData.Id}");
}
catch (RequestFailedException e)
{
Console.WriteLine($"Create twin error: {e.Status}: {e.Message}");
}
}
}
}
}
Please make sure to provide your Azure digital twin instance URL for the variable
adtInstanceUrl
The code sends simulated data to the Digital twin every ten seconds
Once you have application running, you can see the Twin properties values update every ten seconds on the Azure Digital Twin explorer.
Hope this helps! Please let us know if you run into any issues or need further clarification. If the response helped, please do click Accept Answer and Yes. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.