Skip to content

Commit

Permalink
Merge pull request #18 from relinc/#16JSON-File-Format
Browse files Browse the repository at this point in the history
#16 json file format
  • Loading branch information
markhalonen authored Oct 18, 2018
2 parents 8e043bb + 910d13f commit dd03389
Show file tree
Hide file tree
Showing 23 changed files with 648 additions and 202 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests
.idea/httpRequests
Libraries/UnitTestingOutputs/
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ public void addStrainGaugeFired(){
{
dist = Converter.mFromMm(Double.parseDouble(distanceToSampleTF.getText()));
}

bar.strainGauges.add(new StrainGaugeOnBar(file.getPath() + ".txt", dist, specificNameTF.getText()));

if( new File(file.getPath() + ".txt").exists() ) {
bar.strainGauges.add(new StrainGaugeOnBar(file.getPath() + ".txt", dist, specificNameTF.getText()));
} else {
bar.strainGauges.add(new StrainGaugeOnBar(file.getPath() + ".json", dist, specificNameTF.getText()));
}

Stage stage = (Stage) addStrainGaugeButton.getScene().getWindow();
stage.close();
Expand Down Expand Up @@ -201,7 +205,7 @@ public void saveStrainGaugeFired(){
alert.showAndWait();
return;
}
File newFile = new File(file.getPath() + "/" + strainGaugeNameTF.getText() + ".txt");
File newFile = new File(file.getPath() + "/" + strainGaugeNameTF.getText() + ".json");
if(newFile.exists()){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Strain Gauge Already Exists");
Expand Down Expand Up @@ -286,6 +290,7 @@ else if(folderCount > 0)
}
else{
if(Dialogs.showConfirmationDialog("Deleting Strain Gauge", "Confirm", "Are you sure you want to delete this strain gauge?", stage))
new File(file.getPath() + ".json").delete();
new File(file.getPath() + ".txt").delete();
}
updateTreeView();
Expand All @@ -298,12 +303,18 @@ private void selectedItemChanged() {
System.out.println("Directory cannot be strain gauge file.");
return;
}
File newDir = new File(file.getPath() + ".txt");
if(!newDir.exists()){
File strainGaugeFile = new File(file.getPath() + ".json");

if(! strainGaugeFile.exists()) {
// legacy files have .txt extension, try that
strainGaugeFile = new File(file.getPath() + ".txt");
}

if(!strainGaugeFile.exists()){
System.out.println("Strain Gauge doesn't exist");
return;
}
StrainGauge SG = new StrainGauge(newDir.getPath());
StrainGauge SG = new StrainGauge(strainGaugeFile.getPath());

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

// private void findFiles(File dir, TreeItem<String> parent) {
// TreeItem<String> root = new TreeItem<>(dir.getName(), getRootIcon());
// root.setExpanded(true);
// try {
// File[] files = dir.listFiles();
// for (File file : files) {
// if (file.isDirectory()) {
// System.out.println("directory:" + file.getCanonicalPath());
// findFiles(file,root);
// } else {
// if(file.getName().endsWith(".txt"))
// root.getChildren().add(new TreeItem<>(file.getName().substring(0, file.getName().length() - 4),getTextFileIcon()));
// }
// }
// if(parent==null){
// treeView.setRoot(root);
// } else {
//
// parent.getChildren().add(root);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }

}
1 change: 1 addition & 0 deletions Fitter/Fitter.iml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</src_description>
</component>
<component name="NewModuleRootManager">

<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
Expand Down
13 changes: 13 additions & 0 deletions Libraries/Libraries.iml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<libelement value="jar://$MODULE_DIR$/lib/slf4j-api-1.6.1.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/imgscalr-lib-4.2.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/controlsfx-8.40.12.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/json-simple-1.1.1.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/json-simple-1.1.1-javadoc.jar!/" />
<src_description expected_position="0">
<src_folder value="file://$MODULE_DIR$/src" expected_position="0" />
</src_description>
Expand Down Expand Up @@ -122,5 +124,16 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="json-simple-1.1.1.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/json-simple-1.1.1.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar:platform:/resource/SUREPulseImageCorrelation/lib/json-simple-1.1.1-javadoc.jar!/" />
</JAVADOC>
<SOURCES />
</library>
</orderEntry>
</component>
</module>
Binary file added Libraries/lib/json-simple-1.1.1-javadoc.jar
Binary file not shown.
Binary file added Libraries/lib/json-simple-1.1.1.jar
Binary file not shown.
53 changes: 44 additions & 9 deletions Libraries/src/net/relinc/libraries/application/Bar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import java.util.ArrayList;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import net.relinc.libraries.staticClasses.SPSettings;
import org.json.simple.parser.JSONParser;


public class Bar {
Expand Down Expand Up @@ -31,16 +35,25 @@ public class Bar {


public String stringForFile() {
String contents = "SUREPulse Single Bar Setup Version:1" + SPSettings.lineSeperator;
contents += nameDescrip + splitter + name + SPSettings.lineSeperator;
contents += lengthDescrip + splitter + Double.toString(length) + SPSettings.lineSeperator;
contents += densityDescrip + splitter + Double.toString(density) + SPSettings.lineSeperator;
contents += youngsModulusDescrip + splitter + Double.toString(youngsModulus) + SPSettings.lineSeperator;
contents += diameterDescrip + splitter + Double.toString(diameter) + SPSettings.lineSeperator;
contents += speedLimitDescrip + splitter + Double.toString(speedLimit) + SPSettings.lineSeperator;
contents += yieldDescrip + splitter + Double.toString(yield) + SPSettings.lineSeperator;
contents += poissonsRatioDescrip + splitter + Double.toString(poissonsRatio) + SPSettings.lineSeperator;

JSONObject jsonObject = new JSONObject();

jsonObject.put( nameDescrip, name );
jsonObject.put( lengthDescrip, length);
jsonObject.put( densityDescrip, density);
jsonObject.put( youngsModulusDescrip, youngsModulus);
jsonObject.put( diameterDescrip, diameter);
jsonObject.put( speedLimitDescrip, speedLimit);
jsonObject.put( yieldDescrip, yield);
jsonObject.put( poissonsRatioDescrip, poissonsRatio);
jsonObject.put("version",1);
jsonObject.put("description","SUREPulse Single Bar Setup Version");


String contents = jsonObject.toString();
return contents;


}

public void setParametersFromString(String input){
Expand Down Expand Up @@ -77,6 +90,28 @@ public double getRadius() {
return this.diameter / 2;
}

public void parseJSONtoParameters(String input) {

JSONParser jsonParser = new JSONParser();
try {
JSONObject jsonObject = (JSONObject) jsonParser.parse(input);
setParametersJSON(jsonObject);
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
}

private void setParametersJSON(JSONObject jsonObject) {
name = (String)jsonObject.get(nameDescrip);
length = (Double)jsonObject.get(lengthDescrip);
density = (Double)jsonObject.get(densityDescrip);
youngsModulus = (Double)jsonObject.get(youngsModulusDescrip);
diameter = (Double)jsonObject.get(diameterDescrip);
speedLimit = (Double)jsonObject.get(speedLimitDescrip);
yield = (Double)jsonObject.get(yieldDescrip);
poissonsRatio = (Double)jsonObject.get(poissonsRatioDescrip);
}

public double getArea() {
return Math.PI * Math.pow(diameter / 2,2);
}
Expand Down
72 changes: 42 additions & 30 deletions Libraries/src/net/relinc/libraries/application/BarSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,50 @@ public BarSetup(Bar incid, Bar trans){

public BarSetup(String path) {
//path is a .zip file.
//these are temp, no working directory //TODO: This aint that sweet
//these are temp, no working directory //TODO: This aint that sweet
String uuid = UUID.randomUUID().toString();
File incidentDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Incident Bar");
File incidentDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Incident Bar");
File tranmissionDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Transmission Bar");
SPOperations.deleteFolder(incidentDir);
SPOperations.deleteFolder(tranmissionDir);
String fullName = new File(path).getName(); //has .zip
name = fullName.substring(0, fullName.length() - 4);
try {
ZipFile zipFile = new ZipFile(path);
zipFile.extractAll(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid);
} catch (ZipException e) {
e.printStackTrace();
}

IncidentBar = new Bar();
TransmissionBar = new Bar();
IncidentBar.setParametersFromString(SPOperations.readStringFromFile(incidentDir.getPath() + "/Parameters.txt"));
TransmissionBar.setParametersFromString(SPOperations.readStringFromFile(tranmissionDir.getPath() + "/Parameters.txt"));

for(File file : incidentDir.listFiles()){
if(!file.getName().equals("Parameters.txt")){
IncidentBar.strainGauges.add(new StrainGaugeOnBar(file.getPath()));
}
}

for(File file : tranmissionDir.listFiles()){
//System.out.println("Finding sg files. On File: " + file.getPath());
if(!file.getName().equals("Parameters.txt")){
TransmissionBar.strainGauges.add(new StrainGaugeOnBar(file.getPath()));
}
}
try {
ZipFile zipFile = new ZipFile(path);
zipFile.extractAll(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid);
} catch (ZipException e) {
e.printStackTrace();
}

IncidentBar = new Bar();
TransmissionBar = new Bar();
//handle both systems

parseBarFromFile(IncidentBar, incidentDir);
parseBarFromFile(TransmissionBar, tranmissionDir);

readDirectoryForStrainGauges(IncidentBar, incidentDir);
readDirectoryForStrainGauges(TransmissionBar, tranmissionDir);
}

private void parseBarFromFile( Bar bar, File dir ) {
File file = new File(dir.getPath() + "/Parameters.json");

if( file.exists() ) {
bar.parseJSONtoParameters(SPOperations.readStringFromFile(dir.getPath() + "/Parameters.json"));
}
else {
bar.setParametersFromString(SPOperations.readStringFromFile(dir.getPath() + "/Parameters.txt"));
}
}

private void readDirectoryForStrainGauges( Bar bar, File directory ) {
for(File file : directory.listFiles() ) {
if(!file.getName().contains("Parameters")) {
//file.getPath().endsWith(".json")
bar.strainGauges.add(new StrainGaugeOnBar(file.getPath()));
}
}
}

public BarSetup() {
Expand Down Expand Up @@ -92,13 +104,13 @@ public ZipFile createZipFile(String path){
incidentDir.mkdirs();
tranmissionDir.mkdirs();

SPOperations.writeStringToFile(incidentBarFile, incidentDir + "/Parameters.txt");
SPOperations.writeStringToFile(incidentBarFile, incidentDir + "/Parameters.json");
for (StrainGaugeOnBar sg : IncidentBar.strainGauges)
SPOperations.writeStringToFile(sg.stringForFile(), incidentDir + "/" + sg.getNameForFile() + ".txt");
SPOperations.writeStringToFile(sg.stringForFile(), incidentDir + "/" + sg.getNameForFile() + ".json");

SPOperations.writeStringToFile(transmissionBarFile, tranmissionDir + "/Parameters.txt");
SPOperations.writeStringToFile(transmissionBarFile, tranmissionDir + "/Parameters.json");
for (StrainGaugeOnBar sg : TransmissionBar.strainGauges)
SPOperations.writeStringToFile(sg.stringForFile(), tranmissionDir + "/" + sg.getNameForFile() + ".txt");
SPOperations.writeStringToFile(sg.stringForFile(), tranmissionDir + "/" + sg.getNameForFile() + ".json");

zipFile.addFolder(incidentDir, parameters);
zipFile.addFolder(tranmissionDir, parameters);
Expand Down
27 changes: 27 additions & 0 deletions Libraries/src/net/relinc/libraries/application/JsonReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.relinc.libraries.application;

import org.json.simple.JSONObject;

import java.util.Optional;

// Quick wrapper for using Optionals instead of null.
public class JsonReader {
private JSONObject ob;

public JsonReader(JSONObject ob) {
if(ob == null) {
this.ob = new JSONObject();
} else {
this.ob = ob;
}
}

public Optional<Object> get(Object key) {
Object res = this.ob.get(key);
if(res == null) {
return Optional.empty();
} else {
return Optional.of(res);
}
}
}
Loading

0 comments on commit dd03389

Please sign in to comment.