Skip to content

Commit

Permalink
Improved seat massager sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ashbeitz committed Mar 1, 2024
1 parent cbf2c5b commit 86cd19f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
35 changes: 22 additions & 13 deletions dtdl-tools/src/dtdl-validator/DtdlValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,62 @@ class Program
private const int EXIT_SUCCESS = 0;
private const int EXIT_FAILURE = 1;

static string ConvertToDTMI(string filepath, string dirpath, string extension)
/// <summary>
/// Convert a DTDL file's path to a DTMI.
/// </summary>
/// <param name="dtdlFilePath">The DTDL file's full path.</param>
/// <param name="dtdlDirPath">The DTDL directory's path.</param>
/// <param name="extension">The extensin used by the DTDL files.</param>
/// <returns>The corresponding DTMI.</returns>
static string ConvertToDTMI(string dtdlFilePath, string dtdlDirPath, string extension)
{
string relativepath = filepath.Substring(dirpath.Length + 1, filepath.Length - dirpath.Length - extension.Length - 2);
string dtmi = relativepath.Replace('/', ':').Replace('-', ';'); // .ToLower();
// Strip off the directory path and the extension.
string dtmiPpath = dtdlFilePath.Substring(dtdlDirPath.Length + 1, dtdlFilePath.Length - dtdlDirPath.Length - extension.Length - 2);
// Replace each directory separator with a colon and the hyphen with a semicolon.
string dtmi = dtmiPpath.Replace('/', ':').Replace('-', ';');
return dtmi;
}

/// <summary>
/// This method validates all of the DTDL files with the provided extension that are located
/// under the provided directory.
/// </summary>
/// <param name="directory">The directory that contains the DTDL files that we wish to validate.</param>
/// <param name="dtdlDirectory">The directory that contains the DTDL files that we wish to validate.</param>
/// <param name="extension">The extension used by the DTDL files.</param>
/// <returns>
/// EXIT_SUCCESS when all if the DTDL files are valid.
/// EXIT_FAILURE when any of the DTDL files are NOT valid.
/// </returns>
static int ValidateDtdl(DirectoryInfo directory, String extension)
static int ValidateDtdl(DirectoryInfo dtdlDirectory, String extension)
{
if (!directory.Exists)
if (!dtdlDirectory.Exists)
{
Console.WriteLine($"Directory {directory.FullName} does not exist.");
Console.WriteLine($"Directory {dtdlDirectory.FullName} does not exist.");
return EXIT_FAILURE;
}

var files = Directory.GetFiles(directory.FullName, $"*.{extension}", SearchOption.AllDirectories);
var files = Directory.GetFiles(dtdlDirectory.FullName, $"*.{extension}", SearchOption.AllDirectories);

if (!files.Any())
{
Console.WriteLine($"No files with extension .{extension} found in directory {directory}");
Console.WriteLine($"No files with extension .{extension} found in directory {dtdlDirectory}");
return EXIT_FAILURE;
}

var dmrClient = new ModelsRepositoryClient(new Uri(directory.FullName));
var modelRepoClient = new ModelsRepositoryClient(new Uri(dtdlDirectory.FullName));

var parser = new ModelParser(new ParsingOptions()
{
DtmiResolverAsync = dmrClient.ParserDtmiResolverAsync
DtmiResolverAsync = modelRepoClient.ParserDtmiResolverAsync
});

bool failureOccured = false;
foreach (var file in files)
{
try
{
string dtmi = ConvertToDTMI(file, directory.FullName, extension);
var model = dmrClient.GetModelAsync(dtmi).GetAwaiter().GetResult();
string dtmi = ConvertToDTMI(file, dtdlDirectory.FullName, extension);
var model = modelRepoClient.GetModelAsync(dtmi).GetAwaiter().GetResult();
var dictParsed = parser.ParseAsync(model.Content[dtmi]).GetAwaiter().GetResult();
Console.WriteLine($"{file} - ok");
}
Expand Down
16 changes: 13 additions & 3 deletions dtdl-tools/src/dtdl-validator/ModelsRepositoryClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

using Azure.IoT.ModelsRepository;
using DTDLParser;
using System.Runtime.CompilerServices;

internal static class ModelsRepositoryClientExtensions
{
/// <summary>
/// The Parser's DTMI resolver.
/// </summary>
/// <param name="modelRepoClient">The model's repository client.</param>
/// <param name="dtmis"></param>
/// <param name="cancellationToken">The cancellation topken.</param>
/// <returns>The model definitions for the provided DTMIs.</returns>
public static async IAsyncEnumerable<string> ParserDtmiResolverAsync(
this ModelsRepositoryClient client, IReadOnlyCollection<Dtmi> dtmis,
[EnumeratorCancellation] CancellationToken ct = default)
this ModelsRepositoryClient modelRepoClient, IReadOnlyCollection<Dtmi> dtmis,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
foreach (var dtmi in dtmis.Select(s => s.AbsoluteUri))
{
var result = await client.GetModelAsync(dtmi, ModelDependencyResolution.Disabled, ct);
var result = await modelRepoClient.GetModelAsync(dtmi, ModelDependencyResolution.Disabled, cancellationToken);
yield return result.Content[dtmi];
}
}
Expand Down

0 comments on commit 86cd19f

Please sign in to comment.