Adds ability to view configuration
This commit is contained in:
@@ -15,6 +15,9 @@ import com.mongodb.client.model.Aggregates;
|
||||
import com.mongodb.client.model.Sorts;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import com.mongodb.client.MongoCursor;
|
||||
@@ -234,4 +237,69 @@ public class Database {
|
||||
collection.insertOne(doc);
|
||||
return collection.count();
|
||||
}
|
||||
|
||||
static String readFile(String file) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String line = null;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String ls = System.getProperty("line.separator");
|
||||
|
||||
try {
|
||||
while((line = reader.readLine()) != null) {
|
||||
stringBuilder.append(line);
|
||||
stringBuilder.append(ls);
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
public long storeWellSetup(Well thisWell){
|
||||
MongoCollection<Document> collection = database.getCollection("wellConfiguration");
|
||||
|
||||
ArrayList<Document> taperArr = new ArrayList<Document>();
|
||||
for (int i = 1; i <= thisWell.getNumTapers(); i++){
|
||||
Document tap = new Document("length", thisWell.getRodLength(i))
|
||||
.append("diameter", thisWell.getRodDiameter(i))
|
||||
.append("material", thisWell.getRodMaterial(i))
|
||||
.append("dampingFactor", thisWell.getDampingFactor(i));
|
||||
taperArr.add(tap);
|
||||
}
|
||||
|
||||
|
||||
Document doc = new Document("timestamp", Date.from(ZonedDateTime.now().toInstant()))
|
||||
.append("wellName", thisWell.getWellName())
|
||||
.append("deltaT", thisWell.getDt())
|
||||
.append("pumpDiameter", thisWell.getPumpDiameter())
|
||||
.append("fluidGradient", thisWell.getFluidGradient())
|
||||
.append("tubingID", thisWell.getTubingID())
|
||||
.append("tubingOD", thisWell.getTubingOD())
|
||||
.append("tubingAnchorDepth", thisWell.getTubingAnchorDepth())
|
||||
.append("structuralRating", thisWell.getStructuralRating())
|
||||
.append("stuffingBoxFriction", thisWell.getSbfriction())
|
||||
.append("tubingHeadPressure", thisWell.getTubingHeadPressure())
|
||||
.append("tapers", taperArr)
|
||||
.append("storedBy", "poc");
|
||||
|
||||
collection.insertOne(doc);
|
||||
return collection.count();
|
||||
}
|
||||
|
||||
public Document getLatestWellConfiguration(){
|
||||
MongoCollection<Document> wellConfigCollection = database.getCollection("wellConfiguration");
|
||||
MongoCursor<Document> cursor = wellConfigCollection.find().sort(Sorts.descending("timestamp")).limit(1).iterator();
|
||||
Document lastConfig = new Document();
|
||||
try {
|
||||
while (cursor.hasNext()) {
|
||||
lastConfig = cursor.next();
|
||||
}
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
return lastConfig;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ public class IOControl implements Runnable {
|
||||
IOControl(POC poc){
|
||||
this.poc = poc;
|
||||
// IO
|
||||
startBtn = new DigitalIn(8);
|
||||
stopBtn = new DigitalIn(9);
|
||||
startBtn = new DigitalIn(99);
|
||||
stopBtn = new DigitalIn(99);
|
||||
led2 = new DigitalOut(2, 0);
|
||||
led3 = new DigitalOut(3, 0);
|
||||
led4 = new DigitalOut(4, 0);
|
||||
|
||||
@@ -11,24 +11,22 @@ import java.time.ZonedDateTime;
|
||||
public class POC implements Runnable{
|
||||
protected Well thisWell;
|
||||
private Thread t;
|
||||
|
||||
|
||||
private boolean ioEnabled;
|
||||
|
||||
POC(String wellSetupJsonFile, String dbHostname){
|
||||
POC(String dbHostname){
|
||||
ioEnabled = true;
|
||||
thisWell = new Well(dbHostname, 99, 99, 7);
|
||||
thisWell.parseJSONFile(wellSetupJsonFile);
|
||||
thisWell.getWellSetup();
|
||||
}
|
||||
|
||||
POC(String wellSetupJsonFile, String simFileName, boolean ioEnabled, String dbHostname){
|
||||
POC(String simFileName, boolean ioEnabled, String dbHostname){
|
||||
this.ioEnabled = ioEnabled;
|
||||
if (this.ioEnabled) {
|
||||
thisWell = new Well(dbHostname, simFileName,99, 99, 7);
|
||||
} else {
|
||||
thisWell = new Well(dbHostname, simFileName,99, 99, 99);
|
||||
}
|
||||
thisWell.parseJSONFile(wellSetupJsonFile);
|
||||
thisWell.getWellSetup();
|
||||
}
|
||||
|
||||
|
||||
@@ -41,18 +39,15 @@ public class POC implements Runnable{
|
||||
thisWell.setupFluidRatio(0.50, 0.50, 1.12);
|
||||
thisWell.checkSafeties();
|
||||
while (true) {
|
||||
// while (thisWell.getRunStatus() == Well.RUNSTATUS_RUNNING || thisWell.getRunStatus() == Well.RUNSTATUS_STARTING) {
|
||||
for (int i = 0; i <= thisWell.sim.getLastFilledIndex(); i++) {
|
||||
for (int i = 0; i <= thisWell.sim.getLastFilledIndex(); i++) {
|
||||
thisWell.eval(i);
|
||||
|
||||
thisWell.eval(i);
|
||||
|
||||
try {
|
||||
Thread.sleep(sleepMilliseconds);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
Thread.sleep(sleepMilliseconds);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -67,12 +62,41 @@ public class POC implements Runnable{
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args[0].equals("help")){
|
||||
System.out.println("Pass command line parameters...");
|
||||
System.out.println("=============");
|
||||
System.out.println("");
|
||||
System.out.println("Simulation Mode:");
|
||||
System.out.println("-------------");
|
||||
System.out.println("<String: simulation filename> <boolean: use IO> <String: database hostname/address (optional)>");
|
||||
System.out.println("");
|
||||
System.out.println("IO Mode:");
|
||||
System.out.println("-------------");
|
||||
System.out.println("<String: database hostname/address (optional)>");
|
||||
System.out.println("");
|
||||
System.out.println("If updating config via json file, place a config file named 'wellSetup.json' in the project directory.");
|
||||
System.exit(2);
|
||||
}
|
||||
|
||||
|
||||
String dbHostname = "localhost";
|
||||
if (args.length > 3){
|
||||
dbHostname = args[3];
|
||||
}
|
||||
final POC thisPOC = new POC(args[0], args[1], args[2].equals("true"), dbHostname);
|
||||
thisPOC.start();
|
||||
|
||||
if (args.length == 1){
|
||||
dbHostname = args[1];
|
||||
}
|
||||
|
||||
if (args.length < 2){
|
||||
final POC realPOC = new POC(dbHostname);
|
||||
realPOC.start();
|
||||
} else {
|
||||
final POC simPOC = new POC(args[0], args[1].equals("true"), dbHostname);
|
||||
simPOC.start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,19 +6,21 @@ import de.vandermeer.asciitable.v2.V2_AsciiTable;
|
||||
import de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer;
|
||||
import de.vandermeer.asciitable.v2.render.WidthAbsoluteEven;
|
||||
import de.vandermeer.asciitable.v2.themes.V2_E_TableThemes;
|
||||
import org.bson.Document;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import static java.lang.Math.exp;
|
||||
import static java.lang.Math.pow;
|
||||
import static java.lang.Math.sqrt;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by patrickjmcd on 1/31/17.
|
||||
@@ -117,7 +119,7 @@ public class Well {
|
||||
private int direction = DIRECTION_UNKNOWN;
|
||||
private int lastDirection = DIRECTION_UNKNOWN;
|
||||
|
||||
// Modes
|
||||
// MODES
|
||||
private volatile int runMode;
|
||||
public static final int RUNMODE_POC = 0;
|
||||
public static final int RUNMODE_MANUAL = 1;
|
||||
@@ -601,10 +603,13 @@ public class Well {
|
||||
return wtPerFt;
|
||||
};
|
||||
|
||||
void parseJSONFile(String jsonFilename){
|
||||
void getWellSetup(){
|
||||
String jsonFilename = "wellSetup.json";
|
||||
JSONParser parser = new JSONParser();
|
||||
try {
|
||||
|
||||
Object obj = parser.parse(new FileReader(jsonFilename));
|
||||
System.out.println("Reading well configuration from JSON file 'wellSetup.json");
|
||||
JSONObject well = (JSONObject) obj;
|
||||
|
||||
Object newWellName = well.get("wellName");
|
||||
@@ -656,8 +661,65 @@ public class Well {
|
||||
if (newDampingFactor != null) setDampingFactor(currentTaperNum, (Double) newDampingFactor);
|
||||
}
|
||||
|
||||
Path fromFile = Paths.get(jsonFilename);
|
||||
Path toFile = Paths.get(jsonFilename + ".bak");
|
||||
Files.move(fromFile, toFile);
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("No configuration file found. Pulling latest value from database.");
|
||||
Document newConfigDoc = db.getLatestWellConfiguration();
|
||||
|
||||
Object newWellName = newConfigDoc.get("wellName");
|
||||
if (newWellName != null) wellName = (String) newWellName;
|
||||
|
||||
Object newDeltaT = newConfigDoc.get("deltaT");
|
||||
if (newDeltaT != null) dt = (Double) newDeltaT;
|
||||
|
||||
Object newPumpDiameter = newConfigDoc.get("pumpDiameter");
|
||||
if (newPumpDiameter != null) setPumpDiameter((Double) newPumpDiameter);
|
||||
|
||||
Object newFluidGradient = newConfigDoc.get("fluidGradient");
|
||||
if (newFluidGradient != null) fluidGradient = (Double) newFluidGradient;
|
||||
|
||||
Object newTubingID = newConfigDoc.get("tubingID");
|
||||
if (newTubingID != null) setTubingID((Double) newTubingID);
|
||||
|
||||
Object newTubingOD = newConfigDoc.get("tubingOD");
|
||||
if (newTubingOD != null) setTubingOD((Double) newTubingOD);
|
||||
|
||||
Object newTubingAnchorDepth = newConfigDoc.get("tubingAnchorDepth");
|
||||
if (newTubingAnchorDepth != null) tubingAnchorDepth = (Double) newTubingAnchorDepth;
|
||||
|
||||
Object newStructuralRating = newConfigDoc.get("structuralRating");
|
||||
if (newStructuralRating != null) structuralRating = (Double) newStructuralRating;
|
||||
|
||||
Object newStuffingBoxFriction = newConfigDoc.get("stuffingBoxFriction");
|
||||
if (newStuffingBoxFriction != null) setSbfriction((Double) newStuffingBoxFriction);
|
||||
|
||||
Object newTubingHeadPressure = newConfigDoc.get("tubingOD");
|
||||
if (newTubingHeadPressure != null) setTubingOD((Double) newTubingHeadPressure);
|
||||
|
||||
ArrayList<Document> tapers = (ArrayList<Document>) newConfigDoc.get("tapers");
|
||||
numTapers = tapers.size();
|
||||
for (int i = 0; i < numTapers; i++) {
|
||||
int currentTaperNum = i + 1;
|
||||
Document taperObj = tapers.get(i);
|
||||
|
||||
Object newLength = taperObj.get("length");
|
||||
if (newLength != null) setRodLength(currentTaperNum, (Double) newLength);
|
||||
|
||||
Object newDiameter = taperObj.get("diameter");
|
||||
if (newDiameter != null) setRodDiameter(currentTaperNum, (Double) newDiameter);
|
||||
|
||||
Object newMaterial = taperObj.get("material");
|
||||
if (newMaterial != null) setRodYM(currentTaperNum, (String) newMaterial);
|
||||
|
||||
Object newDampingFactor = taperObj.get("dampingFactor");
|
||||
if (newDampingFactor != null) setDampingFactor(currentTaperNum, (Double) newDampingFactor);
|
||||
}
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(3);
|
||||
}
|
||||
updateTapers();
|
||||
|
||||
@@ -822,6 +884,9 @@ public class Well {
|
||||
if (dbFrictionEstimate != -1){
|
||||
frictionEstimate = dbFrictionEstimate;
|
||||
}
|
||||
|
||||
|
||||
db.storeWellSetup(this);
|
||||
}
|
||||
|
||||
private double position(int p)
|
||||
@@ -1182,7 +1247,7 @@ public class Well {
|
||||
|
||||
public static void main( String[] args ){
|
||||
Well thisWell = new Well(args[1], 99, 99, 99);
|
||||
thisWell.parseJSONFile(args[0]);
|
||||
thisWell.getWellSetup();
|
||||
thisWell.printTapers();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user