001package jmri.server.json.signalmast;
002
003import static jmri.server.json.JSON.ASPECT;
004import static jmri.server.json.JSON.ASPECT_DARK;
005import static jmri.server.json.JSON.ASPECT_HELD;
006import static jmri.server.json.JSON.ASPECT_UNKNOWN;
007import static jmri.server.json.JSON.DATA;
008import static jmri.server.json.JSON.LIT;
009import static jmri.server.json.JSON.STATE;
010import static jmri.server.json.JSON.TOKEN_HELD;
011import static jmri.server.json.signalmast.JsonSignalMast.SIGNAL_MAST;
012import static jmri.server.json.signalmast.JsonSignalMast.SIGNAL_MASTS;
013
014import com.fasterxml.jackson.databind.JsonNode;
015import com.fasterxml.jackson.databind.ObjectMapper;
016import com.fasterxml.jackson.databind.node.ObjectNode;
017import javax.servlet.http.HttpServletResponse;
018import jmri.InstanceManager;
019import jmri.ProvidingManager;
020import jmri.SignalMast;
021import jmri.SignalMastManager;
022import jmri.server.json.JsonException;
023import jmri.server.json.JsonNamedBeanHttpService;
024import jmri.server.json.JsonRequest;
025
026/**
027 * JSON HTTP service for {@link jmri.SignalMast}s.
028 *
029 * @author Randall Wood Copyright 2016, 2018
030 */
031public class JsonSignalMastHttpService extends JsonNamedBeanHttpService<SignalMast> {
032
033    public JsonSignalMastHttpService(ObjectMapper mapper) {
034        super(mapper);
035    }
036
037    @Override
038    public ObjectNode doGet(SignalMast signalMast, String name, String type, JsonRequest request) throws JsonException {
039        ObjectNode root = this.getNamedBean(signalMast, name, getType(), request); // throws if signalMast is null
040        ObjectNode data = root.with(DATA);
041        String aspect = signalMast.getAspect();
042        if (aspect == null) {
043            aspect = ASPECT_UNKNOWN; //if null, set aspect to "Unknown"
044        }
045        data.put(ASPECT, aspect);
046        data.put(LIT, signalMast.getLit());
047        data.put(TOKEN_HELD, signalMast.getHeld());
048        // state is appearance, plus flags for held and dark statuses
049        if ((signalMast.getHeld()) && (signalMast.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.HELD) != null)) {
050            data.put(STATE, ASPECT_HELD);
051        } else if ((!signalMast.getLit()) && (signalMast.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.DARK) != null)) {
052            data.put(STATE, ASPECT_DARK);
053        } else {
054            data.put(STATE, aspect);
055        }
056        return root;
057    }
058
059    @Override
060    public ObjectNode doPost(SignalMast signalMast, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
061        if (data.path(STATE).isTextual()) {
062            String aspect = data.path(STATE).asText();
063            if (aspect.equals(ASPECT_HELD)) {
064                signalMast.setHeld(true);
065            } else if (signalMast.getValidAspects().contains(aspect)) {
066                if (signalMast.getHeld()) {
067                    signalMast.setHeld(false);
068                }
069                String thisAspect = signalMast.getAspect();
070                if (thisAspect == null || !thisAspect.equals(aspect)) {
071                    signalMast.setAspect(aspect);
072                }
073            } else {
074                throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorUnknownState", SIGNAL_MAST, aspect), request.id);
075            }
076        }
077        if (data.path(LIT).isTextual()) {
078            signalMast.setLit(data.path(LIT).asBoolean());
079        }
080        if (data.path(TOKEN_HELD).isTextual()) {
081            signalMast.setHeld(data.path(TOKEN_HELD).asBoolean());
082        }
083        return this.doGet(signalMast, name, type, request);
084    }
085
086    @Override
087    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
088        switch (type) {
089            case SIGNAL_MAST:
090            case SIGNAL_MASTS:
091                return doSchema(type,
092                        server,
093                        "jmri/server/json/signalmast/signalMast-server.json",
094                        "jmri/server/json/signalmast/signalMast-client.json",
095                        request.id);
096            default:
097                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
098        }
099    }
100
101    @Override
102    protected String getType() {
103        return SIGNAL_MAST;
104    }
105
106    @Override
107    protected ProvidingManager<SignalMast> getProvidingManager() {
108        return InstanceManager.getDefault(SignalMastManager.class);
109    }
110}