001package jmri.jmrix.openlcb;
002
003import jmri.CommandStation;
004import jmri.jmrix.can.CanSystemConnectionMemo;
005
006import org.openlcb.*;
007
008/**
009 * OpenLcb implementation of part of the CommandStation interface.
010 *
011 * @author Bob Jacobsen Copyright (C) 2007, 2024
012 */
013public class OlcbCommandStation implements CommandStation {
014
015    public OlcbCommandStation(CanSystemConnectionMemo memo) {
016        this.memo = memo;
017    }
018
019    /**
020     * OpenLCB/LCC does not have a complete, generic process for 
021     * commanding that a generic DCC packet be sent to the rails.
022     *<p>
023     * For now, this method just logs an error when invokved
024     *
025     * @param packet  Byte array representing the packet, including the
026     *                error-correction byte. Must not be null.
027     * @param repeats Number of times to repeat the transmission, capped at 9
028     * @return {@code true} if the operation succeeds, {@code false} otherwise.
029     */
030    @Override
031    public boolean sendPacket(byte[] packet, int repeats) {
032        jmri.util.LoggingUtil.warnOnce(log, "OpenLCB/LCC does not implement sending generic DCC packets to the rails");
033        return false;
034    }
035
036    CanSystemConnectionMemo memo;
037
038    @Override
039    public String getUserName() {
040        if (memo == null) {
041            return "OpenLCB";
042        }
043        return memo.getUserName();
044    }
045
046    @Override
047    public String getSystemPrefix() {
048        if (memo == null) {
049            return "M";
050        }
051        return memo.getSystemPrefix();
052    }
053
054    @Override
055    public void sendAccSignalDecoderPkt(int address, int aspect, int count) {
056        Connection connection = memo.get(OlcbInterface.class).getOutputConnection();
057        NodeID srcNodeID = memo.get(OlcbInterface.class).getNodeId();
058        
059        int num = address-1+4;        
060        var content = new byte[]{0x01, 0x01, 0x02, 0x00, 0x01, (byte)((num/256)&0xFF), (byte)(num&0xff), (byte) aspect};
061        EventID eventID = new EventID(content);
062        Message m = new ProducerConsumerEventReportMessage(srcNodeID, eventID);
063        for (int i = 0; i<count; i++) {
064            connection.put(m, null);
065        }
066    }
067
068    @Override
069    public void sendAltAccSignalDecoderPkt(int address, int aspect, int count) {
070        // The alternate space is +4 from the regular space
071        sendAccSignalDecoderPkt(address+4, aspect, count);
072    }
073
074    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OlcbCommandStation.class);
075
076}