001package jmri.jmrix.rps;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.List;
006import jmri.jmrit.XmlFile;
007import jmri.util.FileUtil;
008import org.jdom2.Attribute;
009import org.jdom2.Document;
010import org.jdom2.Element;
011import org.jdom2.ProcessingInstruction;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Persist RPS polling information.
017 *
018 * @author Bob Jacobsen Copyright 2008
019 */
020public class PollingFile extends XmlFile {
021
022    Document doc;
023    Element root;
024
025    /**
026     * Initialize for writing information.
027     * <p>
028     * This is followed by multiple "set" calls, then a "store"
029     */
030    public void prepare() {
031        root = new Element("rpsfile");
032        doc = newDocument(root, dtdLocation + "rpsroster-2-3-8.dtd");
033
034        // add XSLT processing instruction
035        // <?xml-stylesheet type="text/xsl" href="XSLT/rpsroster.xsl"?>
036        java.util.Map<String, String> m = new java.util.HashMap<String, String>();
037        m.put("type", "text/xsl");
038        m.put("href", xsltLocation + "rpsroster.xsl");
039        ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
040        doc.addContent(0, p);
041    }
042
043    public void setPoll() {
044        Element v = new Element("poll");
045        v.setAttribute("active", Engine.instance().getPolling() ? "true" : "false");
046        v.setAttribute("interval", "" + Engine.instance().getPollingInterval());
047        v.setAttribute("bscpoll", Engine.instance().getBscPollMode() ? "true" : "false");
048        v.setAttribute("throttlepoll", Engine.instance().getThrottlePollMode() ? "true" : "false");
049        root.addContent(v);
050    }
051
052    public void setTransmitter(int r) {
053        Element e = new Element("transmitter");
054        if (Engine.instance().getTransmitter(r).getRosterName() != null) {
055            e.setAttribute("rostername", Engine.instance().getTransmitter(r).getRosterName());
056        } else {
057            e.setAttribute("id", Engine.instance().getTransmitter(r).getId());
058        }
059        e.setAttribute("id", Engine.instance().getTransmitter(r).getId());
060        e.setAttribute("address", "" + Engine.instance().getTransmitter(r).getAddress());
061        e.setAttribute("long", Engine.instance().getTransmitter(r).isLongAddress() ? "true" : "false");
062        e.setAttribute("poll", Engine.instance().getTransmitter(r).isPolled() ? "true" : "false");
063        root.addContent(e);
064    }
065
066    public void store(File file) throws IOException {
067        writeXML(file, doc);
068    }
069
070    /**
071     * Read in the file, and make available for examination.
072     * @param f the file to load.
073     * @throws org.jdom2.JDOMException other errors.
074     * @throws java.io.IOException error accessing file.
075     */
076    public void loadFile(File f)
077            throws org.jdom2.JDOMException, java.io.IOException {
078
079        root = rootFromFile(f);
080    }
081
082    public void getPollValues() {
083        Element e = root.getChild("poll");
084
085        Attribute a = e.getAttribute("active");
086        boolean poll = false;
087        if (a != null && a.getValue().equals("true")) {
088            poll = true;
089        }
090        Engine.instance().setPolling(poll);
091
092        a = e.getAttribute("interval");
093        int value = 0;
094        try {
095            if (a != null) {
096                value = a.getIntValue();
097            }
098        } catch (org.jdom2.DataConversionException ex) {
099            log.error("in getPollValues ", ex);
100        }
101        Engine.instance().setPollingInterval(value);
102
103        Engine.instance().setDirectPollMode();
104
105        a = e.getAttribute("bscpoll");
106        boolean bscpoll = false;
107        if (a != null && a.getValue().equals("true")) {
108            bscpoll = true;
109        }
110        if (bscpoll) {
111            Engine.instance().setBscPollMode();
112        }
113
114        a = e.getAttribute("throttlepoll");
115        boolean throttlepoll = false;
116        if (a != null && a.getValue().equals("true")) {
117            throttlepoll = true;
118        }
119        if (throttlepoll) {
120            Engine.instance().setThrottlePollMode();
121        }
122    }
123
124    /**
125     * Get the transmitters from the file.
126     * @param engine a single engine.
127     */
128    public void getTransmitters(Engine engine) {
129        List<Element> l = root.getChildren("transmitter");
130
131        for (int i = 0; i < l.size(); i++) {  // i indexes over the elements in the file
132            Element e = l.get(i);
133            String id = e.getAttribute("id").getValue();
134            if (e.getAttribute("rostername") != null) {
135                id = e.getAttribute("rostername").getValue();
136            } else {
137                log.warn("Using ID as roster name for {}, please save your polling information to remove this warning", id);
138            }
139
140            // find the matching transmitter (from Roster) and load poll value
141            for (int j = 0; j < engine.getNumTransmitters(); j++) { // j indexes over transmitters
142                if (engine.getTransmitter(j).getRosterName().equals(id)) {
143                    Attribute a = e.getAttribute("poll");
144                    boolean poll = false;
145                    if (a != null && a.getValue().equals("true")) {
146                        poll = true;
147                    }
148                    engine.getTransmitter(j).setPolled(poll);
149                    engine.getTransmitter(j).setId(e.getAttribute("id").getValue());
150                    break;
151                }
152            }
153        }
154
155        return;
156    }
157
158    static public String defaultLocation() {
159        String location = FileUtil.getUserFilesPath() + "rps" + File.separator;
160        FileUtil.createDirectory(location);
161        return location;
162    }
163
164    static public String defaultFilename() {
165        return defaultLocation() + "roster.xml";
166    }
167
168    // initialize logging
169    private final static Logger log = LoggerFactory.getLogger(PollingFile.class);
170
171}