implemented basic cockpit to control locos
This commit is contained in:
parent
85ff8770ef
commit
2c542ee99a
3
pom.xml
3
pom.xml
@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.5.5</version>
|
||||
<version>0.5.6</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
@ -90,7 +90,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@ -117,6 +117,7 @@ svg circle{
|
||||
padding: 5px;
|
||||
border-radius: 5px;
|
||||
background: yellow;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.window{
|
||||
|
||||
@ -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('<svg')){
|
||||
$(PLAN).append($(resp));
|
||||
} else if (resp.startsWith('<')) {
|
||||
@ -154,10 +158,6 @@ function runAction(ev){
|
||||
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){
|
||||
var data = ev.data;
|
||||
//console.log("received: ",data);
|
||||
@ -168,6 +168,10 @@ function stream(ev){
|
||||
if (data.startsWith("dropclass")) return dropClass(data.substring(10));
|
||||
}
|
||||
|
||||
function train(id,mode){
|
||||
return request({action:"train",id:id,mode:mode});
|
||||
}
|
||||
|
||||
window.onload = function () {
|
||||
var isDragging = false;
|
||||
$('.menu > 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;
|
||||
}
|
||||
|
||||
@ -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<String,Tile> tiles = new HashMap<String,Tile>();
|
||||
private HashSet<Block> blocks = new HashSet<Block>();
|
||||
private HashMap<Integer, Route> routes = new HashMap<Integer, Route>();
|
||||
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<String, String> 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<String, String> params) throws IOException {
|
||||
LOG.debug("Params: {}",params);
|
||||
switch (params.get(ACTION)) {
|
||||
|
||||
@ -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<String, Car> entry: cars.entrySet()) {
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user