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 *
025 * @author Kevin Dickerson Copyright (C) 2012
026 * @since 2.99.3
027 */
028public class DefaultRailCom extends DefaultIdTag implements jmri.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 setOrientation(int type) {
054        setProperty("orientation", type);
055    }
056
057    @Override
058    public int getOrientation() {
059        Integer t = (Integer)getProperty("orientation");
060        return ( t != null ? t : Sensor.UNKNOWN );
061    }
062
063    @Override
064    public void setActualSpeed(int type) {
065        setProperty("actualspeed", type);
066    }
067
068    @Override
069    public int getActualSpeed() {
070        Integer t = (Integer)getProperty("actualspeed");
071        return ( t != null ? t : -1 );
072    }
073
074    @Override
075    public void setActualLoad(int type) {
076        setProperty("actualload", type);
077    }
078
079    @Override
080    public int getActualLoad() {
081        Integer t = (Integer)getProperty("actualload");
082        return ( t != null ? t : -1 );
083    }
084
085    @Override
086    public void setActualTemperature(int type) {
087        setProperty("actualtemperature", type);
088    }
089
090    @Override
091    public int getActualTemperature() {
092        Integer t = (Integer)getProperty("actualtemperature");
093        return ( t != null ? t : -1 );
094    }
095
096    @Override
097    public void setWaterLevel(int type) {
098        setProperty("waterlevel", type);
099    }
100
101    @Override
102    public int getWaterLevel() {
103        Integer t = (Integer)getProperty("waterlevel");
104        return ( t != null ? t : -1 );
105    }
106
107    @Override
108    public void setFuelLevel(int type) {
109        setProperty("fuellevel", type);
110    }
111
112    @Override
113    public int getFuelLevel() {
114        Integer t = (Integer)getProperty("fuellevel");
115        return ( t != null ? t : -1 );
116    }
117
118    @Override
119    public void setLocation(int type) {
120        setProperty("location", type);
121    }
122
123    @Override
124    public int getLocation() {
125        Integer t = (Integer)getProperty("location");
126        return ( t != null ? t : -1 );
127    }
128
129    @Override
130    public void setRoutingNo(int type) {
131        setProperty("routing", type);
132    }
133
134    @Override
135    public int getRoutingNo() {
136        Integer t = (Integer)getProperty("routing");
137        return ( t != null ? t : -1 );
138    }
139
140    private int expectedCV = -1;
141
142    @Override
143    public void setExpectedCv(int cv) {
144        expectedCV = cv;
145    }
146
147    @Override
148    public int getExpectedCv() {
149        return expectedCV;
150    }
151
152    @Override
153    public void setCvValue(int value) {
154        if (expectedCV == -1) {
155            log.debug("set cv value called but no CV is expected");
156            return;
157        }
158        int exp = expectedCV;
159        expectedCV = -1;
160        setCV(exp, value);
161    }
162
163    @Override
164    public int getCV(int cv) {
165        if (cvValues.containsKey(cv)) {
166            return cvValues.get(cv);
167        }
168        return 0;
169    }
170
171    @Override
172    public void setCV(int cv, int value) {
173        if (cvValues.containsKey(cv)) {
174            if (cvValues.get(cv) == value) {
175                firePropertyChange("cvvalue", cv, value);
176                return;
177            }
178        }
179        cvValues.put(cv, value);
180        firePropertyChange("cvvalue", cv, value);
181    }
182
183    @Override
184    public List<Integer> getCVList() {
185        int[] arr = new int[cvValues.size()];
186        List<Integer> out = new ArrayList<>();
187        Enumeration<Integer> en = cvValues.keys();
188        int i = 0;
189        while (en.hasMoreElements()) {
190            arr[i] = en.nextElement();
191            i++;
192        }
193        //jmri.util.StringUtil.sort(arr);
194        for (i = 0; i < arr.length; i++) {
195            out.add(arr[i]);
196        }
197        return out;
198    }
199
200    Hashtable<Integer, Integer> cvValues = new Hashtable<>();
201
202    @Override
203    public String toReportString() {
204        StringBuilder sb = new StringBuilder(200);
205        switch (getOrientation()) {
206            case ORIENTA:
207                sb.append("Orientation A ");
208                break;
209            case ORIENTB:
210                sb.append( "Orientation B ");
211                break;
212            case UNKNOWN:
213            default:
214                sb.append( "Unknown Orientation ");
215                break;
216        }
217        sb.append("Address ").append(getLocoAddress()).append(" ");
218
219        if (getWaterLevel() != -1) {
220            sb.append("Water ").append(getWaterLevel()).append(" ");
221        }
222        if (getFuelLevel() != -1) {
223            sb.append("Fuel ").append(getFuelLevel()).append(" ");
224        }
225        if ((getLocation() != -1)) {
226            sb.append("Location : ").append(getLocation()).append(" ");
227        }
228        if ((getRoutingNo() != -1)) {
229            sb.append("Routing No : ").append(getRoutingNo()).append(" ");
230        }
231        if ((getActualTemperature() != -1)) {
232            sb.append("Temperature : ").append(getActualTemperature()).append(" ");
233        }
234        if ((getActualLoad() != -1)) {
235            sb.append("Load : ").append(getActualLoad()).append(" ");
236        }
237        if ((getActualSpeed() != -1)) {
238            sb.append("Speed : ").append(getActualSpeed()).append(" ");
239        }
240        return sb.toString();
241    }
242
243    private final static Logger log = LoggerFactory.getLogger(DefaultRailCom.class);
244}