]> git.basschouten.com Git - openhab-addons.git/blob
e66f52767552b1892ab1086818f63d9ca521f8f1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.amazonechocontrol.internal.smarthome;
14
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_CONTACT;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants;
24 import org.openhab.binding.amazonechocontrol.internal.Connection;
25 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.StateDescription;
31 import org.openhab.core.types.UnDefType;
32
33 import com.google.gson.JsonObject;
34
35 /**
36  * The {@link HandlerAcousticEventSensor} is responsible for the Alexa.PowerControllerInterface
37  *
38  * @author Lukas Knoeller - Initial contribution
39  * @author Michael Geramb - Initial contribution
40  */
41 @NonNullByDefault
42 public class HandlerAcousticEventSensor extends HandlerBase {
43     // Interface
44     public static final String INTERFACE = "Alexa.AcousticEventSensor";
45
46     // Channel types
47     private static final ChannelTypeUID CHANNEL_TYPE_GLASS_BREAK_DETECTION_STATE = new ChannelTypeUID(
48             AmazonEchoControlBindingConstants.BINDING_ID, "glassBreakDetectionState");
49     private static final ChannelTypeUID CHANNEL_TYPE_SMOKE_ALARM_DETECTION_STATE = new ChannelTypeUID(
50             AmazonEchoControlBindingConstants.BINDING_ID, "smokeAlarmDetectionState");
51
52     // Channel definitions
53     private static final ChannelInfo GLASS_BREAK_DETECTION_STATE = new ChannelInfo(
54             "glassBreakDetectionState" /* propertyName */ , "glassBreakDetectionState" /* ChannelId */,
55             CHANNEL_TYPE_GLASS_BREAK_DETECTION_STATE /* Channel Type */ , ITEM_TYPE_CONTACT /* Item Type */);
56     private static final ChannelInfo SMOKE_ALARM_DETECTION_STATE = new ChannelInfo(
57             "smokeAlarmDetectionState" /* propertyName */ , "smokeAlarmDetectionState" /* ChannelId */,
58             CHANNEL_TYPE_SMOKE_ALARM_DETECTION_STATE /* Channel Type */ , ITEM_TYPE_CONTACT /* Item Type */);
59
60     private ChannelInfo[] getAlarmChannels() {
61         return new ChannelInfo[] { GLASS_BREAK_DETECTION_STATE, SMOKE_ALARM_DETECTION_STATE };
62     }
63
64     @Override
65     public String[] getSupportedInterface() {
66         return new String[] { INTERFACE };
67     }
68
69     @Override
70     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
71         for (ChannelInfo channelInfo : getAlarmChannels()) {
72             if (channelInfo.propertyName.equals(property)) {
73                 return new ChannelInfo[] { channelInfo };
74             }
75         }
76         return null;
77     }
78
79     @Override
80     public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
81         Boolean glassBreakDetectionStateValue = null;
82         Boolean smokeAlarmDetectionStateValue = null;
83         for (JsonObject state : stateList) {
84             if (GLASS_BREAK_DETECTION_STATE.propertyName.equals(state.get("name").getAsString())) {
85                 if (glassBreakDetectionStateValue == null) {
86                     glassBreakDetectionStateValue = !"NOT_DETECTED"
87                             .equals(state.get("value").getAsJsonObject().get("value").getAsString());
88                 }
89             } else if (SMOKE_ALARM_DETECTION_STATE.propertyName.equals(state.get("name").getAsString())) {
90                 if (smokeAlarmDetectionStateValue == null) {
91                     smokeAlarmDetectionStateValue = !"NOT_DETECTED"
92                             .equals(state.get("value").getAsJsonObject().get("value").getAsString());
93                 }
94             }
95         }
96         updateState(GLASS_BREAK_DETECTION_STATE.channelId, glassBreakDetectionStateValue == null ? UnDefType.UNDEF
97                 : (glassBreakDetectionStateValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
98         updateState(SMOKE_ALARM_DETECTION_STATE.channelId, smokeAlarmDetectionStateValue == null ? UnDefType.UNDEF
99                 : (smokeAlarmDetectionStateValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
100     }
101
102     @Override
103     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
104             SmartHomeCapability[] capabilties, String channelId, Command command) throws IOException {
105         return false;
106     }
107
108     @Override
109     public @Nullable StateDescription findStateDescription(String channelUID, StateDescription originalStateDescription,
110             @Nullable Locale locale) {
111         return null;
112     }
113 }