2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.amazonechocontrol.internal.channelhandler;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
18 import org.apache.commons.lang.StringEscapeUtils;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.amazonechocontrol.internal.Connection;
22 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.Command;
26 import com.google.gson.Gson;
27 import com.google.gson.JsonSyntaxException;
30 * The {@link ChannelHandlerAnnouncement} is responsible for the announcement
33 * @author Michael Geramb - Initial contribution
36 public class ChannelHandlerAnnouncement extends ChannelHandler {
38 private static final String CHANNEL_NAME = "announcement";
40 protected final IEchoThingHandler thingHandler;
42 public ChannelHandlerAnnouncement(IEchoThingHandler thingHandler, Gson gson) {
43 super(thingHandler, gson);
44 this.thingHandler = thingHandler;
48 public boolean tryHandleCommand(Device device, Connection connection, String channelId, Command command)
49 throws IOException, URISyntaxException {
50 if (channelId.equals(CHANNEL_NAME)) {
51 if (command instanceof StringType) {
52 String commandValue = ((StringType) command).toFullString();
53 String body = commandValue;
55 String speak = commandValue;
56 Integer volume = null;
57 if (commandValue.startsWith("{") && commandValue.endsWith("}")) {
59 AnnouncementRequestJson request = parseJson(commandValue, AnnouncementRequestJson.class);
60 if (request != null) {
61 speak = request.speak;
62 if (speak == null || speak.length() == 0) {
63 speak = " "; // blank generates a beep
65 volume = request.volume;
66 title = request.title;
71 Boolean sound = request.sound;
73 if (!sound && !speak.startsWith("<speak>")) {
74 speak = "<speak>" + StringEscapeUtils.escapeXml(speak) + "</speak>";
76 if (sound && speak.startsWith("<speak>")) {
77 body = "Error: The combination of sound and speak in SSML syntax is not allowed";
79 speak = "<speak><lang xml:lang=\"en-UK\">Error: The combination of sound and speak in <prosody rate=\"x-slow\"><say-as interpret-as=\"characters\">SSML</say-as></prosody> syntax is not allowed</lang></speak>";
82 if ("<speak> </speak>".equals(speak)) {
83 volume = -1; // Do not change volume
86 } catch (JsonSyntaxException e) {
87 body = "Invalid Json." + e.getLocalizedMessage();
89 speak = "<speak><lang xml:lang=\"en-US\">" + StringEscapeUtils.escapeXml(body)
91 body = e.getLocalizedMessage();
94 thingHandler.startAnnouncment(device, speak, body, title, volume);
101 private void refreshChannel() {
102 thingHandler.updateChannelState(CHANNEL_NAME, new StringType(""));
105 private static class AnnouncementRequestJson {
106 public @Nullable Boolean sound;
107 public @Nullable String title;
108 public @Nullable String body;
109 public @Nullable String speak;
110 public @Nullable Integer volume;