]> git.basschouten.com Git - openhab-addons.git/blob
4094d637b69398575214a0067bda81a404a7d4a7
[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.touchwand.internal;
14
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
16
17 import java.util.ArrayList;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.touchwand.internal.dto.TouchWandBSensorUnitData;
21 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.binding.builder.ThingBuilder;
27 import org.openhab.core.types.Command;
28
29 /**
30  * The {@link TouchWandBSensorHandler} is responsible for handling command for Binary Sensor unit
31  *
32  * @author Roie Geron - Initial contribution
33  */
34 @NonNullByDefault
35 public class TouchWandBSensorHandler extends TouchWandBaseUnitHandler {
36
37     private boolean isFirstUpdateTouchWandUnitState = true;
38
39     public TouchWandBSensorHandler(Thing thing) {
40         super(thing);
41     }
42
43     @Override
44     void updateTouchWandUnitState(TouchWandUnitData unitData) {
45         if (unitData instanceof TouchWandBSensorUnitData bSensorUnitData) {
46             if (isFirstUpdateTouchWandUnitState) {
47                 removeUnsupportedChannels(bSensorUnitData);
48                 isFirstUpdateTouchWandUnitState = false;
49             }
50             String sensorSubType = bSensorUnitData.getIdData().getSubType();
51             switch (sensorSubType) {
52                 case BSENSOR_SUBTYPE_DOORWINDOW:
53                     updateChannelDoorWindow(bSensorUnitData);
54                     break;
55                 case BSENSOR_SUBTYPE_MOTION:
56                     updateChannelMotion(bSensorUnitData);
57                     break;
58                 case BSENSOR_SUBTYPE_SMOKE:
59                     updateChannelSmoke(bSensorUnitData);
60                     break;
61                 default:
62             }
63         } else {
64             logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance");
65         }
66     }
67
68     @Override
69     void touchWandUnitHandleCommand(Command command) {
70     }
71
72     void updateChannelDoorWindow(TouchWandBSensorUnitData unitData) {
73         OpenClosedType myOpenClose;
74         String isOpen = unitData.getCurrStatus();
75         logger.debug("recieved status {} from door unit {} ", isOpen, unitData.getName());
76         if (isOpen.equals(BSENSOR_STATUS_OPEN)) {
77             myOpenClose = OpenClosedType.OPEN;
78         } else if (isOpen.equals(BSENSOR_STATUS_CLOSE)) {
79             myOpenClose = OpenClosedType.CLOSED;
80         } else {
81             logger.debug("TouchWandBSensorUnitData illegal update value {}", isOpen);
82             return;
83         }
84         updateState(CHANNEL_DOORWINDOW, myOpenClose);
85     }
86
87     void updateChannelMotion(TouchWandBSensorUnitData unitData) {
88         String motion = unitData.getCurrStatus();
89         logger.debug("recieved status {} from motion unit {} ", motion, unitData.getName());
90         OnOffType status;
91         if (motion.equals(BSENSOR_STATUS_OPEN)) {
92             status = OnOffType.ON;
93         } else if (motion.equals(BSENSOR_STATUS_CLOSE)) {
94             status = OnOffType.OFF;
95         } else {
96             logger.debug("TouchWandBSensorUnitData illegal update value {}", motion);
97             return;
98         }
99         updateState(CHANNEL_MOTION, status);
100     }
101
102     void updateChannelSmoke(TouchWandBSensorUnitData unitData) {
103         String hasSmoke = unitData.getCurrStatus();
104         OnOffType status;
105         if (hasSmoke.equals(BSENSOR_STATUS_OPEN)) {
106             status = OnOffType.ON;
107         } else if (hasSmoke.equals(BSENSOR_STATUS_CLOSE)) {
108             status = OnOffType.OFF;
109         } else {
110             logger.debug("TouchWandBSensorUnitData illegal update value {}", hasSmoke);
111             return;
112         }
113         updateState(CHANNEL_SMOKE, status);
114     }
115
116     void removeUnsupportedChannels(TouchWandBSensorUnitData unitData) {
117         ArrayList<Channel> toBeRemovedChannels = new ArrayList<>(thing.getChannels());
118         String sensorSubType = unitData.getIdData().getSubType();
119         switch (sensorSubType) {
120             case BSENSOR_SUBTYPE_DOORWINDOW:
121                 toBeRemovedChannels.remove(thing.getChannel(CHANNEL_DOORWINDOW));
122                 break;
123             case BSENSOR_SUBTYPE_MOTION:
124                 toBeRemovedChannels.remove(thing.getChannel(CHANNEL_MOTION));
125                 break;
126             case BSENSOR_SUBTYPE_SMOKE:
127                 Channel channel = thing.getChannel(CHANNEL_SMOKE);
128                 toBeRemovedChannels.remove(channel);
129                 break;
130         }
131
132         ThingBuilder thingBuilder = editThing();
133         thingBuilder.withoutChannels(toBeRemovedChannels);
134         updateThing(thingBuilder.build());
135     }
136 }