001package jmri.server.json.consist;
002
003import java.beans.PropertyChangeEvent;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.HashSet;
007import jmri.Consist;
008import jmri.ConsistListListener;
009import jmri.ConsistManager;
010import jmri.InstanceManager;
011import jmri.LocoAddress;
012import jmri.beans.Bean;
013import jmri.jmrit.consisttool.ConsistFile;
014import org.jdom2.JDOMException;
015import org.slf4j.Logger;
016import org.slf4j.LoggerFactory;
017
018/**
019 * ConsistManager for the JSON services. This consist manager passes requests
020 * for CS consisting to the
021 *
022 * @author Randall Wood Copyright (C) 2016
023 */
024public class JsonConsistManager extends Bean implements ConsistManager {
025
026    private ConsistManager manager = null;
027    private HashSet<ConsistListListener> listeners = new HashSet<>();
028    private final static Logger log = LoggerFactory.getLogger(JsonConsistManager.class);
029
030    public JsonConsistManager() {
031        super();
032        InstanceManager.addPropertyChangeListener((PropertyChangeEvent evt) -> {
033            if (evt.getPropertyName().equals(InstanceManager.getDefaultsPropertyName(ConsistManager.class))) {
034                this.manager = InstanceManager.getDefault(ConsistManager.class);
035                this.manager.addConsistListListener(() -> {
036                    this.notifyConsistListChanged();
037                });
038                this.manager.requestUpdateFromLayout();
039                try {
040                    (new ConsistFile()).readFile();
041                } catch (JDOMException | IOException ex) {
042                    log.warn("Error reading consist file {} due to {}", ConsistFile.defaultConsistFilename(), ex.getMessage());
043                }
044            }
045        });
046        this.manager = InstanceManager.getNullableDefault(ConsistManager.class);
047        if (this.manager != null) {
048            this.manager.addConsistListListener(() -> {
049                this.notifyConsistListChanged();
050            });
051            this.manager.requestUpdateFromLayout();
052            try {
053                (new ConsistFile()).readFile();
054            } catch (JDOMException | IOException ex) {
055                log.warn("Error reading consist file {} due to {}", ConsistFile.defaultConsistFilename(), ex.getMessage());
056            }
057        }
058    }
059
060    @Override
061    public Consist getConsist(LocoAddress address) {
062        if (this.manager != null) {
063            return this.manager.getConsist(address);
064        }
065        return null;
066    }
067
068    @Override
069    public void delConsist(LocoAddress address) {
070        if (this.manager != null) {
071            this.manager.delConsist(address);
072        }
073    }
074
075    @Override
076    public boolean isCommandStationConsistPossible() {
077        if (this.manager != null) {
078            return this.manager.isCommandStationConsistPossible();
079        }
080        return false;
081    }
082
083    @Override
084    public boolean csConsistNeedsSeperateAddress() {
085        if (this.manager != null) {
086            return this.manager.csConsistNeedsSeperateAddress();
087        }
088        return false;
089    }
090
091    @Override
092    public ArrayList<LocoAddress> getConsistList() {
093        if (this.manager != null) {
094            return this.manager.getConsistList();
095        }
096        return new ArrayList<>();
097    }
098
099    @Override
100    public String decodeErrorCode(int errorCode) {
101        if (this.manager != null) {
102            return this.manager.decodeErrorCode(errorCode);
103        }
104        return "Unknown Status Code: " + errorCode;
105    }
106
107    @Override
108    public void requestUpdateFromLayout() {
109        if (this.manager != null) {
110            this.manager.requestUpdateFromLayout();
111        }
112    }
113
114    @Override
115    public void addConsistListListener(ConsistListListener listener) {
116        this.listeners.add(listener);
117    }
118
119    @Override
120    public void removeConsistListListener(ConsistListListener listener) {
121        this.listeners.remove(listener);
122    }
123
124    @Override
125    public void notifyConsistListChanged() {
126        new HashSet<>(this.listeners).stream().forEach((listener) -> {
127            listener.notifyConsistListChanged();
128        });
129    }
130
131    /**
132     * Test if a real ConsistManager is available.
133     *
134     * @return true if a real consist manager is available, false otherwise.
135     */
136    public boolean isConsistManager() {
137        return this.manager != null;
138    }
139}