]> git.basschouten.com Git - openhab-addons.git/blob
b5d67b28aad6238e774ead16c2c12c37eae0974d
[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.miio.internal.handler;
14
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;
16
17 import java.util.concurrent.TimeUnit;
18
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;
29
30 /**
31  * The {@link MiIoUnsupportedHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Marcel Verpaalen - Initial contribution
35  */
36 @NonNullByDefault
37 public class MiIoUnsupportedHandler extends MiIoAbstractHandler {
38     private final Logger logger = LoggerFactory.getLogger(MiIoUnsupportedHandler.class);
39
40     private final ExpiringCache<Boolean> updateDataCache = new ExpiringCache<>(CACHE_EXPIRY, () -> {
41         scheduler.schedule(this::updateData, 0, TimeUnit.SECONDS);
42         return true;
43     });
44
45     public MiIoUnsupportedHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService) {
46         super(thing, miIoDatabaseWatchService);
47     }
48
49     @Override
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();
55             } else {
56                 logger.debug("Refresh {} skipped. Already refreshing", channelUID);
57             }
58             return;
59         }
60         if (channelUID.getId().equals(CHANNEL_POWER)) {
61             if (command.equals(OnOffType.ON)) {
62                 sendCommand("set_power[\"on\"]");
63             } else {
64                 sendCommand("set_power[\"off\"]");
65             }
66         }
67         if (channelUID.getId().equals(CHANNEL_COMMAND)) {
68             cmds.put(sendCommand(command.toString()), command.toString());
69         }
70         if (channelUID.getId().equals(CHANNEL_TESTCOMMANDS)) {
71             executeExperimentalCommands();
72         }
73     }
74
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];
79         switch (miDevice) {
80             case POWERPLUG:
81             case POWERPLUG2:
82             case POWERSTRIP:
83             case POWERSTRIP2:
84             case YEELIGHT_C1:
85                 break;
86             case VACUUM:
87                 testCommands = new String[] { "miIO.info", "get_current_sound", "get_map_v1", "get_serial_number",
88                         "get_timezone" };
89                 break;
90             case AIR_PURIFIERM:
91             case AIR_PURIFIER1:
92             case AIR_PURIFIER2:
93             case AIR_PURIFIER3:
94             case AIR_PURIFIER6:
95                 break;
96
97             default:
98                 testCommands = new String[] { "miIO.info" };
99                 break;
100         }
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));
104         }
105     }
106
107     @Override
108     protected synchronized void updateData() {
109         if (skipUpdate()) {
110             return;
111         }
112         logger.debug("Periodic update for '{}' ({})", getThing().getUID().toString(), getThing().getThingTypeUID());
113         try {
114             refreshNetwork();
115         } catch (Exception e) {
116             logger.debug("Error while updating '{}' ({})", getThing().getUID().toString(), getThing().getThingTypeUID(),
117                     e);
118         }
119     }
120 }