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