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.handler;
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.*;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.apache.commons.lang.StringUtils;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.amazonechocontrol.internal.Connection;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.storage.Storage;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.binding.BaseThingHandler;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link FlashBriefingProfileHandler} is responsible for storing and loading of a flash briefing configuration
43 * @author Michael Geramb - Initial contribution
46 public class FlashBriefingProfileHandler extends BaseThingHandler {
48 private final Logger logger = LoggerFactory.getLogger(FlashBriefingProfileHandler.class);
51 AccountHandler accountHandler;
52 Storage<String> stateStorage;
53 boolean updatePlayOnDevice = true;
54 String currentConfigurationJson = "";
55 private @Nullable ScheduledFuture<?> updateStateJob;
57 public FlashBriefingProfileHandler(Thing thing, Storage<String> storage) {
59 this.stateStorage = storage;
62 public @Nullable AccountHandler findAccountHandler() {
63 return this.accountHandler;
67 public void initialize() {
68 updatePlayOnDevice = true;
69 logger.info("{} initialized", getClass().getSimpleName());
70 if (!this.currentConfigurationJson.isEmpty()) {
71 updateStatus(ThingStatus.ONLINE);
73 updateStatus(ThingStatus.UNKNOWN);
74 Bridge bridge = this.getBridge();
76 AccountHandler account = (AccountHandler) bridge.getHandler();
77 if (account != null) {
78 account.addFlashBriefingProfileHandler(this);
85 public void dispose() {
86 ScheduledFuture<?> updateStateJob = this.updateStateJob;
87 this.updateStateJob = null;
88 if (updateStateJob != null) {
89 updateStateJob.cancel(false);
95 public void handleCommand(ChannelUID channelUID, Command command) {
96 AccountHandler accountHandler = this.accountHandler;
97 if (accountHandler == null) {
100 int waitForUpdate = -1;
102 ScheduledFuture<?> updateStateJob = this.updateStateJob;
103 this.updateStateJob = null;
104 if (updateStateJob != null) {
105 updateStateJob.cancel(false);
108 String channelId = channelUID.getId();
109 if (command instanceof RefreshType) {
112 if (channelId.equals(CHANNEL_SAVE)) {
113 if (command.equals(OnOffType.ON)) {
114 saveCurrentProfile(accountHandler);
118 if (channelId.equals(CHANNEL_ACTIVE)) {
119 if (command.equals(OnOffType.ON)) {
120 String currentConfigurationJson = this.currentConfigurationJson;
121 if (!currentConfigurationJson.isEmpty()) {
122 accountHandler.setEnabledFlashBriefingsJson(currentConfigurationJson);
123 updateState(CHANNEL_ACTIVE, OnOffType.ON);
128 if (channelId.equals(CHANNEL_PLAY_ON_DEVICE)) {
129 if (command instanceof StringType) {
130 String deviceSerialOrName = ((StringType) command).toFullString();
131 String currentConfigurationJson = this.currentConfigurationJson;
132 if (!currentConfigurationJson.isEmpty()) {
133 String old = accountHandler.getEnabledFlashBriefingsJson();
134 accountHandler.setEnabledFlashBriefingsJson(currentConfigurationJson);
135 Device device = accountHandler.findDeviceJsonBySerialOrName(deviceSerialOrName);
136 if (device == null) {
137 logger.warn("Device '{}' not found", deviceSerialOrName);
140 Connection connection = accountHandler.findConnection();
141 if (connection == null) {
142 logger.warn("Connection for '{}' not found",
143 accountHandler.getThing().getUID().getId());
145 connection.executeSequenceCommand(device, "Alexa.FlashBriefing.Play", null);
147 scheduler.schedule(() -> accountHandler.setEnabledFlashBriefingsJson(old), 1000,
148 TimeUnit.MILLISECONDS);
150 updateState(CHANNEL_ACTIVE, OnOffType.ON);
153 updatePlayOnDevice = true;
154 waitForUpdate = 1000;
158 } catch (IOException | URISyntaxException e) {
159 logger.warn("Handle command failed", e);
161 if (waitForUpdate >= 0) {
162 this.updateStateJob = scheduler.schedule(() -> accountHandler.updateFlashBriefingHandlers(), waitForUpdate,
163 TimeUnit.MILLISECONDS);
167 public boolean initialize(AccountHandler handler, String currentConfigurationJson) {
168 updateState(CHANNEL_SAVE, OnOffType.OFF);
169 if (updatePlayOnDevice) {
170 updateState(CHANNEL_PLAY_ON_DEVICE, new StringType(""));
172 if (this.accountHandler != handler) {
173 this.accountHandler = handler;
174 String configurationJson = this.stateStorage.get("configurationJson");
175 if (configurationJson == null || configurationJson.isEmpty()) {
176 this.currentConfigurationJson = saveCurrentProfile(handler);
178 this.currentConfigurationJson = configurationJson;
180 if (!this.currentConfigurationJson.isEmpty()) {
181 updateStatus(ThingStatus.ONLINE);
184 updateStatus(ThingStatus.UNKNOWN);
187 if (this.currentConfigurationJson.equals(currentConfigurationJson)) {
188 updateState(CHANNEL_ACTIVE, OnOffType.ON);
190 updateState(CHANNEL_ACTIVE, OnOffType.OFF);
192 return StringUtils.equals(this.currentConfigurationJson, currentConfigurationJson);
195 private String saveCurrentProfile(AccountHandler connection) {
196 String configurationJson = "";
197 configurationJson = connection.getEnabledFlashBriefingsJson();
198 this.currentConfigurationJson = configurationJson;
199 if (!configurationJson.isEmpty()) {
200 this.stateStorage.put("configurationJson", configurationJson);
202 return configurationJson;