implemented basic cockpit to control locos

This commit is contained in:
Stephan Richter
2020-10-04 20:36:36 +02:00
parent 85ff8770ef
commit 2c542ee99a
6 changed files with 101 additions and 20 deletions

View File

@@ -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()) {

View File

@@ -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