From aab7537972cd777bff2d9ed18aff01cd2ba1c84d Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Wed, 8 Mar 2017 10:01:03 -0600 Subject: [PATCH] Adds ability to change mode from website --- .idea/workspace.xml | 129 ++++++------------ pocwww/pocwww/__init__.py | 1 + pocwww/pocwww/json.py | 21 +++ pocwww/pocwww/templates/setpoints.jinja2 | 54 ++++++++ .../java/com/henrypump/poc/WebServer.java | 6 +- src/main/java/com/henrypump/poc/Well.java | 18 ++- 6 files changed, 134 insertions(+), 95 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a40ee6c..a6fb124 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,9 +4,8 @@ - - - + + @@ -138,10 +137,12 @@ - - + + + + @@ -274,30 +275,11 @@ - + - - - - - - - - - - - - - - - - - - - - - + + @@ -346,6 +328,7 @@ index fluidGasRatio setupFluidRatio + startupStrokes @@ -778,7 +761,7 @@ @@ -1168,7 +1151,7 @@ - + @@ -1252,27 +1235,8 @@ - - - - - - - - - - - - - - - - - - - - - + + @@ -1407,6 +1371,8 @@ + + @@ -1814,14 +1780,6 @@ - - - - - - - - @@ -1904,36 +1862,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1960,12 +1888,33 @@ + + + + + + + + + + + + + + + + + + + - - + + + + diff --git a/pocwww/pocwww/__init__.py b/pocwww/pocwww/__init__.py index 9ca1c0b..fd2a0dd 100644 --- a/pocwww/pocwww/__init__.py +++ b/pocwww/pocwww/__init__.py @@ -159,6 +159,7 @@ def main(global_config, **settings): config.add_route('config', '/config', factory='pocwww.security.UserLoginFactory') config.add_route('json_setpoints', '/json/setpoints', factory='pocwww.security.UserLoginFactory') + config.add_route('json_mode', '/json/mode', factory='pocwww.security.UserLoginFactory') config.add_route('setpoints', '/setpoints', factory='pocwww.security.UserLoginFactory') config.add_route('admin', '/admin', factory='pocwww.security.UserLoginFactory') diff --git a/pocwww/pocwww/json.py b/pocwww/pocwww/json.py index ff5f395..d4b7f42 100644 --- a/pocwww/pocwww/json.py +++ b/pocwww/pocwww/json.py @@ -270,3 +270,24 @@ def json_setpoints_post(request): return {"updated": list(request.db['setpoints'].find_one({"name": name})), 'status': update_status} except KeyError: return {"status": "bad reqest"} + + +runModes = {0: 'poc', 1: 'manual', 2: 'timer'} + + +@view_config(route_name="json_mode", renderer="prettyjson", request_method='POST', permission='edit') +def json_mode_post(request): + jsb = request.json_body + try: + mode = jsb['mode'] + upsert = request.db['setpoints'].update_one({"name": "runMode"}, {"$set": {'value': mode, 'storedBy': request.authenticated_userid, 'lastStored': datetime.utcnow()}}, upsert=True) + + address = get_poc_address(request) or 'localhost' + url = "http://{}:8000/mode?mode={}&user={}".format(address, runModes[mode], request.authenticated_userid) + print(url) + r = requests.get(url) + update_status = r.text if r.status_code == 200 else {"status": "failure sending command"} + + return {"updated": request.db['setpoints'].find_one({"name": "runMode"}), 'status': update_status} + except KeyError: + return {"status": "bad reqest"} diff --git a/pocwww/pocwww/templates/setpoints.jinja2 b/pocwww/pocwww/templates/setpoints.jinja2 index dc1bdf2..3cd6512 100644 --- a/pocwww/pocwww/templates/setpoints.jinja2 +++ b/pocwww/pocwww/templates/setpoints.jinja2 @@ -5,6 +5,16 @@
+
+
+

Run Mode: Loading...

+ + + +

+

Set by Loading... at Loading... +

+

POC Setpoints

@@ -30,8 +40,19 @@ {% endblock content %} diff --git a/src/main/java/com/henrypump/poc/WebServer.java b/src/main/java/com/henrypump/poc/WebServer.java index a5af539..897faee 100644 --- a/src/main/java/com/henrypump/poc/WebServer.java +++ b/src/main/java/com/henrypump/poc/WebServer.java @@ -213,13 +213,13 @@ public class WebServer{ if ((user.length() > 0) && (action.length() > 0)) { switch (action) { case "manual": - attachedPOC.thisWell.setRunMode("MANUAL"); + attachedPOC.thisWell.setRunModeString("MANUAL"); break; case "poc": - attachedPOC.thisWell.setRunMode("POC"); + attachedPOC.thisWell.setRunModeString("POC"); break; case "timer": - attachedPOC.thisWell.setRunMode("TIMER"); + attachedPOC.thisWell.setRunModeString("TIMER"); break; default: respJSON.put(action, "not implemented"); diff --git a/src/main/java/com/henrypump/poc/Well.java b/src/main/java/com/henrypump/poc/Well.java index db589e1..707a09d 100644 --- a/src/main/java/com/henrypump/poc/Well.java +++ b/src/main/java/com/henrypump/poc/Well.java @@ -106,7 +106,7 @@ public class Well { private boolean permissiveOK; private long strokesSinceStart = 0; - private long startupStrokes = 1; + private long startupStrokes = 10; private long strokesToday = 0; private long strokesLifetime; private int pointCounter =0; @@ -305,6 +305,16 @@ public class Well { } else { fluidGasRatio = dbFluidGasRatio; } + + double dbRunMode = db.getSetpoint("runMode"); + if (dbRunMode == Double.MIN_VALUE){ + runMode = RUNMODE_MANUAL; // DEFAULT VALUE + System.out.println("Using default value of " + runMode + " for runMode."); + db.storeSetpoint("runMode", (double) runMode); + } else { + setRunMode((int) dbRunMode); + System.out.println("Run Mode set to " + getRunModeString()); + } } private void initializeMeasurements(){ @@ -521,7 +531,7 @@ public class Well { } } - public void setRunMode(String runModeString){ + public void setRunModeString(String runModeString){ switch (runModeString){ case "POC": runMode = RUNMODE_POC; @@ -540,6 +550,10 @@ public class Well { } } + public void setRunMode(int runMode) { + this.runMode = runMode; + } + public double getCurrentSurfacePosition() { return currentSurfacePosition; }