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.DigitalTwin.Templates

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

dotnet new dt-sln --help

Create a Solution

Next, run the following command to create a new solution.

dotnet new dt-sln -n MyFirstModel

Add Real-Time Project

Option 1: Visual Studio

  1. Open the Solution in Visual Studio. It should already contain a Messages project.

  2. Choose File > New > Project… or press Ctrl+Shift+N to add a new project.

  3. Select the ScaleOut Real-Time Model template.

  4. Name the project MyRealTimeTwin (or another name of your choosing) and make sure Solution is set to Add to Solution. Then click Create.

Option 2: Command Line

If you are using Linux (or you would rather use the command line to create your project), run the following commands from the folder that contains your sln file:

dotnet new dt-rt -n MyRealTimeTwin
dotnet sln add 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
{
    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 commented-out code in the foreach loop with the following:

switch (message)
{
    case MyRealTimeTwinMessage exampleMessage:
        // Update the digital twin's current value
        digitalTwin.currentValue = exampleMessage.StringPayload;
        // Log an informational message
        context.LogMessage(
            LogSeverity.Informational,
            $"The real-time digital twin '{digitalTwin.Id}' says '{digitalTwin.currentValue}'"
        );
        break;
    default:
        throw new NotImplementedException($"Message processor does not support message type {message.GetType()}");
}

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