-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneResource.cs
65 lines (50 loc) · 2.76 KB
/
SceneResource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fastJSON;
namespace Mujin
{
public class SceneResource
{
private string scenePrimaryKey;
private ControllerClient controllerClient;
public SceneResource(string scenePrimaryKey, ControllerClient controllerClient)
{
this.scenePrimaryKey = scenePrimaryKey;
this.controllerClient = controllerClient;
}
public Task GetOrCreateTaskFromName(string taskName, string taskType, string controllerip = "", int controllerport = 0)
{
return this.GetTaskFromName(taskName, taskType, controllerip, controllerport)
?? this.CreateTaskFromName(taskName, taskType, controllerip, controllerport);
}
private Task GetTaskFromName(string taskName, string taskType, string controllerip, int controllerport)
{
string apiParameters = string.Format("scene/{0}/task/?format=json&limit=1&name={1}&fields=pk,tasktype",
this.scenePrimaryKey, taskName);
Dictionary<string, object> jsonMessage = controllerClient.GetJsonMessage(HttpMethod.GET, apiParameters);
List<object> tasks = (List<object>)jsonMessage["objects"];
if (tasks.Count == 0) return null;
Dictionary<string, object> resultMap = (Dictionary<string, object>)tasks[0];
string taskPrimaryKey = (string)resultMap["pk"];
string taskTypeResult = (string)resultMap["tasktype"];
if (!taskType.Equals(taskTypeResult)) throw new ClientException("unsupported task type: " + taskTypeResult);
return new BinPickingTask(taskPrimaryKey, taskName, controllerip, controllerport, this.controllerClient);
}
private Task CreateTaskFromName(string taskName, string taskType, string controllerip, int controllerport)
{
string apiParameters = string.Format("scene/{0}/task/?format=json&fields=pk", this.scenePrimaryKey);
Dictionary<string, object> command = new Dictionary<string, object>();
command["name"] = taskName;
command["tasktype"] = taskType;
command["scenepk"] = this.scenePrimaryKey;
string message = JSON.ToJSON(command);
Dictionary<string, object> jsonMessage = controllerClient.GetJsonMessage(HttpMethod.POST, apiParameters, message);
string taskPrimaryKeyNew = jsonMessage["pk"].ToString();
string taskTypeNew = jsonMessage["tasktype"].ToString();
if (!taskType.Equals(taskTypeNew)) throw new ClientException("unsupported task type: " + taskTypeNew);
return new BinPickingTask(taskPrimaryKeyNew, taskName, controllerip, controllerport, this.controllerClient);
}
}
}