]> git.basschouten.com Git - openhab-addons.git/blob
6838f72cf9f13c21a3d1ada529e8a4ec0fa07c9d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.amazonechocontrol.internal.channelhandler;
14
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17
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;
25
26 import com.google.gson.Gson;
27 import com.google.gson.JsonSyntaxException;
28
29 /**
30  * The {@link ChannelHandlerAnnouncement} is responsible for the announcement
31  * channel
32  *
33  * @author Michael Geramb - Initial contribution
34  */
35 @NonNullByDefault
36 public class ChannelHandlerAnnouncement extends ChannelHandler {
37
38     private static final String CHANNEL_NAME = "announcement";
39
40     protected final IEchoThingHandler thingHandler;
41
42     public ChannelHandlerAnnouncement(IEchoThingHandler thingHandler, Gson gson) {
43         super(thingHandler, gson);
44         this.thingHandler = thingHandler;
45     }
46
47     @Override
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;
54                 String title = null;
55                 String speak = commandValue;
56                 Integer volume = null;
57                 if (commandValue.startsWith("{") && commandValue.endsWith("}")) {
58                     try {
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
64                             }
65                             volume = request.volume;
66                             title = request.title;
67                             body = request.body;
68                             if (body == null) {
69                                 body = speak;
70                             }
71                             Boolean sound = request.sound;
72                             if (sound != null) {
73                                 if (!sound && !speak.startsWith("<speak>")) {
74                                     speak = "<speak>" + StringEscapeUtils.escapeXml(speak) + "</speak>";
75                                 }
76                                 if (sound && speak.startsWith("<speak>")) {
77                                     body = "Error: The combination of sound and speak in SSML syntax is not allowed";
78                                     title = "Error";
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>";
80                                 }
81                             }
82                             if ("<speak> </speak>".equals(speak)) {
83                                 volume = -1; // Do not change volume
84                             }
85                         }
86                     } catch (JsonSyntaxException e) {
87                         body = "Invalid Json." + e.getLocalizedMessage();
88                         title = "Error";
89                         speak = "<speak><lang xml:lang=\"en-US\">" + StringEscapeUtils.escapeXml(body)
90                                 + "</lang></speak>";
91                         body = e.getLocalizedMessage();
92                     }
93                 }
94                 thingHandler.startAnnouncment(device, speak, body, title, volume);
95             }
96             refreshChannel();
97         }
98         return false;
99     }
100
101     private void refreshChannel() {
102         thingHandler.updateChannelState(CHANNEL_NAME, new StringType(""));
103     }
104
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;
111     }
112 }