001package jmri.jmrix.bidib;
002
003import java.util.Locale;
004import java.util.Map;
005
006import jmri.Reporter;
007import org.bidib.jbidibc.messages.BidibLibrary;
008
009import org.bidib.jbidibc.messages.Node;
010import org.bidib.jbidibc.messages.message.FeedbackGetAddressRangeMessage;
011
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * BiDiBReporterManager implements the ReporterManager for BiDiB
017 *
018 * @author Paul Bender Copyright (C) 2016
019 * @author Eckart Meyer Copyright (C) 2019-2023
020 */
021public class BiDiBReporterManager extends jmri.managers.AbstractReporterManager {
022
023    // ctor has to register for LocoNet events
024    public BiDiBReporterManager(BiDiBSystemConnectionMemo memo) {
025        super(memo);
026    }
027
028    /**
029     * {@inheritDoc}
030     */
031    @Override
032    public BiDiBSystemConnectionMemo getMemo() {
033        return (BiDiBSystemConnectionMemo) memo;
034    }
035
036//    @Override
037//    public void dispose() {
038//        super.dispose();
039//    }
040
041    /**
042     * {@inheritDoc}
043     */
044    @Override
045    public Reporter createNewReporter(String systemName, String userName) {
046        Reporter r = new BiDiBReporter(systemName, this);
047        r.setUserName(userName);
048        register(r);
049        return r;
050    }
051    
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    public String validateSystemNameFormat(String name, Locale locale) {
057        log.trace("validateSystemNameFormat: name: {}, typeLetter: {}", name, typeLetter());
058        validateSystemNamePrefix(name, locale);
059        //validateAddressFormat(name.substring(getSystemNamePrefix().length()));
060        if (!BiDiBAddress.isValidSystemNameFormat(name, typeLetter(), getMemo())) {
061            throw new jmri.NamedBean.BadSystemNameException(Locale.getDefault(), "InvalidSystemName",name);
062        }
063        return name;
064    }
065
066    public void updateNode(Node node) {
067        BiDiBTrafficController tc = getMemo().getBiDiBTrafficController();
068        if ( tc.getNodeFeature(node, BidibLibrary.FEATURE_BM_ADDR_DETECT_ON) > 0) {
069            log.trace("node can detect addresse: {}", node);
070            log.info("Requesting all adresses");
071            tc.sendBiDiBMessage(new FeedbackGetAddressRangeMessage(0, 128), node);
072            log.info("Requesting adress of global detector");
073            tc.sendBiDiBMessage(new FeedbackGetAddressRangeMessage(255, 0), node);
074        }
075    }
076    
077    /**
078     * Get all loco addresses from railcom
079     */
080    public void updateAll() {
081        BiDiBTrafficController tc = getMemo().getBiDiBTrafficController();
082        Map<Long,Node> nodeList = tc.getNodeList();
083        nodeList.forEach((uid, node) -> {
084            updateNode(node);
085        });
086    }
087    
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public String getEntryToolTip() {
093        String entryToolTip = Bundle.getMessage("AddReporterEntryToolTip");
094        return entryToolTip;
095    }
096
097    private final static Logger log = LoggerFactory.getLogger(BiDiBReporterManager.class);
098}