001package jmri.implementation;
002
003import java.util.ArrayList;
004import java.util.Enumeration;
005import java.util.Hashtable;
006import java.util.List;
007import jmri.JmriException;
008import jmri.Sensor;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Concrete implementation of the {@link jmri.RailCom} interface.
014 * <hr>
015 * This file is part of JMRI.
016 * <p>
017 * JMRI is free software; you can redistribute it and/or modify it under the
018 * terms of version 2 of the GNU General Public License as published by the Free
019 * Software Foundation. See the "COPYING" file for a copy of this license.
020 * <p>
021 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
022 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
023 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
024 * <p>
025 *
026 * @author Kevin Dickerson Copyright (C) 2012
027 * @since 2.99.3
028 */
029public class DefaultRailCom extends DefaultIdTag implements jmri.RailCom {
030
031    private int currentState = 0x00;
032
033    public DefaultRailCom(String systemName) {
034        super(systemName);
035        setWhereLastSeen(null);
036    }
037
038    public DefaultRailCom(String systemName, String userName) {
039        super(systemName, userName);
040        setWhereLastSeen(null);
041    }
042
043    @Override
044    public void setState(int s) throws JmriException {
045        this.currentState = s;
046    }
047
048    @Override
049    public int getState() {
050        return this.currentState;
051    }
052
053    @Override
054    public void setOrientation(int type) {
055        setProperty("orientation",Integer.valueOf(type));
056    }
057
058    @Override
059    public int getOrientation() {
060        Integer t = (Integer)getProperty("orientation");
061        if(t != null ){
062           return t.intValue();
063        } else {
064          return Sensor.UNKNOWN;
065        }
066    }
067
068    /**
069     * {@inheritDoc}
070     */
071    @Deprecated
072    @Override
073    public String getAddressTypeAsString() {
074        switch (addressTypeInt) {
075            case SHORT_ADDRESS:
076                return "Short";
077            case LONG_ADDRESS:
078                return "Long";
079            case CONSIST_ADDRESS:
080                return "Consist";
081            default:
082                return "No Address";
083        }
084    }
085
086    int addressTypeInt = 0;
087
088    /**
089     * {@inheritDoc}
090     */
091    @Deprecated
092    @Override
093    public void setAddressType(int type) {
094        addressTypeInt = type;
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Deprecated
101    @Override
102    public int getAddressType() {
103        return addressTypeInt;
104    }
105
106    @Override
107    public void setActualSpeed(int type) {
108        setProperty("actualspeed",Integer.valueOf(type));
109    }
110
111    @Override
112    public int getActualSpeed() {
113        Integer t = (Integer)getProperty("actualspeed");
114        if(t != null ){
115           return t.intValue();
116        } else {
117          return -1;
118        }
119    }
120
121    @Override
122    public void setActualLoad(int type) {
123        setProperty("actualload",Integer.valueOf(type));
124    }
125
126    @Override
127    public int getActualLoad() {
128        Integer t = (Integer)getProperty("actualload");
129        if(t != null ){
130           return t.intValue();
131        } else {
132          return -1;
133        }
134    }
135
136    @Override
137    public void setActualTemperature(int type) {
138        setProperty("actualtemperature",Integer.valueOf(type));
139    }
140
141    @Override
142    public int getActualTemperature() {
143        Integer t = (Integer)getProperty("actualtemperature");
144        if(t != null ){
145           return t.intValue();
146        } else {
147          return -1;
148        }
149    }
150
151    @Override
152    public void setWaterLevel(int type) {
153        setProperty("waterlevel",Integer.valueOf(type));
154    }
155
156    @Override
157    public int getWaterLevel() {
158        Integer t = (Integer)getProperty("waterlevel");
159        if(t != null ){
160           return t.intValue();
161        } else {
162          return -1;
163        }
164    }
165
166    @Override
167    public void setFuelLevel(int type) {
168        setProperty("fuellevel",Integer.valueOf(type));
169    }
170
171    @Override
172    public int getFuelLevel() {
173        Integer t = (Integer)getProperty("fuellevel");
174        if(t != null ){
175           return t.intValue();
176        } else {
177          return -1;
178        }
179    }
180
181    @Override
182    public void setLocation(int type) {
183        setProperty("location",Integer.valueOf(type));
184    }
185
186    @Override
187    public int getLocation() {
188        Integer t = (Integer)getProperty("location");
189        if(t != null ){
190           return t.intValue();
191        } else {
192          return -1;
193        }
194    }
195
196    @Override
197    public void setRoutingNo(int type) {
198        setProperty("routing",Integer.valueOf(type));
199    }
200
201    @Override
202    public int getRoutingNo() {
203        Integer t = (Integer)getProperty("routing");
204        if(t != null ){
205           return t.intValue();
206        } else {
207          return -1;
208        }
209    }
210
211    private int expectedCV = -1;
212
213    @Override
214    public void setExpectedCv(int cv) {
215        expectedCV = cv;
216    }
217
218    @Override
219    public int getExpectedCv() {
220        return expectedCV;
221    }
222
223    @Override
224    public void setCvValue(int value) {
225        if (expectedCV == -1) {
226            log.debug("set cv value called but no CV is expected");
227            return;
228        }
229        int exp = expectedCV;
230        expectedCV = -1;
231        setCV(exp, value);
232    }
233
234    @Override
235    public int getCV(int cv) {
236        if (cvValues.containsKey(cv)) {
237            return cvValues.get(cv);
238        }
239        return 0;
240    }
241
242    @Override
243    public void setCV(int cv, int value) {
244        if (cvValues.containsKey(cv)) {
245            if (cvValues.get(cv) == value) {
246                firePropertyChange("cvvalue", cv, value);
247                return;
248            }
249        }
250        cvValues.put(cv, value);
251        firePropertyChange("cvvalue", cv, value);
252    }
253
254    @Override
255    public List<Integer> getCVList() {
256        int[] arr = new int[cvValues.size()];
257        List<Integer> out = new ArrayList<>();
258        Enumeration<Integer> en = cvValues.keys();
259        int i = 0;
260        while (en.hasMoreElements()) {
261            arr[i] = en.nextElement();
262            i++;
263        }
264        //jmri.util.StringUtil.sort(arr);
265        for (i = 0; i < arr.length; i++) {
266            out.add(arr[i]);
267        }
268        return out;
269    }
270
271    Hashtable<Integer, Integer> cvValues = new Hashtable<>();
272
273    @Override
274    public String toReportString() {
275        String comment;
276        switch (getOrientation()) {
277            case ORIENTA:
278                comment = "Orientation A ";
279                break;
280            case ORIENTB:
281                comment = "Orientation B ";
282                break;
283            case UNKNOWN:
284                comment = "Unknown Orientation ";
285                break;
286            default:
287                comment = "Unknown Orientation ";
288                break;
289        }
290        comment = comment + "Address " + getLocoAddress() + " ";
291
292        if (getWaterLevel() != -1) {
293            comment = comment + "Water " + getWaterLevel() + " ";
294        }
295        if (getFuelLevel() != -1) {
296            comment = comment + "Fuel " + getFuelLevel() + " ";
297        }
298        if ((getLocation() != -1)) {
299            comment = comment + "Location : " + getLocation() + " ";
300        }
301        if ((getRoutingNo() != -1)) {
302            comment = comment + "Routing No : " + getRoutingNo() + " ";
303        }
304        if ((getActualTemperature() != -1)) {
305            comment = comment + "Temperature : " + getActualTemperature() + " ";
306        }
307        if ((getActualLoad() != -1)) {
308            comment = comment + "Load : " + getActualLoad() + " ";
309        }
310        if ((getActualSpeed() != -1)) {
311            comment = comment + "Speed : " + getActualSpeed();
312        }
313        return comment;
314    }
315
316    private final static Logger log = LoggerFactory.getLogger(DefaultRailCom.class);
317}