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