Build Your First Digital Twin Model

In this tutorial, you’ll learn how to build a very simple digital twin model.

First, decide whether you’d like to build your model with C#, Java, or low-code Business Rules. Then follow the instructions below for your preferred language.

Note

If you would like to skip this step and deploy the model immediately, you can download the finished files from here. Then you can jump directly to the Deploy Your Model section.

Install Digital Twin Templates

Install Templates

Run the following command to install Digital Twin project templates. This will enable you to access Digital Twin templates through Visual Studio.

dotnet new install Scaleout.Modules.Templates

You can confirm that the templates were installed correctly by running:

dotnet new dt-rt --help

Create a Real-Time Project

Option 1: Visual Studio

  1. Create a new project and select the ScaleOut Real-Time Digital Twin template.

  2. Name the project MyRealTimeTwin (or another name of your choosing).

Option 2: Command Line

If you are using Linux (or you would rather use the command line to create your project), run the following command to create a real-time digital twin project:

dotnet new dt-rt -n MyRealTimeTwin

Implement the Model

Digital Twin Model

A digital twin model is a template that represents a group of real-world entities, like IoT devices or vehicles. Each digital twin instance corresponds to a specific entity and uses the data structure defined in the model.

A model can contain real-time data, static values, and computed values that are derived when it receives new data. In this basic example, our model only contains a single string property.

Open the file MyRealTimeTwinModel.cs. The class “MyRealTimeTwinModel” has been generated for you. Add the following property inside the class:

public class MyRealTimeTwinModel : DigitalTwinBase<MyRealTimeTwinModel>
{
    public string? CurrentValue { get; set; }
}

Messages

A message is an object that holds data sent from a device to its corresponding digital twin instance. If you’re running a simulation, messages can also be sent by simulated devices. In this step, we define the structure of the message object. For this simple example, it only has one string property.

Find the file called ExampleMessage.cs in the Messages project. Rename it MyRealTimeTwinMessage.cs (or another name of your choosing). If prompted to perform a rename, select “Yes”.

The MyRealTimeTwinMessage class should contain a single string property: “MyProperty”. Rename this property to StringPayload.

Message Processor

When the digital twin system receives new messages from devices, it needs to process those messages and update the corresponding instances. We implement the message processing code in the Message Processor class. The Message Processor can simply update the instance properties with the new values from the message, or it can run more complex logic like computing a moving average or deciding whether to raise an alert.

For this example, we simply update the digital twin instance’s currentValue property to be the value of the StringPayload from the incoming message. We also log a notification with the instance ID and the contents of the message.

Open the file MyRealTimeTwinMessageProcessor.cs.

Replace the ProcessMessageAsync method with the following:

public override Task<ProcessingResult> ProcessMessageAsync(
    ProcessingContext<MyRealTimeTwinModel> context,
    MyRealTimeTwinModel digitalTwin,
    byte[] msgBytes)
{
    // Deserialize the message bytes into our specific message type.
    MyRealTimeTwinMessage? message = System.Text.Json.JsonSerializer.Deserialize<MyRealTimeTwinMessage>(msgBytes);
    if (message is null)
        return Task.FromResult(ProcessingResult.NoUpdate);

    // Modify the instance's state based on the message content.
    digitalTwin.CurrentValue = message.StringPayload;

    // The SOSS object was modified, so return DoUpdate to indicate that
    // it should be updated in the ScaleOut service.
    return Task.FromResult(ProcessingResult.DoUpdate);
}

In the next step, you’ll learn how to test a digital twin model before deployment.