2 * Copyright (c) 2010-2022 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.*;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.amazonechocontrol.internal.Connection;
24 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.storage.Storage;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.binding.BaseThingHandler;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.RefreshType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link FlashBriefingProfileHandler} is responsible for storing and loading of a flash briefing configuration
41 * @author Michael Geramb - Initial contribution
44 public class FlashBriefingProfileHandler extends BaseThingHandler {
46 private final Logger logger = LoggerFactory.getLogger(FlashBriefingProfileHandler.class);
49 AccountHandler accountHandler;
50 Storage<String> stateStorage;
51 boolean updatePlayOnDevice = true;
52 String currentConfigurationJson = "";
53 private @Nullable ScheduledFuture<?> updateStateJob;
55 public FlashBriefingProfileHandler(Thing thing, Storage<String> storage) {
57 this.stateStorage = storage;
60 public @Nullable AccountHandler findAccountHandler() {
61 return this.accountHandler;
65 public void initialize() {
66 updatePlayOnDevice = true;
67 logger.info("{} initialized", getClass().getSimpleName());
68 if (!this.currentConfigurationJson.isEmpty()) {
69 updateStatus(ThingStatus.ONLINE);
71 updateStatus(ThingStatus.UNKNOWN);
72 Bridge bridge = this.getBridge();
74 AccountHandler account = (AccountHandler) bridge.getHandler();
75 if (account != null) {
76 account.addFlashBriefingProfileHandler(this);
83 public void dispose() {
84 ScheduledFuture<?> updateStateJob = this.updateStateJob;
85 this.updateStateJob = null;
86 if (updateStateJob != null) {
87 updateStateJob.cancel(false);
93 public void handleCommand(ChannelUID channelUID, Command command) {
94 AccountHandler accountHandler = this.accountHandler;
95 if (accountHandler == null) {
98 int waitForUpdate = -1;
100 ScheduledFuture<?> updateStateJob = this.updateStateJob;
101 this.updateStateJob = null;
102 if (updateStateJob != null) {
103 updateStateJob.cancel(false);
105 String channelId = channelUID.getId();
106 if (command instanceof RefreshType) {
109 if (channelId.equals(CHANNEL_SAVE)) {
110 if (command.equals(OnOffType.ON)) {
111 saveCurrentProfile(accountHandler);
115 if (channelId.equals(CHANNEL_ACTIVE)) {
116 if (command.equals(OnOffType.ON)) {
117 String currentConfigurationJson = this.currentConfigurationJson;
118 if (!currentConfigurationJson.isEmpty()) {
119 accountHandler.setEnabledFlashBriefingsJson(currentConfigurationJson);
120 updateState(CHANNEL_ACTIVE, OnOffType.ON);
125 if (channelId.equals(CHANNEL_PLAY_ON_DEVICE)) {
126 if (command instanceof StringType) {
127 String deviceSerialOrName = command.toFullString();
128 String currentConfigurationJson = this.currentConfigurationJson;
129 if (!currentConfigurationJson.isEmpty()) {
130 String old = accountHandler.getEnabledFlashBriefingsJson();
131 accountHandler.setEnabledFlashBriefingsJson(currentConfigurationJson);
132 Device device = accountHandler.findDeviceJsonBySerialOrName(deviceSerialOrName);
133 if (device == null) {
134 logger.warn("Device '{}' not found", deviceSerialOrName);
137 Connection connection = accountHandler.findConnection();
138 if (connection == null) {
139 logger.warn("Connection for '{}' not found", accountHandler.getThing().getUID().getId());
141 connection.executeSequenceCommand(device, "Alexa.FlashBriefing.Play", Map.of());
143 scheduler.schedule(() -> accountHandler.setEnabledFlashBriefingsJson(old), 1000,
144 TimeUnit.MILLISECONDS);
146 updateState(CHANNEL_ACTIVE, OnOffType.ON);
149 updatePlayOnDevice = true;
150 waitForUpdate = 1000;
154 if (waitForUpdate >= 0) {
155 this.updateStateJob = scheduler.schedule(() -> accountHandler.updateFlashBriefingHandlers(), waitForUpdate,
156 TimeUnit.MILLISECONDS);
160 @SuppressWarnings("PMD.CompareObjectsWithEquals")
161 public boolean initialize(AccountHandler handler, String currentConfigurationJson) {
162 updateState(CHANNEL_SAVE, OnOffType.OFF);
163 if (updatePlayOnDevice) {
164 updateState(CHANNEL_PLAY_ON_DEVICE, new StringType(""));
166 if (this.accountHandler != handler) {
167 this.accountHandler = handler;
168 String configurationJson = this.stateStorage.get("configurationJson");
169 if (configurationJson == null || configurationJson.isEmpty()) {
170 this.currentConfigurationJson = saveCurrentProfile(handler);
172 this.currentConfigurationJson = configurationJson;
174 if (!this.currentConfigurationJson.isEmpty()) {
175 updateStatus(ThingStatus.ONLINE);
178 updateStatus(ThingStatus.UNKNOWN);
181 if (this.currentConfigurationJson.equals(currentConfigurationJson)) {
182 updateState(CHANNEL_ACTIVE, OnOffType.ON);
184 updateState(CHANNEL_ACTIVE, OnOffType.OFF);
186 return this.currentConfigurationJson.equals(currentConfigurationJson);
189 private String saveCurrentProfile(AccountHandler connection) {
190 String configurationJson = "";
191 configurationJson = connection.getEnabledFlashBriefingsJson();
192 this.currentConfigurationJson = configurationJson;
193 if (!configurationJson.isEmpty()) {
194 this.stateStorage.put("configurationJson", configurationJson);
196 return configurationJson;