private static bool UploadModel(string file) { bool exitProcess = false; StreamReader r = new StreamReader(file); string dtdl = r.ReadToEnd(); r.Close(); object dtdlObj = JsonSerializer.Deserialize <object>(dtdl); try { Response <ModelData[]> res = client.CreateModels(new List <string>() { dtdl }); Log.Ok($"Model {file.Split("\\").Last()} created successfully!"); foreach (ModelData md in res.Value) { LogResponse(md.Model); } } catch (RequestFailedException e) { switch (e.Status) { case 409: // 409 is when the Model already exists - so just skip this model Log.Ok($"Model {file.Split("\\").Last()} already exists, skipped!"); break; case 400: // Model could not be uploaded because of a dependency // first inspect Extends Section exitProcess = ProcessExtendsSection(file, (JsonElement)dtdlObj); if (exitProcess) { return(true); } exitProcess = ProcessPropertiesSection(file, dtdl); if (exitProcess) { return(true); } // now try the original file back again exitProcess = UploadModel(file); break; default: break; } } return(exitProcess); }
static void Main(string[] args) { Parser.Default.ParseArguments <CliOptions>(args) .WithParsed(o => { modelPath = o.ModelPath; }); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { int width = Math.Min(Console.LargestWindowWidth, 150); int height = Math.Min(Console.LargestWindowHeight, 40); Console.SetWindowSize(width, height); } try { // Read configuration data from the IConfiguration config = new ConfigurationBuilder() .AddJsonFile("serviceConfig.json", false, true) .Build(); clientId = config["clientId"]; tenantId = config["tenantId"]; adtInstanceUrl = config["instanceUrl"]; } catch (Exception) { Log.Error($"Could not read service configuration file serviceConfig.json"); Log.Alert($"Please copy serviceConfig.json.TEMPLATE to serviceConfig.json"); Log.Alert($"and edit to reflect your service connection settings."); Log.Alert($"Make sure that 'Copy always' or 'Copy if newer' is set for serviceConfig.json in VS file properties"); Environment.Exit(0); } Log.Ok("Authenticating..."); try { var credential = new InteractiveBrowserCredential(tenantId, clientId); client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential); // force authentication to happen here try { client.GetDigitalTwin("---"); } catch (RequestFailedException) { // As we are intentionally try to retrieve a twin that is most likely not going to exist, this exception is expected // We just do this to force the authentication library to authenticate ahead } catch (Exception e) { Log.Error($"Authentication or client creation error: {e.Message}"); Log.Alert($"Have you checked that the configuration in serviceConfig.json is correct?"); Environment.Exit(0); } } catch (Exception e) { Log.Error($"Authentication or client creation error: {e.Message}"); Log.Alert($"Have you checked that the configuration in serviceConfig.json is correct?"); Environment.Exit(0); } Log.Ok($"Service client created – ready to go"); try { EnumerationOptions options = new EnumerationOptions() { RecurseSubdirectories = true }; foreach (string file in Directory.EnumerateFiles(modelPath, "*.json", options)) { StreamReader r = new StreamReader(file); string dtdl = r.ReadToEnd(); r.Close(); Response <ModelData[]> res = client.CreateModels(new List <string>() { dtdl }); Log.Ok($"Model {file.Split("\\").Last()} created successfully!"); foreach (ModelData md in res.Value) { LogResponse(md.Model); } } } catch (RequestFailedException e) { Log.Error($"Response {e.Status}: {e.Message}"); } catch (Exception ex) { Log.Error($"Error: {ex.Message}"); } }