From 2c542ee99a72e1b68118217f7d5f2dd85c996491 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sun, 4 Oct 2020 20:36:36 +0200 Subject: [PATCH] implemented basic cockpit to control locos --- pom.xml | 3 +- resources/css/style.css | 1 + resources/js/plan.js | 17 ++++--- .../java/de/srsoftware/web4rail/Plan.java | 49 ++++++++++++++++--- .../de/srsoftware/web4rail/moving/Car.java | 9 +++- .../web4rail/moving/Locomotive.java | 42 ++++++++++++++-- 6 files changed, 101 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index c10ea83..bb93f28 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 de.srsoftware web4rail - 0.5.5 + 0.5.6 Web4Rail jar Java Model Railway Control @@ -90,7 +90,6 @@ - diff --git a/resources/css/style.css b/resources/css/style.css index 236acb7..b48d714 100644 --- a/resources/css/style.css +++ b/resources/css/style.css @@ -117,6 +117,7 @@ svg circle{ padding: 5px; border-radius: 5px; background: yellow; + z-index: 100; } .window{ diff --git a/resources/js/plan.js b/resources/js/plan.js index b368254..7804ec7 100644 --- a/resources/js/plan.js +++ b/resources/js/plan.js @@ -16,7 +16,7 @@ function addClass(data){ } function addMessage(txt){ - $('#messages').html(txt).show().delay(5000).fadeOut(5000); + $('#messages').html(txt).show().delay(2000).fadeOut(1); } function addTile(x,y){ @@ -90,6 +90,10 @@ function heartbeat(data){ return false; } +function keypress(ev){ + if (ev.key === "Escape") request({realm:"cu",action:"emergency"}) +} + function moveTile(x,y){ var id = x+"-"+y; return request({action:mode,direction:selected.id,id:id}); @@ -135,7 +139,7 @@ function request(data){ method : POST, data : data, success: function(resp){ - closeWindows(); + if (data.realm != 'car') closeWindows(); if (resp.startsWith(' div').click(closeMenu); @@ -177,5 +181,6 @@ window.onload = function () { $('.menu .trains .list > div').click(runAction); $('.menu .hardware .list > div').click(runAction); $(PLAN).click(planClick); + $(document).keyup(keypress); (new EventSource("stream")).onmessage = stream; } diff --git a/src/main/java/de/srsoftware/web4rail/Plan.java b/src/main/java/de/srsoftware/web4rail/Plan.java index 2d2b525..54eafcc 100644 --- a/src/main/java/de/srsoftware/web4rail/Plan.java +++ b/src/main/java/de/srsoftware/web4rail/Plan.java @@ -119,22 +119,30 @@ public class Plan { public static final String REALM_TILE = "tile"; public static final String REALM_CAR = "car"; private static final String ACTION_CONNECT = "connect"; + private static final String ACTION_POWER = "power"; + private static final String ACTION_EMERGENCY = "emergency"; + public static final String ACTION_FASTER10 = "faster10"; + public static final String ACTION_SLOWER10 = "slower10"; + public static final String ACTION_STOP = "stop"; + public static final String ACTION_TURN = "turn"; public HashMap tiles = new HashMap(); private HashSet blocks = new HashSet(); private HashMap routes = new HashMap(); private ControlUnit controlUnit = new ControlUnit(); + private boolean power = false; public Plan() { new Heartbeat().start(); } private Tag actionMenu() throws IOException { - Tag tileMenu = new Tag("div").clazz("actions").content(t("Actions")); - Tag tiles = new Tag("div").clazz("list").content(""); - new Div("save").content(t("Save plan")).addTo(tiles); - new Div("analyze").content(t("Analyze plan")).addTo(tiles); - return tiles.addTo(tileMenu); + Tag actionMenu = new Tag("div").clazz("actions").content(t("Actions")); + Tag actions = new Tag("div").clazz("list").content(""); + new Div("power").content(t("Toggle power")).addTo(actions); + new Div("save").content(t("Save plan")).addTo(actions); + new Div("analyze").content(t("Analyze plan")).addTo(actions); + return actions.addTo(actionMenu); } public void addClient(OutputStreamWriter client) { @@ -312,10 +320,19 @@ public class Plan { } private Object locoAction(HashMap params) throws IOException { + switch (params.get(ACTION)) { case ACTION_ADD_LOCO: new Locomotive(params.get(Locomotive.NAME)); break; + case ACTION_FASTER10: + return Locomotive.get(params.get(ID)).faster(10); + case ACTION_SLOWER10: + return Locomotive.get(params.get(ID)).faster(-10); + case ACTION_STOP: + return Locomotive.get(params.get(ID)).stop(); + case ACTION_TURN: + return Locomotive.get(params.get(ID)).turn(); } return html(); @@ -413,10 +430,16 @@ public class Plan { case ACTION_ADD: return addTile(params.get(TILE),params.get(X),params.get(Y),null); case ACTION_ADD_LOCO: + case ACTION_FASTER10: + case ACTION_STOP: + case ACTION_SLOWER10: + case ACTION_TURN: return locoAction(params); case ACTION_ADD_TRAIN: case ACTION_TRAIN: return trainAction(params); + case ACTION_ANALYZE: + return analyze(); case ACTION_CAR: return carAction(params); case ACTION_CLICK: @@ -424,13 +447,16 @@ public class Plan { case ACTION_CONNECT: return connect(params); case ACTION_CU_PROPS: - return cuProps(params); - case ACTION_ANALYZE: - return analyze(); + return cuProps(params); case ACTION_LOCOS: return Locomotive.manager(); + case ACTION_EMERGENCY: + power = true; + return togglePower(); case ACTION_MOVE: return moveTile(params.get(DIRECTION),params.get(Tile.ID)); + case ACTION_POWER: + return togglePower(); case ACTION_ROUTE: return routeProperties(Integer.parseInt(params.get(ID))); case ACTION_SAVE: @@ -451,6 +477,13 @@ public class Plan { } } + private Object togglePower() throws IOException { + power = !power; + String PW = power?"ON":"OFF"; + queue("SET {} POWER "+PW); + return t("Turned power {}.",PW); + } + private Object trainAction(HashMap params) throws IOException { LOG.debug("Params: {}",params); switch (params.get(ACTION)) { diff --git a/src/main/java/de/srsoftware/web4rail/moving/Car.java b/src/main/java/de/srsoftware/web4rail/moving/Car.java index 481723b..fd89509 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Car.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Car.java @@ -57,6 +57,10 @@ public class Car { cars.put(id, this); } + protected Tag cockpit(String realm) { + return null; + } + public static Car get(String nameOrId) { Car car = cars.get(nameOrId); // try to get by id if (car == null) { // try to get by name @@ -133,6 +137,9 @@ public class Car { public Object properties() { Window win = new Window("car-props", t("Properties of {}",this)); + Tag cockpit = cockpit("car"); + if (cockpit != null) cockpit.addTo(win); + Tag form = propertyForm(); if (form!=null && form.children().size()>2) { new Button(t("save")).addTo(form); @@ -148,7 +155,7 @@ public class Car { list.addTo(win); return win; } - + public static void saveAll(String filename) throws IOException { BufferedWriter file = new BufferedWriter(new FileWriter(filename)); for (Entry entry: cars.entrySet()) { diff --git a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java index e7d3668..a7ba868 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java @@ -24,6 +24,7 @@ public class Locomotive extends Car implements Device{ private boolean reverse = false; private Protocol proto = Protocol.DCC128; private int address = 3; + private int speed = 0; private boolean init = false; public Locomotive(String name) { @@ -34,6 +35,26 @@ public class Locomotive extends Car implements Device{ super(name,id); } + protected Tag cockpit(String realm) { + Fieldset fieldset = new Fieldset(t("Control")); + String request = "return request({realm:'"+realm+"',id:"+id()+",action:'{}'})"; + new Button(t("Turn"), request.replace("{}", Plan.ACTION_TURN)) .addTo(fieldset); + new Button(t("Faster (10 steps)"), request.replace("{}", Plan.ACTION_FASTER10)).addTo(fieldset); + new Button(t("Slower (10 steps)"), request.replace("{}", Plan.ACTION_SLOWER10)).addTo(fieldset); + new Button(t("Stop"), request.replace("{}", Plan.ACTION_STOP)).addTo(fieldset); + return fieldset; + } + + public Object faster(int steps) { + return setSpeed(speed + steps); + } + + public static Locomotive get(String nameOrId) { + Car car = Car.get(nameOrId); + if (car instanceof Locomotive) return (Locomotive) car; + return null; + } + private void init() { if (init) return; String proto = null; @@ -127,10 +148,25 @@ public class Locomotive extends Car implements Device{ return form; } - public void setSpeed(int v) { + public String setSpeed(int newSpeed) { init(); - plan.queue("SET {} GL "+address+" "+(reverse?1:0)+" "+v+" 128 0 0 0 0 0"); - LOG.debug("{}.setSpeed({})",this,v); + speed = newSpeed; + if (speed > 128) speed = 128; + if (speed < 0) speed = 0; + + plan.queue("SET {} GL "+address+" "+(reverse?1:0)+" "+speed+" 128 0 0 0 0 0"); + return t("Speed of {} set to {}.",this,speed); + } + + public Object stop() { + setSpeed(0); + return t("Stopped {}",this); + } + + public Object turn() { + reverse = !reverse; + stop(); + return t("Stopped and reversed {}.",this); } @Override