001package jmri.server.json.sensor;
002
003import static jmri.server.json.sensor.JsonSensor.SENSOR;
004import static jmri.server.json.sensor.JsonSensor.SENSORS;
005
006import com.fasterxml.jackson.databind.JsonNode;
007import com.fasterxml.jackson.databind.ObjectMapper;
008import com.fasterxml.jackson.databind.node.ObjectNode;
009import javax.servlet.http.HttpServletResponse;
010import jmri.InstanceManager;
011import jmri.JmriException;
012import jmri.NamedBean;
013import jmri.ProvidingManager;
014import jmri.Sensor;
015import jmri.SensorManager;
016import jmri.server.json.JSON;
017import jmri.server.json.JsonException;
018import jmri.server.json.JsonNamedBeanHttpService;
019import jmri.server.json.JsonRequest;
020
021/**
022 * JSON HTTP Service for {@link jmri.Sensor}s.
023 *
024 * @author Randall Wood Copyright 2016, 2018
025 */
026public class JsonSensorHttpService extends JsonNamedBeanHttpService<Sensor> {
027
028    public JsonSensorHttpService(ObjectMapper mapper) {
029        super(mapper);
030    }
031
032    @Override
033    public ObjectNode doGet(Sensor sensor, String name, String type, JsonRequest request) throws JsonException {
034        ObjectNode root = this.getNamedBean(sensor, name, getType(), request); // throws JsonException if sensor == null
035        ObjectNode data = root.with(JSON.DATA);
036        data.put(JSON.INVERTED, sensor.getInverted());
037        switch (sensor.getKnownState()) {
038            case Sensor.ACTIVE:
039                data.put(JSON.STATE, JSON.ACTIVE);
040                break;
041            case Sensor.INACTIVE:
042                data.put(JSON.STATE, JSON.INACTIVE);
043                break;
044            case NamedBean.INCONSISTENT:
045                data.put(JSON.STATE, JSON.INCONSISTENT);
046                break;
047            case NamedBean.UNKNOWN:
048                data.put(JSON.STATE, JSON.UNKNOWN);
049                break;
050            default:
051                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
052                        Bundle.getMessage(request.locale, "ErrorInternal", type), request.id); // NOI18N
053        }
054        return root;
055    }
056
057    @Override
058    public ObjectNode doPost(Sensor sensor, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
059        if (data.path(JSON.INVERTED).isBoolean()) {
060            sensor.setInverted(data.path(JSON.INVERTED).asBoolean());
061        }
062        int state = data.path(JSON.STATE).asInt(JSON.UNKNOWN);
063        try {
064            switch (state) {
065                case JSON.ACTIVE:
066                    sensor.setKnownState(Sensor.ACTIVE);
067                    break;
068                case JSON.INACTIVE:
069                    sensor.setKnownState(Sensor.INACTIVE);
070                    break;
071                case JSON.INCONSISTENT:
072                case JSON.UNKNOWN:
073                    // silently ignore
074                    break;
075                default:
076                    throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorUnknownState", SENSOR, state), request.id);
077            }
078        } catch (JmriException ex) {
079            throw new JsonException(500, ex, request.id);
080        }
081        return this.doGet(sensor, name, type, request);
082    }
083
084    @Override
085    protected void doDelete(Sensor bean, String name, String type, JsonNode data, JsonRequest request)
086            throws JsonException {
087        deleteBean(bean, name, type, data, request);
088    }
089
090    @Override
091    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
092        switch (type) {
093            case SENSOR:
094            case SENSORS:
095                return doSchema(type,
096                        server,
097                        "jmri/server/json/sensor/sensor-server.json",
098                        "jmri/server/json/sensor/sensor-client.json",
099                        request.id);
100            default:
101                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
102                        Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
103        }
104    }
105
106    @Override
107    protected String getType() {
108        return SENSOR;
109    }
110
111    @Override
112    protected ProvidingManager<Sensor> getProvidingManager() {
113        return InstanceManager.getDefault(SensorManager.class);
114    }
115
116}