private static void WaitForJob(Mujin.ControllerClient controllerClient, Options options, string jobPrimaryKey) { string jobStatus = ""; double jobProgress = 0.0; bool jobSeen = false; DateTime startTime = DateTime.Now; while (true) { Dictionary <string, object> job = LookupJob(controllerClient, options, jobPrimaryKey); if (job == null) { // job has been seen before, so must be done now if (jobSeen) { return; } // perhaps job finished too quickly if (DateTime.Now - startTime > TimeSpan.FromSeconds(2)) { return; } // wait a bit more Thread.Sleep(200); continue; } // we have now seen this job at least once jobSeen = true; double newProgress = double.Parse(job["progress"].ToString()); string newStatus = job["status_text"].ToString(); newProgress = Math.Min(1.0, Math.Max(jobProgress, newProgress)); if (newProgress != jobProgress || newStatus != jobStatus) { jobProgress = newProgress; jobStatus = newStatus; Console.WriteLine("Progress {0}%: {1}", Math.Round(jobProgress * 100.0), jobStatus); } string status = job["status"].ToString(); switch (status) { case "succeeded": return; case "lost": case "preempted": throw new Exception(String.Format("Job has stopped unexpectedly: {0}", status)); } Thread.Sleep(200); } }
private static Dictionary <string, object> GetTaskResult(Mujin.ControllerClient controllerClient, Options options, string taskPrimaryKey) { Dictionary <string, object> task = controllerClient.GetSceneTask(options.ScenePrimaryKey, taskPrimaryKey); List <object> results = (List <object>)task["binpickingresults"]; if (results.Count == 0) { return(null); } Dictionary <string, object> result = (Dictionary <string, object>)results[0]; return((Dictionary <string, object>)controllerClient.GetJsonMessage(Mujin.HttpMethod.GET, (string)result["uri"])); }
private static Dictionary <string, object> LookupJob(Mujin.ControllerClient controllerClient, Options options, string jobPrimaryKey) { Dictionary <string, object> jobs = controllerClient.GetJobs(); foreach (Dictionary <string, object> job in (List <object>)jobs["objects"]) { if (job["pk"].ToString() == jobPrimaryKey) { return(job); } } return(null); }
private static void CancelPreviousJobs(Mujin.ControllerClient controllerClient, Options options) { Dictionary <string, object> jobs = controllerClient.GetJobs(); foreach (Dictionary <string, object> job in (List <object>)jobs["objects"]) { if (job["description"].ToString().Contains("/registration-")) { controllerClient.DeleteJob(job["pk"].ToString()); Console.WriteLine("Canceled previous job: {0}", job["pk"].ToString()); } } }
private static Dictionary <string, object> WaitForTaskResult(Mujin.ControllerClient controllerClient, Options options, string taskPrimaryKey) { DateTime startTime = DateTime.Now; while (true) { Dictionary <string, object> result = GetTaskResult(controllerClient, options, taskPrimaryKey); if (result != null) { return(result); } if (DateTime.Now - startTime > TimeSpan.FromSeconds(5)) { throw new Exception("Timed out waiting for task result"); } Thread.Sleep(200); } }
private static void DeletePreviousTasks(Mujin.ControllerClient controllerClient, Options options) { Dictionary <string, object> sceneTasks = controllerClient.GetSceneTasks(options.ScenePrimaryKey); foreach (Dictionary <string, object> sceneTask in (List <object>)sceneTasks["objects"]) { if ((string)sceneTask["tasktype"] == taskType) { try { controllerClient.DeleteSceneTask(options.ScenePrimaryKey, sceneTask["pk"].ToString()); Console.WriteLine("Deleted previous task: {0}", sceneTask["pk"].ToString()); } catch (Exception ex) { Console.WriteLine("Faield to delete previous task: {0}: {0}", sceneTask["pk"].ToString(), ex); } } } }
private async void button1_Click(object sender, EventArgs e) { Button button1 = (Button)sender; button1.Enabled = false; richTextBox1.Text = "Starting backup task ..."; try { string command = "Backup"; if (radioButton2_syncmasterfile.Checked) { command = "SyncMasterFile"; } string controllerurl = controllerurl_input.Text; string controllerusername = username_input.Text; string controllerpassword = password_input.Text; string ftpusername = ftpusername_input.Text; string ftppassword = ftppassword_input.Text; string ftphost = ftphost_input.Text; string ftpport = ftpport_input.Text; string ftpremotePath = ftpremotePath_input.Text; string masterFilePath = masterFilePath_input.Text; Dictionary <string, string> config = new Dictionary <string, string>() { { "controllerurl", controllerurl }, { "controllerusername", controllerusername }, { "controllerpassword", controllerpassword }, { "ftpusername", ftpusername }, { "ftppassword", ftppassword }, { "ftphost", ftphost }, { "ftpport", ftpport }, { "ftpremotePath", ftpremotePath }, { "masterFilePath", masterFilePath } }; System.IO.File.WriteAllText("config.json", JSON.ToNiceJSON(config)); // initialize mujincontrollerclient Mujin.ControllerClient c = new Mujin.ControllerClient(controllerusername, controllerpassword, controllerurl); // get scene pk string scenepk = c.GetCurrentSceneURI().Substring("mujin:/".Length); // create a new task Dictionary <string, object> taskdata = new Dictionary <string, object>(); if (command == "Backup") { taskdata = new Dictionary <string, object>() { { "taskparameters", new Dictionary <string, object>() { { "fileStorageInfo", new Dictionary <string, object>() { { "type", "ftp" }, { "username", ftpusername }, { "password", ftppassword }, { "host", ftphost }, { "port", ftpport }, { "remotePath", ftpremotePath }, } }, { "command", "Backup" } } }, { "name", String.Format("registration-backup-{0}", DateTime.Now.ToString(@"yyyyMMdd-hhmmss")) }, { "tasktype", "registration" }, }; } else if (command == "SyncMasterFile") { taskdata = new Dictionary <string, object>() { { "taskparameters", new Dictionary <string, object>() { { "fileStorageInfo", new Dictionary <string, object>() { { "type", "ftp" }, { "username", ftpusername }, { "password", ftppassword }, { "host", ftphost }, { "port", ftpport }, { "remotePath", ftpremotePath }, } }, { "command", "SyncMasterFile" }, { "remoteMasterFilePath", masterFilePath } } }, { "name", String.Format("registration-syncmasterfile-{0}", DateTime.Now.ToString(@"yyyyMMdd-hhmmss")) }, { "tasktype", "registration" }, }; } // delete previous task Dictionary <string, object> sceneTasks = c.GetSceneTasks(scenepk); foreach (Dictionary <string, object> task in (List <object>)sceneTasks["objects"]) { if ((string)task["tasktype"] == "registration") { try { c.DeleteSceneTask(scenepk, (string)task["pk"]); } catch (Exception ex) { Console.WriteLine("Faield to delete previous task: {0}", ex); } } } Dictionary <string, object> response = c.CreateSceneTask(scenepk, taskdata); string taskpk = (string)response["id"]; // trigger task to execute Dictionary <string, object> runTaskResponse = c.RunScenetaskAsync(scenepk, taskpk); await Progress(scenepk, taskpk, (string)runTaskResponse["jobpk"]); MessageBox.Show(button1, "Done!"); } catch (Exception ex) { richTextBox1.Text = ex.ToString(); } finally { button1.Enabled = true; } }
private async Task Progress(string scenepk, string taskpk, string jobpk) { string controllerurl = controllerurl_input.Text; string controllerusername = username_input.Text; string controllerpassword = password_input.Text; // initialize mujincontrollerclient Mujin.ControllerClient c = new Mujin.ControllerClient(controllerusername, controllerpassword, controllerurl); int progress = 0; bool jobSeen = false; DateTime startTime = DateTime.Now; while (true) { await Task.Delay(200); Dictionary <string, object> job = null; Dictionary <string, object> jobs = c.GetJobs(); foreach (Dictionary <string, object> j in (List <object>)jobs["objects"]) { if (j["pk"].ToString() == jobpk) { job = j; break; } } if (job == null) { // job has been seen before, so must be done now if (jobSeen) { break; } // perhaps job finished too quickly if (DateTime.Now - startTime > TimeSpan.FromSeconds(2)) { break; } // wait a bit more continue; } jobSeen = true; richTextBox1.Text = job["status_text"].ToString(); double progressRaw = double.Parse(job["progress"].ToString()); progress = Math.Min(100, Math.Max(progress, (int)Math.Round(progressRaw * 100))); progressBar1.Invoke(new Action(() => { progressBar1.Value = progress; })); } int objectCount = 0; Dictionary <string, object> task = c.GetSceneTask(scenepk, taskpk); List <object> resultsInfoList = (List <object>)task["binpickingresults"]; if (resultsInfoList.Count > 0) { Dictionary <string, object> resultInfo = (Dictionary <string, object>)resultsInfoList[0]; Dictionary <string, object> result = c.GetJsonMessage(Mujin.HttpMethod.GET, (string)resultInfo["uri"]); Console.WriteLine("{0}", JSON.ToNiceJSON(result)); if (result.ContainsKey("output")) { Dictionary <string, object> output = (Dictionary <string, object>)result["output"]; if (output.ContainsKey("objects")) { List <object> objects = (List <object>)output["objects"]; objectCount = objects.Count; } } } richTextBox1.Text = String.Format("Backed up {0} objects.", objectCount); progressBar1.Invoke(new Action(() => { progressBar1.Value = 100; })); }
private static void Run(Options options) { Console.OutputEncoding = Encoding.GetEncoding(options.ConsoleEncoding); string command = ""; if (options.Backup) { if (command.Length > 0) { throw new ArgumentException("Cannot specify both --backup and --syncMasterFile at the same time."); } command = "Backup"; } if (options.MasterFilePath.Length > 0) { if (command.Length > 0) { throw new ArgumentException("Cannot specify both --backup and --syncMasterFile at the same time."); } command = "SyncMasterFile"; } if (command.Length == 0) { throw new ArgumentException("Have to specify either --backup or --syncMasterFile in command line."); } string taskName = String.Format("registration-{0}-{1}", command.ToLower(), DateTime.Now.ToString(@"yyyyMMdd-HHmmss")); Console.WriteLine("Using task name: {0}", taskName); if (options.OutputFilename.Length == 0) { options.OutputFilename = String.Format("{0}.json", taskName); } Mujin.ControllerClient controllerClient = new Mujin.ControllerClient(options.ControllerUsername, options.ControllerPassword, options.ControllerUrl); // cancel previous running jobs CancelPreviousJobs(controllerClient, options); // get scenepk if (options.ScenePrimaryKey.Length == 0) { options.ScenePrimaryKey = controllerClient.GetCurrentSceneURI().Substring("mujin:/".Length); Console.WriteLine("Using current scene: {0}", options.ScenePrimaryKey); } // delete previous task DeletePreviousTasks(controllerClient, options); // create new task string taskPrimaryKey = CreateTask(controllerClient, options, command, taskName); Console.WriteLine("Task created: {0}", taskPrimaryKey); // trigger task to execute string jobPrimaryKey = RunTaskAsync(controllerClient, options, taskPrimaryKey); Console.WriteLine("Task started: {0}", jobPrimaryKey); // wait for job WaitForJob(controllerClient, options, jobPrimaryKey); Console.WriteLine("Task finished"); // wait until result is written Dictionary <string, object> result = WaitForTaskResult(controllerClient, options, taskPrimaryKey); Console.WriteLine("Result fetched"); // write to file System.IO.File.WriteAllText(options.OutputFilename, JSON.ToNiceJSON(result)); Console.WriteLine("Output written to file: {0}", options.OutputFilename); }
private static string RunTaskAsync(Mujin.ControllerClient controllerClient, Options options, string taskPrimaryKey) { Dictionary <string, object> runSceneTaskResponse = controllerClient.RunScenetaskAsync(options.ScenePrimaryKey, taskPrimaryKey); return(runSceneTaskResponse["jobpk"].ToString()); }
private static string CreateTask(Mujin.ControllerClient controllerClient, Options options, string command, string taskName) { Dictionary <string, object> createSceneTaskResponse = controllerClient.CreateSceneTask(options.ScenePrimaryKey, PrepareTaskData(options, command, taskName)); return(createSceneTaskResponse["id"].ToString()); }