001package jmri.jmrix.rfid.networkdriver;
002
003import jmri.jmrix.rfid.RfidNetworkPortController;
004import jmri.jmrix.rfid.RfidProtocol;
005import jmri.jmrix.rfid.RfidSystemConnectionMemo;
006import jmri.jmrix.rfid.RfidTrafficController;
007import jmri.jmrix.rfid.generic.standalone.StandaloneReporterManager;
008import jmri.jmrix.rfid.generic.standalone.StandaloneSensorManager;
009import jmri.jmrix.rfid.generic.standalone.StandaloneTrafficController;
010import jmri.jmrix.rfid.merg.concentrator.ConcentratorReporterManager;
011import jmri.jmrix.rfid.merg.concentrator.ConcentratorSensorManager;
012import jmri.jmrix.rfid.merg.concentrator.ConcentratorTrafficController;
013import jmri.jmrix.rfid.protocol.coreid.CoreIdRfidProtocol;
014import jmri.jmrix.rfid.protocol.olimex.OlimexRfid1356mifareProtocol;
015import jmri.jmrix.rfid.protocol.olimex.OlimexRfidProtocol;
016import jmri.jmrix.rfid.protocol.parallax.ParallaxRfidProtocol;
017import jmri.jmrix.rfid.protocol.seeedstudio.SeeedStudioRfidProtocol;
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021/**
022 * Implements PortAdapter for a network connection.
023 * <p>
024 * This connects via a telnet connection. Normally
025 * controlled by the NetworkDriverFrame class.
026 *
027 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2003, 2015
028 * @author B. Milhaupt  Copyright (C) 2017
029 */
030public class NetworkDriverAdapter extends RfidNetworkPortController {
031
032    public NetworkDriverAdapter() {
033        super(new RfidSystemConnectionMemo());
034        option1Name = "Adapter"; // NOI18N
035        option2Name = "Concentrator-Range"; // NOI18N
036        option3Name = "Protocol"; // NOI18N
037        option4Name = "Device"; // NOI18N
038        options.put(option1Name, new Option(Bundle.getMessage("ConnectionAdapter"), new String[]{"Generic Stand-alone", "MERG Concentrator"}, false)); // NOI18N
039        options.put(option2Name, new Option(Bundle.getMessage("ConnectionConcentratorRange"), new String[]{"A-H", "I-P"}, false)); // NOI18N
040        options.put(option3Name, new Option(Bundle.getMessage("ConnectionProtocol"), new String[]{"CORE-ID", "Olimex", "Parallax", "SeeedStudio"}, false)); // NOI18N
041        options.put(option4Name, new Option(Bundle.getMessage("ConnectionDeviceType"), new String[] {"MOD-RFID125", "MOD-RFID1356MIFARE"}, false)); // NOI18N
042        setManufacturer(jmri.jmrix.rfid.RfidConnectionTypeList.RFID);
043    }
044
045    /**
046     * Set up all of the other objects to operate connected to this port.
047     */
048    @Override
049    public void configure() {
050        RfidTrafficController control;
051        RfidProtocol protocol;
052
053        // set up the system connection first
054        String opt1 = getOptionState(option1Name);
055        switch (opt1) {
056            case "Generic Stand-alone": // NOI18N
057                // create a Generic Stand-alone port controller
058                log.debug("Create Generic Standalone SpecificTrafficController"); // NOI18N
059                control = new StandaloneTrafficController(this.getSystemConnectionMemo());
060                this.getSystemConnectionMemo().setRfidTrafficController(control);
061                this.getSystemConnectionMemo().configureManagers(
062                        new StandaloneSensorManager(this.getSystemConnectionMemo()),
063                        new StandaloneReporterManager(this.getSystemConnectionMemo()));
064                break;
065            case "MERG Concentrator": // NOI18N
066                // create a MERG Concentrator port controller
067                log.debug("Create MERG Concentrator SpecificTrafficController"); // NOI18N
068                control = new ConcentratorTrafficController(this.getSystemConnectionMemo(), getOptionState(option2Name));
069                this.getSystemConnectionMemo().setRfidTrafficController(control);
070                this.getSystemConnectionMemo().configureManagers(
071                        new ConcentratorSensorManager(this.getSystemConnectionMemo()),
072                        new ConcentratorReporterManager(this.getSystemConnectionMemo()));
073                break;
074            default:
075                // no connection at all - warn
076                log.warn("adapter option {} defaults to Generic Stand-alone", opt1); // NOI18N
077                // create a Generic Stand-alone port controller
078                control = new StandaloneTrafficController(this.getSystemConnectionMemo());
079                this.getSystemConnectionMemo().setRfidTrafficController(control);
080                this.getSystemConnectionMemo().configureManagers(
081                        new StandaloneSensorManager(this.getSystemConnectionMemo()),
082                        new StandaloneReporterManager(this.getSystemConnectionMemo()));
083                break;
084        }
085
086        // Now do the protocol
087        String opt3 = getOptionState(option3Name);
088        String opt4 = getOptionState(option4Name);
089        if (opt1.equals("MERG Concentrator")) { // NOI18N
090            // MERG Concentrator only supports CORE-ID
091            log.info("set protocol to CORE-ID"); // NOI18N
092            String opt2 = getOptionState(option2Name);
093            switch (opt2) {
094                case "A-H" :
095                    log.info("set concentrator range to 'A-H' at position 1"); // NOI18N
096                    protocol = new CoreIdRfidProtocol('A', 'H', 1);
097                    break;
098                case "I-P" :
099                    log.info("set concentrator range to 'I-P' at position 1"); // NOI18N
100                    protocol = new CoreIdRfidProtocol('I', 'P', 1);
101                    break;
102                default :
103                    // unrecognised concentrator range - warn
104                    log.warn("concentrator range '{}' not supported - default to no concentrator", opt2); // NOI18N
105                    protocol = new CoreIdRfidProtocol();
106                    break;
107            }
108        } else {
109            switch (opt3) {
110                case "CORE-ID": // NOI18N
111                    log.info("set protocol to CORE-ID"); // NOI18N
112                    protocol = new CoreIdRfidProtocol();
113                    break;
114                case "Olimex": // NOI18N
115                    if (opt4.equals("MOD-RFID1356MIFARE")) { // NOI18N
116                        log.info("set protocol for Olimex MOD-RFID1356MIFARE"); // NOI18N
117                        protocol = new OlimexRfid1356mifareProtocol();
118                    } else {
119                        log.info("set protocol for Olimex MOD-RFID125"); // NOI18N
120                        protocol = new OlimexRfidProtocol();
121                    }
122                    break;
123                case "Parallax": // NOI18N
124                    log.info("set protocol to Parallax"); // NOI18N
125                    protocol = new ParallaxRfidProtocol();
126                    break;
127                case "SeeedStudio": // NOI18N
128                    log.info("set protocol to SeeedStudio"); // NOI18N
129                    protocol = new SeeedStudioRfidProtocol();
130                    break;
131                default:
132                    // no protocol at all - warn
133                    log.warn("protocol option {} defaults to CORE-ID", opt3); // NOI18N
134                    // create a coreid protocol
135                    protocol = new CoreIdRfidProtocol();
136                    break;
137            }
138        }
139        this.getSystemConnectionMemo().setProtocol(protocol);
140
141        // connect to the traffic controller
142        this.getSystemConnectionMemo().setRfidTrafficController(control);
143        control.setAdapterMemo(this.getSystemConnectionMemo());
144        control.connectPort(this);
145        control.sendInitString();
146    }
147
148
149    private final static Logger log = LoggerFactory.getLogger(NetworkDriverAdapter.class);
150
151}