package de.srsoftware.web4rail.tiles; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeoutException; import org.json.JSONObject; import de.srsoftware.tools.Tag; import de.srsoftware.web4rail.Command; import de.srsoftware.web4rail.Command.Reply; import de.srsoftware.web4rail.Device; import de.srsoftware.web4rail.Protocol; import de.srsoftware.web4rail.tags.Fieldset; import de.srsoftware.web4rail.tags.Input; import de.srsoftware.web4rail.tags.Radio; import de.srsoftware.web4rail.tags.Window; public class Relay extends Tile implements Device{ private static final String PORT_A = "port_a"; private static final String PORT_B = "port_b"; protected static final String STRAIGHT = "straight"; public static final String DEFAULT_LABEL_A = "A"; public static final String DEFAULT_LABEL_B = "B"; private Protocol protocol = Protocol.DCC128; protected int address = 0; protected int portA = 0, portB = 1; protected int delay = 400; protected boolean initialized = false; protected boolean error = false; public String stateLabelA = DEFAULT_LABEL_A; public String stateLabelB = DEFAULT_LABEL_B; private String name = t("Relay"); protected boolean state = true; public static final boolean STATE_A = true,STATE_B=false; private static final String LABEL_A = "label_a"; private static final String LABEL_B = "label_b"; private static final String NAME = "name"; public int address() { return address; } @Override public Object click(boolean shift) throws IOException { Object o = super.click(shift); if (!shift) state(!state); return o; } protected String commandFor(boolean newState) { return "SET {} GA "+address+" "+(newState?portA:portB)+" 1 "+delay; } public void error(Reply reply) { this.error = true; try { plan.stream(tag(null).toString()); } catch (IOException e) { LOG.error("Was not able to stream: ",e); } throw new RuntimeException(reply.message()); } protected Reply init() { if (!initialized) { Command command = new Command("INIT {} GA "+address+" "+proto()) { @Override public void onSuccess() { super.onSuccess(); initialized = true; } @Override public void onFailure(Reply r) { super.onSuccess(); initialized = false; } }; try { return plan.queue(command).reply(); } catch (TimeoutException e) { LOG.warn(e.getMessage()); } } return new Reply(200, "OK"); } @Override public JSONObject json() { JSONObject json = super.json(); if (portA != 0) json.put(PORT_A, portA); if (portB != 1) json.put(PORT_B, portB); if (address != 0) json.put(ADDRESS, address); json.put(PROTOCOL, protocol); json.put(LABEL_A, stateLabelA); json.put(LABEL_B, stateLabelB); json.put(NAME, name); return json; } @Override public Tile load(JSONObject json) { if (json.has(ADDRESS)) address = json.getInt(ADDRESS); if (json.has(PORT_A)) portA = json.getInt(PORT_A); if (json.has(PORT_B)) portB = json.getInt(PORT_B); if (json.has(LABEL_A)) stateLabelA = json.getString(LABEL_A); if (json.has(LABEL_B)) stateLabelB = json.getString(LABEL_B); if (json.has(PROTOCOL)) protocol = Protocol.valueOf(json.getString(PROTOCOL)); if (json.has(NAME)) name = json.getString(NAME); return super.load(json); } @Override public Tile position(int x, int y) { super.position(x, y); return this; } @Override protected Window properties(List