001package jmri.server.json.route;
002
003import jmri.Route;
004import jmri.Sensor;
005import jmri.server.json.JsonConnection;
006import jmri.server.json.JsonNamedBeanSocketService;
007
008/**
009 * JSON socket service provider for managing {@link jmri.Route}s.
010 *
011 * @author Randall Wood
012 */
013public class JsonRouteSocketService extends JsonNamedBeanSocketService<Route, JsonRouteHttpService> {
014
015    public JsonRouteSocketService(JsonConnection connection) {
016        super(connection, new JsonRouteHttpService(connection.getObjectMapper()));
017    }
018    
019    @Override
020    protected void addListenerToBean(Route bean) {
021        if (bean != null) {
022            NamedBeanListener listener = new NamedBeanListener(bean);
023            bean.addPropertyChangeListener(listener);
024            Sensor sensor = bean.getTurnoutsAlgdSensor();
025            if (sensor != null) {
026                sensor.addPropertyChangeListener(listener);
027            }
028            this.beanListeners.put(bean, listener);
029        }
030    }
031
032    @Override
033    public void onClose() {
034        beanListeners.values().stream().forEach((listener) -> {
035            Sensor sensor = listener.bean.getTurnoutsAlgdSensor();
036            if (sensor != null) {
037                sensor.removePropertyChangeListener(listener);
038            }
039        });
040        super.onClose();
041    }
042}