From a90e776cdb0a87612119a84c43894253536e43e7 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Wed, 1 Mar 2017 17:20:36 -0600 Subject: [PATCH] Start, stop, and update config from main webserver --- .idea/workspace.xml | 546 +++--------------- src/main/java/com/henrypump/poc/POC.java | 4 +- .../java/com/henrypump/poc/WebServer.java | 14 + src/main/java/com/henrypump/poc/Well.java | 56 +- www/pocwww/pocwww/__init__.py | 4 + www/pocwww/pocwww/json.py | 50 +- www/pocwww/pocwww/templates/config.jinja2 | 27 +- www/pocwww/pocwww/templates/dashboard.jinja2 | 13 +- www/pocwww/pocwww/templates/layout.jinja2 | 55 +- www/pocwww/setup.py | 1 + 10 files changed, 263 insertions(+), 507 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5d76b79..cc2c510 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,16 @@ - + + + + + + + @@ -103,13 +109,16 @@ + + + - - + + @@ -118,8 +127,8 @@ - - + + @@ -128,26 +137,20 @@ - - + + - - - - - - - + - - + + @@ -166,14 +169,13 @@ - + - - + + - @@ -184,81 +186,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -298,11 +226,9 @@ - - - - - + + + @@ -311,7 +237,7 @@ - + @@ -336,6 +262,7 @@ simLoops fill arraycopy + nT1 @@ -751,8 +678,8 @@ @@ -767,10 +694,10 @@ - @@ -788,8 +715,7 @@ - - + @@ -844,7 +770,8 @@ - + + @@ -852,7 +779,7 @@ - + @@ -1118,8 +1045,17 @@ + + + + + + + + + - + @@ -1136,7 +1072,8 @@ - + + @@ -1150,7 +1087,6 @@ - @@ -1232,9 +1168,7 @@ - - - + @@ -1276,81 +1210,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1360,12 +1220,6 @@ - - - - - - @@ -1374,9 +1228,7 @@ - - - + @@ -1398,81 +1250,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1480,81 +1258,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1741,81 +1445,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1849,45 +1479,28 @@ - - - - - - - - + - + - - - - - - - - - - + + - - - - - + + + @@ -1899,40 +1512,41 @@ - + - - + + - - + + + + + + + + + + - - + + - - - - - - - + - - + + - - + diff --git a/src/main/java/com/henrypump/poc/POC.java b/src/main/java/com/henrypump/poc/POC.java index 5a8a2ff..7cbe121 100644 --- a/src/main/java/com/henrypump/poc/POC.java +++ b/src/main/java/com/henrypump/poc/POC.java @@ -80,8 +80,8 @@ public class POC implements Runnable{ String dbHostname = "localhost"; - if (args.length > 3){ - dbHostname = args[3]; + if (args.length > 2){ + dbHostname = args[2]; } if (args.length == 1){ diff --git a/src/main/java/com/henrypump/poc/WebServer.java b/src/main/java/com/henrypump/poc/WebServer.java index 8713cc4..0fd376d 100644 --- a/src/main/java/com/henrypump/poc/WebServer.java +++ b/src/main/java/com/henrypump/poc/WebServer.java @@ -26,6 +26,7 @@ public class WebServer{ HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); server.createContext("/command", new CommandHandler()); server.createContext("/config", new ConfigHandler()); + server.createContext("/shake", new HandshakeHandler()); server.setExecutor(null); // creates a default executor server.start(); } @@ -182,6 +183,19 @@ public class WebServer{ os.close(); } } + + class HandshakeHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + JSONObject respJSON = new JSONObject(); + respJSON.put("shake", "shook"); + String response = respJSON.toJSONString(); + t.sendResponseHeaders(200, response.getBytes().length); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + } } diff --git a/src/main/java/com/henrypump/poc/Well.java b/src/main/java/com/henrypump/poc/Well.java index 487f64f..ed82596 100644 --- a/src/main/java/com/henrypump/poc/Well.java +++ b/src/main/java/com/henrypump/poc/Well.java @@ -27,13 +27,13 @@ import java.util.ArrayList; */ public class Well { private String wellName; - protected Simulation sim; - protected Database db; - protected WellTest wellTest; + Simulation sim; + Database db; + WellTest wellTest; /* IO */ - AnalogIn inclinometer; - AnalogIn loadCell; + private AnalogIn inclinometer; + private AnalogIn loadCell; private double currentSurfacePosition; private double currentSurfaceLoad; private double currentDownholePosition; @@ -41,7 +41,7 @@ public class Well { /* CARDS */ private Card currentCard; - public Card[] cardStorage = new Card[100]; + Card[] cardStorage = new Card[100]; // CONSTANTS private static double YM_STEEL = 30.5; @@ -67,7 +67,6 @@ public class Well { // CALCULATED TAPER PARAMETERS - private int nT1; // should always be equal to number of tapers + 1 private double frictionEstimate; private double theoreticalMaxFluidLoad; private double[] a = new double[11]; @@ -98,12 +97,12 @@ public class Well { // Statuses private volatile int runStatus; - public static final int RUNSTATUS_STOPPED = 0; - public static final int RUNSTATUS_STARTING = 1; - public static final int RUNSTATUS_RUNNING = 2; - public static final int RUNSTATUS_PUMPEDOFF = 3; - public static final int RUNSTATUS_FAULTED = 4; - public static final int RUNSTATUS_LOCKEDOUT = 5; + static final int RUNSTATUS_STOPPED = 0; + static final int RUNSTATUS_STARTING = 1; + static final int RUNSTATUS_RUNNING = 2; + static final int RUNSTATUS_PUMPEDOFF = 3; + static final int RUNSTATUS_FAULTED = 4; + static final int RUNSTATUS_LOCKEDOUT = 5; private boolean permissiveOK; private long strokesSinceStart = 0; @@ -120,7 +119,7 @@ public class Well { private int lastDirection = DIRECTION_UNKNOWN; // MODES - private volatile int runMode; + private volatile int runMode = 0; public static final int RUNMODE_POC = 0; public static final int RUNMODE_MANUAL = 1; public static final int RUNMODE_TIMER = 2; @@ -410,6 +409,19 @@ public class Well { return runMode; } + public String getRunModeString(){ + switch (runMode){ + case RUNMODE_POC: + return "POC"; + case RUNMODE_MANUAL: + return "MANUAL"; + case RUNMODE_TIMER: + return "TIMER"; + default: + return "UNKNOWN"; + } + } + public double getCurrentSurfacePosition() { return currentSurfacePosition; } @@ -681,6 +693,7 @@ public class Well { Files.delete(toFile); Files.move(fromFile, toFile); } + updateTapers(true); } catch (FileNotFoundException e) { System.out.println("No configuration file found. Pulling latest value from database."); @@ -734,11 +747,12 @@ public class Well { Object newDampingFactor = taperObj.get("dampingFactor"); if (newDampingFactor != null) setDampingFactor(currentTaperNum, (Double) newDampingFactor); } + updateTapers(false); } catch (IOException | ParseException e) { e.printStackTrace(); System.exit(3); } - updateTapers(); + } @@ -814,8 +828,8 @@ public class Well { }; - private void updateTapers(){ - nT1 = numTapers + 1; + private void updateTapers(boolean storeTapersInDB){ + int nT1 = numTapers + 1; // start by setting everything to 0 a[0] = 0.0; @@ -902,8 +916,7 @@ public class Well { frictionEstimate = dbFrictionEstimate; } - - db.storeWellSetup(this); + if (storeTapersInDB) db.storeWellSetup(this); } private double position(int p) @@ -1057,8 +1070,7 @@ public class Well { } j++; } - LPStatus downholeValues = new LPStatus(dPosition, dLoad, status); - return downholeValues; + return new LPStatus(dPosition, dLoad, status); }; public void endOfStroke(){ @@ -1116,7 +1128,7 @@ public class Well { } - public void gaugeOff(){ + void gaugeOff(){ strokeSpeed.endOfDay(); downholeGrossStroke.endOfDay(); downholeNetStroke.endOfDay(); diff --git a/www/pocwww/pocwww/__init__.py b/www/pocwww/pocwww/__init__.py index cfeede2..59a2675 100644 --- a/www/pocwww/pocwww/__init__.py +++ b/www/pocwww/pocwww/__init__.py @@ -159,5 +159,9 @@ def main(global_config, **settings): config.add_route("json_updateconfig", "/json/updateconfig") + config.add_route("json_cmd_start", "/json/cmd/start") + config.add_route("json_cmd_stop", "/json/cmd/stop") + config.add_route("json_cmd_shake", "/json/cmd/shake") + config.scan() return config.make_wsgi_app() diff --git a/www/pocwww/pocwww/json.py b/www/pocwww/pocwww/json.py index 6ef50f1..7b84e2f 100644 --- a/www/pocwww/pocwww/json.py +++ b/www/pocwww/pocwww/json.py @@ -1,6 +1,7 @@ from pyramid.view import view_config from .view_helpers import * from bson import json_util +import requests # JSON @@ -131,7 +132,7 @@ def json_updateconfig(request): t_conv_to_float = ['length', 'diameter', 'dampingFactor'] jsb = request.json_body new_config = {} - new_config['timestamp'] = datetime.now() + new_config['timestamp'] = datetime.utcnow() new_config['storedBy'] = "web" new_config['wellName'] = jsb['wellName'] new_config['tapers'] = [] @@ -147,4 +148,49 @@ def json_updateconfig(request): result = request.db['wellConfiguration'].insert(new_config) - return {'new_config': request.json_body, 'stored_result': result} + addr_obj = list(request.db['pocConfiguration'].find({"_id": "pocIPAddress"})) + address = 'localhost' + if len(addr_obj) > 0: + address = addr_obj[0]['pocIPAddress'] + + update_url = "http://{}:8000/config?update=true".format(address) + r = requests.get(update_url) + pocCmdSts = "OK" if r.status_code == 200 else "failed" + + return {'new_config': request.json_body, 'stored_result': result, 'updated': pocCmdSts} + + +@view_config(route_name="json_cmd_start", renderer="prettyjson") +def json_start(request): + addr_obj = list(request.db['pocConfiguration'].find({"_id": "pocIPAddress"})) + address = 'localhost' + if len(addr_obj) > 0: + address = addr_obj[0]['pocIPAddress'] + + start_url = "http://{}:8000/command?start=true".format(address) + r = requests.get(start_url) + return r.text if r.status_code == 200 else {"status": "failure sending command"} + + +@view_config(route_name="json_cmd_stop", renderer="prettyjson") +def json_stop(request): + addr_obj = list(request.db['pocConfiguration'].find({"_id": "pocIPAddress"})) + address = 'localhost' + if len(addr_obj) > 0: + address = addr_obj[0]['pocIPAddress'] + + stop_url = "http://{}:8000/command?stop=true".format(address) + r = requests.get(stop_url) + return r.text if r.status_code == 200 else {"status": "failure sending command"} + + +@view_config(route_name="json_cmd_shake", renderer="prettyjson") +def json_shake(request): + addr_obj = list(request.db['pocConfiguration'].find({"_id": "pocIPAddress"})) + address = 'localhost' + if len(addr_obj) > 0: + address = addr_obj[0]['pocIPAddress'] + + shake_url = "http://{}:8000/shake".format(address) + r = requests.get(shake_url) + return r.text if r.status_code == 200 else {"status": "failure sending command"} diff --git a/www/pocwww/pocwww/templates/config.jinja2 b/www/pocwww/pocwww/templates/config.jinja2 index b64533f..c2e3bc5 100644 --- a/www/pocwww/pocwww/templates/config.jinja2 +++ b/www/pocwww/pocwww/templates/config.jinja2 @@ -2,6 +2,7 @@ {% extends "layout.jinja2" %} {% block content %} +
@@ -88,10 +89,21 @@ +
- - + + + +
@@ -106,12 +118,19 @@ } }); - function printOut(data){ + function configUpdatedSuccessfully(data){ console.log(data); + if(data.stored_result){ + $('#update-success').removeClass('hidden'); + } else { + $('#update-failed').removeClass('hidden'); + } } $("#submit").click(function(event){ event.preventDefault(); + $('#update-success').addClass('hidden'); + $('#update-failed').addClass('hidden'); var newObject = {}; newObject.wellName = $("#wellName").val(); newObject.deltaT = parseFloat($("#deltaT").val()); @@ -145,7 +164,7 @@ data: JSON.stringify(newObject), contentType: "application/json; charset=utf-8", url: "/json/updateconfig", - success: printOut + success: configUpdatedSuccessfully }); }) diff --git a/www/pocwww/pocwww/templates/dashboard.jinja2 b/www/pocwww/pocwww/templates/dashboard.jinja2 index 2258f2a..1e2cffc 100644 --- a/www/pocwww/pocwww/templates/dashboard.jinja2 +++ b/www/pocwww/pocwww/templates/dashboard.jinja2 @@ -65,11 +65,14 @@ diff --git a/www/pocwww/setup.py b/www/pocwww/setup.py index 6bd60a0..219dcc1 100644 --- a/www/pocwww/setup.py +++ b/www/pocwww/setup.py @@ -14,6 +14,7 @@ requires = [ 'pyramid_debugtoolbar', 'waitress', 'pymongo', + 'requests' ] tests_require = [