001package jmri.server.json.light;
002
003import static jmri.server.json.JSON.DATA;
004import static jmri.server.json.JSON.INCONSISTENT;
005import static jmri.server.json.JSON.OFF;
006import static jmri.server.json.JSON.ON;
007import static jmri.server.json.JSON.STATE;
008import static jmri.server.json.JSON.UNKNOWN;
009import static jmri.server.json.light.JsonLight.LIGHT;
010import static jmri.server.json.light.JsonLight.LIGHTS;
011
012import com.fasterxml.jackson.databind.JsonNode;
013import com.fasterxml.jackson.databind.ObjectMapper;
014import com.fasterxml.jackson.databind.node.ObjectNode;
015import javax.servlet.http.HttpServletResponse;
016
017import jmri.DigitalIO;
018import jmri.InstanceManager;
019import jmri.Light;
020import jmri.LightManager;
021import jmri.NamedBean;
022import jmri.ProvidingManager;
023import jmri.server.json.JsonException;
024import jmri.server.json.JsonNamedBeanHttpService;
025import jmri.server.json.JsonRequest;
026
027/**
028 *
029 * @author Randall Wood
030 */
031public class JsonLightHttpService extends JsonNamedBeanHttpService<Light> {
032
033    public JsonLightHttpService(ObjectMapper mapper) {
034        super(mapper);
035    }
036
037    @Override
038    public ObjectNode doGet(Light light, String name, String type, JsonRequest request) throws JsonException {
039        ObjectNode root = this.getNamedBean(light, name, getType(), request);
040        ObjectNode data = root.with(DATA);
041        if (light != null) {
042            switch (light.getState()) {
043                case DigitalIO.ON:
044                    data.put(STATE, ON);
045                    break;
046                case DigitalIO.OFF:
047                    data.put(STATE, OFF);
048                    break;
049                case NamedBean.INCONSISTENT:
050                    data.put(STATE, INCONSISTENT);
051                    break;
052                case NamedBean.UNKNOWN:
053                default:
054                    data.put(STATE, UNKNOWN);
055                    break;
056            }
057        }
058        return root;
059    }
060
061    @Override
062    public ObjectNode doPost(Light light, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
063        int state = data.path(STATE).asInt(UNKNOWN);
064        switch (state) {
065            case ON:
066                light.setState(DigitalIO.ON);
067                break;
068            case OFF:
069                light.setState(DigitalIO.OFF);
070                break;
071            case UNKNOWN:
072                // leave state alone in this case
073                break;
074            default:
075                throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorUnknownState", LIGHT, state), request.id);
076        }
077        return this.doGet(light, name, type, request);
078    }
079
080    @Override
081    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
082        switch (type) {
083            case LIGHT:
084            case LIGHTS:
085                return doSchema(type,
086                        server,
087                        "jmri/server/json/light/light-server.json",
088                        "jmri/server/json/light/light-client.json",
089                        request.id);
090            default:
091                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
092        }
093    }
094
095    @Override
096    protected String getType() {
097        return LIGHT;
098    }
099
100    @Override
101    protected ProvidingManager<Light> getProvidingManager() {
102        return InstanceManager.getDefault(LightManager.class);
103    }
104}