]> git.basschouten.com Git - openhab-addons.git/blob
92c0792b45a71506c7866239090ce382f3ceceae
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.time.LocalDateTime;
18
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
28 /**
29  * The {@link ChannelHandlerSendMessage} is responsible for the announcement
30  * channel
31  *
32  * @author Michael Geramb - Initial contribution
33  */
34 @NonNullByDefault
35 public class ChannelHandlerSendMessage extends ChannelHandler {
36
37     private static final String CHANNEL_NAME = "sendMessage";
38     private @Nullable AccountJson accountJson;
39     private int lastMessageId = 1000;
40
41     public ChannelHandlerSendMessage(IAmazonThingHandler thingHandler, Gson gson) {
42         super(thingHandler, gson);
43     }
44
45     @Override
46     public boolean tryHandleCommand(Device device, Connection connection, String channelId, Command command)
47             throws IOException, URISyntaxException, InterruptedException {
48         if (channelId.equals(CHANNEL_NAME)) {
49             if (command instanceof StringType) {
50                 String commandValue = ((StringType) command).toFullString();
51                 String baseUrl = "https://alexa-comms-mobile-service." + connection.getAmazonSite();
52
53                 AccountJson currentAccountJson = this.accountJson;
54                 if (currentAccountJson == null) {
55                     String accountResult = connection.makeRequestAndReturnString(baseUrl + "/accounts");
56                     AccountJson @Nullable [] accountsJson = gson.fromJson(accountResult, AccountJson[].class);
57                     if (accountsJson == null) {
58                         return false;
59                     }
60                     for (AccountJson accountJson : accountsJson) {
61                         Boolean signedInUser = accountJson.signedInUser;
62                         if (signedInUser != null && signedInUser) {
63                             this.accountJson = accountJson;
64                             currentAccountJson = accountJson;
65                             break;
66                         }
67                     }
68                 }
69                 if (currentAccountJson == null) {
70                     return false;
71                 }
72                 String commsId = currentAccountJson.commsId;
73                 if (commsId == null) {
74                     return false;
75                 }
76                 String senderCommsId = commsId;
77                 String receiverCommsId = commsId;
78
79                 SendConversationJson conversationJson = new SendConversationJson();
80                 conversationJson.conversationId = "amzn1.comms.messaging.id.conversationV2~31e6fe8f-8b0c-4e84-a1e4-80030a09009b";
81                 conversationJson.clientMessageId = java.util.UUID.randomUUID().toString();
82                 conversationJson.messageId = lastMessageId++;
83                 conversationJson.sender = senderCommsId;
84                 conversationJson.time = LocalDateTime.now().toString();
85                 conversationJson.payload.text = commandValue;
86
87                 String sendConversationBody = this.gson.toJson(new SendConversationJson[] { conversationJson });
88                 String sendUrl = baseUrl + "/users/" + senderCommsId + "/conversations/" + receiverCommsId
89                         + "/messages";
90                 connection.makeRequestAndReturnString("POST", sendUrl, sendConversationBody, true, null);
91             }
92             refreshChannel();
93         }
94         return false;
95     }
96
97     private void refreshChannel() {
98         thingHandler.updateChannelState(CHANNEL_NAME, new StringType(""));
99     }
100
101     @SuppressWarnings("unused")
102     private static class AccountJson {
103         public @Nullable String commsId;
104         public @Nullable String directedId;
105         public @Nullable String phoneCountryCode;
106         public @Nullable String phoneNumber;
107         public @Nullable String firstName;
108         public @Nullable String lastName;
109         public @Nullable String phoneticFirstName;
110         public @Nullable String phoneticLastName;
111         public @Nullable String commsProvisionStatus;
112         public @Nullable Boolean isChild;
113         public @Nullable Boolean signedInUser;
114         public @Nullable Boolean commsProvisioned;
115         public @Nullable Boolean speakerProvisioned;
116     }
117
118     @SuppressWarnings("unused")
119     private static class SendConversationJson {
120         public @Nullable String conversationId;
121         public @Nullable String clientMessageId;
122         public @Nullable Integer messageId;
123         public @Nullable String time;
124         public @Nullable String sender;
125         public String type = "message/text";
126         public Payload payload = new Payload();
127         public Integer status = 1;
128
129         private static class Payload {
130             public @Nullable String text;
131         }
132     }
133 }