added WaitForContact action

This commit is contained in:
Stephan Richter
2021-01-15 10:50:44 +01:00
parent 703820b38f
commit c825268b5f
5 changed files with 157 additions and 2 deletions

View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>1.3.16</version>
<version>1.3.17</version>
<name>Web4Rail</name>
<packaging>jar</packaging>
<description>Java Model Railway Control</description>

View File

@@ -189,6 +189,7 @@ On : An
One of : eine von
One way : Richtung
Online Documentation : Online-Dokumentation
On timeout (after {} ms) : Bei Zeitüberschreitung (nach {} ms)
open other plan : anderen Plan öffnen
Open plan... : Plan öffnen...
Operating System : Betriebssystem
@@ -338,6 +339,8 @@ turn within train : innerhalb des Zugs drehen
Turns the train, as if it went through a loop. : Dreht den ZUg, als wenn er eine Wendeschleife passiert hätte.
Unknown action\: {} : Unbekannte Aktion: {}
unset : ungesetzt
WaitForContact : Auf Kontakt warten
Wait for {}, then : auf {} warten, dann
Wait {} ms, then\: : {} ms warten, dann:
{} waiting {} secs... : {} wartet {} Sekunden...
Wait times : Wartezeiten

View File

@@ -66,7 +66,8 @@ public abstract class Action extends BaseClass {
StartStopAuto.class,
StopTrain.class,
SwitchFunction.class,
TriggerContact.class
TriggerContact.class,
WaitForContact.class
);
}

View File

@@ -0,0 +1,134 @@
package de.srsoftware.web4rail.actions;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tags.Fieldset;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tiles.Contact;
import de.srsoftware.web4rail.tiles.Contact.Listener;
public class WaitForContact extends ActionList {
private static final String TIMEOUT = "timeout";
private static final String TIMEOUT_ACTIONS = "timeout_actions";
ActionList timeoutActions = new ActionList(this);
boolean fired = false;
public WaitForContact(BaseClass parent) {
super(parent);
}
private Contact contact = null;
private int timeout = 10000;
@Override
public boolean fire(Context context) {
LOG.debug("{}.fire(...) called, waiting for {}.",this,contact);
if (isNull(contact)) return false;
fired = false;
Listener listener = new Listener() {
@Override
public void fired() {
LOG.debug("{} triggered, firing {}",contact,actions);
fired = true;
contact.removeListener(this);
WaitForContact.super.fire(context);
}
};
contact.addListener(listener);
Application.threadPool.execute(new Thread() { // remove listener after timout
@Override
public void run() {
try {
sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
contact.removeListener(listener);
LOG.debug("{} timed out, firing {}",this,timeoutActions);
if (!fired) timeoutActions.fire(context);
}
});
return true;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
if (isSet(contact)) json.put(CONTACT, contact.id());
json.put(TIMEOUT, timeout);
if (!timeoutActions.isEmpty()) {
json.put(TIMEOUT_ACTIONS, timeoutActions.json());
}
return json;
}
@Override
public Tag list() {
Tag list = super.list();
for (Tag child : list.children()) {
if (child.is("ol")) {
break;
}
}
timeoutActions.list().addTo(new Tag("span").content(t("On timeout (after {} ms)",timeout)+":")).addTo(list);
return list;
}
@Override
public Action load(JSONObject json) {
if (json.has(CONTACT)) {
String cid = json.getString(CONTACT);
contact = BaseClass.get(new Id(cid));
if (isNull(contact)) {
Application.threadPool.execute(new Thread() {
@Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
contact = BaseClass.get(new Id(cid));
}
});
}
}
if (json.has(TIMEOUT)) timeout = json.getInt(TIMEOUT);
if (json.has(TIMEOUT_ACTIONS)) timeoutActions.load(json.getJSONObject(TIMEOUT_ACTIONS));
return super.load(json);
}
@Override
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
formInputs.add(t("Contact"),Contact.selector(contact));
formInputs.add(t("Timeout"),new Input(TIMEOUT,timeout).numeric().addTo(new Tag("span")).content(NBSP+"ms"));
Fieldset fieldset = new Fieldset(t("Actions on timeout"));
fieldset.id("actions");
timeoutActions.list().addTo(fieldset);
postForm.add(fieldset);
return super.properties(preForm, formInputs, postForm);
}
@Override
public String toString() {
return isSet(contact) ? t("Wait for {}, then",contact) : "["+t("Click here to setup contact")+"]";
}
@Override
protected Object update(HashMap<String, String> params) {
if (params.containsKey(CONTACT)) contact = BaseClass.get(new Id(params.get(CONTACT)));
if (params.containsKey(TIMEOUT)) timeout = Integer.parseInt(params.get(TIMEOUT));
return super.update(params);
}
}

View File

@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -30,6 +31,11 @@ public class Contact extends Tile{
protected int addr = 0;
private ActionList actions;
private OffTimer timer = null;
private HashSet<Listener> listeners = new HashSet<Listener>();
public interface Listener{
public void fired();
}
public Contact() {
actions = new ActionList(this);
@@ -86,6 +92,9 @@ public class Contact extends Tile{
context = new Context(this);
actions.fire(context);
}
for (Listener listener : listeners) {
listener.fired();
}
stream();
}
@@ -102,6 +111,10 @@ public class Contact extends Tile{
return this;
}
public void addListener(Listener l) {
listeners.add(l);
}
@Override
public Object click() throws IOException {
trigger(200);
@@ -199,6 +212,10 @@ public class Contact extends Tile{
super.removeChild(child);
}
public void removeListener(Listener listener) {
listeners.remove(listener);
}
public static Select selector(Contact preselect) {
TreeMap<String,Contact> sortedSet = new TreeMap<String, Contact>(); // Map from Name to Contact
for (Contact contact : BaseClass.listElements(Contact.class)) sortedSet.put(contact.toString(), contact);