001package jmri.jmrit.ussctc;
002
003import jmri.InstanceManager;
004import jmri.JmriException;
005import jmri.Route;
006import jmri.RouteManager;
007import jmri.Turnout;
008import jmri.implementation.DefaultRoute;
009
010/**
011 * Provide bean-like access to the collection of Logix, Routes, Memories, etc
012 * that make up a Follower.
013 *
014 * @see jmri.jmrit.ussctc.FollowerFrame
015 * @see jmri.jmrit.ussctc.FollowerPanel
016 * @see jmri.jmrit.ussctc.FollowerAction
017 * @author Bob Jacobsen Copyright (C) 2007
018 */
019public class Follower implements Constants {
020
021    static String routePrefix = InstanceManager.getDefault(jmri.RouteManager.class).getSystemNamePrefix();
022    final static String namePrefix = routePrefix + ":" + commonNamePrefix + "FOLLOWER" + commonNameSuffix; // NOI18N
023
024    /**
025     * Nobody can build anonymous object
026     */
027    //private Follower() {}
028    /**
029     * Create one from scratch
030     *
031     * @param output Output turnout to be driven
032     * @param sensor Sensor checking for OS occupancy
033     * @param invert route ISONACTIVE inverted.
034     * @param veto   veto Sensor, or ""
035     */
036    public Follower(String output, String sensor, boolean invert, String veto) {
037        this.veto = veto;
038        this.sensor = sensor;
039        this.invert = invert;
040        this.output = output;
041    }
042
043    /**
044     * Create the underlying objects that implement this
045     */
046    public void instantiate() {
047        String nameT = namePrefix + "T" + nameDivider + output;
048        String nameC = namePrefix + "C" + nameDivider + output;
049
050        RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
051
052        Route rt = rm.getBySystemName(nameT);
053        // if an old one exists, remove it
054        if (rt != null) {
055            rt.deActivateRoute();
056            rm.deleteRoute(rt);
057        }
058        Route rc = rm.getBySystemName(nameC);
059        // if an old one exists, remove it
060        if (rc != null) {
061            rc.deActivateRoute();
062            rm.deleteRoute(rc);
063        }
064
065        // create a new one
066        rt = new DefaultRoute(nameT);
067        rc = new DefaultRoute(nameC);
068
069        // add trigger Sensor
070        rt.addSensorToRoute(sensor, invert ? Route.ONINACTIVE : Route.ONACTIVE);
071        rc.addSensorToRoute(sensor, !invert ? Route.ONINACTIVE : Route.ONACTIVE);
072
073        // optionally, add veto
074        if (!veto.isEmpty()) {
075            rt.addSensorToRoute(veto, Route.VETOACTIVE);
076            rc.addSensorToRoute(veto, Route.VETOACTIVE);
077        }
078
079        // add output
080        rt.addOutputTurnout(output, Turnout.THROWN);
081        rc.addOutputTurnout(output, Turnout.CLOSED);
082
083        // and put Route into operation
084        rt.activateRoute();
085        rc.activateRoute();
086        rm.register(rt);
087        rm.register(rc);
088
089    }
090
091    /**
092     * Create an object to represent an existing Follower.
093     *
094     * @param outputName name of output Turnout that drives the indicator
095     * @throws JmriException if no such Follower exists, or some problem found
096     */
097    public Follower(String outputName) throws jmri.JmriException {
098        this.output = outputName;
099
100        // find existing thrown route to get info
101        String nameT = namePrefix + "T" + nameDivider + output;
102
103        RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
104        Route r = rm.getBySystemName(nameT);
105        if (r == null) {
106            throw new jmri.JmriException("Route does not exist");   // NOI18N
107        }
108
109        // and load internals from the route
110        sensor = r.getRouteSensorName(0);
111        invert = (r.getRouteSensorMode(0) == Route.ONINACTIVE);
112        veto = r.getRouteSensorName(1);
113        if (veto == null) {
114            veto = "";
115        }
116
117    }
118
119    public String getOutputName() {
120        return output;
121    }
122
123    public String getSensorName() {
124        return sensor;
125    }
126
127    public boolean getInvert() {
128        return invert;
129    }
130
131    public String getVetoName() {
132        return veto;
133    }
134
135    String output;
136    String sensor;
137    String veto;
138    boolean invert;
139
140}