001package jmri.jmrix.mqtt;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.jmrix.ConfiguringSystemConnectionMemo;
009import jmri.jmrix.DefaultSystemConnectionMemo;
010import jmri.util.NamedBeanComparator;
011
012/**
013 *
014 * @author Lionel Jeanson
015 * @author Dean Cording (c) 2023
016 */
017public class MqttSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
018
019    private MqttAdapter mqttAdapter;
020
021    public MqttSystemConnectionMemo() {
022        super("M", "MQTT");
023        InstanceManager.store(MqttSystemConnectionMemo.this, MqttSystemConnectionMemo.class);
024    }
025
026    @Override
027    public void configureManagers() {
028
029        InstanceManager.setTurnoutManager(getTurnoutManager());
030        InstanceManager.setSensorManager(getSensorManager());
031        InstanceManager.setLightManager(getLightManager());
032        InstanceManager.setReporterManager(getReporterManager());
033        InstanceManager.setThrottleManager(getThrottleManager());
034        InstanceManager.store(getPowerManager(), PowerManager.class);
035        InstanceManager.store(getConsistManager(), ConsistManager.class);
036
037        // prefix for MqttSignalMasts
038        MqttSignalMast.setSendTopicPrefix(getMqttAdapter().getOptionState("15"));
039
040        register();
041    }
042
043    @Override
044    public void dispose() {
045        InstanceManager.deregister(this, MqttSystemConnectionMemo.class);
046        super.dispose();
047    }
048
049    @Override
050    protected ResourceBundle getActionModelResourceBundle() {
051        return null;
052    }
053
054    @Override
055    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
056        return new NamedBeanComparator<>();
057    }
058
059    public MqttTurnoutManager getTurnoutManager() {
060        if (getDisabled()) {
061            return null;
062        }
063        return (MqttTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class,(Class<?> c) -> {
064                    MqttTurnoutManager t = new MqttTurnoutManager(this);
065                    t.setSendTopicPrefix(getMqttAdapter().getOptionState("10.3"));
066                    t.setRcvTopicPrefix(getMqttAdapter().getOptionState("10.5"));
067                    return t;
068                });
069
070    }
071
072    public MqttSensorManager getSensorManager() {
073        if (getDisabled()) {
074            return null;
075        }
076        return (MqttSensorManager) classObjectMap.computeIfAbsent(SensorManager.class,(Class<?> c) -> {
077                    MqttSensorManager t = new MqttSensorManager(this);
078                    t.setSendTopicPrefix(getMqttAdapter().getOptionState("11.3"));
079                    t.setRcvTopicPrefix(getMqttAdapter().getOptionState("11.5"));
080                    return t;
081                });
082    }
083
084    public MqttLightManager getLightManager() {
085        if (getDisabled()) {
086            return null;
087        }
088        return (MqttLightManager) classObjectMap.computeIfAbsent(LightManager.class,(Class<?> c) -> {
089                    MqttLightManager t = new MqttLightManager(this);
090                    t.setSendTopicPrefix(getMqttAdapter().getOptionState("12.3"));
091                    t.setRcvTopicPrefix(getMqttAdapter().getOptionState("12.5"));
092                    return t;
093                });
094    }
095
096    public MqttReporterManager getReporterManager() {
097        if (getDisabled()) {
098            return null;
099        }
100        return (MqttReporterManager) classObjectMap.computeIfAbsent(ReporterManager.class,(Class<?> c) -> {
101                    MqttReporterManager t = new MqttReporterManager(this);
102                    t.setRcvTopicPrefix(getMqttAdapter().getOptionState("13"));
103                    return t;
104                });
105    }
106
107    public MqttThrottleManager getThrottleManager() {
108        if (getDisabled()) {
109            return null;
110        }
111        return (MqttThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class,(Class<?> c) -> {
112                    MqttThrottleManager t = new MqttThrottleManager(this);
113                    t.setSendThrottleTopic(getMqttAdapter().getOptionState("16.3"));
114                    t.setRcvThrottleTopic(getMqttAdapter().getOptionState("16.5"));
115                    t.setSendDirectionTopic(getMqttAdapter().getOptionState("17.3"));
116                    t.setRcvDirectionTopic(getMqttAdapter().getOptionState("17.5"));
117                    t.setSendFunctionTopic(getMqttAdapter().getOptionState("18.3"));
118                    t.setRcvFunctionTopic(getMqttAdapter().getOptionState("18.5"));
119                    return t;
120                });
121    }
122
123    public MqttPowerManager getPowerManager() {
124        if (getDisabled()) {
125            return null;
126        }
127        return (MqttPowerManager) classObjectMap.computeIfAbsent(PowerManager.class,(Class<?> c) -> {
128                    MqttPowerManager t = new MqttPowerManager(this);
129                    t.setSendTopic(getMqttAdapter().getOptionState("20.3"));
130                    t.setRcvTopic(getMqttAdapter().getOptionState("20.5"));
131                    return t;
132                });
133    }
134
135    @Override
136    public MqttConsistManager getConsistManager() {
137        if (getDisabled()) {
138            return null;
139        }
140        return (MqttConsistManager) classObjectMap.computeIfAbsent(ConsistManager.class,(Class<?> c) -> {
141                    MqttConsistManager t = new MqttConsistManager(this);
142                    t.setSendTopic(getMqttAdapter().getOptionState("19.3"));
143                    return t;
144                });
145    }
146
147    public void setPowerManager(@Nonnull PowerManager p) {
148        store(p,PowerManager.class);
149    }
150
151    @Override
152    public void setConsistManager(@Nonnull ConsistManager c) {
153        store(c,ConsistManager.class);
154    }
155
156    void setMqttAdapter(MqttAdapter ma) {
157        mqttAdapter = ma;
158    }
159
160    public MqttAdapter getMqttAdapter() {
161        return mqttAdapter;
162    }
163}