001package jmri.server.json.time;
002
003import static jmri.server.json.JSON.OFF;
004import static jmri.server.json.JSON.ON;
005import static jmri.server.json.JSON.RATE;
006import static jmri.server.json.JSON.STATE;
007import static jmri.server.json.JSON.TIME;
008
009import com.fasterxml.jackson.databind.JsonNode;
010import com.fasterxml.jackson.databind.ObjectMapper;
011import com.fasterxml.jackson.databind.node.ArrayNode;
012import com.fasterxml.jackson.databind.node.ObjectNode;
013import com.fasterxml.jackson.databind.util.StdDateFormat;
014import java.text.ParseException;
015import java.util.Date;
016import javax.annotation.CheckForNull;
017import javax.annotation.Nonnull;
018import javax.servlet.http.HttpServletResponse;
019import jmri.InstanceManager;
020import jmri.Timebase;
021import jmri.TimebaseRateException;
022import jmri.server.json.JsonException;
023import jmri.server.json.JsonHttpService;
024import jmri.server.json.JsonRequest;
025
026/**
027 * @author Randall Wood
028 */
029public class JsonTimeHttpService extends JsonHttpService {
030
031    public JsonTimeHttpService(ObjectMapper mapper) {
032        super(mapper);
033    }
034
035    @Override
036    // using @CheckForNull to override @Nonnull in super class
037    public JsonNode doGet(String type, @CheckForNull String name, JsonNode data, JsonRequest request)
038            throws JsonException {
039        Timebase timebase = InstanceManager.getDefault(Timebase.class);
040        return doGet(timebase, timebase.getTime(), request.id);
041    }
042
043    public JsonNode doGet(@Nonnull Timebase timebase, @Nonnull Date date, int id) {
044        ObjectNode data = this.mapper.createObjectNode();
045        data.put(TIME, new StdDateFormat().format(date));
046        data.put(RATE, timebase.getRate());
047        data.put(STATE, timebase.getRun() ? ON : OFF);
048        return message(TIME, data, id);
049    }
050
051    @Override
052    // using @CheckForNull to override @Nonnull in super class
053    public JsonNode doPost(String type, @CheckForNull String name, JsonNode data, JsonRequest request)
054            throws JsonException {
055        Timebase timebase = InstanceManager.getDefault(Timebase.class);
056        try {
057            if (data.path(TIME).isTextual()) {
058                timebase.setTime(new StdDateFormat().parse(data.path(TIME).asText()));
059            }
060            if (data.path(RATE).isDouble() || data.path(RATE).isInt()) {
061                timebase.userSetRate(data.path(RATE).asDouble());
062            }
063            int state = data.path(STATE).asInt(0);
064            // passing the state UNKNOWN (0) will not trigger change
065            if (state == ON || state == OFF) {
066                timebase.setRun(state == ON);
067            }
068        } catch (ParseException ex) {
069            throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorTimeFormat"), request.id);
070        } catch (TimebaseRateException e) {
071            throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorRateFactor"), request.id);
072        }
073        return this.doGet(type, name, data, request);
074    }
075
076    @Override
077    public JsonNode doGetList(String type, JsonNode data, JsonRequest request) throws JsonException {
078        ArrayNode array = this.mapper.createArrayNode();
079        array.add(this.doGet(type, null, data, request));
080        return message(array, request.id);
081    }
082
083    @Override
084    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
085        if (TIME.equals(type)) {
086            return doSchema(type,
087                    server,
088                    "jmri/server/json/time/time-server.json",
089                    "jmri/server/json/time/time-client.json",
090                    request.id);
091        } else {
092            throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
093                    Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
094        }
095    }
096}