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