]> git.basschouten.com Git - openhab-addons.git/blob
a77af44cf3a7b87be73bfcfa34f766a5223b58ba
[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.CHANNEL_COMMAND;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
19 import org.openhab.core.thing.ChannelUID;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.types.Command;
22 import org.openhab.core.types.RefreshType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link MiIoGenericHandler} is responsible for handling commands for devices that are not yet defined.
28  * Once the device has been determined, the proper handler is loaded.
29  *
30  * @author Marcel Verpaalen - Initial contribution
31  */
32 @NonNullByDefault
33 public class MiIoGenericHandler extends MiIoAbstractHandler {
34     private final Logger logger = LoggerFactory.getLogger(MiIoGenericHandler.class);
35
36     public MiIoGenericHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService) {
37         super(thing, miIoDatabaseWatchService);
38     }
39
40     @Override
41     public void handleCommand(ChannelUID channelUID, Command command) {
42         if (command == RefreshType.REFRESH) {
43             logger.debug("Refreshing {}", channelUID);
44             updateData();
45             return;
46         }
47         if (channelUID.getId().equals(CHANNEL_COMMAND)) {
48             cmds.put(sendCommand(command.toString()), command.toString());
49         }
50     }
51
52     @Override
53     protected synchronized void updateData() {
54         if (skipUpdate()) {
55             return;
56         }
57         logger.debug("Periodic update for '{}' ({})", getThing().getUID().toString(), getThing().getThingTypeUID());
58         try {
59             refreshNetwork();
60         } catch (Exception e) {
61             logger.debug("Error while updating '{}'", getThing().getUID().toString(), e);
62         }
63     }
64 }