implemented locomotive functions
This commit is contained in:
@@ -156,7 +156,7 @@ function request(data){
|
||||
method : POST,
|
||||
data : data,
|
||||
success: function(resp){
|
||||
if (data.realm != 'car') closeWindows();
|
||||
if (data.realm != 'car' && data.realm != 'loco') closeWindows();
|
||||
if (resp.startsWith('<svg')){
|
||||
$(PLAN).append($(resp));
|
||||
} else if (resp.startsWith('<')) {
|
||||
|
||||
@@ -66,7 +66,7 @@ public class Application implements Constants{
|
||||
case REALM_CU:
|
||||
return plan.controlUnit().process(params);
|
||||
case REALM_LOCO:
|
||||
return Locomotive.action(params);
|
||||
return Locomotive.action(params,plan);
|
||||
case REALM_PLAN:
|
||||
return plan.action(params);
|
||||
case REALM_ROUTE:
|
||||
|
||||
@@ -3,9 +3,6 @@ package de.srsoftware.web4rail;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public interface Constants {
|
||||
public static final String ACTION = "action";
|
||||
public static final String ACTION_ADD = "add";
|
||||
@@ -22,6 +19,10 @@ public interface Constants {
|
||||
public static final String ACTION_SLOWER10 = "slower10";
|
||||
public static final String ACTION_START = "start";
|
||||
public static final String ACTION_STOP = "stop";
|
||||
public static final String ACTION_TOGGLE_F1 = "f1";
|
||||
public static final String ACTION_TOGGLE_F2 = "f2";
|
||||
public static final String ACTION_TOGGLE_F3 = "f3";
|
||||
public static final String ACTION_TOGGLE_F4 = "f4";
|
||||
public static final String ACTION_TURN = "turn";
|
||||
public static final String ACTION_UPDATE = "update";
|
||||
|
||||
|
||||
@@ -137,6 +137,8 @@ public class ControlUnit extends Thread implements Constants{
|
||||
case ACTION_CONNECT:
|
||||
restart();
|
||||
return t("Control unit (re)started.");
|
||||
case ACTION_EMERGENCY:
|
||||
power = true;
|
||||
case ACTION_POWER:
|
||||
return togglePower();
|
||||
case ACTION_PROPS:
|
||||
|
||||
@@ -69,7 +69,7 @@ public class Car implements Constants {
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
protected Tag cockpit(String realm) {
|
||||
protected Tag cockpit() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ public class Car implements Constants {
|
||||
public Object properties() {
|
||||
Window win = new Window("car-props", t("Properties of {}",this));
|
||||
|
||||
Tag cockpit = cockpit("car");
|
||||
Tag cockpit = cockpit();
|
||||
if (cockpit != null) cockpit.addTo(win);
|
||||
|
||||
Tag form = propertyForm();
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.json.JSONObject;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
@@ -22,10 +23,12 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
|
||||
private static final String REVERSE = "reverse";
|
||||
public static final String LOCOMOTIVE = "locomotive";
|
||||
private static final int VMAX = 128;
|
||||
private boolean reverse = false;
|
||||
private Protocol proto = Protocol.DCC128;
|
||||
private int address = 3;
|
||||
private int speed = 0;
|
||||
private boolean f1,f2,f3,f4;
|
||||
private boolean init = false;
|
||||
|
||||
public Locomotive(String name) {
|
||||
@@ -36,12 +39,12 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
super(name,id);
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params) throws IOException {
|
||||
public static Object action(HashMap<String, String> params, Plan plan) throws IOException {
|
||||
String id = params.get(ID);
|
||||
Locomotive loco = id == null ? null : Locomotive.get(id);
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD:
|
||||
return new Locomotive(params.get(Locomotive.NAME));
|
||||
return new Locomotive(params.get(Locomotive.NAME)).plan(plan);
|
||||
case ACTION_FASTER10:
|
||||
return loco.faster(10);
|
||||
case ACTION_PROPS:
|
||||
@@ -50,6 +53,14 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
return loco.faster(-10);
|
||||
case ACTION_STOP:
|
||||
return loco.stop();
|
||||
case ACTION_TOGGLE_F1:
|
||||
return loco.toggleFunction(1);
|
||||
case ACTION_TOGGLE_F2:
|
||||
return loco.toggleFunction(2);
|
||||
case ACTION_TOGGLE_F3:
|
||||
return loco.toggleFunction(3);
|
||||
case ACTION_TOGGLE_F4:
|
||||
return loco.toggleFunction(4);
|
||||
case ACTION_TURN:
|
||||
return loco.turn();
|
||||
}
|
||||
@@ -57,16 +68,53 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
protected Tag cockpit(String realm) {
|
||||
private Object toggleFunction(int f) {
|
||||
boolean active;
|
||||
switch (f) {
|
||||
case 1:
|
||||
f1 =! f1;
|
||||
active = f1;
|
||||
break;
|
||||
case 2:
|
||||
f2 =! f2;
|
||||
active = f2;
|
||||
break;
|
||||
case 3:
|
||||
f3 =! f3;
|
||||
active = f3;
|
||||
break;
|
||||
case 4:
|
||||
f4 =! f4;
|
||||
active = f4;
|
||||
break;
|
||||
default:
|
||||
return t("Unknown function: {}",f);
|
||||
}
|
||||
queue();
|
||||
return t("{} F{}",t(active?"Activated":"Deavtivated"),f);
|
||||
}
|
||||
|
||||
protected Tag cockpit() {
|
||||
Fieldset fieldset = new Fieldset(t("Control"));
|
||||
String request = "return request({realm:'"+realm+"',id:"+id()+",action:'{}'})";
|
||||
new Button(t("Turn"), request.replace("{}", ACTION_TURN)) .addTo(fieldset);
|
||||
String request = "return request({realm:'"+REALM_LOCO+"',id:"+id()+",action:'{}'})";
|
||||
new Button(t("Turn"), request.replace("{}", ACTION_TURN)).addTo(fieldset);
|
||||
new Button(t("Faster (10 steps)"), request.replace("{}", ACTION_FASTER10)).addTo(fieldset);
|
||||
new Button(t("Slower (10 steps)"), request.replace("{}", ACTION_SLOWER10)).addTo(fieldset);
|
||||
new Button(t("Stop"), request.replace("{}", ACTION_STOP)).addTo(fieldset);
|
||||
Tag span = new Tag("p");
|
||||
new Button(t("F1"),request.replace("{}", ACTION_TOGGLE_F1)).addTo(span);
|
||||
new Button(t("F2"),request.replace("{}", ACTION_TOGGLE_F2)).addTo(span);
|
||||
new Button(t("F3"),request.replace("{}", ACTION_TOGGLE_F3)).addTo(span);
|
||||
new Button(t("F4"),request.replace("{}", ACTION_TOGGLE_F4)).addTo(span);
|
||||
span.addTo(fieldset);
|
||||
return fieldset;
|
||||
}
|
||||
|
||||
private String detail() {
|
||||
return getClass().getSimpleName()+"("+name()+", "+proto+", "+address+")";
|
||||
}
|
||||
|
||||
|
||||
public Object faster(int steps) {
|
||||
return setSpeed(speed + steps);
|
||||
}
|
||||
@@ -160,6 +208,12 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
@Override
|
||||
public Tag propertyForm() {
|
||||
Tag form = super.propertyForm();
|
||||
for (Tag tag : form.children()) {
|
||||
if (REALM.equals(tag.get(Input.NAME)) && REALM_CAR.equals(tag.get(Input.VALUE))) {
|
||||
tag.attr(REALM, REALM_LOCO);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Fieldset fieldset = new Fieldset("Decoder settings");
|
||||
Label protocol = new Label(t("Protocol:"));
|
||||
for (Protocol proto : Protocol.values()) {
|
||||
@@ -170,14 +224,19 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
fieldset.addTo(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
private void queue() {
|
||||
plan.queue("SET {} GL "+address+" "+(reverse?1:0)+" "+speed+" "+VMAX+" "+(f1?1:0)+" "+(f2?1:0)+" "+(f3?1:0)+" "+(f4?1:0));
|
||||
}
|
||||
|
||||
public String setSpeed(int newSpeed) {
|
||||
LOG.debug(this.detail()+".setSpeed({})",newSpeed);
|
||||
init();
|
||||
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");
|
||||
queue();
|
||||
return t("Speed of {} set to {}.",this,speed);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ import de.srsoftware.tools.Tag;
|
||||
public class Input extends Tag{
|
||||
|
||||
private static final long serialVersionUID = -330127933233033028L;
|
||||
public static final String NAME = "name";
|
||||
public static final String VALUE = "value";
|
||||
|
||||
public Input(String name) {
|
||||
super("input");
|
||||
@@ -13,7 +15,7 @@ public class Input extends Tag{
|
||||
|
||||
public Input(String name, Object value) {
|
||||
super("input");
|
||||
attr("type","text").attr("name", name).attr("value", value.toString());
|
||||
attr("type","text").attr(NAME, name).attr(VALUE, value.toString());
|
||||
}
|
||||
|
||||
public Tag hideIn(Tag form) {
|
||||
|
||||
Reference in New Issue
Block a user