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