]> git.basschouten.com Git - openhab-addons.git/blob
f6d3f3bf6b799811cad095ca7370be924f03131f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.net.URLEncoder;
18 import java.net.UnknownHostException;
19 import java.time.ZonedDateTime;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Set;
29 import java.util.concurrent.CopyOnWriteArraySet;
30 import java.util.concurrent.LinkedBlockingQueue;
31 import java.util.concurrent.ScheduledFuture;
32 import java.util.concurrent.TimeUnit;
33 import java.util.stream.Collectors;
34
35 import org.eclipse.jdt.annotation.NonNullByDefault;
36 import org.eclipse.jdt.annotation.Nullable;
37 import org.openhab.binding.amazonechocontrol.internal.AccountHandlerConfig;
38 import org.openhab.binding.amazonechocontrol.internal.AccountServlet;
39 import org.openhab.binding.amazonechocontrol.internal.Connection;
40 import org.openhab.binding.amazonechocontrol.internal.ConnectionException;
41 import org.openhab.binding.amazonechocontrol.internal.HttpException;
42 import org.openhab.binding.amazonechocontrol.internal.IWebSocketCommandHandler;
43 import org.openhab.binding.amazonechocontrol.internal.WebSocketConnection;
44 import org.openhab.binding.amazonechocontrol.internal.channelhandler.ChannelHandler;
45 import org.openhab.binding.amazonechocontrol.internal.channelhandler.ChannelHandlerSendMessage;
46 import org.openhab.binding.amazonechocontrol.internal.channelhandler.IAmazonThingHandler;
47 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonActivities.Activity.SourceDeviceId;
48 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonAscendingAlarm.AscendingAlarmModel;
49 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonBluetoothStates;
50 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonBluetoothStates.BluetoothState;
51 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonCommandPayloadPushActivity;
52 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonCommandPayloadPushActivity.Key;
53 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonCommandPayloadPushDevice;
54 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonCommandPayloadPushDevice.DopplerId;
55 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonCommandPayloadPushNotificationChange;
56 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDeviceNotificationState.DeviceNotificationState;
57 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
58 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonFeed;
59 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonMusicProvider;
60 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonNotificationResponse;
61 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonNotificationSound;
62 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonPlaylists;
63 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonPushCommand;
64 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
65 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonWakeWords.WakeWord;
66 import org.openhab.binding.amazonechocontrol.internal.jsons.SmartHomeBaseDevice;
67 import org.openhab.binding.amazonechocontrol.internal.smarthome.SmartHomeDeviceStateGroupUpdateCalculator;
68 import org.openhab.core.storage.Storage;
69 import org.openhab.core.thing.Bridge;
70 import org.openhab.core.thing.ChannelUID;
71 import org.openhab.core.thing.Thing;
72 import org.openhab.core.thing.ThingStatus;
73 import org.openhab.core.thing.ThingStatusDetail;
74 import org.openhab.core.thing.ThingUID;
75 import org.openhab.core.thing.binding.BaseBridgeHandler;
76 import org.openhab.core.thing.binding.ThingHandler;
77 import org.openhab.core.types.Command;
78 import org.openhab.core.types.RefreshType;
79 import org.openhab.core.types.State;
80 import org.osgi.service.http.HttpService;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
83
84 import com.google.gson.Gson;
85 import com.google.gson.JsonArray;
86 import com.google.gson.JsonSyntaxException;
87
88 /**
89  * Handles the connection to the amazon server.
90  *
91  * @author Michael Geramb - Initial Contribution
92  */
93 @NonNullByDefault
94 public class AccountHandler extends BaseBridgeHandler implements IWebSocketCommandHandler, IAmazonThingHandler {
95     private final Logger logger = LoggerFactory.getLogger(AccountHandler.class);
96     private final Storage<String> stateStorage;
97     private @Nullable Connection connection;
98     private @Nullable WebSocketConnection webSocketConnection;
99
100     private final Set<EchoHandler> echoHandlers = new CopyOnWriteArraySet<>();
101     private final Set<SmartHomeDeviceHandler> smartHomeDeviceHandlers = new CopyOnWriteArraySet<>();
102     private final Set<FlashBriefingProfileHandler> flashBriefingProfileHandlers = new CopyOnWriteArraySet<>();
103
104     private final Object synchronizeConnection = new Object();
105     private Map<String, Device> jsonSerialNumberDeviceMapping = new HashMap<>();
106     private Map<String, SmartHomeBaseDevice> jsonIdSmartHomeDeviceMapping = new HashMap<>();
107
108     private @Nullable ScheduledFuture<?> checkDataJob;
109     private @Nullable ScheduledFuture<?> checkLoginJob;
110     private @Nullable ScheduledFuture<?> updateSmartHomeStateJob;
111     private @Nullable ScheduledFuture<?> refreshAfterCommandJob;
112     private @Nullable ScheduledFuture<?> refreshSmartHomeAfterCommandJob;
113     private final Object synchronizeSmartHomeJobScheduler = new Object();
114     private @Nullable ScheduledFuture<?> forceCheckDataJob;
115     private String currentFlashBriefingJson = "";
116     private final HttpService httpService;
117     private @Nullable AccountServlet accountServlet;
118     private final Gson gson;
119     private int checkDataCounter;
120     private final LinkedBlockingQueue<String> requestedDeviceUpdates = new LinkedBlockingQueue<>();
121     private @Nullable SmartHomeDeviceStateGroupUpdateCalculator smartHomeDeviceStateGroupUpdateCalculator;
122     private List<ChannelHandler> channelHandlers = new ArrayList<>();
123
124     private AccountHandlerConfig handlerConfig = new AccountHandlerConfig();
125
126     public AccountHandler(Bridge bridge, HttpService httpService, Storage<String> stateStorage, Gson gson) {
127         super(bridge);
128         this.gson = gson;
129         this.httpService = httpService;
130         this.stateStorage = stateStorage;
131         channelHandlers.add(new ChannelHandlerSendMessage(this, this.gson));
132     }
133
134     @Override
135     public void initialize() {
136         handlerConfig = getConfig().as(AccountHandlerConfig.class);
137
138         synchronized (synchronizeConnection) {
139             Connection connection = this.connection;
140             if (connection == null) {
141                 this.connection = new Connection(null, gson);
142             }
143         }
144
145         if (accountServlet == null) {
146             try {
147                 accountServlet = new AccountServlet(httpService, this.getThing().getUID().getId(), this, gson);
148             } catch (IllegalStateException e) {
149                 logger.warn("Failed to create account servlet", e);
150             }
151         }
152
153         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Wait for login");
154
155         checkLoginJob = scheduler.scheduleWithFixedDelay(this::checkLogin, 0, 60, TimeUnit.SECONDS);
156         checkDataJob = scheduler.scheduleWithFixedDelay(this::checkData, 4, 60, TimeUnit.SECONDS);
157
158         int pollingIntervalAlexa = handlerConfig.pollingIntervalSmartHomeAlexa;
159         if (pollingIntervalAlexa < 10) {
160             pollingIntervalAlexa = 10;
161         }
162         int pollingIntervalSkills = handlerConfig.pollingIntervalSmartSkills;
163         if (pollingIntervalSkills < 60) {
164             pollingIntervalSkills = 60;
165         }
166         smartHomeDeviceStateGroupUpdateCalculator = new SmartHomeDeviceStateGroupUpdateCalculator(pollingIntervalAlexa,
167                 pollingIntervalSkills);
168         updateSmartHomeStateJob = scheduler.scheduleWithFixedDelay(() -> updateSmartHomeState(null), 20, 10,
169                 TimeUnit.SECONDS);
170     }
171
172     @Override
173     public void updateChannelState(String channelId, State state) {
174         updateState(channelId, state);
175     }
176
177     @Override
178     public void handleCommand(ChannelUID channelUID, Command command) {
179         try {
180             logger.trace("Command '{}' received for channel '{}'", command, channelUID);
181             Connection connection = this.connection;
182             if (connection == null) {
183                 return;
184             }
185
186             String channelId = channelUID.getId();
187             for (ChannelHandler channelHandler : channelHandlers) {
188                 if (channelHandler.tryHandleCommand(new Device(), connection, channelId, command)) {
189                     return;
190                 }
191             }
192             if (command instanceof RefreshType) {
193                 refreshData();
194             }
195         } catch (IOException | URISyntaxException | InterruptedException e) {
196             logger.info("handleCommand fails", e);
197         }
198     }
199
200     @Override
201     public void startAnnouncement(Device device, String speak, String bodyText, @Nullable String title,
202             @Nullable Integer volume) throws IOException, URISyntaxException {
203         EchoHandler echoHandler = findEchoHandlerBySerialNumber(device.serialNumber);
204         if (echoHandler != null) {
205             echoHandler.startAnnouncement(device, speak, bodyText, title, volume);
206         }
207     }
208
209     public List<FlashBriefingProfileHandler> getFlashBriefingProfileHandlers() {
210         return new ArrayList<>(flashBriefingProfileHandlers);
211     }
212
213     public List<Device> getLastKnownDevices() {
214         return new ArrayList<>(jsonSerialNumberDeviceMapping.values());
215     }
216
217     public List<SmartHomeBaseDevice> getLastKnownSmartHomeDevices() {
218         return new ArrayList<>(jsonIdSmartHomeDeviceMapping.values());
219     }
220
221     public void addEchoHandler(EchoHandler echoHandler) {
222         if (echoHandlers.add(echoHandler)) {
223
224             forceCheckData();
225         }
226     }
227
228     public void addSmartHomeDeviceHandler(SmartHomeDeviceHandler smartHomeDeviceHandler) {
229         if (smartHomeDeviceHandlers.add(smartHomeDeviceHandler)) {
230             forceCheckData();
231         }
232     }
233
234     public void forceCheckData() {
235         if (forceCheckDataJob == null) {
236             forceCheckDataJob = scheduler.schedule(this::checkData, 1000, TimeUnit.MILLISECONDS);
237         }
238     }
239
240     public @Nullable Thing findThingBySerialNumber(@Nullable String deviceSerialNumber) {
241         EchoHandler echoHandler = findEchoHandlerBySerialNumber(deviceSerialNumber);
242         if (echoHandler != null) {
243             return echoHandler.getThing();
244         }
245         return null;
246     }
247
248     public @Nullable EchoHandler findEchoHandlerBySerialNumber(@Nullable String deviceSerialNumber) {
249         for (EchoHandler echoHandler : echoHandlers) {
250             if (deviceSerialNumber != null && deviceSerialNumber.equals(echoHandler.findSerialNumber())) {
251                 return echoHandler;
252             }
253         }
254         return null;
255     }
256
257     public void addFlashBriefingProfileHandler(FlashBriefingProfileHandler flashBriefingProfileHandler) {
258         flashBriefingProfileHandlers.add(flashBriefingProfileHandler);
259         Connection connection = this.connection;
260         if (connection != null && connection.getIsLoggedIn()) {
261             if (currentFlashBriefingJson.isEmpty()) {
262                 updateFlashBriefingProfiles(connection);
263             }
264             flashBriefingProfileHandler.initialize(this, currentFlashBriefingJson);
265         }
266     }
267
268     private void scheduleUpdate() {
269         checkDataCounter = 999;
270     }
271
272     @Override
273     public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
274         super.childHandlerInitialized(childHandler, childThing);
275         scheduleUpdate();
276     }
277
278     @Override
279     public void handleRemoval() {
280         cleanup();
281         super.handleRemoval();
282     }
283
284     @Override
285     public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
286         // check for echo handler
287         if (childHandler instanceof EchoHandler) {
288             echoHandlers.remove(childHandler);
289         }
290         // check for flash briefing profile handler
291         if (childHandler instanceof FlashBriefingProfileHandler) {
292             flashBriefingProfileHandlers.remove(childHandler);
293         }
294         // check for flash briefing profile handler
295         if (childHandler instanceof SmartHomeDeviceHandler) {
296             smartHomeDeviceHandlers.remove(childHandler);
297         }
298         super.childHandlerDisposed(childHandler, childThing);
299     }
300
301     @Override
302     public void dispose() {
303         AccountServlet accountServlet = this.accountServlet;
304         if (accountServlet != null) {
305             accountServlet.dispose();
306         }
307         this.accountServlet = null;
308         cleanup();
309         super.dispose();
310     }
311
312     private void cleanup() {
313         logger.debug("cleanup {}", getThing().getUID().getAsString());
314         ScheduledFuture<?> updateSmartHomeStateJob = this.updateSmartHomeStateJob;
315         if (updateSmartHomeStateJob != null) {
316             updateSmartHomeStateJob.cancel(true);
317             this.updateSmartHomeStateJob = null;
318         }
319         ScheduledFuture<?> refreshJob = this.checkDataJob;
320         if (refreshJob != null) {
321             refreshJob.cancel(true);
322             this.checkDataJob = null;
323         }
324         ScheduledFuture<?> refreshLogin = this.checkLoginJob;
325         if (refreshLogin != null) {
326             refreshLogin.cancel(true);
327             this.checkLoginJob = null;
328         }
329         ScheduledFuture<?> foceCheckDataJob = this.forceCheckDataJob;
330         if (foceCheckDataJob != null) {
331             foceCheckDataJob.cancel(true);
332             this.forceCheckDataJob = null;
333         }
334         ScheduledFuture<?> refreshAfterCommandJob = this.refreshAfterCommandJob;
335         if (refreshAfterCommandJob != null) {
336             refreshAfterCommandJob.cancel(true);
337             this.refreshAfterCommandJob = null;
338         }
339         ScheduledFuture<?> refreshSmartHomeAfterCommandJob = this.refreshSmartHomeAfterCommandJob;
340         if (refreshSmartHomeAfterCommandJob != null) {
341             refreshSmartHomeAfterCommandJob.cancel(true);
342             this.refreshSmartHomeAfterCommandJob = null;
343         }
344         Connection connection = this.connection;
345         if (connection != null) {
346             connection.logout();
347             this.connection = null;
348         }
349         closeWebSocketConnection();
350     }
351
352     private void checkLogin() {
353         try {
354             ThingUID uid = getThing().getUID();
355             logger.debug("check login {}", uid.getAsString());
356
357             synchronized (synchronizeConnection) {
358                 Connection currentConnection = this.connection;
359                 if (currentConnection == null) {
360                     return;
361                 }
362
363                 try {
364                     if (currentConnection.getIsLoggedIn()) {
365                         if (currentConnection.checkRenewSession()) {
366                             setConnection(currentConnection);
367                         }
368                     } else {
369                         // read session data from property
370                         String sessionStore = this.stateStorage.get("sessionStorage");
371
372                         // try use the session data
373                         if (currentConnection.tryRestoreLogin(sessionStore, null)) {
374                             setConnection(currentConnection);
375                         }
376                     }
377                     if (!currentConnection.getIsLoggedIn()) {
378                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
379                                 "Please login in through web site: http(s)://<YOUROPENHAB>:<YOURPORT>/amazonechocontrol/"
380                                         + URLEncoder.encode(uid.getId(), "UTF8"));
381                     }
382                 } catch (ConnectionException e) {
383                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
384                 } catch (HttpException e) {
385                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
386                 } catch (UnknownHostException e) {
387                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
388                             "Unknown host name '" + e.getMessage() + "'. Maybe your internet connection is offline");
389                 } catch (IOException e) {
390                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage());
391                 } catch (URISyntaxException e) {
392                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage());
393                 }
394             }
395         } catch (Exception e) { // this handler can be removed later, if we know that nothing else can fail.
396             logger.error("check login fails with unexpected error", e);
397         }
398     }
399
400     // used to set a valid connection from the web proxy login
401     public void setConnection(@Nullable Connection connection) {
402         this.connection = connection;
403         if (connection != null) {
404             String serializedStorage = connection.serializeLoginData();
405             this.stateStorage.put("sessionStorage", serializedStorage);
406         } else {
407             this.stateStorage.put("sessionStorage", null);
408             updateStatus(ThingStatus.OFFLINE);
409         }
410         closeWebSocketConnection();
411         if (connection != null) {
412             updateDeviceList();
413             updateSmartHomeDeviceList(false);
414             updateFlashBriefingHandlers();
415             updateStatus(ThingStatus.ONLINE);
416             scheduleUpdate();
417             checkData();
418         }
419     }
420
421     void closeWebSocketConnection() {
422         WebSocketConnection webSocketConnection = this.webSocketConnection;
423         this.webSocketConnection = null;
424         if (webSocketConnection != null) {
425             webSocketConnection.close();
426         }
427     }
428
429     private boolean checkWebSocketConnection() {
430         WebSocketConnection webSocketConnection = this.webSocketConnection;
431         if (webSocketConnection == null || webSocketConnection.isClosed()) {
432             Connection connection = this.connection;
433             if (connection != null && connection.getIsLoggedIn()) {
434                 try {
435                     this.webSocketConnection = new WebSocketConnection(connection.getAmazonSite(),
436                             connection.getSessionCookies(), this);
437                 } catch (IOException e) {
438                     logger.warn("Web socket connection starting failed", e);
439                 }
440             }
441             return false;
442         }
443         return true;
444     }
445
446     private void checkData() {
447         synchronized (synchronizeConnection) {
448             try {
449                 Connection connection = this.connection;
450                 if (connection != null && connection.getIsLoggedIn()) {
451                     checkDataCounter++;
452                     if (checkDataCounter > 60 || forceCheckDataJob != null) {
453                         checkDataCounter = 0;
454                         forceCheckDataJob = null;
455                     }
456                     if (!checkWebSocketConnection() || checkDataCounter == 0) {
457                         refreshData();
458                     }
459                 }
460                 logger.debug("checkData {} finished", getThing().getUID().getAsString());
461             } catch (HttpException | JsonSyntaxException | ConnectionException e) {
462                 logger.debug("checkData fails", e);
463             } catch (Exception e) { // this handler can be removed later, if we know that nothing else can fail.
464                 logger.error("checkData fails with unexpected error", e);
465             }
466         }
467     }
468
469     private void refreshNotifications(@Nullable JsonCommandPayloadPushNotificationChange pushPayload) {
470         Connection currentConnection = this.connection;
471         if (currentConnection == null) {
472             return;
473         }
474         if (!currentConnection.getIsLoggedIn()) {
475             return;
476         }
477         JsonNotificationResponse[] notifications;
478         ZonedDateTime timeStamp = ZonedDateTime.now();
479         try {
480             notifications = currentConnection.notifications();
481         } catch (IOException | URISyntaxException | InterruptedException e) {
482             logger.debug("refreshNotifications failed", e);
483             return;
484         }
485         ZonedDateTime timeStampNow = ZonedDateTime.now();
486
487         echoHandlers.forEach(
488                 echoHandler -> echoHandler.updateNotifications(timeStamp, timeStampNow, pushPayload, notifications));
489     }
490
491     private void refreshData() {
492         synchronized (synchronizeConnection) {
493             try {
494                 logger.debug("refreshing data {}", getThing().getUID().getAsString());
495
496                 // check if logged in
497                 Connection currentConnection = null;
498                 currentConnection = connection;
499                 if (currentConnection != null) {
500                     if (!currentConnection.getIsLoggedIn()) {
501                         return;
502                     }
503                 }
504                 if (currentConnection == null) {
505                     return;
506                 }
507
508                 // get all devices registered in the account
509                 updateDeviceList();
510                 updateSmartHomeDeviceList(false);
511                 updateFlashBriefingHandlers();
512
513                 DeviceNotificationState[] deviceNotificationStates = null;
514                 AscendingAlarmModel[] ascendingAlarmModels = null;
515                 JsonBluetoothStates states = null;
516                 List<JsonMusicProvider> musicProviders = null;
517                 if (currentConnection.getIsLoggedIn()) {
518                     // update notification states
519                     deviceNotificationStates = currentConnection.getDeviceNotificationStates();
520
521                     // update ascending alarm
522                     ascendingAlarmModels = currentConnection.getAscendingAlarm();
523
524                     // update bluetooth states
525                     states = currentConnection.getBluetoothConnectionStates();
526
527                     // update music providers
528                     if (currentConnection.getIsLoggedIn()) {
529                         try {
530                             musicProviders = currentConnection.getMusicProviders();
531                         } catch (HttpException | JsonSyntaxException | ConnectionException e) {
532                             logger.debug("Update music provider failed", e);
533                         }
534                     }
535                 }
536                 // forward device information to echo handler
537                 for (EchoHandler child : echoHandlers) {
538                     Device device = findDeviceJson(child.findSerialNumber());
539
540                     JsonNotificationSound[] notificationSounds = null;
541                     JsonPlaylists playlists = null;
542                     if (device != null && currentConnection.getIsLoggedIn()) {
543                         // update notification sounds
544                         try {
545                             notificationSounds = currentConnection.getNotificationSounds(device);
546                         } catch (IOException | HttpException | JsonSyntaxException | ConnectionException e) {
547                             logger.debug("Update notification sounds failed", e);
548                         }
549                         // update playlists
550                         try {
551                             playlists = currentConnection.getPlaylists(device);
552                         } catch (IOException | HttpException | JsonSyntaxException | ConnectionException e) {
553                             logger.debug("Update playlist failed", e);
554                         }
555                     }
556
557                     BluetoothState state = null;
558                     if (states != null) {
559                         state = states.findStateByDevice(device);
560                     }
561                     DeviceNotificationState deviceNotificationState = null;
562                     AscendingAlarmModel ascendingAlarmModel = null;
563                     if (device != null) {
564                         final String serialNumber = device.serialNumber;
565                         if (serialNumber != null) {
566                             if (ascendingAlarmModels != null) {
567                                 ascendingAlarmModel = Arrays.stream(ascendingAlarmModels).filter(Objects::nonNull)
568                                         .filter(current -> serialNumber.equals(current.deviceSerialNumber)).findFirst()
569                                         .orElse(null);
570                             }
571                             if (deviceNotificationStates != null) {
572                                 deviceNotificationState = Arrays.stream(deviceNotificationStates)
573                                         .filter(Objects::nonNull)
574                                         .filter(current -> serialNumber.equals(current.deviceSerialNumber)).findFirst()
575                                         .orElse(null);
576                             }
577                         }
578                     }
579                     child.updateState(this, device, state, deviceNotificationState, ascendingAlarmModel, playlists,
580                             notificationSounds, musicProviders);
581                 }
582
583                 // refresh notifications
584                 refreshNotifications(null);
585
586                 // update account state
587                 updateStatus(ThingStatus.ONLINE);
588
589                 logger.debug("refresh data {} finished", getThing().getUID().getAsString());
590             } catch (HttpException | JsonSyntaxException | ConnectionException e) {
591                 logger.debug("refresh data fails", e);
592             } catch (Exception e) { // this handler can be removed later, if we know that nothing else can fail.
593                 logger.error("refresh data fails with unexpected error", e);
594             }
595         }
596     }
597
598     public @Nullable Device findDeviceJson(@Nullable String serialNumber) {
599         if (serialNumber == null || serialNumber.isEmpty()) {
600             return null;
601         }
602         return this.jsonSerialNumberDeviceMapping.get(serialNumber);
603     }
604
605     public @Nullable Device findDeviceJsonBySerialOrName(@Nullable String serialOrName) {
606         if (serialOrName == null || serialOrName.isEmpty()) {
607             return null;
608         }
609
610         return this.jsonSerialNumberDeviceMapping.values().stream().filter(
611                 d -> serialOrName.equalsIgnoreCase(d.serialNumber) || serialOrName.equalsIgnoreCase(d.accountName))
612                 .findFirst().orElse(null);
613     }
614
615     public List<Device> updateDeviceList() {
616         Connection currentConnection = connection;
617         if (currentConnection == null) {
618             return new ArrayList<>();
619         }
620
621         List<Device> devices = null;
622         try {
623             if (currentConnection.getIsLoggedIn()) {
624                 devices = currentConnection.getDeviceList();
625             }
626         } catch (IOException | URISyntaxException | InterruptedException e) {
627             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage());
628         }
629         if (devices != null) {
630             // create new device map
631             jsonSerialNumberDeviceMapping = devices.stream().filter(device -> device.serialNumber != null)
632                     .collect(Collectors.toMap(d -> Objects.requireNonNull(d.serialNumber), d -> d));
633         }
634
635         WakeWord[] wakeWords = currentConnection.getWakeWords();
636         // update handlers
637         for (EchoHandler echoHandler : echoHandlers) {
638             String serialNumber = echoHandler.findSerialNumber();
639             String deviceWakeWord = null;
640             for (WakeWord wakeWord : wakeWords) {
641                 if (wakeWord != null) {
642                     if (serialNumber.equals(wakeWord.deviceSerialNumber)) {
643                         deviceWakeWord = wakeWord.wakeWord;
644                         break;
645                     }
646                 }
647             }
648             echoHandler.setDeviceAndUpdateThingState(this, findDeviceJson(serialNumber), deviceWakeWord);
649         }
650
651         if (devices != null) {
652             return devices;
653         }
654         return List.of();
655     }
656
657     public void setEnabledFlashBriefingsJson(String flashBriefingJson) {
658         Connection currentConnection = connection;
659         JsonFeed[] feeds = gson.fromJson(flashBriefingJson, JsonFeed[].class);
660         if (currentConnection != null && feeds != null) {
661             try {
662                 currentConnection.setEnabledFlashBriefings(feeds);
663             } catch (IOException | URISyntaxException | InterruptedException e) {
664                 logger.warn("Set flashbriefing profile failed", e);
665             }
666         }
667         updateFlashBriefingHandlers();
668     }
669
670     public String getNewCurrentFlashbriefingConfiguration() {
671         return updateFlashBriefingHandlers();
672     }
673
674     public String updateFlashBriefingHandlers() {
675         Connection currentConnection = connection;
676         if (currentConnection != null) {
677             return updateFlashBriefingHandlers(currentConnection);
678         }
679         return "";
680     }
681
682     private String updateFlashBriefingHandlers(Connection currentConnection) {
683         if (!flashBriefingProfileHandlers.isEmpty() || currentFlashBriefingJson.isEmpty()) {
684             updateFlashBriefingProfiles(currentConnection);
685         }
686         boolean flashBriefingProfileFound = false;
687         for (FlashBriefingProfileHandler child : flashBriefingProfileHandlers) {
688             flashBriefingProfileFound |= child.initialize(this, currentFlashBriefingJson);
689         }
690         if (flashBriefingProfileFound) {
691             return "";
692         }
693         return this.currentFlashBriefingJson;
694     }
695
696     public @Nullable Connection findConnection() {
697         return this.connection;
698     }
699
700     public String getEnabledFlashBriefingsJson() {
701         Connection currentConnection = this.connection;
702         if (currentConnection == null) {
703             return "";
704         }
705         updateFlashBriefingProfiles(currentConnection);
706         return this.currentFlashBriefingJson;
707     }
708
709     private void updateFlashBriefingProfiles(Connection currentConnection) {
710         try {
711             JsonFeed[] feeds = currentConnection.getEnabledFlashBriefings();
712             // Make a copy and remove changeable parts
713             JsonFeed[] forSerializer = new JsonFeed[feeds.length];
714             for (int i = 0; i < feeds.length; i++) {
715                 JsonFeed source = feeds[i];
716                 JsonFeed copy = new JsonFeed();
717                 copy.feedId = source.feedId;
718                 copy.skillId = source.skillId;
719                 // Do not copy imageUrl here, because it will change
720                 forSerializer[i] = copy;
721             }
722             this.currentFlashBriefingJson = gson.toJson(forSerializer);
723         } catch (HttpException | JsonSyntaxException | IOException | URISyntaxException | ConnectionException
724                 | InterruptedException e) {
725             logger.warn("get flash briefing profiles fails", e);
726         }
727     }
728
729     @Override
730     public void webSocketCommandReceived(JsonPushCommand pushCommand) {
731         try {
732             handleWebsocketCommand(pushCommand);
733         } catch (Exception e) {
734             // should never happen, but if the exception is going out of this function, the binding stop working.
735             logger.warn("handling of websockets fails", e);
736         }
737     }
738
739     void handleWebsocketCommand(JsonPushCommand pushCommand) {
740         String command = pushCommand.command;
741         if (command != null) {
742             ScheduledFuture<?> refreshDataDelayed = this.refreshAfterCommandJob;
743             switch (command) {
744                 case "PUSH_ACTIVITY":
745                     handlePushActivity(pushCommand.payload);
746                     break;
747                 case "PUSH_DOPPLER_CONNECTION_CHANGE":
748                 case "PUSH_BLUETOOTH_STATE_CHANGE":
749                     if (refreshDataDelayed != null) {
750                         refreshDataDelayed.cancel(false);
751                     }
752                     this.refreshAfterCommandJob = scheduler.schedule(this::refreshAfterCommand, 700,
753                             TimeUnit.MILLISECONDS);
754                     break;
755                 case "PUSH_NOTIFICATION_CHANGE":
756                     JsonCommandPayloadPushNotificationChange pushPayload = gson.fromJson(pushCommand.payload,
757                             JsonCommandPayloadPushNotificationChange.class);
758                     refreshNotifications(pushPayload);
759                     break;
760                 default:
761                     String payload = pushCommand.payload;
762                     if (payload != null && payload.startsWith("{") && payload.endsWith("}")) {
763                         JsonCommandPayloadPushDevice devicePayload = Objects
764                                 .requireNonNull(gson.fromJson(payload, JsonCommandPayloadPushDevice.class));
765                         DopplerId dopplerId = devicePayload.dopplerId;
766                         if (dopplerId != null) {
767                             handlePushDeviceCommand(dopplerId, command, payload);
768                         }
769                     }
770                     break;
771             }
772         }
773     }
774
775     private void handlePushDeviceCommand(DopplerId dopplerId, String command, String payload) {
776         EchoHandler echoHandler = findEchoHandlerBySerialNumber(dopplerId.deviceSerialNumber);
777         if (echoHandler != null) {
778             echoHandler.handlePushCommand(command, payload);
779         }
780     }
781
782     private void handlePushActivity(@Nullable String payload) {
783         if (payload == null) {
784             return;
785         }
786         JsonCommandPayloadPushActivity pushActivity = Objects
787                 .requireNonNull(gson.fromJson(payload, JsonCommandPayloadPushActivity.class));
788
789         Key key = pushActivity.key;
790         if (key == null) {
791             return;
792         }
793
794         Connection connection = this.connection;
795         if (connection == null || !connection.getIsLoggedIn()) {
796             return;
797         }
798
799         String search = key.registeredUserId + "#" + key.entryId;
800         Arrays.stream(connection.getActivities(10, pushActivity.timestamp))
801                 .filter(activity -> activity != null && search.equals(activity.id)).findFirst()
802                 .ifPresent(currentActivity -> {
803                     SourceDeviceId[] sourceDeviceIds = currentActivity.sourceDeviceIds;
804                     if (sourceDeviceIds != null) {
805                         Arrays.stream(sourceDeviceIds).filter(Objects::nonNull)
806                                 .map(sourceDeviceId -> findEchoHandlerBySerialNumber(sourceDeviceId.serialNumber))
807                                 .filter(Objects::nonNull).forEach(echoHandler -> Objects.requireNonNull(echoHandler)
808                                         .handlePushActivity(currentActivity));
809                     }
810                 });
811     }
812
813     void refreshAfterCommand() {
814         refreshData();
815     }
816
817     private @Nullable SmartHomeBaseDevice findSmartDeviceHomeJson(SmartHomeDeviceHandler handler) {
818         String id = handler.getId();
819         if (!id.isEmpty()) {
820             return jsonIdSmartHomeDeviceMapping.get(id);
821         }
822         return null;
823     }
824
825     public int getSmartHomeDevicesDiscoveryMode() {
826         return handlerConfig.discoverSmartHome;
827     }
828
829     public List<SmartHomeBaseDevice> updateSmartHomeDeviceList(boolean forceUpdate) {
830         Connection currentConnection = connection;
831         if (currentConnection == null) {
832             return Collections.emptyList();
833         }
834
835         if (!forceUpdate && smartHomeDeviceHandlers.isEmpty() && getSmartHomeDevicesDiscoveryMode() == 0) {
836             return Collections.emptyList();
837         }
838
839         List<SmartHomeBaseDevice> smartHomeDevices = null;
840         try {
841             if (currentConnection.getIsLoggedIn()) {
842                 smartHomeDevices = currentConnection.getSmarthomeDeviceList();
843             }
844         } catch (IOException | URISyntaxException | InterruptedException e) {
845             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage());
846         }
847         if (smartHomeDevices != null) {
848             // create new id map
849             Map<String, SmartHomeBaseDevice> newJsonIdSmartHomeDeviceMapping = new HashMap<>();
850             for (Object smartHomeDevice : smartHomeDevices) {
851                 if (smartHomeDevice instanceof SmartHomeBaseDevice) {
852                     SmartHomeBaseDevice smartHomeBaseDevice = (SmartHomeBaseDevice) smartHomeDevice;
853                     String id = smartHomeBaseDevice.findId();
854                     if (id != null) {
855                         newJsonIdSmartHomeDeviceMapping.put(id, smartHomeBaseDevice);
856                     }
857                 }
858             }
859             jsonIdSmartHomeDeviceMapping = newJsonIdSmartHomeDeviceMapping;
860         }
861         // update handlers
862         smartHomeDeviceHandlers
863                 .forEach(child -> child.setDeviceAndUpdateThingState(this, findSmartDeviceHomeJson(child)));
864
865         if (smartHomeDevices != null) {
866             return smartHomeDevices;
867         }
868
869         return Collections.emptyList();
870     }
871
872     public void forceDelayedSmartHomeStateUpdate(@Nullable String deviceId) {
873         if (deviceId == null) {
874             return;
875         }
876         synchronized (synchronizeSmartHomeJobScheduler) {
877             requestedDeviceUpdates.add(deviceId);
878             ScheduledFuture<?> refreshSmartHomeAfterCommandJob = this.refreshSmartHomeAfterCommandJob;
879             if (refreshSmartHomeAfterCommandJob != null) {
880                 refreshSmartHomeAfterCommandJob.cancel(false);
881             }
882             this.refreshSmartHomeAfterCommandJob = scheduler.schedule(this::updateSmartHomeStateJob, 500,
883                     TimeUnit.MILLISECONDS);
884         }
885     }
886
887     private void updateSmartHomeStateJob() {
888         Set<String> deviceUpdates = new HashSet<>();
889
890         synchronized (synchronizeSmartHomeJobScheduler) {
891             Connection connection = this.connection;
892             if (connection == null || !connection.getIsLoggedIn()) {
893                 this.refreshSmartHomeAfterCommandJob = scheduler.schedule(this::updateSmartHomeStateJob, 1000,
894                         TimeUnit.MILLISECONDS);
895                 return;
896             }
897             requestedDeviceUpdates.drainTo(deviceUpdates);
898             this.refreshSmartHomeAfterCommandJob = null;
899         }
900
901         deviceUpdates.forEach(this::updateSmartHomeState);
902     }
903
904     private synchronized void updateSmartHomeState(@Nullable String deviceFilterId) {
905         try {
906             logger.debug("updateSmartHomeState started with deviceFilterId={}", deviceFilterId);
907             Connection connection = this.connection;
908             if (connection == null || !connection.getIsLoggedIn()) {
909                 return;
910             }
911             List<SmartHomeBaseDevice> allDevices = getLastKnownSmartHomeDevices();
912             Set<String> applianceIds = new HashSet<>();
913             if (deviceFilterId != null) {
914                 applianceIds.add(deviceFilterId);
915             } else {
916                 SmartHomeDeviceStateGroupUpdateCalculator smartHomeDeviceStateGroupUpdateCalculator = this.smartHomeDeviceStateGroupUpdateCalculator;
917                 if (smartHomeDeviceStateGroupUpdateCalculator == null) {
918                     return;
919                 }
920                 if (smartHomeDeviceHandlers.isEmpty()) {
921                     return;
922                 }
923                 List<SmartHomeDevice> devicesToUpdate = new ArrayList<>();
924                 for (SmartHomeDeviceHandler device : smartHomeDeviceHandlers) {
925                     String id = device.getId();
926                     SmartHomeBaseDevice baseDevice = jsonIdSmartHomeDeviceMapping.get(id);
927                     SmartHomeDeviceHandler.getSupportedSmartHomeDevices(baseDevice, allDevices)
928                             .forEach(devicesToUpdate::add);
929                 }
930                 smartHomeDeviceStateGroupUpdateCalculator.removeDevicesWithNoUpdate(devicesToUpdate);
931                 devicesToUpdate.stream().map(shd -> shd.applianceId).forEach(applianceId -> {
932                     if (applianceId != null) {
933                         applianceIds.add(applianceId);
934                     }
935                 });
936                 if (applianceIds.isEmpty()) {
937                     return;
938                 }
939
940             }
941             Map<String, JsonArray> applianceIdToCapabilityStates = connection
942                     .getSmartHomeDeviceStatesJson(applianceIds);
943
944             for (SmartHomeDeviceHandler smartHomeDeviceHandler : smartHomeDeviceHandlers) {
945                 String id = smartHomeDeviceHandler.getId();
946                 if (requestedDeviceUpdates.contains(id)) {
947                     logger.debug("Device update {} suspended", id);
948                     continue;
949                 }
950                 if (deviceFilterId == null || id.equals(deviceFilterId)) {
951                     smartHomeDeviceHandler.updateChannelStates(allDevices, applianceIdToCapabilityStates);
952                 } else {
953                     logger.trace("Id {} not matching filter {}", id, deviceFilterId);
954                 }
955             }
956
957             logger.debug("updateSmartHomeState finished");
958         } catch (HttpException | JsonSyntaxException | ConnectionException e) {
959             logger.debug("updateSmartHomeState fails", e);
960         } catch (Exception e) { // this handler can be removed later, if we know that nothing else can fail.
961             logger.warn("updateSmartHomeState fails with unexpected error", e);
962         }
963     }
964 }