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.miio.internal.handler;
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;
17 import java.util.concurrent.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
21 import org.openhab.core.cache.ExpiringCache;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.RefreshType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link MiIoUnsupportedHandler} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Marcel Verpaalen - Initial contribution
37 public class MiIoUnsupportedHandler extends MiIoAbstractHandler {
38 private final Logger logger = LoggerFactory.getLogger(MiIoUnsupportedHandler.class);
40 private final ExpiringCache<Boolean> updateDataCache = new ExpiringCache<>(CACHE_EXPIRY, () -> {
41 scheduler.schedule(this::updateData, 0, TimeUnit.SECONDS);
45 public MiIoUnsupportedHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService) {
46 super(thing, miIoDatabaseWatchService);
50 public void handleCommand(ChannelUID channelUID, Command command) {
51 if (command == RefreshType.REFRESH) {
52 if (updateDataCache.isExpired()) {
53 logger.debug("Refreshing {}", channelUID);
54 updateDataCache.getValue();
56 logger.debug("Refresh {} skipped. Already refreshing", channelUID);
60 if (channelUID.getId().equals(CHANNEL_POWER)) {
61 if (command.equals(OnOffType.ON)) {
62 sendCommand("set_power[\"on\"]");
64 sendCommand("set_power[\"off\"]");
67 if (channelUID.getId().equals(CHANNEL_COMMAND)) {
68 cmds.put(sendCommand(command.toString()), command.toString());
70 if (channelUID.getId().equals(CHANNEL_TESTCOMMANDS)) {
71 executeExperimentalCommands();
75 // TODO: In future version this ideally would test all known commands (e.g. from the database) and create/enable a
76 // channel if they appear to be supported
77 private void executeExperimentalCommands() {
78 String[] testCommands = new String[0];
87 testCommands = new String[] { "miIO.info", "get_current_sound", "get_map_v1", "get_serial_number",
98 testCommands = new String[] { "miIO.info" };
101 logger.info("Start Experimental Testing of commands for device '{}'. ", miDevice.toString());
102 for (String c : testCommands) {
103 logger.info("Test command '{}'. Response: '{}'", c, sendCommand(c));
108 protected synchronized void updateData() {
112 logger.debug("Periodic update for '{}' ({})", getThing().getUID().toString(), getThing().getThingTypeUID());
115 } catch (Exception e) {
116 logger.debug("Error while updating '{}' ({})", getThing().getUID().toString(), getThing().getThingTypeUID(),