All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.jbidibc.DccAccessory Maven / Gradle / Ivy

package org.bidib.jbidibc;

import org.bidib.jbidibc.core.node.CommandStationNode;
import org.bidib.jbidibc.messages.Node;
import org.bidib.jbidibc.messages.enums.AccessoryAcknowledge;
import org.bidib.jbidibc.messages.enums.ActivateCoilEnum;
import org.bidib.jbidibc.messages.enums.AddressTypeEnum;
import org.bidib.jbidibc.messages.enums.TimeBaseUnitEnum;
import org.bidib.jbidibc.messages.enums.TimingControlEnum;
import org.bidib.jbidibc.messages.exception.InvalidConfigurationException;
import org.bidib.jbidibc.messages.exception.PortNotFoundException;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

/**
 * This commands reads the value of the specified CV from the specified node.
 * 
 */
@Command
public class DccAccessory extends BidibNodeCommand {
    @Option(names = { "-address" }, description = "The DCC decoder address", required = true)
    private int decoderAddress;

    @Option(names = { "-activate" }, description = "0: coil-off, 1: coil-on", required = true)
    private int activate;

    @Option(names = { "-aspect" }, description = "The aspect to set", required = true)
    private int aspect;

    public static void main(String[] args) {
        run(new DccAccessory(), args);
    }

    @Override
    public Integer call() {
        int result = 20;

        try {
            openPort(getPortName(), null);

            Node node = findNode();

            if (node != null) {
                CommandStationNode bidibNode = getBidib().getCommandStationNode(node);

                AddressTypeEnum addressType = AddressTypeEnum.ACCESSORY;
                TimingControlEnum timingControl = TimingControlEnum.OUTPUT_UNIT;
                ActivateCoilEnum activateCoil = (activate != 0 ? ActivateCoilEnum.COIL_ON : ActivateCoilEnum.COIL_OFF);

                TimeBaseUnitEnum timeBaseUnit = TimeBaseUnitEnum.UNIT_100MS;
                byte time = 5;

                AccessoryAcknowledge acknowledge =
                    bidibNode
                        .setAccessory(decoderAddress, addressType, timingControl, activateCoil, aspect, timeBaseUnit,
                            time);

                System.out.println("Acknowledge: " + acknowledge); // NOSONAR
            }
            else {
                System.err.println("node with unique id \"" + getNodeIdentifier() + "\" not found"); // NOSONAR
            }

            getBidib().close();

        }
        catch (InvalidConfigurationException ex) {
            System.err.println("Execute command failed: " + ex); // NOSONAR
        }
        catch (PortNotFoundException ex) {
            System.err
                .println("The provided port was not found: " + ex.getMessage() // NOSONAR
                    + ". Verify that the BiDiB device is connected.");
        }
        catch (Exception ex) {
            System.err.println("Execute command failed: " + ex); // NOSONAR
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy