001package jmri.server.json.power;
002
003import static jmri.server.json.JSON.DEFAULT;
004import static jmri.server.json.JSON.NAME;
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.power.JsonPowerServiceFactory.POWER;
010
011import com.fasterxml.jackson.databind.JsonNode;
012import com.fasterxml.jackson.databind.ObjectMapper;
013import com.fasterxml.jackson.databind.node.ArrayNode;
014import com.fasterxml.jackson.databind.node.ObjectNode;
015import javax.annotation.CheckForNull;
016import javax.servlet.http.HttpServletResponse;
017import jmri.InstanceManager;
018import jmri.JmriException;
019import jmri.PowerManager;
020import jmri.server.json.JsonException;
021import jmri.server.json.JsonHttpService;
022import jmri.server.json.JsonRequest;
023
024/**
025 * @author Randall Wood Copyright 2016, 2018
026 */
027public class JsonPowerHttpService extends JsonHttpService {
028
029    public JsonPowerHttpService(ObjectMapper mapper) {
030        super(mapper);
031    }
032
033    @Override
034    // Nullable to override inherited NonNull requirement
035    public JsonNode doGet(String type, @CheckForNull String name, JsonNode parameters, JsonRequest request)
036            throws JsonException {
037        ObjectNode data = mapper.createObjectNode();
038        PowerManager manager = InstanceManager.getNullableDefault(PowerManager.class);
039        if (name != null && !name.isEmpty()) {
040            for (PowerManager pm : InstanceManager.getList(PowerManager.class)) {
041                if (pm.getUserName().equals(name)) {
042                    manager = pm;
043                }
044            }
045        }
046        if (manager != null) {
047            data.put(NAME, manager.getUserName());
048            switch (manager.getPower()) {
049                case PowerManager.OFF:
050                    data.put(STATE, OFF);
051                    break;
052                case PowerManager.ON:
053                    data.put(STATE, ON);
054                    break;
055                default:
056                    data.put(STATE, UNKNOWN);
057                    break;
058            }
059            data.put(DEFAULT, false);
060            if (manager.equals(InstanceManager.getDefault(PowerManager.class))) {
061                data.put(DEFAULT, true);
062            }
063        } else {
064            // No PowerManager is defined; just report it as UNKNOWN
065            data.put(STATE, UNKNOWN);
066            data.put(NAME, "");
067            data.put(DEFAULT, false);
068        }
069        return message(POWER, data, request.id);
070    }
071
072    @Override
073    public JsonNode doPost(String type, String name, JsonNode data, JsonRequest request) throws JsonException {
074        int state = data.path(STATE).asInt(UNKNOWN);
075        if (state != UNKNOWN) {
076            try {
077                PowerManager manager = InstanceManager.getNullableDefault(PowerManager.class);
078                if (!name.isEmpty()) {
079                    for (PowerManager pm : InstanceManager.getList(PowerManager.class)) {
080                        if (pm.getUserName().equals(name)) {
081                            manager = pm;
082                        }
083                    }
084                }
085                if (manager != null) {
086                    switch (state) {
087                        case OFF:
088                            manager.setPower(PowerManager.OFF);
089                            break;
090                        case ON:
091                            manager.setPower(PowerManager.ON);
092                            break;
093                        default:
094                            throw new JsonException(HttpServletResponse.SC_BAD_REQUEST,
095                                    Bundle.getMessage(request.locale, "ErrorUnknownState", POWER, state), request.id);
096                    }
097                }
098            } catch (JmriException ex) {
099                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex, request.id);
100            }
101        }
102        return this.doGet(type, name, data, request);
103    }
104
105    @Override
106    public JsonNode doGetList(String type, JsonNode data, JsonRequest request) throws JsonException {
107        ArrayNode array = this.mapper.createArrayNode();
108        for (PowerManager manager : InstanceManager.getList(PowerManager.class)) {
109            array.add(this.doGet(type, manager.getUserName(), data, request));
110        }
111        return message(array, request.id);
112    }
113
114    @Override
115    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
116        if (POWER.equals(type)) {
117            return doSchema(type,
118                    server,
119                    "jmri/server/json/power/power-server.json",
120                    "jmri/server/json/power/power-client.json",
121                    request.id);
122        } else {
123            throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
124                    Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
125        }
126    }
127}