]> git.basschouten.com Git - openhab-addons.git/blob
e64b84add09ad64f90c504312cdb41f72ac4c1cf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.elroconnects.internal.devices;
14
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.POWER_STATE;
16
17 import java.io.IOException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
21 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link ElroConnectsDevicePowerSocket} is representing an ELRO Connects power socket device.
32  *
33  * @author Mark Herwege - Initial contribution
34  */
35 @NonNullByDefault
36 public class ElroConnectsDevicePowerSocket extends ElroConnectsDevice {
37
38     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDevicePowerSocket.class);
39
40     // device states
41     private static final String STAT_ON = "00";
42     private static final String STAT_OFF = "01";
43
44     // device commands
45     protected static final String CMD_OFF = "0101FFFF";
46     protected static final String CMD_ON = "0100FFFF";
47
48     public ElroConnectsDevicePowerSocket(int deviceId, ElroConnectsBridgeHandler bridge) {
49         super(deviceId, bridge);
50     }
51
52     @Override
53     public void switchState(boolean state) {
54         try {
55             bridge.deviceControl(deviceId, state ? CMD_ON : CMD_OFF);
56         } catch (IOException e) {
57             logger.debug("Failed to control device: {}", e.getMessage());
58         }
59     }
60
61     @Override
62     public void updateState() {
63         ElroConnectsDeviceHandler handler = getHandler();
64         if (handler == null) {
65             return;
66         }
67
68         String deviceStatus = this.deviceStatus;
69         if (deviceStatus.length() < 6) {
70             logger.debug("Could not decode device status: {}", deviceStatus);
71             return;
72         }
73
74         String status = deviceStatus.substring(4, 6);
75         State state = STAT_ON.equals(status) ? OnOffType.ON
76                 : (STAT_OFF.equals(status) ? OnOffType.OFF : UnDefType.UNDEF);
77         handler.updateState(POWER_STATE, state);
78         if (UnDefType.UNDEF.equals(state)) {
79             handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
80                     "Device " + deviceId + " is not syncing with K1 hub");
81         } else {
82             handler.updateStatus(ThingStatus.ONLINE);
83         }
84     }
85
86     @Override
87     public void testAlarm() {
88         // nothing
89     }
90
91     @Override
92     public void muteAlarm() {
93         // nothing
94     }
95 }