]> git.basschouten.com Git - openhab-addons.git/blob
9dd0df2503c4e333d43862c915e003a82632e4c8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.handler;
14
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
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;
37
38 /**
39  * The {@link FlashBriefingProfileHandler} is responsible for storing and loading of a flash briefing configuration
40  *
41  * @author Michael Geramb - Initial contribution
42  */
43 @NonNullByDefault
44 public class FlashBriefingProfileHandler extends BaseThingHandler {
45
46     private final Logger logger = LoggerFactory.getLogger(FlashBriefingProfileHandler.class);
47
48     @Nullable
49     AccountHandler accountHandler;
50     Storage<String> stateStorage;
51     boolean updatePlayOnDevice = true;
52     String currentConfigurationJson = "";
53     private @Nullable ScheduledFuture<?> updateStateJob;
54
55     public FlashBriefingProfileHandler(Thing thing, Storage<String> storage) {
56         super(thing);
57         this.stateStorage = storage;
58     }
59
60     public @Nullable AccountHandler findAccountHandler() {
61         return this.accountHandler;
62     }
63
64     @Override
65     public void initialize() {
66         updatePlayOnDevice = true;
67         logger.info("{} initialized", getClass().getSimpleName());
68         if (!this.currentConfigurationJson.isEmpty()) {
69             updateStatus(ThingStatus.ONLINE);
70         } else {
71             updateStatus(ThingStatus.UNKNOWN);
72             Bridge bridge = this.getBridge();
73             if (bridge != null) {
74                 AccountHandler account = (AccountHandler) bridge.getHandler();
75                 if (account != null) {
76                     account.addFlashBriefingProfileHandler(this);
77                 }
78             }
79         }
80     }
81
82     @Override
83     public void dispose() {
84         ScheduledFuture<?> updateStateJob = this.updateStateJob;
85         this.updateStateJob = null;
86         if (updateStateJob != null) {
87             updateStateJob.cancel(false);
88         }
89         super.dispose();
90     }
91
92     @Override
93     public void handleCommand(ChannelUID channelUID, Command command) {
94         AccountHandler accountHandler = this.accountHandler;
95         if (accountHandler == null) {
96             return;
97         }
98         int waitForUpdate = -1;
99
100         ScheduledFuture<?> updateStateJob = this.updateStateJob;
101         this.updateStateJob = null;
102         if (updateStateJob != null) {
103             updateStateJob.cancel(false);
104         }
105         String channelId = channelUID.getId();
106         if (command instanceof RefreshType) {
107             waitForUpdate = 0;
108         }
109         if (channelId.equals(CHANNEL_SAVE)) {
110             if (command.equals(OnOffType.ON)) {
111                 saveCurrentProfile(accountHandler);
112                 waitForUpdate = 500;
113             }
114         }
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);
121                     waitForUpdate = 500;
122                 }
123             }
124         }
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);
135                     } else {
136                         @Nullable
137                         Connection connection = accountHandler.findConnection();
138                         if (connection == null) {
139                             logger.warn("Connection for '{}' not found", accountHandler.getThing().getUID().getId());
140                         } else {
141                             connection.executeSequenceCommand(device, "Alexa.FlashBriefing.Play", Map.of());
142
143                             scheduler.schedule(() -> accountHandler.setEnabledFlashBriefingsJson(old), 1000,
144                                     TimeUnit.MILLISECONDS);
145
146                             updateState(CHANNEL_ACTIVE, OnOffType.ON);
147                         }
148                     }
149                     updatePlayOnDevice = true;
150                     waitForUpdate = 1000;
151                 }
152             }
153         }
154         if (waitForUpdate >= 0) {
155             this.updateStateJob = scheduler.schedule(() -> accountHandler.updateFlashBriefingHandlers(), waitForUpdate,
156                     TimeUnit.MILLISECONDS);
157         }
158     }
159
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(""));
165         }
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);
171             } else {
172                 this.currentConfigurationJson = configurationJson;
173             }
174             if (!this.currentConfigurationJson.isEmpty()) {
175                 updateStatus(ThingStatus.ONLINE);
176
177             } else {
178                 updateStatus(ThingStatus.UNKNOWN);
179             }
180         }
181         if (this.currentConfigurationJson.equals(currentConfigurationJson)) {
182             updateState(CHANNEL_ACTIVE, OnOffType.ON);
183         } else {
184             updateState(CHANNEL_ACTIVE, OnOffType.OFF);
185         }
186         return this.currentConfigurationJson.equals(currentConfigurationJson);
187     }
188
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);
195         }
196         return configurationJson;
197     }
198 }