001package jmri.jmrix.ecos.utilities;
002
003import jmri.jmrit.roster.RosterEntry;
004import jmri.jmrix.ecos.EcosListener;
005import jmri.jmrix.ecos.EcosLocoAddress;
006import jmri.jmrix.ecos.EcosLocoAddressManager;
007import jmri.jmrix.ecos.EcosMessage;
008import jmri.jmrix.ecos.EcosPreferences;
009import jmri.jmrix.ecos.EcosReply;
010import jmri.jmrix.ecos.EcosSystemConnectionMemo;
011import jmri.jmrix.ecos.EcosTrafficController;
012
013public class RosterToEcos implements EcosListener {
014
015    private EcosLocoAddress objEcosLoco = null;
016    private EcosLocoAddressManager objEcosLocoManager;
017    protected RosterEntry _re = null;
018    private EcosPreferences ep;
019    //private String _rosterid;
020    EcosTrafficController tc;
021    private boolean createloco;
022    EcosSystemConnectionMemo adaptermemo;
023
024    public RosterToEcos(EcosSystemConnectionMemo memo) {
025        adaptermemo = memo;
026        tc = adaptermemo.getTrafficController();
027        ep = adaptermemo.getPreferenceManager();
028        objEcosLocoManager = adaptermemo.getLocoAddressManager();
029    }
030
031    public void createEcosLoco(RosterEntry re) {
032        if (createloco == true) {
033            return;
034        }
035        createloco = true;
036        _re = re;
037
038        String protocol;
039        switch (re.getProtocol()) {
040            case MOTOROLA:
041                protocol = "MM28";
042                break;
043            case SELECTRIX:
044                protocol = "SX28";
045                break;
046            case MFX:
047                protocol = "MMFKT";
048                break;
049            default:
050                protocol = "DCC128";
051        }
052
053        String message = "create(10, addr[" + _re.getDccAddress() + "], name[\"" + description() + "\"], protocol[" + protocol + "], append)";
054
055        EcosMessage m = new EcosMessage(message);
056        tc.sendEcosMessage(m, this);
057
058    }
059
060    private String description() {
061
062        String result = "";
063        String str = ep.getEcosLocoDescription();
064        if ((str == null) || (str.isEmpty())) {
065            return _re.getId();
066        }
067        char comp = '%';
068        for (int i = 0; i < str.length(); i++) {
069            if (str.charAt(i) == comp) {
070                switch (str.charAt(i + 1)) {
071                    case 'i':
072                        result = result + _re.getId();
073                        break;
074                    case 'r':
075                        result = result + _re.getRoadName();
076                        break;
077                    case 'n':
078                        result = result + _re.getRoadNumber();
079                        break;
080                    case 'm':
081                        result = result + _re.getMfg();
082                        break;
083                    case 'o':
084                        result = result + _re.getOwner();
085                        break;
086                    case 'l':
087                        result = result + _re.getModel();
088                        break;
089                    case 'c':
090                        result = result + _re.getComment();
091                        break;
092                    default:
093                        break;
094                }
095                i++;
096            } else {
097                result = result + str.charAt(i);
098            }
099        }
100        return result;
101
102    }
103
104    //Need to deal with the loco not being created somehow.
105    //If we get the error, then we could simply delete the loco from our loco list.
106    @Override
107    public void reply(EcosReply m) {
108        int start;
109        int end;
110        String msg = m.toString();
111        String[] lines = msg.split("\n");
112        if (m.getResultCode() == 0) {
113            if (lines[0].startsWith("<REPLY create(10, addr")) {
114                for (int i = 1; i < lines.length - 1; i++) {
115                    if (lines[i].contains("10 id[")) {
116
117                        start = lines[i].indexOf('[') + 1;
118                        end = lines[i].indexOf(']');
119                        String EcosAddr = lines[i].substring(start, end);
120                        objEcosLoco = objEcosLocoManager.provideByEcosObject(EcosAddr);
121                        objEcosLoco.setEcosTempEntry(false);
122                        _re.putAttribute(ep.getRosterAttribute(), EcosAddr);
123                        objEcosLoco.setRosterId(_re.getId());
124                        objEcosLoco.setEcosDescription(description());
125                        objEcosLoco.setLocoAddress(Integer.parseInt(_re.getDccAddress()));
126                        switch (_re.getProtocol()) {
127                            case MOTOROLA:
128                                objEcosLoco.setProtocol("MM28");
129                                break;
130                            case SELECTRIX:
131                                objEcosLoco.setProtocol("SX28");
132                                break;
133                            case MFX:
134                                objEcosLoco.setProtocol("MMFKT");
135                                break;
136                            default:
137                                objEcosLoco.setProtocol("DCC128");
138                        }
139                        _re.writeFile(null, null);
140                        jmri.jmrit.roster.Roster.getDefault().writeRoster();
141                        objEcosLocoManager.register(objEcosLoco);
142                        createloco = false;
143                        dispose();
144                    }
145                }
146            }
147        }
148    }
149
150    @Override
151    public void message(EcosMessage m) {
152
153    }
154
155    void dispose() {
156        objEcosLoco = null;
157        objEcosLocoManager = null;
158        _re = null;
159        createloco = false;
160    }
161
162}