Automation of JSON file in Digital Twins

Gorthi Sai Sri Sindhuja 60 Reputation points
2023-08-03T09:41:37.74+00:00

Is there a solution to automatically update/upload the JSon Files from our desktop to digital twins

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
231 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Maher JENDOUBI 156 Reputation points
    2023-08-03T10:53:20.1166667+00:00
    Yes, you can automatically update or upload JSON files from your desktop to Azure Digital Twins by creating a script or application that leverages the Azure Digital Twins APIs and relevant SDKs. Here's a general guide to accomplishing this task:
    
    ### 1. **Set Up Your Development Environment**
    
    Make sure you have the required SDKs and tools installed:
    
    - Azure Digital Twins SDK
    - A code editor (such as Visual Studio or Visual Studio Code)
    - Required programming language runtime (e.g., .NET, Node.js, etc.)
    
    ### 2. **Authenticate with Azure Digital Twins**
    
    You'll need to authenticate your application with Azure Digital Twins. You can utilize the Azure Identity library to authenticate your application.
    
    ### 3. **Watch the Directory for Changes**
    
    To automatically update or upload files, you can create a file watcher that monitors your desktop (or specific directory) for changes to JSON files.
    
    For example, in a .NET application, you might use the `FileSystemWatcher` class.
    
    ### 4. **Upload the JSON Files to Azure Digital Twins**
    
    When a change is detected, read the JSON file and use the Digital Twins SDK to upload or update the data in Azure Digital Twins.
    
    Here's a high-level code snippet using C#:
    
    ```csharp
    using Azure.Identity;
    using Azure.DigitalTwins.Core;
    using System.IO;
    
    // Set up file watcher
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"C:\path\to\your\json\files";
    watcher.Filter = "*.json";
    
    // Add event handlers
    watcher.Changed += OnChanged;
    
    // Start watching
    watcher.EnableRaisingEvents = true;
    
    // Event handler
    private static async void OnChanged(object source, FileSystemEventArgs e)
    {
        // Read the JSON file
        string jsonContent = File.ReadAllText(e.FullPath);
    
        // Authenticate with Azure Digital Twins
        var credential = new DefaultAzureCredential();
        DigitalTwinsClient client = new DigitalTwinsClient(new Uri("<your-digital-twins-url>"), credential);
    
        // Upload or update the data in Azure Digital Twins
        // This part depends on your specific use case and the structure of the JSON data
        await client.CreateOrReplaceDigitalTwinAsync("<twin-id>", jsonContent);
    }
    

    This code snippet provides a starting point and must be adapted to fit your specific use case and the structure of your JSON data.

    5. Handle Errors and Edge Cases

    Ensure that your code includes appropriate error handling and accounts for edge cases, such as conflicts in concurrent updates or handling large files.

    6. Compliance and Security Considerations

    Ensure that the solution complies with your organization's security policies and practices. This includes the secure handling and transmission of data and adherence to any relevant regulations or standards.

    Conclusion

    By combining a file watcher on the desktop with the Azure Digital Twins SDK, you can create an automated solution to upload or update JSON files in Azure Digital Twins whenever changes are made on the desktop. Make sure to consult the specific SDK documentation and tailor the solution to fit your data model and business requirements.

    
    

  2. Sander van de Velde | MVP 33,136 Reputation points MVP
    2023-08-04T11:45:44.62+00:00

    Hello @Gorthi Sai Sri Sindhuja ,

    you want to upload DTDL models in bulk, from your desktop.

    This can be done with Digital Twins C# SDK.

    Here is a code sample:

    // Authenticate with Digital Twins on your laptop 
    // Give your account the 'Azure Digital Twins Data Owner' role for your instance of Digital Twins 
    // Run 'az login' in the terminal app of your device to login to Azure 
    var credential = 
      new DefaultAzureCredential(
        new DefaultAzureCredentialOptions { ExcludeSharedTokenCacheCredential = true });  
    
    // Replace the following values with those from your instance of Digital Twins 
    var adtEndpoint = "https://[environment].api.[region].digitaltwins.azure.net";  
    var client = new DigitalTwinsClient(new Uri(adtEndpoint), credential);  
    
    // add models in bulk from disk 
    var sampleModel = File.ReadAllText("SampleModel.json"); 
    var extraSampleModel = File.ReadAllText("ExtraSampleModel.json"); 
    var modelList =  new List<string>{sampleModel, extraSampleModel};  
    
    // Models are created in the order they are listed in the modelList 
    // Models are not replacable, so if you try to create a model that already exists, an exception will be thrown 
    var createdModels = client.CreateModelsAsync(modelList).Result;  
    
    Console.WriteLine($"Models added: {createdModels.Value[0].Id} and {createdModels.Value[1].Id}"); 
    

    Notice you have to provide credentials.

    Before executing this run this console command (the Azure CLI must be installed):

    az login
    

    Here, I executed the code:

    User's image

    The result is seen in the digital twin explorer:

    User's image

    You are now able to upload models in bulk.

    See this blog post for more details.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.