001package jmri.server.json.audio;
002
003import static jmri.server.json.audio.JsonAudio.AUDIO;
004import static jmri.server.json.audio.JsonAudio.AUDIOS;
005
006import com.fasterxml.jackson.databind.JsonNode;
007import com.fasterxml.jackson.databind.ObjectMapper;
008import com.fasterxml.jackson.databind.node.ObjectNode;
009
010import javax.servlet.http.HttpServletResponse;
011
012import jmri.InstanceManager;
013import jmri.Manager;
014import jmri.ProvidingManager;
015import jmri.Audio;
016import jmri.AudioManager;
017import jmri.jmrit.audio.AudioSource;
018import jmri.server.json.JSON;
019import jmri.server.json.JsonException;
020import jmri.server.json.JsonNamedBeanHttpService;
021import jmri.server.json.JsonRequest;
022
023/**
024 * JSON HTTP Service for {@link jmri.Audio}s.
025 *
026 * @author Randall Wood      Copyright 2016, 2018
027 * @author Daniel Bergqvist  Copyright 2023
028 */
029public class JsonAudioHttpService extends JsonNamedBeanHttpService<Audio> {
030
031    public JsonAudioHttpService(ObjectMapper mapper) {
032        super(mapper);
033    }
034
035    @Override
036    public ObjectNode doGet(Audio audio, String name, String type, JsonRequest request) throws JsonException {
037        if (audio.getSubType() != Audio.SOURCE || (!(audio instanceof AudioSource))) {
038            throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorAudioNotSource", AUDIO, audio.getSubType()), request.id);
039        }
040        AudioSource audioSource = (AudioSource) audio;
041        ObjectNode root = this.getNamedBean(audio, name, getType(), request); // throws JsonException if audio == null
042        ObjectNode data = root.with(JSON.DATA);
043        switch (audio.getState()) {
044            case Audio.STATE_PLAYING:
045                data.put(JSON.STATE, JSON.AUDIO_PLAYING);
046                data.put(JSON.AUDIO_COMMAND_PLAY_NUM_LOOPS, audioSource.getLastNumLoops());
047                break;
048            case Audio.STATE_STOPPED:
049                data.put(JSON.STATE, JSON.AUDIO_STOPPED);
050                break;
051            default:
052                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
053                        Bundle.getMessage(request.locale, "ErrorInternal", type), request.id); // NOI18N
054        }
055        return root;
056    }
057
058    @Override
059    public ObjectNode doPost(Audio audio, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
060
061        if (audio.getSubType() != Audio.SOURCE || (!(audio instanceof AudioSource))) {
062            throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorAudioNotSource", AUDIO, audio.getSubType()), request.id);
063        }
064
065        AudioSource audioSource = (AudioSource) audio;
066        String command = data.path(JSON.AUDIO_COMMAND).asText();
067        switch (command) {
068            case JSON.AUDIO_COMMAND_PLAY:
069                audioSource.play();
070                break;
071            case JSON.AUDIO_COMMAND_STOP:
072                audioSource.stop();
073                break;
074            default:
075                throw new JsonException(400, Bundle.getMessage(request.locale, "ErrorAudioUnknownCommand", command), request.id);
076        }
077        return this.doGet(audio, name, type, request);
078    }
079
080    @Override
081    protected void doDelete(Audio bean, String name, String type, JsonNode data, JsonRequest request)
082            throws JsonException {
083        deleteBean(bean, name, type, data, request);
084    }
085
086    @Override
087    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
088        switch (type) {
089            case AUDIO:
090            case AUDIOS:
091                return doSchema(type,
092                        server,
093                        "jmri/server/json/audio/audio-server.json",
094                        "jmri/server/json/audio/audio-client.json",
095                        request.id);
096            default:
097                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
098                        Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
099        }
100    }
101
102    @Override
103    protected String getType() {
104        return AUDIO;
105    }
106
107    @Override
108    protected Manager<Audio> getManager() {
109        return InstanceManager.getDefault(AudioManager.class);
110    }
111
112    @Override
113    protected ProvidingManager<Audio> getProvidingManager() throws UnsupportedOperationException {
114        throw new UnsupportedOperationException("Not supported since AudioManager is not a ProvidingManager");
115    }
116
117}