From 0c429749212cc467e795668ef7b474664c21f60d Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Tue, 28 Feb 2017 21:49:52 -0600 Subject: [PATCH] Lightweight web server for web commands, start of web config post --- .idea/workspace.xml | 177 ++++++++++++------ src/main/java/com/henrypump/poc/POC.java | 12 ++ .../java/com/henrypump/poc/WebServer.java | 85 +++++++++ www/pocwww/pocwww/__init__.py | 2 + www/pocwww/pocwww/json.py | 5 + www/pocwww/pocwww/templates/config.jinja2 | 100 +++++++++- 6 files changed, 312 insertions(+), 69 deletions(-) create mode 100644 src/main/java/com/henrypump/poc/WebServer.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 71e2364..e26b4a2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,17 +2,10 @@ - + - - - - - - - - + @@ -115,14 +108,9 @@ - - - - - - - - + + + @@ -130,17 +118,17 @@ - - + + - + - + @@ -149,6 +137,18 @@ + + + + + + + + + + + + @@ -748,8 +748,9 @@ @@ -770,7 +771,7 @@ - + @@ -783,7 +784,65 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -791,6 +850,7 @@ + @@ -1057,7 +1117,7 @@ - + @@ -1073,8 +1133,8 @@ - - + + @@ -1114,9 +1174,9 @@ - + - + @@ -1135,16 +1195,6 @@ - - - - - - - - - - @@ -1800,19 +1850,6 @@ - - - - - - - - - - - - - @@ -1829,14 +1866,6 @@ - - - - - - - - @@ -1857,7 +1886,7 @@ - + @@ -1865,6 +1894,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/henrypump/poc/POC.java b/src/main/java/com/henrypump/poc/POC.java index 556e2ae..5a8a2ff 100644 --- a/src/main/java/com/henrypump/poc/POC.java +++ b/src/main/java/com/henrypump/poc/POC.java @@ -90,9 +90,21 @@ public class POC implements Runnable{ if (args.length < 2){ final POC realPOC = new POC(dbHostname); + try { + WebServer httpServer = new WebServer(8000, realPOC); + } catch (Exception e){ + System.out.println("problem starting web server"); + System.out.println(e); + } realPOC.start(); } else { final POC simPOC = new POC(args[0], args[1].equals("true"), dbHostname); + try { + WebServer httpServer = new WebServer(8000, simPOC); + } catch (Exception e){ + System.out.println("problem starting web server"); + System.out.println(e); + } simPOC.start(); } diff --git a/src/main/java/com/henrypump/poc/WebServer.java b/src/main/java/com/henrypump/poc/WebServer.java new file mode 100644 index 0000000..436bafd --- /dev/null +++ b/src/main/java/com/henrypump/poc/WebServer.java @@ -0,0 +1,85 @@ +package com.henrypump.poc; + +import java.io.*; +import java.net.InetSocketAddress; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; +import org.json.simple.JSONObject; + + +/** + * Created by patrickjmcd on 2/28/17. + */ + + +public class WebServer{ + private POC attachedPOC; + private int port; + + WebServer(int port, POC attachedPOC) throws Exception { + this.attachedPOC = attachedPOC; + this.port = port; + HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); + server.createContext("/cmd/start", new StartHandler()); + server.createContext("/cmd/stop", new StopHandler()); + server.createContext("/sts", new StatusHandler()); + server.createContext("/cmd/updateconfig", new UpdateConfigHandler()); + server.setExecutor(null); // creates a default executor + server.start(); + } + + class StartHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + attachedPOC.thisWell.start("webserver"); + String response = attachedPOC.thisWell.getRunStatusString(); + t.sendResponseHeaders(200, response.getBytes().length); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + } + + class StopHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + attachedPOC.thisWell.stop("webserver"); + String response = attachedPOC.thisWell.getRunStatusString(); + t.sendResponseHeaders(200, response.getBytes().length); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + } + + class StatusHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + String response = attachedPOC.thisWell.getRunStatusString(); + t.sendResponseHeaders(200, response.getBytes().length); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + } + + class UpdateConfigHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + attachedPOC.thisWell.getWellSetup(); + JSONObject respJSON = new JSONObject(); + respJSON.put("status", "OK"); + String response = respJSON.toJSONString(); + t.sendResponseHeaders(200, response.getBytes().length); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + } + + +} + + diff --git a/www/pocwww/pocwww/__init__.py b/www/pocwww/pocwww/__init__.py index 87317ea..cfeede2 100644 --- a/www/pocwww/pocwww/__init__.py +++ b/www/pocwww/pocwww/__init__.py @@ -157,5 +157,7 @@ def main(global_config, **settings): config.add_route('json_singlevaluebetween', "/json/values/tag/{tagname}") config.add_route("json_singlevaluedaterange", "/json/values/tag/{tagname}/daterange") + config.add_route("json_updateconfig", "/json/updateconfig") + config.scan() return config.make_wsgi_app() diff --git a/www/pocwww/pocwww/json.py b/www/pocwww/pocwww/json.py index e8991cc..b288f1f 100644 --- a/www/pocwww/pocwww/json.py +++ b/www/pocwww/pocwww/json.py @@ -113,3 +113,8 @@ def json_runstatusnow(request): pass return {'runstatus': status} + + +@view_config(route_name="json_updateconfig", renderer="prettyjson", request_method='POST') +def json_updateconfig(request): + return {'POST': request.json_body} diff --git a/www/pocwww/pocwww/templates/config.jinja2 b/www/pocwww/pocwww/templates/config.jinja2 index 50e1e25..b64533f 100644 --- a/www/pocwww/pocwww/templates/config.jinja2 +++ b/www/pocwww/pocwww/templates/config.jinja2 @@ -19,7 +19,7 @@
- +
@@ -29,40 +29,124 @@
- +
- +
- +
- +
- +
- +
- +

Taper Setup

+
+ + + + + + + + + + + + + {% for taper in config.tapers %} + + + + + + + + + {% endfor %} + + +
TaperLengthDiameterMaterialDamping Factor
{{loop.index}}
+ + + +
+ + {% endblock content %}