001package jmri.jmrit.sensorgroup;
002
003import java.util.ArrayList;
004import jmri.InstanceManager;
005import jmri.Route;
006import jmri.RouteManager;
007import jmri.Sensor;
008import jmri.implementation.DefaultRoute;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Object for representing, creating and editing sensor groups.
014 * <p>
015 * Sensor groups are implemented by (groups) of Routes, not by any other object.
016 * <p>
017 * They are not (currently) NamedBean objects.
018 *
019 * @author Bob Jacobsen Copyright (C) 2007
020 */
021public class SensorGroup {
022
023    /**
024     * Nobody can build an anonymous object
025     */
026    //private SensorGroup() {
027    //}
028    private final static String namePrefix = "SENSOR GROUP:";  // should be upper case
029    private final static String nameDivider = ":";
030
031    String name;
032    ArrayList<String> sensorList;
033
034    /**
035     * Create one, looking up an existing one if present
036     * @param name Name of the group
037     */
038    SensorGroup(String name) {
039        this.name = name;
040        // find suitable 
041        RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
042        String group = name;
043        String prefix = (namePrefix + group + nameDivider);
044
045        sensorList = new ArrayList<String>();
046        for (Route route : rm.getNamedBeanSet()) {
047            String routeName = route.getSystemName();
048            if (routeName.startsWith(prefix)) {
049                String sensor = routeName.substring(prefix.length());
050                // remember that sensor
051                sensorList.add(sensor);
052            }
053        }
054    }
055
056    void addPressed() {
057        log.debug("start with {} lines", sensorList.size());
058        RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
059        String group = name;
060
061        // remove the old routes
062        String prefix = (namePrefix + group + nameDivider);
063
064        for (Route r : rm.getNamedBeanSet()) {
065            String routeName = r.getSystemName();
066            if (routeName.startsWith(prefix)) {
067                // OK, kill this one
068                r.deActivateRoute();
069                rm.deleteRoute(r);
070            }
071        }
072
073        // add the new routes
074        for (int i = 0; i < sensorList.size(); i++) {
075            String sensor = sensorList.get(i);
076            String routeName = namePrefix + group + nameDivider + sensor;
077            Route r = new DefaultRoute(routeName);
078            // add the control sensor
079            r.addSensorToRoute(sensor, Route.ONACTIVE);
080            // add the output sensors
081            for (int j = 0; j < sensorList.size(); j++) {
082                String outSensor = sensorList.get(j);
083                int mode = Sensor.INACTIVE;
084                if (i == j) {
085                    mode = Sensor.ACTIVE;
086                }
087                r.addOutputSensor(outSensor, mode);
088            }
089            // make it persistant & activate
090            r.activateRoute();
091            rm.register(r);
092        }
093    }
094
095    private final static Logger log = LoggerFactory.getLogger(SensorGroup.class);
096
097}