001package jmri.jmrix.loconet;
002
003/**
004 * Provide access to throttle-messaging on a LocoNet.
005 * <p>
006 * Some of the message formats used in this class are Copyright Digitrax, Inc.
007 * and used with permission as part of the JMRI project. That permission does
008 * not extend to uses in other software products. If you wish to use this code,
009 * algorithm or these message formats outside of JMRI, please contact Digitrax
010 * Inc for separate permission.
011 *
012 * @author Bob Jacobsen Copyright (C) 2001
013 */
014public class LnMessageManager implements LocoNetListener {
015
016    public LnMessageManager(LnTrafficController tc) {
017        // connect to the TrafficManager
018        this.tc = tc;
019        this.tc.addLocoNetListener(~0, this);
020    }
021
022    public void sendMessage(String text) {
023        sendMessage(0, text);
024    }
025
026    public void sendMessage(int id, String text) {
027        LocoNetMessage l = new LocoNetMessage(16);
028        String localText = text + "        "; // ensure at least 8 characters
029        l.setOpCode(LnConstants.OPC_PEER_XFER);
030        l.setElement(1, 0x10);
031        l.setElement(2, 0x7F);   // throttle message
032        l.setElement(3, id & 0x7F);
033        l.setElement(4, id / 128);
034        l.setElement(5, 0);
035        l.setElement(6, localText.charAt(0));
036        l.setElement(7, localText.charAt(1));
037        l.setElement(8, localText.charAt(2));
038        l.setElement(9, localText.charAt(3));
039        l.setElement(10, 0);
040        l.setElement(11, localText.charAt(4));
041        l.setElement(12, localText.charAt(5));
042        l.setElement(13, localText.charAt(6));
043        l.setElement(14, localText.charAt(7));
044        tc.sendLocoNetMessage(l);
045    }
046
047    /**
048     * Free resources when no longer used.
049     */
050    public void dispose() {
051        tc.removeLocoNetListener(~0, this);
052        tc = null;
053    }
054
055    LnTrafficController tc = null;
056
057    /**
058     * Listen for status changes from LocoNet.
059     * <p>
060     * This doesn't do anything now. Eventually, it will handle the user
061     * response.
062     */
063    @Override
064    public void message(LocoNetMessage m) {
065    }
066
067}