1 package com.neuronrobotics.bowlerstudio.vitamins;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Iterator;
11 import com.neuronrobotics.sdk.common.Log;
13 import eu.mihosoft.vrl.v3d.CSG;
14 import eu.mihosoft.vrl.v3d.STL;
15 import eu.mihosoft.vrl.v3d.parametrics.LengthParameter;
16 import eu.mihosoft.vrl.v3d.parametrics.StringParameter;
18 import com.neuronrobotics.bowlerstudio.BowlerKernel;
19 import com.neuronrobotics.bowlerstudio.IssueReportingExceptionHandler;
20 import com.neuronrobotics.bowlerstudio.scripting.PasswordManager;
22 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
26 import com.neuronrobotics.bowlerstudio.vitamins.Vitamins;
28 import java.io.InputStream;
29 import java.lang.reflect.Type;
30 import java.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.io.IOUtils;
36 import org.eclipse.jgit.api.errors.CheckoutConflictException;
37 import org.eclipse.jgit.api.errors.GitAPIException;
38 import org.eclipse.jgit.api.errors.InvalidRemoteException;
39 import org.eclipse.jgit.api.errors.NoHeadException;
40 import org.eclipse.jgit.api.errors.TransportException;
41 import org.kohsuke.github.GHIssueState;
42 import org.kohsuke.github.GHPullRequest;
43 import org.kohsuke.github.GHRepository;
45 import com.google.gson.Gson;
46 import com.google.gson.GsonBuilder;
47 import com.google.gson.reflect.TypeToken;
52 private static final Map<String, CSG>
fileLastLoaded =
new HashMap<String, CSG>();
53 private static final Map<String, HashMap<String, HashMap<String, Object>>>
databaseSet =
new HashMap<String, HashMap<String, HashMap<String, Object>>>();
58 private static Type
TT_mapStringString =
new TypeToken<HashMap<String, HashMap<String, Object>>>() {
61 private static Gson
gson =
new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
63 private static HashMap<String,Runnable>
changeListeners =
new HashMap<String, Runnable>();
65 System.out.println(
"Vitamins Database Cleraing, reloading files");
67 HashMap<String, HashMap<String, Object>> data =
databaseSet.get(keys);
68 for(String key2:data.keySet()) {
69 HashMap<String, Object> data2 = data.get(key2);
77 public static CSG get(File resource) {
103 }
catch (IOException e) {
112 public static CSG get(String type, String id, String purchasingVariant)
throws Exception {
113 String key = type +
id + purchasingVariant;
119 (ArrayList<Double>) Arrays.asList(data, data));
120 parameter.
setMM(data);
125 }
catch (Exception e) {
130 return get(type, id);
140 public static CSG get(String type, String id)
throws Exception {
141 return get(type, id, 0);
144 private static CSG get(String type, String id,
int depthGauge)
throws Exception {
145 String key = type + id;
149 Map<String, Object> script =
getMeta(type);
152 Object file = script.get(
"scriptGit");
153 Object repostring = script.get(
"scriptFile");
154 Object repo = repostring;
155 if (file !=
null && repo !=
null) {
156 ArrayList<Object> servoMeasurments =
new ArrayList<Object>();
157 servoMeasurments.add(
id);
160 repostring.toString(),
164 Log.
error(key +
" Failed to load from script");
167 }
catch (Exception e) {
171 if (depthGauge < 2) {
172 return get(type, id, depthGauge + 1);
180 Map<String, Object> script =
getMeta(type);
184 }
catch (InvalidRemoteException e) {
187 }
catch (TransportException e) {
190 }
catch (GitAPIException e) {
193 }
catch (IOException e) {
200 public static Map<String, Object>
getMeta(String type) {
204 public static void setScript(String type, String git, String file)
throws Exception {
212 public static void putMeasurment(String type, String size,String measurementName, Object measurmentValue) {
215 public static Object
getMeasurement(String type, String size,String measurementName) {
219 HashMap<String, HashMap<String, Object>> database =
getDatabase(type);
220 if (database.get(
id) ==
null) {
221 database.put(
id,
new HashMap<String, Object>());
223 for(
int j=0;j<5;j++) {
225 HashMap<String, Object> hashMap = database.get(
id);
226 Object[] array = hashMap.keySet().toArray();
227 for (
int i=0;i<array.length;i++) {
228 String key = (String)array[i];
232 }
catch (java.util.ConcurrentModificationException ex) {
238 }
catch (InterruptedException e) {
245 return new HashMap<String, Object>();
262 "Making changes to "+type+
" by "+
PasswordManager.
getUsername()+
"\n\nAuto-save inside com.neuronrobotics.bowlerstudio.vitamins.Vitamins inside bowler-scripting-kernel");
264 System.out.println(
"Database saved "+
getVitaminFile(type,
null,
false).getAbsolutePath());
265 }
catch (org.eclipse.jgit.api.errors.TransportException ex) {
268 "You do not have permission to push to this repo, change the GIT repo to your fork with setGitRpoDatabase(String gitRpoDatabase) ");
276 GHRepository repo = github.getRepository(
"madhephaestus/Hardware-Dimensions");
279 }
catch (org.eclipse.jgit.api.errors.TransportException ex) {
282 GHRepository newRepo = repo.fork();
292 List<GHPullRequest> asList1 = myrepo.queryPullRequests().state(GHIssueState.OPEN).head(
"madhephaestus:master")
295 if(asList1.size()==0) {
297 GHPullRequest request = myrepo.createPullRequest(
"Update from source",
298 "madhephaestus:master",
300 "## Upstream add vitamins",
305 }
catch(org.kohsuke.github.HttpException ex) {
313 List<GHPullRequest> asList = repo.queryPullRequests()
314 .state(GHIssueState.OPEN)
317 if(asList.size()==0) {
318 System.err.println(
"Creating PR for "+head);
319 GHPullRequest request = repo.createPullRequest(
"User Added vitamins to "+type,
322 "## User added vitamins",
326 }
catch (URISyntaxException e) {
333 }
catch(Exception ex) {
340 private static void processSelfPR(GHPullRequest request)
throws IOException {
344 if (request.getMergeable()) {
345 request.merge(
"Auto Merging Master");
351 }
catch (URISyntaxException e) {
356 }
catch(java.lang.NullPointerException ex) {
357 ex.printStackTrace();
360 public static void newVitamin(String type, String
id)
throws Exception {
361 HashMap<String, HashMap<String, Object>> database =
getDatabase(type);
362 if (database.keySet().size() > 0) {
363 String exampleKey =
null;
364 for (String key : database.keySet()) {
365 if (!key.contains(
"meta")) {
369 if (exampleKey !=
null) {
373 for (String key : exampleConfiguration.keySet()) {
374 newConfig.put(key, exampleConfiguration.get(key));
384 public static void setParameter(String type, String
id, String parameterName, Object parameter)
throws Exception {
387 config.put(parameterName, parameter);
393 private static void sanatize(String parameterName, HashMap<String, Object> config) {
394 Object parameter=config.get(parameterName);
396 config.put(parameterName, Double.parseDouble(parameter.toString()));
397 }
catch (NumberFormatException ex) {
398 config.put(parameterName, parameter);
402 public static HashMap<String, HashMap<String, Object>>
getDatabase(String type) {
409 InputStream inPut =
null;
415 Runnable onChange=
null;
419 System.out.println(
"Re-loading "+type);
421 new RuntimeException().printStackTrace();
429 HashMap<String, HashMap<String, Object>> database;
432 inPut = FileUtils.openInputStream(f);
434 jsonString = IOUtils.toString(inPut);
436 System.out.println(
"JSON loading Loading "+type+
" "+jsonString.length());
440 throw new RuntimeException(
"Database failed to read");
442 database=
new HashMap<String, HashMap<String,Object>>();
446 for (String key :
databaseSet.get(type).keySet()) {
447 HashMap<String, Object> conf = database.get(key);
448 for (String confKey : conf.keySet()) {
450 double num = Double.parseDouble(conf.get(confKey).toString());
451 conf.put(confKey, num);
452 }
catch (NumberFormatException ex) {
455 conf.put(confKey, conf.get(confKey).toString());
460 }
catch (Exception e) {
462 databaseSet.put(type,
new HashMap<String, HashMap<String, Object>>());
469 public static File
getVitaminFile(String type, Runnable onChange,
boolean oneShot)
470 throws InvalidRemoteException, TransportException, GitAPIException, IOException {
489 ArrayList<String> actuators =
new ArrayList<String>();
493 actuators.add(vitaminsType);
498 ArrayList<String> actuators =
new ArrayList<String>();
501 actuators.add(vitaminsType);
506 public static boolean isShaft(String vitaminsType) {
508 if (meta !=
null && meta.containsKey(
"shaft"))
514 if (meta !=
null && meta.containsKey(
"actuator"))
529 ArrayList<String> types =
new ArrayList<String>();
533 File[] listOfFiles = folder.listFiles();
535 for (File f : listOfFiles) {
536 if (!f.isDirectory() && f.getName().endsWith(
".json")) {
537 types.add(f.getName().substring(0, f.getName().indexOf(
".json")));
541 }
catch (Exception e) {
545 Collections.sort(types);
551 ArrayList<String> types =
new ArrayList<String>();
552 HashMap<String, HashMap<String, Object>> database =
getDatabase(type);
553 Set<String> keys = database.keySet();
554 for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
555 String s = iterator.next();
557 if (!s.contains(
"meta")) {
562 Collections.sort(types);
576 String myAssets = repo.getGitTransportUrl().replaceAll(
"git://",
"https://");
580 throw new org.kohsuke.github.GHFileNotFoundException();
582 }
catch(Exception ex) {
586 }
catch (Exception ex) {
599 }
catch (CheckoutConflictException|NoHeadException e) {
603 }
catch (Exception e1) {
605 e1.printStackTrace();
607 }
catch (Exception e) {
static void upenURL(String string)
void except(Throwable e, String stacktraceFromCatch)
void uncaughtException(Thread t, Throwable e)
static String getUsername()
static String getLoginID()
static GitHub getGithub()
static void pushCodeToGit(String id, String branch, String FileName, String content, String commitMessage)
static File cloneRepo(String remoteURI, String branch)
static void deleteRepo(String remoteURI)
static Object gitScriptRun(String gitURL, String Filename)
static boolean setAutoupdate(boolean autoupdate)
static String getFullBranch(String remoteURI)
static File fileFromGit(String remoteURI, String fileInRepo)
static File getRepositoryCloneDirectory(String remoteURI)
static void pull(String remoteURI, String branch)
HashMap< String, Double > getVariantParameters()
static PurchasingData get(String type, String size, String variant)
static String makeJson(String type)
static Map< String, Object > getConfiguration(String type, String id)
static Object getMeasurement(String type, String size, String measurementName)
static ArrayList< String > listVitaminTypes()
static ArrayList< String > listVitaminShafts()
static boolean isActuator(String vitaminsType)
static void saveDatabase(String type)
static Map< String, Object > getMeta(String type)
static void saveDatabaseForkIfMissing(String type)
static final Map< String, CSG > fileLastLoaded
static File getVitaminFile(String type, Runnable onChange, boolean oneShot)
static void setIsActuator(String type)
static String jsonRootDir
static void setParameter(String type, String id, String parameterName, Object parameter)
static boolean isShaft(String vitaminsType)
static String getGitRepoDatabase()
static final String defaultgitRpoDatabase
static ArrayList< String > listVitaminActuators()
static void reLoadDatabaseFromFiles()
static String getJsonRootDir()
static void setGitRepoDatabase(String gitRpoDatabase)
static HashMap< String, Runnable > changeListeners
static void processSelfPR(GHPullRequest request)
static ArrayList< String > listVitaminSizes(String type)
static Type TT_mapStringString
static void putMeasurment(String type, String size, String measurementName, Object measurmentValue)
static HashMap< String, HashMap< String, Object > > getDatabase(String type)
static void setIsShaft(String type)
static String gitRpoDatabase
static final Map< String, HashMap< String, HashMap< String, Object > > > databaseSet
static void sanatize(String parameterName, HashMap< String, Object > config)
static File getScriptFile(String type)
static String getRootFolder()
static HashMap< String, Object > getConfigurationRW(String type, String id)
static void setJsonRootDir(String jsonRootDir)
static void newVitamin(String type, String id)
static void setScript(String type, String git, String file)
static void error(String message)
static CSG file(URL path)
void setMM(double newVal)
void setStrValue(String newValue)