Browse Source

implemented basic cockpit to control locos

lookup-tables
Stephan Richter 5 years ago
parent
commit
2c542ee99a
  1. 3
      pom.xml
  2. 1
      resources/css/style.css
  3. 17
      resources/js/plan.js
  4. 47
      src/main/java/de/srsoftware/web4rail/Plan.java
  5. 7
      src/main/java/de/srsoftware/web4rail/moving/Car.java
  6. 42
      src/main/java/de/srsoftware/web4rail/moving/Locomotive.java

3
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId> <groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId> <artifactId>web4rail</artifactId>
<version>0.5.5</version> <version>0.5.6</version>
<name>Web4Rail</name> <name>Web4Rail</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Java Model Railway Control</description> <description>Java Model Railway Control</description>
@ -90,7 +90,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

1
resources/css/style.css

@ -117,6 +117,7 @@ svg circle{
padding: 5px; padding: 5px;
border-radius: 5px; border-radius: 5px;
background: yellow; background: yellow;
z-index: 100;
} }
.window{ .window{

17
resources/js/plan.js

@ -16,7 +16,7 @@ function addClass(data){
} }
function addMessage(txt){ function addMessage(txt){
$('#messages').html(txt).show().delay(5000).fadeOut(5000); $('#messages').html(txt).show().delay(2000).fadeOut(1);
} }
function addTile(x,y){ function addTile(x,y){
@ -90,6 +90,10 @@ function heartbeat(data){
return false; return false;
} }
function keypress(ev){
if (ev.key === "Escape") request({realm:"cu",action:"emergency"})
}
function moveTile(x,y){ function moveTile(x,y){
var id = x+"-"+y; var id = x+"-"+y;
return request({action:mode,direction:selected.id,id:id}); return request({action:mode,direction:selected.id,id:id});
@ -135,7 +139,7 @@ function request(data){
method : POST, method : POST,
data : data, data : data,
success: function(resp){ success: function(resp){
closeWindows(); if (data.realm != 'car') closeWindows();
if (resp.startsWith('<svg')){ if (resp.startsWith('<svg')){
$(PLAN).append($(resp)); $(PLAN).append($(resp));
} else if (resp.startsWith('<')) { } else if (resp.startsWith('<')) {
@ -154,10 +158,6 @@ function runAction(ev){
return request({action:ev.target.id,file:'default'}); // TODO: ask for name return request({action:ev.target.id,file:'default'}); // TODO: ask for name
} }
function train(id,mode){
return request({action:"train",id:id,mode:mode});
}
function stream(ev){ function stream(ev){
var data = ev.data; var data = ev.data;
//console.log("received: ",data); //console.log("received: ",data);
@ -168,6 +168,10 @@ function stream(ev){
if (data.startsWith("dropclass")) return dropClass(data.substring(10)); if (data.startsWith("dropclass")) return dropClass(data.substring(10));
} }
function train(id,mode){
return request({action:"train",id:id,mode:mode});
}
window.onload = function () { window.onload = function () {
var isDragging = false; var isDragging = false;
$('.menu > div').click(closeMenu); $('.menu > div').click(closeMenu);
@ -177,5 +181,6 @@ window.onload = function () {
$('.menu .trains .list > div').click(runAction); $('.menu .trains .list > div').click(runAction);
$('.menu .hardware .list > div').click(runAction); $('.menu .hardware .list > div').click(runAction);
$(PLAN).click(planClick); $(PLAN).click(planClick);
$(document).keyup(keypress);
(new EventSource("stream")).onmessage = stream; (new EventSource("stream")).onmessage = stream;
} }

47
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_TILE = "tile";
public static final String REALM_CAR = "car"; public static final String REALM_CAR = "car";
private static final String ACTION_CONNECT = "connect"; 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<String,Tile> tiles = new HashMap<String,Tile>(); public HashMap<String,Tile> tiles = new HashMap<String,Tile>();
private HashSet<Block> blocks = new HashSet<Block>(); private HashSet<Block> blocks = new HashSet<Block>();
private HashMap<Integer, Route> routes = new HashMap<Integer, Route>(); private HashMap<Integer, Route> routes = new HashMap<Integer, Route>();
private ControlUnit controlUnit = new ControlUnit(); private ControlUnit controlUnit = new ControlUnit();
private boolean power = false;
public Plan() { public Plan() {
new Heartbeat().start(); new Heartbeat().start();
} }
private Tag actionMenu() throws IOException { private Tag actionMenu() throws IOException {
Tag tileMenu = new Tag("div").clazz("actions").content(t("Actions")); Tag actionMenu = new Tag("div").clazz("actions").content(t("Actions"));
Tag tiles = new Tag("div").clazz("list").content(""); Tag actions = new Tag("div").clazz("list").content("");
new Div("save").content(t("Save plan")).addTo(tiles); new Div("power").content(t("Toggle power")).addTo(actions);
new Div("analyze").content(t("Analyze plan")).addTo(tiles); new Div("save").content(t("Save plan")).addTo(actions);
return tiles.addTo(tileMenu); new Div("analyze").content(t("Analyze plan")).addTo(actions);
return actions.addTo(actionMenu);
} }
public void addClient(OutputStreamWriter client) { public void addClient(OutputStreamWriter client) {
@ -312,10 +320,19 @@ public class Plan {
} }
private Object locoAction(HashMap<String, String> params) throws IOException { private Object locoAction(HashMap<String, String> params) throws IOException {
switch (params.get(ACTION)) { switch (params.get(ACTION)) {
case ACTION_ADD_LOCO: case ACTION_ADD_LOCO:
new Locomotive(params.get(Locomotive.NAME)); new Locomotive(params.get(Locomotive.NAME));
break; 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(); return html();
@ -413,10 +430,16 @@ public class Plan {
case ACTION_ADD: case ACTION_ADD:
return addTile(params.get(TILE),params.get(X),params.get(Y),null); return addTile(params.get(TILE),params.get(X),params.get(Y),null);
case ACTION_ADD_LOCO: case ACTION_ADD_LOCO:
case ACTION_FASTER10:
case ACTION_STOP:
case ACTION_SLOWER10:
case ACTION_TURN:
return locoAction(params); return locoAction(params);
case ACTION_ADD_TRAIN: case ACTION_ADD_TRAIN:
case ACTION_TRAIN: case ACTION_TRAIN:
return trainAction(params); return trainAction(params);
case ACTION_ANALYZE:
return analyze();
case ACTION_CAR: case ACTION_CAR:
return carAction(params); return carAction(params);
case ACTION_CLICK: case ACTION_CLICK:
@ -425,12 +448,15 @@ public class Plan {
return connect(params); return connect(params);
case ACTION_CU_PROPS: case ACTION_CU_PROPS:
return cuProps(params); return cuProps(params);
case ACTION_ANALYZE:
return analyze();
case ACTION_LOCOS: case ACTION_LOCOS:
return Locomotive.manager(); return Locomotive.manager();
case ACTION_EMERGENCY:
power = true;
return togglePower();
case ACTION_MOVE: case ACTION_MOVE:
return moveTile(params.get(DIRECTION),params.get(Tile.ID)); return moveTile(params.get(DIRECTION),params.get(Tile.ID));
case ACTION_POWER:
return togglePower();
case ACTION_ROUTE: case ACTION_ROUTE:
return routeProperties(Integer.parseInt(params.get(ID))); return routeProperties(Integer.parseInt(params.get(ID)));
case ACTION_SAVE: 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<String, String> params) throws IOException { private Object trainAction(HashMap<String, String> params) throws IOException {
LOG.debug("Params: {}",params); LOG.debug("Params: {}",params);
switch (params.get(ACTION)) { switch (params.get(ACTION)) {

7
src/main/java/de/srsoftware/web4rail/moving/Car.java

@ -57,6 +57,10 @@ public class Car {
cars.put(id, this); cars.put(id, this);
} }
protected Tag cockpit(String realm) {
return null;
}
public static Car get(String nameOrId) { public static Car get(String nameOrId) {
Car car = cars.get(nameOrId); // try to get by id Car car = cars.get(nameOrId); // try to get by id
if (car == null) { // try to get by name if (car == null) { // try to get by name
@ -133,6 +137,9 @@ public class Car {
public Object properties() { public Object properties() {
Window win = new Window("car-props", t("Properties of {}",this)); Window win = new Window("car-props", t("Properties of {}",this));
Tag cockpit = cockpit("car");
if (cockpit != null) cockpit.addTo(win);
Tag form = propertyForm(); Tag form = propertyForm();
if (form!=null && form.children().size()>2) { if (form!=null && form.children().size()>2) {
new Button(t("save")).addTo(form); new Button(t("save")).addTo(form);

42
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 boolean reverse = false;
private Protocol proto = Protocol.DCC128; private Protocol proto = Protocol.DCC128;
private int address = 3; private int address = 3;
private int speed = 0;
private boolean init = false; private boolean init = false;
public Locomotive(String name) { public Locomotive(String name) {
@ -34,6 +35,26 @@ public class Locomotive extends Car implements Device{
super(name,id); 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() { private void init() {
if (init) return; if (init) return;
String proto = null; String proto = null;
@ -127,10 +148,25 @@ public class Locomotive extends Car implements Device{
return form; return form;
} }
public void setSpeed(int v) { public String setSpeed(int newSpeed) {
init(); init();
plan.queue("SET {} GL "+address+" "+(reverse?1:0)+" "+v+" 128 0 0 0 0 0"); speed = newSpeed;
LOG.debug("{}.setSpeed({})",this,v); 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 @Override

Loading…
Cancel
Save