Skip to content

Commit dd03389

Browse files
authored
Merge pull request #18 from relinc/#16JSON-File-Format
#16 json file format
2 parents 8e043bb + 910d13f commit dd03389

23 files changed

+648
-202
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ crashlytics-build.properties
6767
fabric.properties
6868

6969
# Editor-based Rest Client
70-
.idea/httpRequests
70+
.idea/httpRequests
71+
Libraries/UnitTestingOutputs/

DataProcessor/src/net/relinc/processor/controllers/StrainGaugeController.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,12 @@ public void addStrainGaugeFired(){
165165
{
166166
dist = Converter.mFromMm(Double.parseDouble(distanceToSampleTF.getText()));
167167
}
168-
169-
bar.strainGauges.add(new StrainGaugeOnBar(file.getPath() + ".txt", dist, specificNameTF.getText()));
168+
169+
if( new File(file.getPath() + ".txt").exists() ) {
170+
bar.strainGauges.add(new StrainGaugeOnBar(file.getPath() + ".txt", dist, specificNameTF.getText()));
171+
} else {
172+
bar.strainGauges.add(new StrainGaugeOnBar(file.getPath() + ".json", dist, specificNameTF.getText()));
173+
}
170174

171175
Stage stage = (Stage) addStrainGaugeButton.getScene().getWindow();
172176
stage.close();
@@ -201,7 +205,7 @@ public void saveStrainGaugeFired(){
201205
alert.showAndWait();
202206
return;
203207
}
204-
File newFile = new File(file.getPath() + "/" + strainGaugeNameTF.getText() + ".txt");
208+
File newFile = new File(file.getPath() + "/" + strainGaugeNameTF.getText() + ".json");
205209
if(newFile.exists()){
206210
Alert alert = new Alert(AlertType.INFORMATION);
207211
alert.setTitle("Strain Gauge Already Exists");
@@ -286,6 +290,7 @@ else if(folderCount > 0)
286290
}
287291
else{
288292
if(Dialogs.showConfirmationDialog("Deleting Strain Gauge", "Confirm", "Are you sure you want to delete this strain gauge?", stage))
293+
new File(file.getPath() + ".json").delete();
289294
new File(file.getPath() + ".txt").delete();
290295
}
291296
updateTreeView();
@@ -298,12 +303,18 @@ private void selectedItemChanged() {
298303
System.out.println("Directory cannot be strain gauge file.");
299304
return;
300305
}
301-
File newDir = new File(file.getPath() + ".txt");
302-
if(!newDir.exists()){
306+
File strainGaugeFile = new File(file.getPath() + ".json");
307+
308+
if(! strainGaugeFile.exists()) {
309+
// legacy files have .txt extension, try that
310+
strainGaugeFile = new File(file.getPath() + ".txt");
311+
}
312+
313+
if(!strainGaugeFile.exists()){
303314
System.out.println("Strain Gauge doesn't exist");
304315
return;
305316
}
306-
StrainGauge SG = new StrainGauge(newDir.getPath());
317+
StrainGauge SG = new StrainGauge(strainGaugeFile.getPath());
307318

308319
strainGaugeNameTF.setText(SG.genericName);
309320
resistanceTF.setNumberText(Double.toString(SG.resistance));
@@ -390,32 +401,6 @@ private boolean checkStrainGaugeParameters() {
390401
private void updateTreeView(){
391402
File home = new File(currentWorkingDirectory.getPath() + "/Strain Gauges");
392403
SPOperations.findFiles(home, null, treeView, SPOperations.folderImageLocation, SPOperations.strainGaugeImageLocation);
393-
//findFiles(home, null);
394404
}
395405

396-
// private void findFiles(File dir, TreeItem<String> parent) {
397-
// TreeItem<String> root = new TreeItem<>(dir.getName(), getRootIcon());
398-
// root.setExpanded(true);
399-
// try {
400-
// File[] files = dir.listFiles();
401-
// for (File file : files) {
402-
// if (file.isDirectory()) {
403-
// System.out.println("directory:" + file.getCanonicalPath());
404-
// findFiles(file,root);
405-
// } else {
406-
// if(file.getName().endsWith(".txt"))
407-
// root.getChildren().add(new TreeItem<>(file.getName().substring(0, file.getName().length() - 4),getTextFileIcon()));
408-
// }
409-
// }
410-
// if(parent==null){
411-
// treeView.setRoot(root);
412-
// } else {
413-
//
414-
// parent.getChildren().add(root);
415-
// }
416-
// } catch (IOException e) {
417-
// e.printStackTrace();
418-
// }
419-
// }
420-
421406
}

Fitter/Fitter.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</src_description>
1010
</component>
1111
<component name="NewModuleRootManager">
12+
1213
<output url="file://$MODULE_DIR$/bin" />
1314
<exclude-output />
1415
<content url="file://$MODULE_DIR$">

Libraries/Libraries.iml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<libelement value="jar://$MODULE_DIR$/lib/slf4j-api-1.6.1.jar!/" />
1212
<libelement value="jar://$MODULE_DIR$/lib/imgscalr-lib-4.2.jar!/" />
1313
<libelement value="jar://$MODULE_DIR$/lib/controlsfx-8.40.12.jar!/" />
14+
<libelement value="jar://$MODULE_DIR$/lib/json-simple-1.1.1.jar!/" />
15+
<libelement value="jar://$MODULE_DIR$/lib/json-simple-1.1.1-javadoc.jar!/" />
1416
<src_description expected_position="0">
1517
<src_folder value="file://$MODULE_DIR$/src" expected_position="0" />
1618
</src_description>
@@ -122,5 +124,16 @@
122124
<SOURCES />
123125
</library>
124126
</orderEntry>
127+
<orderEntry type="module-library">
128+
<library name="json-simple-1.1.1.jar">
129+
<CLASSES>
130+
<root url="jar://$MODULE_DIR$/lib/json-simple-1.1.1.jar!/" />
131+
</CLASSES>
132+
<JAVADOC>
133+
<root url="jar:platform:/resource/SUREPulseImageCorrelation/lib/json-simple-1.1.1-javadoc.jar!/" />
134+
</JAVADOC>
135+
<SOURCES />
136+
</library>
137+
</orderEntry>
125138
</component>
126139
</module>
85.3 KB
Binary file not shown.

Libraries/lib/json-simple-1.1.1.jar

23.4 KB
Binary file not shown.

Libraries/src/net/relinc/libraries/application/Bar.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.util.ArrayList;
44

5+
import org.json.simple.JSONArray;
6+
import org.json.simple.JSONObject;
7+
58
import net.relinc.libraries.staticClasses.SPSettings;
9+
import org.json.simple.parser.JSONParser;
610

711

812
public class Bar {
@@ -31,16 +35,25 @@ public class Bar {
3135

3236

3337
public String stringForFile() {
34-
String contents = "SUREPulse Single Bar Setup Version:1" + SPSettings.lineSeperator;
35-
contents += nameDescrip + splitter + name + SPSettings.lineSeperator;
36-
contents += lengthDescrip + splitter + Double.toString(length) + SPSettings.lineSeperator;
37-
contents += densityDescrip + splitter + Double.toString(density) + SPSettings.lineSeperator;
38-
contents += youngsModulusDescrip + splitter + Double.toString(youngsModulus) + SPSettings.lineSeperator;
39-
contents += diameterDescrip + splitter + Double.toString(diameter) + SPSettings.lineSeperator;
40-
contents += speedLimitDescrip + splitter + Double.toString(speedLimit) + SPSettings.lineSeperator;
41-
contents += yieldDescrip + splitter + Double.toString(yield) + SPSettings.lineSeperator;
42-
contents += poissonsRatioDescrip + splitter + Double.toString(poissonsRatio) + SPSettings.lineSeperator;
38+
39+
JSONObject jsonObject = new JSONObject();
40+
41+
jsonObject.put( nameDescrip, name );
42+
jsonObject.put( lengthDescrip, length);
43+
jsonObject.put( densityDescrip, density);
44+
jsonObject.put( youngsModulusDescrip, youngsModulus);
45+
jsonObject.put( diameterDescrip, diameter);
46+
jsonObject.put( speedLimitDescrip, speedLimit);
47+
jsonObject.put( yieldDescrip, yield);
48+
jsonObject.put( poissonsRatioDescrip, poissonsRatio);
49+
jsonObject.put("version",1);
50+
jsonObject.put("description","SUREPulse Single Bar Setup Version");
51+
52+
53+
String contents = jsonObject.toString();
4354
return contents;
55+
56+
4457
}
4558

4659
public void setParametersFromString(String input){
@@ -77,6 +90,28 @@ public double getRadius() {
7790
return this.diameter / 2;
7891
}
7992

93+
public void parseJSONtoParameters(String input) {
94+
95+
JSONParser jsonParser = new JSONParser();
96+
try {
97+
JSONObject jsonObject = (JSONObject) jsonParser.parse(input);
98+
setParametersJSON(jsonObject);
99+
} catch (org.json.simple.parser.ParseException e) {
100+
e.printStackTrace();
101+
}
102+
}
103+
104+
private void setParametersJSON(JSONObject jsonObject) {
105+
name = (String)jsonObject.get(nameDescrip);
106+
length = (Double)jsonObject.get(lengthDescrip);
107+
density = (Double)jsonObject.get(densityDescrip);
108+
youngsModulus = (Double)jsonObject.get(youngsModulusDescrip);
109+
diameter = (Double)jsonObject.get(diameterDescrip);
110+
speedLimit = (Double)jsonObject.get(speedLimitDescrip);
111+
yield = (Double)jsonObject.get(yieldDescrip);
112+
poissonsRatio = (Double)jsonObject.get(poissonsRatioDescrip);
113+
}
114+
80115
public double getArea() {
81116
return Math.PI * Math.pow(diameter / 2,2);
82117
}

Libraries/src/net/relinc/libraries/application/BarSetup.java

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,50 @@ public BarSetup(Bar incid, Bar trans){
2323

2424
public BarSetup(String path) {
2525
//path is a .zip file.
26-
//these are temp, no working directory //TODO: This aint that sweet
26+
//these are temp, no working directory //TODO: This aint that sweet
2727
String uuid = UUID.randomUUID().toString();
28-
File incidentDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Incident Bar");
28+
File incidentDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Incident Bar");
2929
File tranmissionDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Transmission Bar");
3030
SPOperations.deleteFolder(incidentDir);
3131
SPOperations.deleteFolder(tranmissionDir);
3232
String fullName = new File(path).getName(); //has .zip
3333
name = fullName.substring(0, fullName.length() - 4);
34-
try {
35-
ZipFile zipFile = new ZipFile(path);
36-
zipFile.extractAll(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid);
37-
} catch (ZipException e) {
38-
e.printStackTrace();
39-
}
40-
41-
IncidentBar = new Bar();
42-
TransmissionBar = new Bar();
43-
IncidentBar.setParametersFromString(SPOperations.readStringFromFile(incidentDir.getPath() + "/Parameters.txt"));
44-
TransmissionBar.setParametersFromString(SPOperations.readStringFromFile(tranmissionDir.getPath() + "/Parameters.txt"));
45-
46-
for(File file : incidentDir.listFiles()){
47-
if(!file.getName().equals("Parameters.txt")){
48-
IncidentBar.strainGauges.add(new StrainGaugeOnBar(file.getPath()));
49-
}
50-
}
51-
52-
for(File file : tranmissionDir.listFiles()){
53-
//System.out.println("Finding sg files. On File: " + file.getPath());
54-
if(!file.getName().equals("Parameters.txt")){
55-
TransmissionBar.strainGauges.add(new StrainGaugeOnBar(file.getPath()));
56-
}
57-
}
34+
try {
35+
ZipFile zipFile = new ZipFile(path);
36+
zipFile.extractAll(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid);
37+
} catch (ZipException e) {
38+
e.printStackTrace();
39+
}
40+
41+
IncidentBar = new Bar();
42+
TransmissionBar = new Bar();
43+
//handle both systems
44+
45+
parseBarFromFile(IncidentBar, incidentDir);
46+
parseBarFromFile(TransmissionBar, tranmissionDir);
47+
48+
readDirectoryForStrainGauges(IncidentBar, incidentDir);
49+
readDirectoryForStrainGauges(TransmissionBar, tranmissionDir);
50+
}
51+
52+
private void parseBarFromFile( Bar bar, File dir ) {
53+
File file = new File(dir.getPath() + "/Parameters.json");
54+
55+
if( file.exists() ) {
56+
bar.parseJSONtoParameters(SPOperations.readStringFromFile(dir.getPath() + "/Parameters.json"));
57+
}
58+
else {
59+
bar.setParametersFromString(SPOperations.readStringFromFile(dir.getPath() + "/Parameters.txt"));
60+
}
61+
}
62+
63+
private void readDirectoryForStrainGauges( Bar bar, File directory ) {
64+
for(File file : directory.listFiles() ) {
65+
if(!file.getName().contains("Parameters")) {
66+
//file.getPath().endsWith(".json")
67+
bar.strainGauges.add(new StrainGaugeOnBar(file.getPath()));
68+
}
69+
}
5870
}
5971

6072
public BarSetup() {
@@ -92,13 +104,13 @@ public ZipFile createZipFile(String path){
92104
incidentDir.mkdirs();
93105
tranmissionDir.mkdirs();
94106

95-
SPOperations.writeStringToFile(incidentBarFile, incidentDir + "/Parameters.txt");
107+
SPOperations.writeStringToFile(incidentBarFile, incidentDir + "/Parameters.json");
96108
for (StrainGaugeOnBar sg : IncidentBar.strainGauges)
97-
SPOperations.writeStringToFile(sg.stringForFile(), incidentDir + "/" + sg.getNameForFile() + ".txt");
109+
SPOperations.writeStringToFile(sg.stringForFile(), incidentDir + "/" + sg.getNameForFile() + ".json");
98110

99-
SPOperations.writeStringToFile(transmissionBarFile, tranmissionDir + "/Parameters.txt");
111+
SPOperations.writeStringToFile(transmissionBarFile, tranmissionDir + "/Parameters.json");
100112
for (StrainGaugeOnBar sg : TransmissionBar.strainGauges)
101-
SPOperations.writeStringToFile(sg.stringForFile(), tranmissionDir + "/" + sg.getNameForFile() + ".txt");
113+
SPOperations.writeStringToFile(sg.stringForFile(), tranmissionDir + "/" + sg.getNameForFile() + ".json");
102114

103115
zipFile.addFolder(incidentDir, parameters);
104116
zipFile.addFolder(tranmissionDir, parameters);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.relinc.libraries.application;
2+
3+
import org.json.simple.JSONObject;
4+
5+
import java.util.Optional;
6+
7+
// Quick wrapper for using Optionals instead of null.
8+
public class JsonReader {
9+
private JSONObject ob;
10+
11+
public JsonReader(JSONObject ob) {
12+
if(ob == null) {
13+
this.ob = new JSONObject();
14+
} else {
15+
this.ob = ob;
16+
}
17+
}
18+
19+
public Optional<Object> get(Object key) {
20+
Object res = this.ob.get(key);
21+
if(res == null) {
22+
return Optional.empty();
23+
} else {
24+
return Optional.of(res);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)