public static dtdlInterfaceModel GetDTDLModel(DigitalTwinsClient client, string dtmi) { dtdlInterfaceModel dtdlModel = new dtdlInterfaceModel(); try { Response <ModelData> r = client.GetModel(dtmi); if (r.Value != null) { if (!Program.gVehicle.Exists(item => item.id == dtmi)) { dtdlModel = DigitalTwinInMemory.LoadInterfaceModel(r.Value.Model); } } } catch (RequestFailedException e) { Console.WriteLine($"Response {e.Status}: {e.Message}"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } return(dtdlModel); }
private static bool ProcessExtendsSectionItem(string file, string extendsSectionItem) { bool exitProcess = false; MatchCollection dtmiExtends = Regex.Matches(extendsSectionItem, "dtmi:[:a-zA-Z0-9;_]*", RegexOptions.Singleline); foreach (Match dtmiExtendsMatch in dtmiExtends) { // find this model try { Response <ModelData> res = client.GetModel(dtmiExtendsMatch.Value); // model found! keep going } catch (RequestFailedException e) { // Model Not Found - find it in the directory and call Upload Model if (e.Status == 404) { string missingInterface = dtmiExtendsMatch.Value; int missingPartIndex = missingInterface.LastIndexOf(":"); missingInterface = missingInterface.Substring(missingPartIndex + 1).Replace(";1", ""); missingInterface = missingInterface.Replace(";", ""); string[] missingFile = Directory.GetFiles(modelPath, missingInterface + ".json*", SearchOption.AllDirectories); if (missingFile.Count <string>() == 0) { // no file found, perhaps the definition of the schema is contained within the current file, lets try exitProcess = UploadModel(file); if (exitProcess == true) { Log.Error($"Could not find a definition for Interace {" + missingInterface + "}"); } } else { if (missingFile.Count <string>() > 1) { // More than one file was matched, log a warning Log.Alert("More than one file matched the prefix " + missingInterface + ".json in the ModelPath directory. Processing only the first"); } // try to Upload the Model in the extends section exitProcess = UploadModel(missingFile[0]); } } else { Log.Error($"Error in extends section in Model {file.Split("\\").Last()}"); Log.Error($"Response {e.Status}: {e.Message}"); exitProcess = true; } } } return(exitProcess); }
/// <summary> /// Create a twin with the specified properties /// </summary> public static async Task CreateTwin(DigitalTwinsClient client) { Console.Write("vehicleId: "); string vehicleID = Console.ReadLine(); Console.Write("modelId: "); string dtmi = Console.ReadLine(); try { Response <ModelData> res = client.GetModel(dtmi); string twinId = string.Format("{0}-{1}", res.Value.DisplayName.Values.ElementAt(0), vehicleID); Console.Write("twinId (suggest {0}): ", twinId); twinId = Console.ReadLine(); var twinData = new BasicDigitalTwin { Id = twinId, Metadata = { ModelId = dtmi, }, }; try { await client.CreateDigitalTwinAsync(twinData.Id, JsonSerializer.Serialize(twinData)); Console.WriteLine($"Twin '{twinId}' created successfully!"); } catch (RequestFailedException e) { Console.WriteLine($"Error {e.Status}: {e.Message}"); } catch (Exception ex) { Console.WriteLine($"Error: {ex}"); } } catch (RequestFailedException e) { Console.WriteLine($"Error {e.Status}: {e.Message}"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } }
public ModelDto GetModel(string modelId) { var modelDatum = _digitalTwinsClient.GetModel(modelId); return(ModelDto.MapFromModelData(modelDatum.Value)); }