001package jmri.jmrix.loconet.lnsvf1;
002
003import jmri.jmrix.loconet.Lnsv1DevicesManager;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007import javax.annotation.concurrent.GuardedBy;
008import java.util.ArrayList;
009import java.util.List;
010
011/**
012 * Manage an array of Lnsv1Device items. See {@link Lnsv1DevicesManager}
013 * Based on Lnsvf2Devices by B. Milhaupt
014 * @author Egbert Broerse 2020, 2025
015 */
016public class Lnsv1Devices {
017
018    @GuardedBy("this")
019    private final List<Lnsv1Device> deviceList;
020
021    public Lnsv1Devices() {
022        deviceList = new ArrayList<>();
023    }
024
025    /**
026     * Add a device that responded to a PROBE_ALL request (or simply sent a READ_ONE reply) to the list of LNSV1 Devices.
027     *
028     * @param d the device object, containing its properties
029     * @return true if device was added, false if not e.g. it was already in the list
030     */
031    public synchronized boolean addDevice(Lnsv1Device d) {
032        if (!deviceExists(d)) {
033            deviceList.add(d);
034            log.debug("added device with addr {}", d.getDestAddr());
035            return true;
036        } else {
037            log.debug("device already in list: addr {} ", d.getDestAddr());
038            return false;
039        }
040    }
041
042    public synchronized void removeAllDevices() {
043        deviceList.clear();
044    }
045
046    /**
047     * Get index in deviceList of the first device matching (only) the Device Address.
048     * Where a deviceToBeFound parameter is -1, that parameter is not compared.
049     *
050     * @param deviceToBeFound Device we try to find in known LNSV1 devices list
051     * @return index of found device, -1 if matching device not found
052     */
053    public synchronized int isDeviceExistant(Lnsv1Device deviceToBeFound) {
054        log.debug("Looking for a known LNSV1 device which matches characteristics: address {}.",
055                deviceToBeFound.getDestAddr());
056        for (int i = 0; i < deviceList.size(); ++i) {
057            Lnsv1Device dev = deviceList.get(i);
058            log.debug("Comparing against known device: addr {}.",
059                    deviceToBeFound.getDestAddr());
060            if ((deviceToBeFound.getDestAddr() == -1) ||
061                    (dev.getDestAddr() == deviceToBeFound.getDestAddr())) {
062                log.debug("Match Found! Searched device matched against known device: addr {}.",
063                        dev.getDestAddr());
064                return i;
065            }
066        }
067        log.debug("No matching known device was found!");
068        return -1;
069    }
070
071    public boolean deviceExists(Lnsv1Device d) {
072        int i = isDeviceExistant(d);
073        log.debug("deviceExists found {}", i);
074        return (i >= 0);
075    }
076
077    public synchronized Lnsv1Device getDevice(int index) {
078        return deviceList.get(index);
079    }
080
081    public synchronized Lnsv1Device[] getDevices() {
082        Lnsv1Device[] d = {};
083        return deviceList.toArray(d);
084    }
085    public synchronized int size() {
086        return deviceList.size();
087    }
088
089    private final static Logger log = LoggerFactory.getLogger(Lnsv1Devices.class);
090
091}