001package jmri.server.json.oblock;
002
003import com.fasterxml.jackson.databind.JsonNode;
004import com.fasterxml.jackson.databind.ObjectMapper;
005//import com.fasterxml.jackson.databind.node.ArrayNode;
006import com.fasterxml.jackson.databind.node.ObjectNode;
007import jmri.*;
008import jmri.jmrit.logix.OBlock;
009import jmri.jmrit.logix.OBlockManager;
010import jmri.server.json.JSON;
011import jmri.server.json.JsonException;
012import jmri.server.json.JsonNamedBeanHttpService;
013import jmri.server.json.JsonRequest;
014import jmri.server.json.sensor.JsonSensor;
015
016import javax.servlet.http.HttpServletResponse;
017
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021import static jmri.server.json.oblock.JsonOblock.OBLOCK;
022import static jmri.server.json.oblock.JsonOblock.OBLOCKS;
023
024/**
025 * Copied from jmri/server/json/blocks.java
026 *
027 * @author mstevetodd Copyright 2018
028 * @author Randall Wood Copyright 2018, 2019
029 * @author Egbert Broerse Copyright 2020
030 */
031public class JsonOblockHttpService extends JsonNamedBeanHttpService<OBlock> {
032
033//    private JsonIdTagHttpService idTagService = new JsonIdTagHttpService(mapper);
034//    private JsonReporterHttpService reporterService = new JsonReporterHttpService(mapper);
035//    private JsonRosterHttpService rosterService = new JsonRosterHttpService(mapper);
036
037    public JsonOblockHttpService(ObjectMapper mapper) {
038        super(mapper);
039    }
040
041    @Override
042    public ObjectNode doGet(OBlock oblock, String name, String type, JsonRequest request) throws JsonException {
043        ObjectNode root = this.getNamedBean(oblock, name, getType(), request);
044        ObjectNode data = root.with(JSON.DATA);
045        log.debug("oblock.getState() = {}", oblock.getState());
046        switch (oblock.getState()) {
047            case OBlock.UNDETECTED:
048            case NamedBean.UNKNOWN:
049                data.put(JsonOblock.STATUS, JSON.UNKNOWN);
050                break;
051            default:
052                // add OBlock status, includes special values for Allocated 0x10, OutOfService 0x40 etc.
053                data.put(JsonOblock.STATUS, oblock.getState());
054        }
055        data.put(JsonSensor.SENSOR, oblock.getSensor() != null ? oblock.getSensor().getSystemName() : null);
056        data.put(JsonOblock.WARRANT, oblock.getWarrant() != null ? oblock.getWarrant().getDisplayName() : null); // add OBlock Warrant name
057        data.put(JsonOblock.TRAIN, oblock.getWarrant() != null ? oblock.getWarrant().getTrainName() : null); // add OBlock Warrant name
058
059        return root;
060    }
061
062    @Override
063    public ObjectNode doPost(OBlock oblock, String name, String type, JsonNode data, JsonRequest request)
064            throws JsonException {
065        if (!data.path(JSON.VALUE).isMissingNode()) {
066            if (data.path(JSON.VALUE).isNull()) {
067                oblock.setValue(null);
068            } else {
069                oblock.setValue(data.path(JSON.VALUE).asText());
070            }
071        }
072        int status = data.path(JsonOblock.STATUS).asInt(JSON.UNKNOWN);
073        switch (status) {
074            case JSON.ACTIVE:
075                oblock.setState(OBlock.OCCUPIED);
076                break;
077            case JSON.INACTIVE:
078                oblock.setState(OBlock.UNOCCUPIED);
079                break;
080            case JSON.UNKNOWN:
081                // leave state alone in this case
082                break;
083            case JSON.ALLOCATED:
084                oblock.setState(OBlock.ALLOCATED);
085                break;
086            case JSON.RUNNING:
087                oblock.setState(OBlock.RUNNING);
088                break;
089            case JSON.OUT_OF_SERVICE:
090                oblock.setState(OBlock.OUT_OF_SERVICE);
091                break;
092            case JSON.TRACK_ERROR:
093                oblock.setState(OBlock.TRACK_ERROR);
094                break;
095            default:
096                throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorUnknownState", OBLOCK, status),
097                        request.id);
098        }
099        if (!data.path(JsonSensor.SENSOR).isMissingNode()) {
100            JsonNode node = data.path(JsonSensor.SENSOR);
101            if (node.isNull()) {
102                oblock.setSensor(null);
103            } else {
104                Sensor sensor = InstanceManager.getDefault(SensorManager.class).getBySystemName(node.asText());
105                if (sensor != null) {
106                    oblock.setSensor(sensor.getSystemName());
107                } else {
108                    throw new JsonException(404,
109                            Bundle.getMessage(request.locale, JsonException.ERROR_NOT_FOUND, JsonSensor.SENSOR,
110                                    node.asText()),
111                            request.id);
112                }
113            }
114        }
115        if (!data.path(JsonOblock.TRAIN).isMissingNode()) {
116            String text = data.path(JsonOblock.TRAIN).asText(oblock.getBlockSpeed());
117            oblock.getWarrant().setTrainName(text);
118        }
119        // TODO add Train, Warrant
120        return this.doGet(oblock, name, type, request);
121    }
122
123    @Override
124    protected void doDelete(OBlock bean, String name, String type, JsonNode data, JsonRequest request)
125            throws JsonException {
126        deleteBean(bean, name, type, data, request);
127    }
128
129    @Override
130    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
131        switch (type) {
132            case OBLOCK:
133            case OBLOCKS:
134                return doSchema(type,
135                        server,
136                        "jmri/server/json/oblock/oblock-server.json",
137                        "jmri/server/json/oblock/oblock-client.json",
138                        request.id);
139            default:
140                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
141                        Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
142        }
143    }
144
145    @Override
146    protected String getType() {
147        return OBLOCK;
148    }
149
150    @Override
151    protected ProvidingManager<OBlock> getProvidingManager() {
152        return InstanceManager.getDefault(OBlockManager.class);
153    }
154
155    private final static Logger log = LoggerFactory.getLogger(JsonOblockHttpService.class);
156
157}