001package jmri.jmrit.ussctc;
002
003import jmri.*;
004
005/**
006 * Drive a single Maintainer Call section on a USS CTC panel.
007 *
008 * @author Bob Jacobsen Copyright (C) 2007, 2017
009 * TODO - add field state diagram
010 */
011
012public class MaintainerCallSection implements Section<CodeGroupOneBit, CodeGroupNoBits> {
013
014    /**
015     *  Anonymous object only for testing
016     */
017    MaintainerCallSection() {}
018
019
020    /**
021     * Create and configure.
022     *
023     * Accepts user or system names.
024     *
025     * @param inputSensor  Sensor for input from central CTC machine
026     * @param layoutOutput  Turnout name for maintainer call on layout
027     * @param station Station to which this Section belongs
028     */
029    public MaintainerCallSection(String inputSensor, String layoutOutput, Station<CodeGroupOneBit,CodeGroupNoBits> station) {
030        this.station = station;
031
032        NamedBeanHandleManager hm = InstanceManager.getDefault(NamedBeanHandleManager.class);
033        TurnoutManager tm = InstanceManager.getDefault(TurnoutManager.class);
034        SensorManager sm = InstanceManager.getDefault(SensorManager.class);
035
036        hInputSensor = hm.getNamedBeanHandle(inputSensor, sm.provideSensor(inputSensor));
037        hLayoutOutput = hm.getNamedBeanHandle(layoutOutput, tm.provideTurnout(layoutOutput));
038
039        // aligns at start
040        codeValueDelivered(codeSendStart());
041    }
042
043    NamedBeanHandle<Sensor> hInputSensor;
044    NamedBeanHandle<Turnout> hLayoutOutput;
045
046    Station<CodeGroupOneBit,CodeGroupNoBits> station;
047    @Override
048    public Station<CodeGroupOneBit,CodeGroupNoBits> getStation() { return station; }
049    @Override
050    public String getName() { return "MC for "+hLayoutOutput.getBean().getDisplayName(); }
051
052     /**
053     * Start of sending code operation.
054     * @return code line value to transmit
055     */
056    @Override
057    public CodeGroupOneBit codeSendStart() {
058        log.debug("codeSendStart with Sensor state == {}", hInputSensor.getBean().getKnownState());
059        // return the settings to send
060        if (hInputSensor.getBean().getKnownState()==Sensor.ACTIVE) return CodeGroupOneBit.Single1;
061        return CodeGroupOneBit.Single0;
062    }
063
064    /**
065     * Process values received from the field unit.
066     */
067    @Override
068    public void indicationComplete(CodeGroupNoBits value) {
069    }
070
071    /**
072     * Notification that code has arrived in the field. Sets the turnout on the layout.
073     */
074    @Override
075    public void codeValueDelivered(CodeGroupOneBit value) {
076        log.debug("codeValueDelivered({}) with Turnout state == {}", value, hLayoutOutput.getBean().getCommandedState());
077        // Set out as commanded, skipping redundant operations
078        if (value == CodeGroupOneBit.Single0 && hLayoutOutput.getBean().getCommandedState() != Turnout.CLOSED) {
079            hLayoutOutput.getBean().setCommandedState(Turnout.CLOSED);
080            log.debug("Layout MC output set CLOSED");
081        } else if (value == CodeGroupOneBit.Single1 && hLayoutOutput.getBean().getCommandedState() != Turnout.THROWN) {
082            hLayoutOutput.getBean().setCommandedState(Turnout.THROWN);
083            log.debug("Layout MC output set THROWN");
084        }
085    }
086
087    /**
088     * Provide state that's returned from field to machine via indication.
089     */
090    @Override
091    public CodeGroupNoBits indicationStart() {
092        return CodeGroupNoBits.None;
093    }
094
095
096    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MaintainerCallSection.class);
097}