BowlerKernel
VitaminBomManager.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.vitamins;
2 
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.lang.reflect.Type;
8 import java.nio.file.Files;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.Map;
12 
13 import org.apache.commons.io.FileUtils;
14 import org.eclipse.jgit.api.errors.GitAPIException;
15 import org.eclipse.jgit.api.errors.InvalidRemoteException;
16 import org.eclipse.jgit.api.errors.TransportException;
17 
18 import com.google.gson.Gson;
19 import com.google.gson.GsonBuilder;
20 import com.google.gson.reflect.TypeToken;
21 import com.neuronrobotics.bowlerstudio.physics.TransformFactory;
22 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
23 import com.neuronrobotics.sdk.addons.kinematics.MobileBase;
24 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
25 
26 import eu.mihosoft.vrl.v3d.CSG;
27 import javafx.scene.paint.Color;
28 
29 public class VitaminBomManager {
30  public static final String MANUFACTURING_BOM_BASE = "manufacturing/bom";
31  public static final String MANUFACTURING_BOM_JSON = MANUFACTURING_BOM_BASE + ".json";
32  public static final String MANUFACTURING_BOM_CSV = MANUFACTURING_BOM_BASE + ".csv";
33  private static boolean saving = false;
34 
35  private class VitaminElement {
36  String name;
37  String type;
38  String size;
39  TransformNR pose;
40  }
41 
42  Type type = new TypeToken<HashMap<String, ArrayList<VitaminElement>>>() {
43  }.getType();
44  Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
45  private HashMap<String, ArrayList<VitaminElement>> database = null;//
46  private String baseURL;
47 
48  public VitaminBomManager(String url) throws IOException {
49  baseURL = url;
50  File baseWorkspaceFile = ScriptingEngine.getRepositoryCloneDirectory(baseURL);
51  File bom = new File(baseWorkspaceFile.getAbsolutePath() + "/" + MANUFACTURING_BOM_JSON);
52  if (!bom.exists()) {
53  if (!bom.getParentFile().exists()) {
54  bom.getParentFile().mkdir();
55  }
56  try {
57  bom.createNewFile();
58  } catch (IOException e) {
59  // TODO Auto-generated catch block
60  e.printStackTrace();
61  }
62  save();
63  } else {
64  String source;
65  byte[] bytes;
66  try {
67  bytes = Files.readAllBytes(bom.toPath());
68  source = new String(bytes, "UTF-8");
69  database = gson.fromJson(source, type);
70  } catch (Exception ex) {
71  ex.printStackTrace();
72  }
73  }
74  if(database==null) {
75  database=new HashMap<String, ArrayList<VitaminElement>>();
76  }
77  }
78 
79  public void set(String name, String type, String size, TransformNR location) {
80  VitaminElement newElement = getElement(name);
81  boolean toAdd = false;
82  if (newElement == null) {
83  newElement = new VitaminElement();
84  newElement.name = name;
85  toAdd = true;
86  }
87  newElement.pose = location;
88  newElement.size = size;
89  newElement.type = type;
90  String key = type + ":" + size;
91  // synchronized (database) {
92  if (database.get(key) == null) {
93  database.put(key, new ArrayList<VitaminBomManager.VitaminElement>());
94  }
95  if (toAdd)
96  database.get(key).add(newElement);
97  // }
98  save();
99  // newElement.url=(String) getConfiguration(name).get("source");
100  }
101 
102  public CSG get(String name) {
103  VitaminElement e = getElement(name);
104  if (e == null)
105  throw new RuntimeException("Vitamin must be defined before it is used: " + name);
106 
107  try {
108  CSG transformed = Vitamins.get(e.type, e.size).transformed(TransformFactory.nrToCSG(e.pose));
109  transformed.setManufacturing(incominng -> {
110  return null;
111  });
112  transformed.setColor(Color.SILVER);
113  return transformed;
114 
115  } catch (Exception e1) {
116  // TODO Auto-generated catch block
117  e1.printStackTrace();
118  }
119  return null;
120  }
121 
122  public TransformNR getCoMLocation(String name) {
123  VitaminElement e = getElement(name);
124  double x = (double) getConfiguration(name).get("massCentroidX");
125  double y = (double) getConfiguration(name).get("massCentroidY");
126 
127  double z = (double) getConfiguration(name).get("massCentroidZ");
128 
129  return e.pose.copy().translateX(x).translateY(y).translateZ(z);
130  }
131 
132  public double getMassKg(String name) {
133  return (double) getConfiguration(name).get("massKg");
134  }
135 
136  public Map<String, Object> getConfiguration(String name) {
137  VitaminElement e = getElement(name);
138  if (e == null)
139  throw new RuntimeException("Vitamin must be defined before it is used: " + name);
140 
141  return Vitamins.getConfiguration(e.type, e.size);
142  }
143 
144  private VitaminElement getElement(String name) {
145  // synchronized (database) {
146  for (String testName : database.keySet()) {
147  ArrayList<VitaminElement> list = database.get(testName);
148  for (VitaminElement el : list) {
149  if (el.name.contentEquals(name))
150  return el;
151  }
152  }
153  // }
154  return null;
155  }
156 
157  public void clear() {
158  // synchronized (database) {
159  database.clear();
160  // }
161  }
162 
163  private void saveLocal() {
164  saving = true;
165  String csv = "name,qty,source\n";
166  String content = null;
167 
168  content = gson.toJson(database);
169  // String[] source = base.getGitSelfSource();
170 
171  for (String key : database.keySet()) {
172  ArrayList<VitaminElement> list = database.get(key);
173  if (list.size() > 0) {
174  VitaminElement e = list.get(0);
175  String size = database.get(key).size() + "";
176  String URL = (String) getConfiguration(e.name).get("source");
177  csv += key + "," + size + "," + URL + "\n";
178  } else {
179  System.out.println("Failure on " + key);
180  }
181  }
182 
183  try {
186  if (current.contentEquals(csv) && currentJ.contentEquals(content)) {
187  //System.out.println("No update, BoM current");
188  saving = false;
189  return;
190  }
191  } catch (Exception e1) {
192  // TODO Auto-generated catch block
193  e1.printStackTrace();
194  }
195  try {
196  write(MANUFACTURING_BOM_JSON, content);
198 // ScriptingEngine.commit(baseURL, ScriptingEngine.getBranch(baseURL), MANUFACTURING_BOM_JSON, content,
199 // "Save Bill Of Material", true);
200 // ScriptingEngine.commit(baseURL, ScriptingEngine.getBranch(baseURL), MANUFACTURING_BOM_CSV, csv,
201 // "Save Bill Of Material", true);
202  } catch (Exception e) {
203  // TODO Auto-generated catch block
204  e.printStackTrace();
205  }
206  saving = false;
207 
208  }
209 
210  private void write(String file, String content)
211  throws InvalidRemoteException, TransportException, GitAPIException, IOException {
212  File f = ScriptingEngine.fileFromGit(baseURL, file);
213  if (!f.getParentFile().exists())
214  f.getParentFile().mkdir();
215  if (!f.exists()) {
216  f.createNewFile();
217  }
218  BufferedWriter writer = new BufferedWriter(new FileWriter(f.getAbsolutePath()));
219  writer.write(content);
220  writer.close();
221  }
222 
223  public void save() {
224  saveLocal();
225  }
226 
227 }
static eu.mihosoft.vrl.v3d.Transform nrToCSG(TransformNR nr)
static String[] codeFromGit(String id, String FileName)
static File fileFromGit(String remoteURI, String fileInRepo)
HashMap< String, ArrayList< VitaminElement > > database
static Map< String, Object > getConfiguration(String type, String id)
Definition: Vitamins.java:209
CSG transformed(Transform transform)
Definition: CSG.java:1676
CSG setManufacturing(PrepForManufacturing manufactuing)
Definition: CSG.java:2225
CSG setColor(Color color)
Definition: CSG.java:207