From d9289a0b4a9016dcf535741461530c71651e4d2f Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Thu, 29 Jun 2017 17:37:58 -0500 Subject: [PATCH] Working with Postgresql --- ...0_1_.xml => Gradle___IOBoard_RPi_0_1_.xml} | 4 +- ...radle__com_google_code_gson_gson_2_8_1.xml | 11 + ...adle__org_postgresql_postgresql_42_1_1.xml | 11 + ..._org_postgresql_postgresql_42_1_1_jre8.xml | 9 - .idea/modules/poc-java_main.iml | 19 +- .idea/modules/poc-java_test.iml | 20 +- .idea/workspace.xml | 484 ++++++++++----- build.gradle | 5 +- db_setup.sql | 2 +- libs/IOBoard-0.1.jar | Bin 8778 -> 8911 bytes libs/IOBoard-RPi-0.1.jar | Bin 19152 -> 19224 bytes src/main/java/com/henrypump/poc/App.java | 13 - src/main/java/com/henrypump/poc/Database.java | 585 +++++++++--------- .../java/com/henrypump/poc/IOControl.java | 6 +- .../java/com/henrypump/poc/Measurement.java | 81 +-- .../java/com/henrypump/poc/MongoDatabase.java | 347 +++++++++++ src/main/java/com/henrypump/poc/POC.java | 2 - src/main/java/com/henrypump/poc/Well.java | 280 +++------ 18 files changed, 1138 insertions(+), 741 deletions(-) rename .idea/libraries/{Gradle___IOBoard_0_1_.xml => Gradle___IOBoard_RPi_0_1_.xml} (51%) create mode 100644 .idea/libraries/Gradle__com_google_code_gson_gson_2_8_1.xml create mode 100644 .idea/libraries/Gradle__org_postgresql_postgresql_42_1_1.xml delete mode 100644 .idea/libraries/Gradle__org_postgresql_postgresql_42_1_1_jre8.xml delete mode 100644 src/main/java/com/henrypump/poc/App.java create mode 100644 src/main/java/com/henrypump/poc/MongoDatabase.java diff --git a/.idea/libraries/Gradle___IOBoard_0_1_.xml b/.idea/libraries/Gradle___IOBoard_RPi_0_1_.xml similarity index 51% rename from .idea/libraries/Gradle___IOBoard_0_1_.xml rename to .idea/libraries/Gradle___IOBoard_RPi_0_1_.xml index 6d943ff..cbce192 100644 --- a/.idea/libraries/Gradle___IOBoard_0_1_.xml +++ b/.idea/libraries/Gradle___IOBoard_RPi_0_1_.xml @@ -1,7 +1,7 @@ - + - + diff --git a/.idea/libraries/Gradle__com_google_code_gson_gson_2_8_1.xml b/.idea/libraries/Gradle__com_google_code_gson_gson_2_8_1.xml new file mode 100644 index 0000000..623ed41 --- /dev/null +++ b/.idea/libraries/Gradle__com_google_code_gson_gson_2_8_1.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_postgresql_postgresql_42_1_1.xml b/.idea/libraries/Gradle__org_postgresql_postgresql_42_1_1.xml new file mode 100644 index 0000000..2d4fafc --- /dev/null +++ b/.idea/libraries/Gradle__org_postgresql_postgresql_42_1_1.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_postgresql_postgresql_42_1_1_jre8.xml b/.idea/libraries/Gradle__org_postgresql_postgresql_42_1_1_jre8.xml deleted file mode 100644 index 27448b5..0000000 --- a/.idea/libraries/Gradle__org_postgresql_postgresql_42_1_1_jre8.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules/poc-java_main.iml b/.idea/modules/poc-java_main.iml index c8e9149..1a24cb1 100644 --- a/.idea/modules/poc-java_main.iml +++ b/.idea/modules/poc-java_main.iml @@ -9,17 +9,18 @@ - - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/poc-java_test.iml b/.idea/modules/poc-java_test.iml index 45bcf58..48f9e84 100644 --- a/.idea/modules/poc-java_test.iml +++ b/.idea/modules/poc-java_test.iml @@ -9,17 +9,19 @@ - - - - - - - - - + + + + + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c564c07..d07092e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,21 @@ + + + + - + + + @@ -113,25 +119,63 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - @@ -140,10 +184,40 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -152,8 +226,8 @@ - - + + @@ -184,6 +258,10 @@ setupFluidRatio startupStrokes updat + atZone + measurments_num + dbConn.commit(); + measurement_config @@ -387,8 +465,8 @@ @@ -658,6 +737,8 @@ + + @@ -720,6 +801,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - @@ -747,6 +892,7 @@ + @@ -1107,41 +1253,50 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1257,13 +1412,6 @@ - - - - - - - @@ -1283,7 +1431,6 @@ - @@ -1295,13 +1442,6 @@ - - - - - - - @@ -1317,13 +1457,6 @@ - - - - - - - @@ -1338,28 +1471,6 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -1367,62 +1478,143 @@ - + - - + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - + + - + - - - - - + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - diff --git a/build.gradle b/build.gradle index 3246a27..e9d29ec 100644 --- a/build.gradle +++ b/build.gradle @@ -24,8 +24,9 @@ dependencies { compile group: 'com.googlecode.json-simple', name: 'json-simple', version:'1.1.1' compile group: 'org.mongodb', name: 'mongodb-driver', version:'3.4.2' compile group: 'de.vandermeer', name: 'asciitable', version:'0.2.5' - compile name: 'IOBoard-0.1' - compile 'org.postgresql:postgresql:42.1.1.jre8' + compile name: 'IOBoard-RPi-0.1' + compile group: 'org.postgresql', name: 'postgresql', version: '42.1.1' + compile 'com.google.code.gson:gson:2.8.1' testCompile group: 'junit', name: 'junit', version:'3.8.1' } diff --git a/db_setup.sql b/db_setup.sql index e7fda14..e0b2475 100644 --- a/db_setup.sql +++ b/db_setup.sql @@ -86,7 +86,7 @@ CREATE TABLE IF NOT EXISTS well_configs ( CREATE TABLE IF NOT EXISTS setpoints ( id SERIAL PRIMARY KEY, - param TEXT NOT NULL, + param TEXT UNIQUE NOT NULL, setpoint_value REAL NOT NULL, ts_added TIMESTAMP DEFAULT NOW() ); \ No newline at end of file diff --git a/libs/IOBoard-0.1.jar b/libs/IOBoard-0.1.jar index a9654d28a4268b819c7acb43b2db993619236742..68bd497b2baa9c0b53cd8dfb1c8fc58a41e264f8 100644 GIT binary patch delta 2763 zcmZ9Oc{CLK8pp?;os7ZAR+j8r%h(xFQFg`_vdybOwqdeO7)o{%vNVi_P{!U^D>Gyp z#x98niC&`-nYZNL>fUoY_x}F*{?7S+&htEfeugj5EcorL+1PmifMdr1L01>^`1{#@ z#f*l)&#+?G*;{+S4FEh4!3wh|p}A2`FwrCLz+2cAg_pUeWbhkS?DZk8s#bZSW9QTYTn$pQw_cw$iHUYtuLCohDo?5P6^>A*&4JoO?ZDF=X-6n>YQSZKb1) z(NZ<6lKz*`jssjAErPcD;(X50_udvj;i{;~n{c>-F@{p$is{0{yAEDNV@}levBpQS zsxY)gV|X{Dqfu{lIaK4Qj)@wHzr8 zG?6y3@wKz)&2fWN`3~mjwNKr7@&l5^6%hXF&YeS)I6O7ptN&Cb##n|T7SF7d@g^5J z$M<)A9U4$eU23D1q3h6)P1O>iIPZO8LhQlkEd=yA@x5fCr`#*)0!ArR`pb%kPU6Cy zfqrI;KjWIrbEVY-=R?yZ@O;%{2TN$uahK(htvJ$VdN;m82fuM8j@YXDd8BjT3OuVO zS@~htzELbz!WMjW#c6EXF))Kwv#M(^;j28(VOe_@WNzv9Jp_%oc1uS|v|w!%RavWx z6X7o~Vz^qJ|KbLI2z%C3*KYH%Ry?G{k~;XT)xq-e?i{W3@Avqq=oiWNr1s|K8ZMH+ zv1bMr1v*<*aQ8~<*SIL#hi5_^m%YlfC&&B7P{XgwY`;p*d-a%@*;MFCO^7V)1$)k( zn+ln{oh$X&t5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_-^BCLk=gTD(>7KpKE-foNS%nf5n8;RF@XbQc1r;>vI#1a!QZFGTWEA{!Xfvsgeh;&wvNL21NW{_ kQHfIhzltNewJ$>YI&3_aW|!!HTO8*dLw8PnH-2yb0j`eoAOHXW diff --git a/src/main/java/com/henrypump/poc/App.java b/src/main/java/com/henrypump/poc/App.java deleted file mode 100644 index e7d0ba5..0000000 --- a/src/main/java/com/henrypump/poc/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.henrypump.poc; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/src/main/java/com/henrypump/poc/Database.java b/src/main/java/com/henrypump/poc/Database.java index bd8bf19..d7d227f 100644 --- a/src/main/java/com/henrypump/poc/Database.java +++ b/src/main/java/com/henrypump/poc/Database.java @@ -4,27 +4,14 @@ package com.henrypump.poc; * Created by patrickjmcd on 2/2/17. */ -import com.mongodb.*; - -import com.mongodb.client.AggregateIterable; -import com.mongodb.client.MongoDatabase; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.model.*; - -import org.bson.BSON; +import com.google.gson.*; import org.bson.Document; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; import java.sql.*; import java.time.ZoneId; import java.time.ZonedDateTime; +import org.json.simple.JSONObject; +import org.postgresql.util.PGobject; -import com.mongodb.client.MongoCursor; -import static com.mongodb.client.model.Filters.*; - -import javax.print.Doc; import java.util.*; import java.util.Date; @@ -40,7 +27,10 @@ public class Database{ { Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://" + dbHost + "/"+ dbName; - conn = DriverManager.getConnection(url); + Properties props = new Properties(); + props.setProperty("user","poc"); + props.setProperty("password","poc"); + conn = DriverManager.getConnection(url, props); } catch (ClassNotFoundException e) { @@ -74,16 +64,15 @@ public class Database{ //CARD FUNCTIONS - long getLastStrokeNum(){ + public long getLastStrokeNum(){ long lastStroke = -1; - Statement selStmt = null; try { - String selString = "SELECT COUNT(id) numCards FROM cards"; + String selString = "SELECT MAX(id) numCards FROM cards"; PreparedStatement ps = dbConn.prepareStatement(selString); ResultSet rs = ps.executeQuery(); while (rs.next()){ - lastStroke = rs.getInt("lastStroke"); + lastStroke = rs.getInt("numCards"); } rs.close(); ps.close(); @@ -95,14 +84,14 @@ public class Database{ return lastStroke; } - long newCard(Card inpCard){ + public long newCard(Card inpCard){ String sqlInsert = "INSERT INTO cards (stroke_number, surface_position, surface_load, downhole_position, downhole_load) VALUES (?, ?, ?, ?, ?)"; long numCards = -1; try { - Array surf_pos = dbConn.createArrayOf("real", makeObjectArray(inpCard.getSurfacePosition())); - Array surf_lod = dbConn.createArrayOf("real", makeObjectArray(inpCard.getSurfaceLoad())); - Array down_pos = dbConn.createArrayOf("real", makeObjectArray(inpCard.getDownholePosition())); - Array down_lod = dbConn.createArrayOf("real", makeObjectArray(inpCard.getDownholeLoad())); + Array surf_pos = dbConn.createArrayOf("float4", makeObjectArray(inpCard.getSurfacePosition())); + Array surf_lod = dbConn.createArrayOf("float4", makeObjectArray(inpCard.getSurfaceLoad())); + Array down_pos = dbConn.createArrayOf("float4", makeObjectArray(inpCard.getDownholePosition())); + Array down_lod = dbConn.createArrayOf("float4", makeObjectArray(inpCard.getDownholeLoad())); PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); prepInsert.setLong(1, inpCard.getStrokeNumber()); prepInsert.setArray(2, surf_pos); @@ -111,7 +100,6 @@ public class Database{ prepInsert.setArray(5, down_lod); prepInsert.executeUpdate(); - dbConn.commit(); String selString = "SELECT COUNT(id) numCards FROM cards"; PreparedStatement ps = dbConn.prepareStatement(selString); @@ -129,15 +117,64 @@ public class Database{ } // MEASUREMENT FUNCTIONS + public long tryNewMeasurementConfig(Measurement inpMeasurement){ + String sqlSelect = "SELECT id FROM measurement_configs WHERE m_name = ?"; + long dbId = -1; + try { + PreparedStatement ps = dbConn.prepareStatement(sqlSelect); + ps.setString(1, inpMeasurement.getTagName()); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + dbId = rs.getInt("id"); + } + rs.close(); + ps.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + //If measurement already exists + if (dbId == -1){ + String sqlInsert = "INSERT INTO measurement_configs (m_name, units, store_delta, store_time, use_total, use_average) " + + "VALUES (?, ?, ?, ?, ?, ?)"; + try { + PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); + prepInsert.setString(1, inpMeasurement.getTagName()); + prepInsert.setString(2, inpMeasurement.getUnits()); + prepInsert.setDouble(3, inpMeasurement.getSendDelta()); + prepInsert.setDouble(4, inpMeasurement.getSendTimeDelta()); + prepInsert.setBoolean(5, inpMeasurement.isUseTotal()); + prepInsert.setBoolean(6, inpMeasurement.isUseAverage()); + + prepInsert.executeUpdate(); + + String selString = "SELECT id FROM measurement_configs WHERE m_name = ?"; + PreparedStatement ps = dbConn.prepareStatement(selString); + ps.setString(1, inpMeasurement.getTagName()); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + dbId = rs.getLong("id"); + } + rs.close(); + ps.close(); + } catch (SQLException e){ + e.printStackTrace(); + } + + } + return dbId; + + } + + public long newMeasurementValue(Measurement inpMeasurement){ String sqlInsert = "INSERT INTO measurements_num (m_id, val) VALUES (?, ?)"; long numMeasurements = -1; try { PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); - prepInsert.setInt(1, inpMeasurement.getDbId()); + prepInsert.setLong(1, inpMeasurement.getDbId()); prepInsert.setDouble(2, inpMeasurement.getCurrentValue()); prepInsert.executeUpdate(); - dbConn.commit(); String selString = "SELECT COUNT(id) numMeasurements FROM measurements_num"; PreparedStatement ps = dbConn.prepareStatement(selString); @@ -154,12 +191,14 @@ public class Database{ return numMeasurements; } - public double getLastStoredMeasurement(int dbId){ - String sqlSelect = "SELECT val FROM measurments_num WHERE id = (SELECT MAX(id) FROM measurements_num WHERE db_id = ?)"; + public double getLastStoredMeasurement(long dbId){ + String sqlSelect = "SELECT val FROM measurements_num WHERE id = (SELECT MAX(id) FROM measurements_num WHERE m_id = ?)"; //TODO: Add query to find current average, number of measurements, total, max, and min for today. - double retValue = -1; + + double retValue = Double.MIN_VALUE; try { PreparedStatement ps = dbConn.prepareStatement(sqlSelect); + ps.setLong(1, dbId); ResultSet rs = ps.executeQuery(); while (rs.next()){ retValue = rs.getInt("val"); @@ -180,14 +219,13 @@ public class Database{ long numMeasurements = -1; try { PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); - prepInsert.setInt(1, inpMeasurement.getDbId()); + prepInsert.setLong(1, inpMeasurement.getDbId()); prepInsert.setDouble(2, inpMeasurement.getCurrentValue()); prepInsert.setDouble(3, inpMeasurement.getDailyMax()); prepInsert.setDouble(4, inpMeasurement.getDailyMin()); prepInsert.setDouble(5, inpMeasurement.getAverage()); prepInsert.setDouble(6, inpMeasurement.getTotal()); prepInsert.executeUpdate(); - dbConn.commit(); String selString = "SELECT COUNT(id) numMeasurements FROM daily_totals"; PreparedStatement ps = dbConn.prepareStatement(selString); @@ -205,6 +243,24 @@ public class Database{ } + public double getPreviousDailyTotal(ZonedDateTime inpDateTime){ + String sqlSelect = "SELECT total_value FROM daily_totals WHERE id = (SELECT MAX(id) FROM daily_totals WHERE m_id = " + + "(SELECT m_id FROM well_configs WHERE m_name = 'Fluid Produced'))"; + double lastTotal = -1; + try { + PreparedStatement ps = dbConn.prepareStatement(sqlSelect); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + lastTotal = rs.getInt("total_value"); + } + rs.close(); + ps.close(); + } catch (SQLException e){ + e.printStackTrace(); + } + return lastTotal; + } + public long newWellTest(WellTest inp){ String sqlInsert = "INSERT INTO well_tests (test_completed_timestamp, test_hours, total_bbl, oil_bbl, h2o_bbl, gas_mcf," + " k_factor, oil_bbl_ratio, h2o_bbl_ratio, gas_mcf_ratio) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; @@ -223,7 +279,6 @@ public class Database{ prepInsert.setDouble(9, inp.getWaterRatio()); prepInsert.setDouble(10, inp.getGasMCFRatio()); prepInsert.executeUpdate(); - dbConn.commit(); String selString = "SELECT COUNT(id) numTests FROM well_tests"; PreparedStatement ps = dbConn.prepareStatement(selString); @@ -240,329 +295,241 @@ public class Database{ return numTests; } - -} - -class MGODatabase { - private String pocDatabase = "poc"; - private MongoClient mongoClient; - private MongoDatabase database; - private MongoCredential credential = MongoCredential.createCredential("poc_java", pocDatabase, "HenryPump@1903".toCharArray()); - private MongoCollection cardCollection, wellDataCollection, gaugeOffCollection, wellTestCollection, - fluidShotsCollection, runStatusCollection, wellConfigCollection, setpointCollection; - - public void initCollections(){ - cardCollection = database.getCollection("cards"); - cardCollection.createIndex(Indexes.ascending("timestamp", "strokeNumber")); - - wellDataCollection = database.getCollection("measurements"); - wellDataCollection.createIndex(Indexes.ascending("dateStored", "tagName")); - - gaugeOffCollection = database.getCollection("gaugeOff"); - gaugeOffCollection.createIndex(Indexes.ascending("timestamp", "tagName")); - - - wellTestCollection = database.getCollection("wellTests"); - wellTestCollection.createIndex(Indexes.ascending("testStartTime")); - - fluidShotsCollection = database.getCollection("fluidShots"); - fluidShotsCollection.createIndex(Indexes.ascending("timestamp")); - - runStatusCollection = database.getCollection("runStatus"); - runStatusCollection.createIndex(Indexes.ascending("timestamp")); - - wellConfigCollection = database.getCollection("wellConfiguration"); - wellConfigCollection.createIndex(Indexes.ascending("timestamp")); - - setpointCollection = database.getCollection("setpoints"); - setpointCollection.createIndex(Indexes.ascending("name")); - } - - - MGODatabase(){ - ServerAddress server = new ServerAddress("localhost", 27017); - List credentials = Arrays.asList(credential); - mongoClient = new MongoClient(server, credentials); - database = mongoClient.getDatabase(pocDatabase); - - initCollections(); - - - } - - MGODatabase(String dbHostname){ - ServerAddress server = new ServerAddress(dbHostname, 27017); - List credentials = Arrays.asList(credential); - mongoClient = new MongoClient(server, credentials); - database = mongoClient.getDatabase(pocDatabase); - - initCollections(); - } - - public void close(){ - mongoClient.close(); - } - - public long getLastStrokeNum(){ - - long lastStroke = 0; - - MongoCollection collection = database.getCollection("cards"); - MongoCursor cursor = collection.aggregate( - Arrays.asList( - Aggregates.group("strokeNumber", Accumulators.last("lastStroke", "$strokeNumber")) - ) - ).iterator(); - + public WellTest getPreviousWellTest(ZonedDateTime inpDateTime){ + String selectSql = "SELECT * FROM well_tests WHERE test_completed_timestamp <= ? ORDER BY test_completed_timestamp DESC LIMIT 1"; + WellTest wt = null; try { - while (cursor.hasNext()) { - long docStroke = cursor.next().getLong("lastStroke"); - if (docStroke > lastStroke) { lastStroke = docStroke; } + PreparedStatement ps = dbConn.prepareStatement(selectSql); + ps.setTimestamp(1, new Timestamp(Date.from(inpDateTime.toInstant()).getTime())); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + wt = new WellTest(rs.getTimestamp("test_completed_timestamp").toInstant().atZone(ZoneId.of("GMT")), rs.getDouble("test_hours"), + rs.getDouble("total_bbl"), rs.getDouble("oil_bbl"), rs.getDouble("h20_bbl"), + rs.getDouble("gas_mcf"), rs.getDouble("k_factor")); } - } finally { - cursor.close(); - } - System.out.println("Found last stroke to be " + lastStroke); - return lastStroke; - } - - public long newCard(Card inpCard){ - - List s_p = new ArrayList(); - List s_l = new ArrayList(); - List d_p = new ArrayList(); - List d_l = new ArrayList(); - int numPointsScan = inpCard.getNumPointsUsed(); - double[] sp = inpCard.getSurfacePosition(); - double[] sl = inpCard.getSurfaceLoad(); - double[] dp = inpCard.getDownholePosition(); - double[] dl = inpCard.getDownholeLoad(); - for(int i = 0; i < numPointsScan; i++ ) { - s_p.add(sp[i]); - s_l.add(sl[i]); - d_p.add(dp[i]); - d_l.add(dl[i]); + rs.close(); + ps.close(); + } catch (SQLException e){ + e.printStackTrace(); } - Document doc = new Document("strokeNumber", inpCard.getStrokeNumber()) - .append("surface_position", s_p) - .append("surface_load", s_l) - .append("downhole_position", d_p) - .append("downhole_load", d_l) - .append("timestamp", Date.from(ZonedDateTime.now().toInstant())); - cardCollection.insertOne(doc); - System.out.println("Stored stroke number " + inpCard.getStrokeNumber() + " as " + doc.getObjectId("_id")); - return cardCollection.count(); - } - - - public long newMeasurement(Measurement inpMeasurement){ - ZonedDateTime nowDate = ZonedDateTime.now(ZoneId.of("UTC")); - ZonedDateTime todayDate = ZonedDateTime.of(nowDate.getYear(), nowDate.getMonthValue(), nowDate.getDayOfMonth(), 0, 0, 0, 0, ZoneId.of("UTC")); - Date storeDate = Date.from(todayDate.toInstant()); - Document nowMeasurement = new Document("values." + nowDate.getHour() + "." + nowDate.getMinute(), inpMeasurement.getCurrentValue()) - .append("currentValue", inpMeasurement.getCurrentValue()) - .append("maxValue", inpMeasurement.getDailyMax()) - .append("minValue", inpMeasurement.getDailyMin()) - .append("averageValue", inpMeasurement.getAverage()) - .append("totalValue", inpMeasurement.getTotal()) - .append("numMeasurements", inpMeasurement.getNumMeasurements()) - .append("units", inpMeasurement.getUnits()) - .append("storeDelta", inpMeasurement.getSendDelta()) - .append("storeTime", inpMeasurement.getSendTimeDelta()) - .append("useTotal", inpMeasurement.isUseTotal()) - .append("useAverage", inpMeasurement.isUseAverage()) - .append("dateStored", storeDate); - - wellDataCollection.updateOne(and(eq("tagname", inpMeasurement.getTagName()), eq("dateStored", storeDate)), new Document("$set", nowMeasurement), (new UpdateOptions()).upsert(true)); -// wellDataCollection.updateOne(and(eq("tagname", inpMeasurement.getTagName()), eq("dateStored", todayDate)), new Document("$set", aggregateData), (new UpdateOptions()).upsert(true)); - - System.out.println("Stored " + inpMeasurement.getCurrentValue() + " for " + inpMeasurement.getTagName()); - return wellDataCollection.count(); - } - - public Document getLastStoredMeasurement(String tagName){ - MongoCursor cursor = wellDataCollection.find(eq("tagname", tagName)).sort(Sorts.descending("dateStored")).limit(1).iterator(); - Document lastStoredDoc = new Document(); - try { - while (cursor.hasNext()) { - lastStoredDoc = cursor.next(); - } - } finally { - cursor.close(); - } - return lastStoredDoc; - } - - public long newDailyTotal(Measurement inpMeasurement){ - Document doc = new Document("tagname", inpMeasurement.getTagName()) - .append("currentValue", inpMeasurement.getCurrentValue()) - .append("maxDailyValue", inpMeasurement.getDailyMax()) - .append("minDailyValue", inpMeasurement.getDailyMin()) - .append("dailyAverage", inpMeasurement.getAverage()) - .append("dailyTotal", inpMeasurement.getTotal()) - .append("timestamp", Date.from(ZonedDateTime.now().toInstant())); - gaugeOffCollection.insertOne(doc); - return gaugeOffCollection.count(); - - } - - public double getPreviousDailyTotal(ZonedDateTime inpDateTime){ - String isoInpDateTime = inpDateTime.toString(); - MongoCursor cursor = wellTestCollection.find(and(eq("tagname", "Fluid Produced"), lte("timestamp", isoInpDateTime))) - .sort(Sorts.descending("timestamp")).limit(1).iterator(); - double lastTotal = -1.0; - try { - while (cursor.hasNext()) { - lastTotal = cursor.next().getDouble("dailyTotal"); - } - } finally { - cursor.close(); - } - return lastTotal; - }; - - public long newWellTest(WellTest inp){ - Document doc = new Document("testStartTime", Date.from(inp.getTestStart().toInstant())) - .append("testHours", inp.getTestHours()) - .append("testTotalBBL", inp.getTotalFluidBBL()) - .append("testOilBBL", inp.getTestOilBBL()) - .append("testWaterBBL", inp.getTestWaterBBL()) - .append("testGasMCF", inp.getTestGasMCF()) - .append("kFactor", inp.getkFactor()) - .append("oilRatio", inp.getOilRatio()) - .append("waterRatio", inp.getWaterRatio()) - .append("gasMCFRatio", inp.getGasMCFRatio()); - wellTestCollection.insertOne(doc); - return wellTestCollection.count(); - } - - public Document getPreviousWellTest(ZonedDateTime inpDateTime){ - MongoCursor cursor = wellTestCollection.find(lte("testStartTime", Date.from(inpDateTime.toInstant()))) - .sort(Sorts.descending("testStartTime")).limit(1).iterator(); - Document lastTest = new Document("kFactor", (Double) 1.0); - try { - while (cursor.hasNext()) { - lastTest = cursor.next(); - } - } finally { - cursor.close(); - } - return lastTest; + return wt; }; public double getLatestKFactor(){ - MongoCursor cursor = wellTestCollection.find().sort(Sorts.descending("testStartTime")).limit(1).iterator(); + String selectSql = "SELECT k_factor from well_tests WHERE id = (SELECT MAX(id) FROM well_tests)"; double kFactor = 1.0; + try { - while (cursor.hasNext()) { - kFactor = cursor.next().getDouble("kFactor"); + PreparedStatement ps = dbConn.prepareStatement(selectSql); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + kFactor = rs.getDouble("k_factor"); } - } finally { - cursor.close(); + rs.close(); + ps.close(); + } catch (SQLException e){ + e.printStackTrace(); } return kFactor; } public long newFluidShot(FluidShot inp){ - String df = ZonedDateTime.now().toString(); - Document doc = new Document("timestamp", Date.from(ZonedDateTime.now().toInstant())) - .append("fluidLevel", inp.getFluidLevel()) - .append("pumpIntakePressure", inp.getPumpIntakePressure()) - .append("frictionEstimate", inp.getFrictionEstimate()); - fluidShotsCollection.insertOne(doc); - return fluidShotsCollection.count(); + String sqlInsert = "INSERT INTO fluid_shots (shot_timestamp, fluid_level, pump_intake_pressure, friction_estimate) VALUES (?, ?, ?, ?)"; + Timestamp ts = new Timestamp(Date.from(ZonedDateTime.now().toInstant()).getTime()); + long numShots = -1; + try { + PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); + prepInsert.setTimestamp(1, ts); + prepInsert.setDouble(2, inp.getFluidLevel()); + prepInsert.setDouble(3, inp.getPumpIntakePressure()); + prepInsert.setDouble(4, inp.getFrictionEstimate()); + prepInsert.executeUpdate(); + + String selString = "SELECT COUNT(id) numShots FROM fluid_shots"; + PreparedStatement ps = dbConn.prepareStatement(selString); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + numShots = rs.getLong("numShots"); + } + rs.close(); + ps.close(); + + } catch (SQLException e){ + e.printStackTrace(); + } + return numShots; } public double getLatestFrictionEstimate(){ - MongoCursor cursor = fluidShotsCollection.find().sort(Sorts.descending("timestamp")).limit(1).iterator(); - double frictionEstimate = -1; + String selectSql = "SELECT friction_estimate from fluid_shots WHERE id = (SELECT MAX(id) FROM fluid_shots)"; + double frictionEstimate = 1.0; + try { - while (cursor.hasNext()) { - frictionEstimate = cursor.next().getDouble("frictionEstimate"); + PreparedStatement ps = dbConn.prepareStatement(selectSql); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + frictionEstimate = rs.getDouble("friction_estimate"); } - } finally { - cursor.close(); + rs.close(); + ps.close(); + } catch (SQLException e){ + e.printStackTrace(); } return frictionEstimate; } public long newRunStatus(String runStatus, String initiator){ - Document doc = new Document("status", runStatus) - .append("initiator", initiator) - .append("timestamp", Date.from(ZonedDateTime.now().toInstant())); - runStatusCollection.insertOne(doc); - return runStatusCollection.count(); + String sqlInsert = "INSERT INTO device_statuses (status, initiator) VALUES (?, ?)"; + + long numStatuses = -1; + try { + PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); + prepInsert.setString(1, runStatus); + prepInsert.setString(2, initiator); + prepInsert.executeUpdate(); + + String selString = "SELECT COUNT(id) numStatuses FROM device_statuses"; + PreparedStatement ps = dbConn.prepareStatement(selString); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + numStatuses = rs.getLong("numStatuses"); + } + rs.close(); + ps.close(); + + } catch (SQLException e){ + e.printStackTrace(); + } + return numStatuses; } public long storeWellSetup(Well thisWell){ - ArrayList taperArr = new ArrayList(); + Gson gson = new Gson(); + JsonObject wellConfig = new JsonObject(); + JsonArray taperArr = new JsonArray(); + + PGobject wellConfigObj = new PGobject(); + wellConfigObj.setType("json"); + + PGobject taperArrayObj = new PGobject(); + taperArrayObj.setType("json"); + 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)); + JsonObject tap = new JsonObject(); + tap.add("length", new JsonPrimitive(thisWell.getRodLength(i))); + tap.add("diameter", new JsonPrimitive(thisWell.getRodDiameter(i))); + tap.add("material", new JsonPrimitive(thisWell.getRodMaterial(i))); + tap.add("dampingFactor", new JsonPrimitive(thisWell.getDampingFactor(i))); taperArr.add(tap); } + wellConfig.add("wellName", new JsonPrimitive(thisWell.getWellName())); + wellConfig.add("deltaT", new JsonPrimitive(thisWell.getDt())); + wellConfig.add("pumpDiameter", new JsonPrimitive(thisWell.getPumpDiameter())); + wellConfig.add("fluidGradient", new JsonPrimitive(thisWell.getFluidGradient())); + wellConfig.add("tubingID", new JsonPrimitive(thisWell.getTubingID())); + wellConfig.add("tubingOD", new JsonPrimitive(thisWell.getTubingOD())); + wellConfig.add("tubingAnchorDepth", new JsonPrimitive(thisWell.getTubingAnchorDepth())); + wellConfig.add("structuralRating", new JsonPrimitive(thisWell.getStructuralRating())); + wellConfig.add("stuffingBoxFriction", new JsonPrimitive(thisWell.getSbfriction())); + wellConfig.add("tubingHeadPressure", new JsonPrimitive(thisWell.getTubingHeadPressure())); + wellConfig.add("tapers", taperArr); + wellConfig.add("storedBy", new JsonPrimitive("poc")); - 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"); + String sqlInsert = "INSERT INTO well_configs (taper_setup, well_config) VALUES (?, ?)"; + long numConfigs = -1; + try { - wellConfigCollection.insertOne(doc); - return wellConfigCollection.count(); + PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); + + taperArrayObj.setValue(gson.toJson(taperArr)); + prepInsert.setObject(1, taperArrayObj); + + wellConfigObj.setValue(gson.toJson(wellConfig)); + prepInsert.setObject(2, wellConfigObj); + + prepInsert.executeUpdate(); + + String selString = "SELECT COUNT(id) numConfigs FROM well_configs"; + PreparedStatement ps = dbConn.prepareStatement(selString); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + numConfigs = rs.getLong("numConfigs"); + } + rs.close(); + ps.close(); + + } catch (SQLException e){ + e.printStackTrace(); + } + return numConfigs; } - public Document getLatestWellConfiguration(){ - MongoCursor cursor = wellConfigCollection.find().sort(Sorts.descending("timestamp")).limit(1).iterator(); - Document lastConfig = new Document(); + public JsonObject getLatestWellConfiguration(){ + Gson gson = new Gson(); + String selectSql = "SELECT * from well_configs WHERE id = (SELECT MAX(id) FROM well_configs)"; + JsonObject config = new JsonObject(); + try { - while (cursor.hasNext()) { - lastConfig = cursor.next(); + PreparedStatement ps = dbConn.prepareStatement(selectSql); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + config.add("tapers", new JsonPrimitive(rs.getString("taper_setup"))); + config.add("well_config", new JsonPrimitive(rs.getString("well_config"))); } - } finally { - cursor.close(); + rs.close(); + ps.close(); + } catch (SQLException e){ + e.printStackTrace(); } - return lastConfig; + return config; } public long storeSetpoint(String setpointName, double setpointValue){ - Document newSetpoint = new Document("name", setpointName) - .append("value", setpointValue) - .append("lastStored", Date.from(ZonedDateTime.now().toInstant())) - .append("storedBy", "poc"); - setpointCollection.updateOne(eq("name", setpointName), new Document("$set", newSetpoint), (new UpdateOptions()).upsert(true)); - return setpointCollection.count(); + String sqlInsert = "INSERT INTO setpoints (param, setpoint_value) VALUES (?, ?) ON CONFLICT DO UPDATE setpoints SET setpoint_value = ? WHERE param = ?"; + long numSetpoints = -1; + try { + PreparedStatement prepInsert = dbConn.prepareStatement(sqlInsert); + prepInsert.setString(1, setpointName); + prepInsert.setDouble(2, setpointValue); + prepInsert.setDouble(3, setpointValue); + prepInsert.setString(4, setpointName); + prepInsert.executeUpdate(); + String selString = "SELECT COUNT(id) numSetpoints FROM well_configs"; + PreparedStatement ps = dbConn.prepareStatement(selString); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + numSetpoints = rs.getLong("numSetpoints"); + } + rs.close(); + ps.close(); + + } catch (SQLException e){ + e.printStackTrace(); + } + return numSetpoints; } public Double getSetpoint(String setpointName){ - Document setpointDoc = setpointCollection.find(eq("name", setpointName)).first(); - double setpoint; + String sqlSelect = "SELECT setpoint_value FROM setpoints WHERE param = ?"; + double setpoint = -1; + try { - setpoint = setpointDoc.getDouble("value"); - String storedBy = setpointDoc.getString("storedBy"); - System.out.println("Setpoint retrieved from database: [" + setpointName + " = " + setpoint + " stored by " + storedBy + "]"); - } catch (ClassCastException e){ - setpoint = Double.parseDouble(setpointDoc.get("value").toString()); - String storedBy = setpointDoc.getString("storedBy"); - System.out.println("Setpoint retrieved from database: [" + setpointName + " = " + setpoint + " stored by " + storedBy + "]"); - } catch (NullPointerException e){ - setpoint = Double.MIN_VALUE; - System.out.println("Could not find a value for " + setpointName + " in the database."); + PreparedStatement ps = dbConn.prepareStatement(sqlSelect); + ps.setString(1, setpointName); + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + setpoint = rs.getDouble("setpoint_value"); + } + rs.close(); + ps.close(); + } catch (SQLException e){ + e.printStackTrace(); } return setpoint; } + } diff --git a/src/main/java/com/henrypump/poc/IOControl.java b/src/main/java/com/henrypump/poc/IOControl.java index 48f6ae8..6d7a202 100644 --- a/src/main/java/com/henrypump/poc/IOControl.java +++ b/src/main/java/com/henrypump/poc/IOControl.java @@ -1,8 +1,8 @@ package com.henrypump.poc; -import com.henrypump.io.DigitalIn; -import com.henrypump.io.DigitalOut; -import com.henrypump.io.MuxSetup; +import com.henrypump.rpio.DigitalIn; +import com.henrypump.rpio.DigitalOut; +import com.henrypump.rpio.MuxSetup; /** * Created by patrickjmcd on 2/10/17. diff --git a/src/main/java/com/henrypump/poc/Measurement.java b/src/main/java/com/henrypump/poc/Measurement.java index 49a73f5..894e24d 100644 --- a/src/main/java/com/henrypump/poc/Measurement.java +++ b/src/main/java/com/henrypump/poc/Measurement.java @@ -31,10 +31,15 @@ public class Measurement { private String units = null; private boolean useTotal; private boolean useAverage; - private int dbId; + private long dbId; - Measurement(String tagName, Database db){ + Measurement(String tagName, Database db, String units, double sendDelta, long sendTimeDelta, boolean useTotal, boolean useAverage){ this.tagName = tagName; + this.units = units; + this.sendDelta = sendDelta; + this.sendTimeDelta = sendTimeDelta; + this.useTotal = useTotal; + this.useAverage = useAverage; average = 0; total = 0; numMeasurements = 0; @@ -45,40 +50,46 @@ public class Measurement { this.db = db; this.lastSentValue = 0.0; - Document lastStored = this.db.getLastStoredMeasurement(this.tagName); - try { - Date lastStoredTimestamp = (Date) lastStored.get("dateStored"); - ZonedDateTime timestamp = ZonedDateTime.ofInstant(lastStoredTimestamp.toInstant(), - ZoneId.systemDefault()); - if (isToday(timestamp)){ - this.average = lastStored.getDouble("averageValue"); - this.total = lastStored.getDouble("totalValue"); -// this.lastSentValue = lastStored.getDouble("currentValue"); - this.dailyMax = lastStored.getDouble("maxValue"); - this.dailyMin = lastStored.getDouble("minValue"); - this.sendDelta = lastStored.getDouble("storeDelta"); - this.sendTimeDelta = lastStored.getLong("storeTime"); - this.units = lastStored.getString("units"); -// this.lastSentTimestamp = timestamp.toEpochSecond(); - Document values = (Document) lastStored.get("values"); - Object[] hours = values.keySet().toArray(); - String maxHour = (String) hours[hours.length - 1]; - Document maxHourDoc = (Document) values.get(maxHour); - Object[] minutes = maxHourDoc.keySet().toArray(); - String maxMinute = (String) minutes[minutes.length - 1]; - this.lastSentValue = maxHourDoc.getDouble(maxMinute); - this.numMeasurements = lastStored.getLong("numMeasurements"); + this.dbId = this.db.tryNewMeasurementConfig(this); - System.out.println("Using stored value from " + ZonedDateTime.of(timestamp.getYear(), - timestamp.getMonthValue(), timestamp.getDayOfMonth(), Integer.parseInt(maxHour), - Integer.parseInt(maxMinute), 0, 0, timestamp.getZone()).toString() + " for " + this.tagName); - } else { - System.out.println("Cannot use stored value from " + timestamp.toString() + " for " + this.tagName); - } - } catch (NullPointerException e){ - System.out.println("There was no previous measurement in the database for " + this.tagName); + double lastStored = this.db.getLastStoredMeasurement(this.dbId); + if (lastStored != Double.MIN_VALUE) { + this.lastSentValue = this.db.getLastStoredMeasurement(this.dbId); } +// try { +// Date lastStoredTimestamp = (Date) lastStored.get("dateStored"); +// ZonedDateTime timestamp = ZonedDateTime.ofInstant(lastStoredTimestamp.toInstant(), +// ZoneId.systemDefault()); +// if (isToday(timestamp)){ +// this.average = lastStored.getDouble("averageValue"); +// this.total = lastStored.getDouble("totalValue"); +//// this.lastSentValue = lastStored.getDouble("currentValue"); +// this.dailyMax = lastStored.getDouble("maxValue"); +// this.dailyMin = lastStored.getDouble("minValue"); +// this.sendDelta = lastStored.getDouble("storeDelta"); +// this.sendTimeDelta = lastStored.getLong("storeTime"); +// this.units = lastStored.getString("units"); +//// this.lastSentTimestamp = timestamp.toEpochSecond(); +// Document values = (Document) lastStored.get("values"); +// Object[] hours = values.keySet().toArray(); +// String maxHour = (String) hours[hours.length - 1]; +// Document maxHourDoc = (Document) values.get(maxHour); +// Object[] minutes = maxHourDoc.keySet().toArray(); +// String maxMinute = (String) minutes[minutes.length - 1]; +// this.lastSentValue = maxHourDoc.getDouble(maxMinute); +// this.numMeasurements = lastStored.getLong("numMeasurements"); +// +// +// System.out.println("Using stored value from " + ZonedDateTime.of(timestamp.getYear(), +// timestamp.getMonthValue(), timestamp.getDayOfMonth(), Integer.parseInt(maxHour), +// Integer.parseInt(maxMinute), 0, 0, timestamp.getZone()).toString() + " for " + this.tagName); +// } else { +// System.out.println("Cannot use stored value from " + timestamp.toString() + " for " + this.tagName); +// } +// } catch (NullPointerException e){ +// System.out.println("There was no previous measurement in the database for " + this.tagName); +// } } @@ -155,7 +166,7 @@ public class Measurement { this.useAverage = useAverage; } - public int getDbId() { + public long getDbId() { return dbId; } @@ -181,7 +192,7 @@ public class Measurement { dailyMin = Math.min(dailyMin, currentValue); if(abs(currentValue - lastSentValue) > sendDelta || (currentTimestamp - lastSentTimestamp) > (sendTimeDelta)){ - long l = db.newMeasurement(this); + long l = db.newMeasurementValue(this); lastSentValue = currentValue; lastSentTimestamp = currentTimestamp; } diff --git a/src/main/java/com/henrypump/poc/MongoDatabase.java b/src/main/java/com/henrypump/poc/MongoDatabase.java new file mode 100644 index 0000000..e624f94 --- /dev/null +++ b/src/main/java/com/henrypump/poc/MongoDatabase.java @@ -0,0 +1,347 @@ +package com.henrypump.poc; + +import com.mongodb.MongoClient; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.model.*; +import org.bson.Document; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import static com.mongodb.client.model.Filters.and; +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.lte; + +/** + * Created by patrickjmcd on 6/28/17. + */ +public class MongoDatabase { + + private String pocDatabase = "poc"; + private MongoClient mongoClient; + private com.mongodb.client.MongoDatabase database; + private MongoCredential credential = MongoCredential.createCredential("poc_java", pocDatabase, "HenryPump@1903".toCharArray()); + private MongoCollection cardCollection, wellDataCollection, gaugeOffCollection, wellTestCollection, + fluidShotsCollection, runStatusCollection, wellConfigCollection, setpointCollection; + + public void initCollections(){ + cardCollection = database.getCollection("cards"); + cardCollection.createIndex(Indexes.ascending("timestamp", "strokeNumber")); + + wellDataCollection = database.getCollection("measurements"); + wellDataCollection.createIndex(Indexes.ascending("dateStored", "tagName")); + + gaugeOffCollection = database.getCollection("gaugeOff"); + gaugeOffCollection.createIndex(Indexes.ascending("timestamp", "tagName")); + + + wellTestCollection = database.getCollection("wellTests"); + wellTestCollection.createIndex(Indexes.ascending("testStartTime")); + + fluidShotsCollection = database.getCollection("fluidShots"); + fluidShotsCollection.createIndex(Indexes.ascending("timestamp")); + + runStatusCollection = database.getCollection("runStatus"); + runStatusCollection.createIndex(Indexes.ascending("timestamp")); + + wellConfigCollection = database.getCollection("wellConfiguration"); + wellConfigCollection.createIndex(Indexes.ascending("timestamp")); + + setpointCollection = database.getCollection("setpoints"); + setpointCollection.createIndex(Indexes.ascending("name")); + } + + + MongoDatabase(){ + ServerAddress server = new ServerAddress("localhost", 27017); + List credentials = Arrays.asList(credential); + mongoClient = new MongoClient(server, credentials); + database = mongoClient.getDatabase(pocDatabase); + + initCollections(); + + + } + + MongoDatabase(String dbHostname){ + ServerAddress server = new ServerAddress(dbHostname, 27017); + List credentials = Arrays.asList(credential); + mongoClient = new MongoClient(server, credentials); + database = mongoClient.getDatabase(pocDatabase); + + initCollections(); + } + + public void close(){ + mongoClient.close(); + } + + public long getLastStrokeNum(){ + + long lastStroke = 0; + + MongoCollection collection = database.getCollection("cards"); + MongoCursor cursor = collection.aggregate( + Arrays.asList( + Aggregates.group("strokeNumber", Accumulators.last("lastStroke", "$strokeNumber")) + ) + ).iterator(); + + try { + while (cursor.hasNext()) { + long docStroke = cursor.next().getLong("lastStroke"); + if (docStroke > lastStroke) { lastStroke = docStroke; } + } + } finally { + cursor.close(); + } + System.out.println("Found last stroke to be " + lastStroke); + return lastStroke; + } + + public long newCard(Card inpCard){ + + List s_p = new ArrayList(); + List s_l = new ArrayList(); + List d_p = new ArrayList(); + List d_l = new ArrayList(); + int numPointsScan = inpCard.getNumPointsUsed(); + double[] sp = inpCard.getSurfacePosition(); + double[] sl = inpCard.getSurfaceLoad(); + double[] dp = inpCard.getDownholePosition(); + double[] dl = inpCard.getDownholeLoad(); + for(int i = 0; i < numPointsScan; i++ ) { + s_p.add(sp[i]); + s_l.add(sl[i]); + d_p.add(dp[i]); + d_l.add(dl[i]); + } + + Document doc = new Document("strokeNumber", inpCard.getStrokeNumber()) + .append("surface_position", s_p) + .append("surface_load", s_l) + .append("downhole_position", d_p) + .append("downhole_load", d_l) + .append("timestamp", Date.from(ZonedDateTime.now().toInstant())); + cardCollection.insertOne(doc); + System.out.println("Stored stroke number " + inpCard.getStrokeNumber() + " as " + doc.getObjectId("_id")); + return cardCollection.count(); + } + + + public long newMeasurement(Measurement inpMeasurement){ + ZonedDateTime nowDate = ZonedDateTime.now(ZoneId.of("UTC")); + ZonedDateTime todayDate = ZonedDateTime.of(nowDate.getYear(), nowDate.getMonthValue(), nowDate.getDayOfMonth(), 0, 0, 0, 0, ZoneId.of("UTC")); + Date storeDate = Date.from(todayDate.toInstant()); + Document nowMeasurement = new Document("values." + nowDate.getHour() + "." + nowDate.getMinute(), inpMeasurement.getCurrentValue()) + .append("currentValue", inpMeasurement.getCurrentValue()) + .append("maxValue", inpMeasurement.getDailyMax()) + .append("minValue", inpMeasurement.getDailyMin()) + .append("averageValue", inpMeasurement.getAverage()) + .append("totalValue", inpMeasurement.getTotal()) + .append("numMeasurements", inpMeasurement.getNumMeasurements()) + .append("units", inpMeasurement.getUnits()) + .append("storeDelta", inpMeasurement.getSendDelta()) + .append("storeTime", inpMeasurement.getSendTimeDelta()) + .append("useTotal", inpMeasurement.isUseTotal()) + .append("useAverage", inpMeasurement.isUseAverage()) + .append("dateStored", storeDate); + + wellDataCollection.updateOne(and(eq("tagname", inpMeasurement.getTagName()), eq("dateStored", storeDate)), new Document("$set", nowMeasurement), (new UpdateOptions()).upsert(true)); +// wellDataCollection.updateOne(and(eq("tagname", inpMeasurement.getTagName()), eq("dateStored", todayDate)), new Document("$set", aggregateData), (new UpdateOptions()).upsert(true)); + + System.out.println("Stored " + inpMeasurement.getCurrentValue() + " for " + inpMeasurement.getTagName()); + return wellDataCollection.count(); + } + + public Document getLastStoredMeasurement(String tagName){ + MongoCursor cursor = wellDataCollection.find(eq("tagname", tagName)).sort(Sorts.descending("dateStored")).limit(1).iterator(); + Document lastStoredDoc = new Document(); + try { + while (cursor.hasNext()) { + lastStoredDoc = cursor.next(); + } + } finally { + cursor.close(); + } + return lastStoredDoc; + } + + public long newDailyTotal(Measurement inpMeasurement){ + Document doc = new Document("tagname", inpMeasurement.getTagName()) + .append("currentValue", inpMeasurement.getCurrentValue()) + .append("maxDailyValue", inpMeasurement.getDailyMax()) + .append("minDailyValue", inpMeasurement.getDailyMin()) + .append("dailyAverage", inpMeasurement.getAverage()) + .append("dailyTotal", inpMeasurement.getTotal()) + .append("timestamp", Date.from(ZonedDateTime.now().toInstant())); + gaugeOffCollection.insertOne(doc); + return gaugeOffCollection.count(); + + } + + public double getPreviousDailyTotal(ZonedDateTime inpDateTime){ + String isoInpDateTime = inpDateTime.toString(); + MongoCursor cursor = wellTestCollection.find(and(eq("tagname", "Fluid Produced"), lte("timestamp", isoInpDateTime))) + .sort(Sorts.descending("timestamp")).limit(1).iterator(); + double lastTotal = -1.0; + try { + while (cursor.hasNext()) { + lastTotal = cursor.next().getDouble("dailyTotal"); + } + } finally { + cursor.close(); + } + return lastTotal; + }; + + public long newWellTest(WellTest inp){ + Document doc = new Document("testStartTime", Date.from(inp.getTestStart().toInstant())) + .append("testHours", inp.getTestHours()) + .append("testTotalBBL", inp.getTotalFluidBBL()) + .append("testOilBBL", inp.getTestOilBBL()) + .append("testWaterBBL", inp.getTestWaterBBL()) + .append("testGasMCF", inp.getTestGasMCF()) + .append("kFactor", inp.getkFactor()) + .append("oilRatio", inp.getOilRatio()) + .append("waterRatio", inp.getWaterRatio()) + .append("gasMCFRatio", inp.getGasMCFRatio()); + wellTestCollection.insertOne(doc); + return wellTestCollection.count(); + } + + public Document getPreviousWellTest(ZonedDateTime inpDateTime){ + MongoCursor cursor = wellTestCollection.find(lte("testStartTime", Date.from(inpDateTime.toInstant()))) + .sort(Sorts.descending("testStartTime")).limit(1).iterator(); + Document lastTest = new Document("kFactor", (Double) 1.0); + try { + while (cursor.hasNext()) { + lastTest = cursor.next(); + } + } finally { + cursor.close(); + } + return lastTest; + }; + + public double getLatestKFactor(){ + MongoCursor cursor = wellTestCollection.find().sort(Sorts.descending("testStartTime")).limit(1).iterator(); + double kFactor = 1.0; + try { + while (cursor.hasNext()) { + kFactor = cursor.next().getDouble("kFactor"); + } + } finally { + cursor.close(); + } + return kFactor; + } + + public long newFluidShot(FluidShot inp){ + String df = ZonedDateTime.now().toString(); + Document doc = new Document("timestamp", Date.from(ZonedDateTime.now().toInstant())) + .append("fluidLevel", inp.getFluidLevel()) + .append("pumpIntakePressure", inp.getPumpIntakePressure()) + .append("frictionEstimate", inp.getFrictionEstimate()); + fluidShotsCollection.insertOne(doc); + return fluidShotsCollection.count(); + } + + public double getLatestFrictionEstimate(){ + MongoCursor cursor = fluidShotsCollection.find().sort(Sorts.descending("timestamp")).limit(1).iterator(); + double frictionEstimate = -1; + try { + while (cursor.hasNext()) { + frictionEstimate = cursor.next().getDouble("frictionEstimate"); + } + } finally { + cursor.close(); + } + return frictionEstimate; + } + + public long newRunStatus(String runStatus, String initiator){ + Document doc = new Document("status", runStatus) + .append("initiator", initiator) + .append("timestamp", Date.from(ZonedDateTime.now().toInstant())); + runStatusCollection.insertOne(doc); + return runStatusCollection.count(); + } + + public long storeWellSetup(Well thisWell){ + ArrayList taperArr = new ArrayList(); + 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"); + + wellConfigCollection.insertOne(doc); + return wellConfigCollection.count(); + } + + public Document getLatestWellConfiguration(){ + MongoCursor 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; + } + + public long storeSetpoint(String setpointName, double setpointValue){ + Document newSetpoint = new Document("name", setpointName) + .append("value", setpointValue) + .append("lastStored", Date.from(ZonedDateTime.now().toInstant())) + .append("storedBy", "poc"); + setpointCollection.updateOne(eq("name", setpointName), new Document("$set", newSetpoint), (new UpdateOptions()).upsert(true)); + return setpointCollection.count(); + + } + + public Double getSetpoint(String setpointName){ + Document setpointDoc = setpointCollection.find(eq("name", setpointName)).first(); + double setpoint; + try { + setpoint = setpointDoc.getDouble("value"); + String storedBy = setpointDoc.getString("storedBy"); + System.out.println("Setpoint retrieved from database: [" + setpointName + " = " + setpoint + " stored by " + storedBy + "]"); + } catch (ClassCastException e){ + setpoint = Double.parseDouble(setpointDoc.get("value").toString()); + String storedBy = setpointDoc.getString("storedBy"); + System.out.println("Setpoint retrieved from database: [" + setpointName + " = " + setpoint + " stored by " + storedBy + "]"); + } catch (NullPointerException e){ + setpoint = Double.MIN_VALUE; + System.out.println("Could not find a value for " + setpointName + " in the database."); + } + return setpoint; + } +} diff --git a/src/main/java/com/henrypump/poc/POC.java b/src/main/java/com/henrypump/poc/POC.java index 216040b..411c09b 100644 --- a/src/main/java/com/henrypump/poc/POC.java +++ b/src/main/java/com/henrypump/poc/POC.java @@ -1,7 +1,5 @@ package com.henrypump.poc; -import java.time.ZonedDateTime; - /** * Created by patrickjmcd on 2/1/17. * POC Class diff --git a/src/main/java/com/henrypump/poc/Well.java b/src/main/java/com/henrypump/poc/Well.java index 4541930..92cf42d 100644 --- a/src/main/java/com/henrypump/poc/Well.java +++ b/src/main/java/com/henrypump/poc/Well.java @@ -1,8 +1,10 @@ package com.henrypump.poc; -import com.henrypump.io.AnalogIn; -import com.henrypump.io.MuxSetup; +import com.google.gson.*; +import com.google.gson.internal.bind.JsonTreeReader; +import com.henrypump.rpio.AnalogIn; +import com.henrypump.rpio.MuxSetup; import de.vandermeer.asciitable.v2.RenderedTable; import de.vandermeer.asciitable.v2.V2_AsciiTable; import de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer; @@ -242,12 +244,12 @@ public class Well { int minRaw = Integer.MAX_VALUE; while (Duration.between(startTime, Instant.now()).toMillis() < 30000){ inclinometer.read(ioMux); - if (inclinometer.rawValue > maxRaw){ - maxRaw = inclinometer.rawValue; + if (inclinometer.getRawValue() > maxRaw){ + maxRaw = inclinometer.getRawValue(); } - if (inclinometer.rawValue < minRaw){ - minRaw = inclinometer.rawValue; + if (inclinometer.getRawValue() < minRaw){ + minRaw = inclinometer.getRawValue(); } try { Thread.sleep(10); @@ -351,167 +353,27 @@ public class Well { } private void initializeMeasurements(){ - strokeSpeed = new Measurement("Stroke Speed", db); - if (strokeSpeed.getUnits() == null) { - strokeSpeed.setUnits("SPM"); - strokeSpeed.setSendDelta(0.5); - strokeSpeed.setSendTimeDelta(600); - strokeSpeed.setUseAverage(true); - strokeSpeed.setUseTotal(false); - } + strokeSpeed = new Measurement("Stroke Speed", db, "SPM", 0.5, 600, true, false); + downholeGrossStroke = new Measurement("Downhole Gross Stroke", db, "in.", 0.5, 600, true, false); + downholeNetStroke = new Measurement("Downhole Net Stroke", db, "in.", 0.5, 600, true, false); + fluidLevel = new Measurement("Fluid Level", db, "ft.", 10.0, 600, true, false); + fluidLoad = new Measurement("Fluid Load", db, "lbs.", 20.0, 600, true, false); + inflowRate = new Measurement("Inflow Rate", db, "BBL/day", 0.5, 600, true, false); + peakPolishedRodLoad = new Measurement("Peak PRL", db, "lbs.", 50, 600, true, false); + minPolishedRodLoad = new Measurement("Min PRL", db, "lbs.", 50.0, 600, true,false); + percentRun = new Measurement("Percent Run", db, "%", 0.5, 600, false, false); + polishedRodHP = new Measurement("Polished Rod HP", db, "HP", 0.25, 600, true,false); + pumpHP = new Measurement("Pump HP", db, "HP", 0.25, 600, true, false); + fluidProduced = new Measurement("Fluid Produced", db, "BBL", 1.0, 600, true, true); + fluidProducedAdjusted = new Measurement("Fluid Produced (adjusted)", db, "BBL", 1.0, 600, true, true); + oilProduced = new Measurement("Oil Produced", db, "BBL", 0.5, 600, true, true); + waterProduced = new Measurement("Water Produced", db, "BBL", 1.0, 600, true, true); + gasProduced = new Measurement("Gas Produced", db, "MCF", 1.0, 600, true, true); + pumpIntakePressure = new Measurement("Pump Intake Pressure", db, "PSI", 5.0, 600, true, false); + surfaceStrokeLength = new Measurement("Surface Stroke", db, "in.", 0.5, 600, true, false); + tubingMovement = new Measurement("Tubing Movement", db, "in.", 0.5, 600, true, false); + pumpFillPercent = new Measurement("Pump Fill Percent", db, "%", 1.0, 600, true, false); - downholeGrossStroke = new Measurement("Downhole Gross Stroke", db); - if (downholeGrossStroke.getUnits() == null) { - downholeGrossStroke.setUnits("in."); - downholeGrossStroke.setSendDelta(0.5); - downholeGrossStroke.setSendTimeDelta(600); - downholeGrossStroke.setUseAverage(true); - downholeGrossStroke.setUseTotal(false); - } - downholeNetStroke = new Measurement("Downhole Net Stroke", db); - if (downholeNetStroke.getUnits() == null) { - downholeNetStroke.setUnits("in."); - downholeNetStroke.setSendDelta(0.5); - downholeNetStroke.setSendTimeDelta(600); - downholeNetStroke.setUseAverage(true); - downholeNetStroke.setUseTotal(false); - } - fluidLevel = new Measurement("Fluid Level", db); - if (fluidLevel.getUnits() == null) { - fluidLevel.setUnits("ft."); - fluidLevel.setSendDelta(10.0); - fluidLevel.setSendTimeDelta(600); - fluidLevel.setUseAverage(true); - fluidLevel.setUseTotal(false); - } - fluidLoad = new Measurement("Fluid Load", db); - if (fluidLoad.getUnits() == null) { - fluidLoad.setUnits("lbs."); - fluidLoad.setSendDelta(20.0); - fluidLoad.setSendTimeDelta(600); - fluidLoad.setUseAverage(true); - fluidLoad.setUseTotal(false); - } - inflowRate = new Measurement("Inflow Rate", db); - if (inflowRate.getUnits() == null) { - inflowRate.setUnits("BBL/day"); - inflowRate.setSendDelta(0.5); - inflowRate.setSendTimeDelta(600); - inflowRate.setUseAverage(true); - inflowRate.setUseTotal(false); - } - peakPolishedRodLoad = new Measurement("Peak PRL", db); - if (peakPolishedRodLoad.getUnits() == null) { - peakPolishedRodLoad.setUnits("lbs."); - peakPolishedRodLoad.setSendDelta(50); - peakPolishedRodLoad.setSendTimeDelta(600); - peakPolishedRodLoad.setUseAverage(true); - peakPolishedRodLoad.setUseTotal(false); - } - minPolishedRodLoad = new Measurement("Min PRL", db); - if (minPolishedRodLoad.getUnits() == null) { - minPolishedRodLoad.setUnits("lbs."); - minPolishedRodLoad.setSendDelta(50.0); - minPolishedRodLoad.setSendTimeDelta(600); - minPolishedRodLoad.setUseAverage(true); - minPolishedRodLoad.setUseTotal(false); - } - percentRun = new Measurement("Percent Run", db); - if (percentRun.getUnits() == null) { - percentRun.setUnits("%"); - percentRun.setSendDelta(0.5); - percentRun.setSendTimeDelta(600); - percentRun.setUseAverage(false); - percentRun.setUseTotal(false); - } - polishedRodHP = new Measurement("Polished Rod HP", db); - if (polishedRodHP.getUnits() == null) { - polishedRodHP.setUnits("HP"); - polishedRodHP.setSendDelta(0.25); - polishedRodHP.setSendTimeDelta(600); - polishedRodHP.setUseAverage(true); - polishedRodHP.setUseTotal(false); - } - pumpHP = new Measurement("Pump HP", db); - if (pumpHP.getUnits() == null) { - pumpHP.setUnits("HP"); - pumpHP.setSendDelta(0.25); - pumpHP.setSendTimeDelta(600); - pumpHP.setUseAverage(true); - pumpHP.setUseTotal(false); - } - fluidProduced = new Measurement("Fluid Produced", db); - if (fluidProduced.getUnits() == null) { - fluidProduced.setUnits("BBL"); - fluidProduced.setSendDelta(1.0); - fluidProduced.setSendTimeDelta(600); - fluidProduced.setUseAverage(true); - fluidProduced.setUseTotal(true); - } - fluidProducedAdjusted = new Measurement("Fluid Produced (adjusted)", db); - if (fluidProducedAdjusted.getUnits() == null) { - fluidProducedAdjusted.setUnits("BBL"); - fluidProducedAdjusted.setSendDelta(1.0); - fluidProducedAdjusted.setSendTimeDelta(600); - fluidProducedAdjusted.setUseAverage(true); - fluidProducedAdjusted.setUseTotal(true); - } - oilProduced = new Measurement("Oil Produced", db); - if (oilProduced.getUnits() == null) { - oilProduced.setUnits("BBL"); - oilProduced.setSendDelta(0.5); - oilProduced.setSendTimeDelta(600); - oilProduced.setUseAverage(true); - oilProduced.setUseTotal(true); - } - waterProduced = new Measurement("Water Produced", db); - if (waterProduced.getUnits() == null) { - waterProduced.setUnits("BBL"); - waterProduced.setSendDelta(1.0); - waterProduced.setSendTimeDelta(600); - waterProduced.setUseAverage(true); - waterProduced.setUseTotal(true); - } - gasProduced = new Measurement("Gas Produced", db); - if (gasProduced.getUnits() == null) { - gasProduced.setUnits("MCF"); - gasProduced.setSendDelta(1.0); - gasProduced.setSendTimeDelta(600); - gasProduced.setUseAverage(true); - gasProduced.setUseTotal(true); - } - pumpIntakePressure = new Measurement("Pump Intake Pressure", db); - if (pumpIntakePressure.getUnits() == null) { - pumpIntakePressure.setUnits("PSI"); - pumpIntakePressure.setSendDelta(5.0); - pumpIntakePressure.setSendTimeDelta(600); - pumpIntakePressure.setUseAverage(true); - pumpIntakePressure.setUseTotal(false); - } - surfaceStrokeLength = new Measurement("Surface Stroke", db); - if (surfaceStrokeLength.getUnits() == null) { - surfaceStrokeLength.setUnits("in."); - surfaceStrokeLength.setSendDelta(0.5); - surfaceStrokeLength.setSendTimeDelta(600); - surfaceStrokeLength.setUseAverage(true); - surfaceStrokeLength.setUseTotal(false); - } - tubingMovement = new Measurement("Tubing Movement", db); - if (tubingMovement.getUnits() == null) { - tubingMovement.setUnits("in."); - tubingMovement.setSendDelta(0.5); - tubingMovement.setSendTimeDelta(600); - tubingMovement.setUseAverage(true); - tubingMovement.setUseTotal(false); - } - pumpFillPercent = new Measurement("Pump Fill Percent", db); - if (pumpFillPercent.getUnits() == null) { - pumpFillPercent.setUnits("%"); - pumpFillPercent.setSendDelta(1.0); - pumpFillPercent.setSendTimeDelta(600); - pumpFillPercent.setUseAverage(true); - pumpFillPercent.setUseTotal(false); - } } public double getDt() { @@ -867,7 +729,7 @@ public class Well { pumpStartTime = ZonedDateTime.now(); db.newRunStatus(getRunStatusString(), initiator); int[] inclMinMax = calibrateInclinometer(); - inclinometer = new AnalogIn(inclinometer.channel, inclMinMax[0], inclMinMax[1], inclinometer.euMin, inclinometer.euMax); + inclinometer = new AnalogIn(inclinometer.getChannel(), inclMinMax[0], inclMinMax[1], inclinometer.getEuMin(), inclinometer.getEuMax()); } } @@ -959,6 +821,7 @@ public class Well { }; void getWellSetup(){ + Gson gson = new Gson(); String jsonFilename = "wellSetup.json"; JSONParser parser = new JSONParser(); try { @@ -1028,55 +891,70 @@ public class Well { } catch (FileNotFoundException e) { System.out.println("No configuration file found. Pulling latest value from database."); - Document newConfigDoc = db.getLatestWellConfiguration(); + JsonObject 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; + JsonPrimitive wellConfigPrimitive = newConfigDoc.getAsJsonPrimitive("well_config"); + String wellConfigString = wellConfigPrimitive.getAsString(); + JsonObject wellConfig = gson.fromJson(wellConfigString, JsonObject.class); - Object newPumpDiameter = newConfigDoc.get("pumpDiameter"); - if (newPumpDiameter != null) setPumpDiameter((Double) newPumpDiameter); - Object newFluidGradient = newConfigDoc.get("fluidGradient"); - if (newFluidGradient != null) fluidGradient = (Double) newFluidGradient; + JsonElement newWellName = wellConfig.get("wellName"); + if (newWellName != null) wellName = newWellName.getAsString(); - Object newTubingID = newConfigDoc.get("tubingID"); - if (newTubingID != null) setTubingID((Double) newTubingID); + JsonElement newDeltaT = wellConfig.get("deltaT"); + if (newDeltaT != null) dt = newDeltaT.getAsDouble(); - Object newTubingOD = newConfigDoc.get("tubingOD"); - if (newTubingOD != null) setTubingOD((Double) newTubingOD); + JsonElement newPumpDiameter = wellConfig.get("pumpDiameter"); + if (newPumpDiameter != null) setPumpDiameter(newPumpDiameter.getAsDouble()); - Object newTubingAnchorDepth = newConfigDoc.get("tubingAnchorDepth"); - if (newTubingAnchorDepth != null) tubingAnchorDepth = (Double) newTubingAnchorDepth; + JsonElement newFluidGradient = wellConfig.get("fluidGradient"); + if (newFluidGradient != null) fluidGradient = newFluidGradient.getAsDouble(); - Object newStructuralRating = newConfigDoc.get("structuralRating"); - if (newStructuralRating != null) structuralRating = (Double) newStructuralRating; + JsonElement newTubingID = wellConfig.get("tubingID"); + if (newTubingID != null) setTubingID(newTubingID.getAsDouble()); - Object newStuffingBoxFriction = newConfigDoc.get("stuffingBoxFriction"); - if (newStuffingBoxFriction != null) setSbfriction((Double) newStuffingBoxFriction); + JsonElement newTubingOD = wellConfig.get("tubingOD"); + if (newTubingOD != null) setTubingOD(newTubingOD.getAsDouble()); - Object newTubingHeadPressure = newConfigDoc.get("tubingOD"); - if (newTubingHeadPressure != null) setTubingOD((Double) newTubingHeadPressure); + JsonElement newTubingAnchorDepth = wellConfig.get("tubingAnchorDepth"); + if (newTubingAnchorDepth != null) tubingAnchorDepth = newTubingAnchorDepth.getAsDouble(); - ArrayList tapers = (ArrayList) newConfigDoc.get("tapers"); - numTapers = tapers.size(); - for (int i = 0; i < numTapers; i++) { - int currentTaperNum = i + 1; - Document taperObj = tapers.get(i); + JsonElement newStructuralRating = wellConfig.get("structuralRating"); + if (newStructuralRating != null) structuralRating = newStructuralRating.getAsDouble(); - Object newLength = taperObj.get("length"); - if (newLength != null) setRodLength(currentTaperNum, (Double) newLength); + JsonElement newStuffingBoxFriction = wellConfig.get("stuffingBoxFriction"); + if (newStuffingBoxFriction != null) setSbfriction(newStuffingBoxFriction.getAsDouble()); - Object newDiameter = taperObj.get("diameter"); - if (newDiameter != null) setRodDiameter(currentTaperNum, (Double) newDiameter); + JsonElement newTubingHeadPressure = wellConfig.get("tubingOD"); + if (newTubingHeadPressure != null) setTubingOD(newTubingHeadPressure.getAsDouble()); - Object newMaterial = taperObj.get("material"); - if (newMaterial != null) setRodYM(currentTaperNum, (String) newMaterial); + JSONParser jparser = new JSONParser(); + JsonPrimitive taperPrimitive = newConfigDoc.getAsJsonPrimitive("tapers"); + String taperString = taperPrimitive.getAsString(); + try { + JSONArray tapers = (JSONArray) parser.parse(taperString); - Object newDampingFactor = taperObj.get("dampingFactor"); - if (newDampingFactor != null) setDampingFactor(currentTaperNum, (Double) newDampingFactor); +// JsonObject tapers = gson.fromJson(taperString, JsonObject.class); + numTapers = tapers.size(); + for (int i = 0; i < numTapers; i++) { + int currentTaperNum = i + 1; + JSONObject taperObj = (JSONObject) 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 (ParseException e1) { + e1.printStackTrace(); } updateTapers(false); } catch (IOException | ParseException e) {