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