]> git.basschouten.com Git - openhab-addons.git/blob
7ffec9c4ef4a7693ec2107ccfa59ba5fea1e8c9e
[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.satel.internal.handler;
14
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.satel.internal.types.StateType;
19 import org.openhab.binding.satel.internal.types.TroubleState;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.binding.builder.ChannelBuilder;
23 import org.openhab.core.thing.binding.builder.ThingBuilder;
24
25 /**
26  * The {@link WirelessChannelsHandler} is base thing handler class for things that can be wireless devices.
27  * It adds support for two additional channels:
28  * <ul>
29  * <li><i>device_lobatt</i> - low battery indication</li>
30  * <li><i>device_nocomm</i> - communication problems indication</li>
31  * </ul>
32  * adding them if the device is configured as a wireless device.
33  *
34  * @author Krzysztof Goworek - Initial contribution
35  */
36 @NonNullByDefault
37 public abstract class WirelessChannelsHandler extends SatelStateThingHandler {
38
39     public WirelessChannelsHandler(Thing thing) {
40         super(thing);
41     }
42
43     @Override
44     public void initialize() {
45         super.initialize();
46
47         withBridgeHandlerPresent(bridgeHandler -> {
48             // add/remove channels depending on whether or not the device is wireless
49             final int wirelessDeviceId = getThingConfig().getId() - bridgeHandler.getIntegraType().getOnMainboard();
50             ThingBuilder thingBuilder = editThing();
51             if (isWirelessDevice() && wirelessDeviceId > 0) {
52                 // if a wireless device, remove channels for wireless devices
53                 if (getChannel(TroubleState.DEVICE_LOBATT) == null) {
54                     thingBuilder.withChannel(ChannelBuilder.create(getChannelUID(TroubleState.DEVICE_LOBATT), "Switch")
55                             .withType(CHANNEL_TYPE_LOBATT).build());
56                 }
57                 if (getChannel(TroubleState.DEVICE_NOCOMM) == null) {
58                     thingBuilder.withChannel(ChannelBuilder.create(getChannelUID(TroubleState.DEVICE_NOCOMM), "Switch")
59                             .withType(CHANNEL_TYPE_NOCOMM).build());
60                 }
61             } else {
62                 // if not a wireless device, remove channels for wireless devices
63                 thingBuilder.withoutChannel(getChannelUID(TroubleState.DEVICE_LOBATT))
64                         .withoutChannel(getChannelUID(TroubleState.DEVICE_NOCOMM));
65             }
66             updateThing(thingBuilder.build());
67         });
68     }
69
70     /**
71      * Defines the thing as a wireless or wired device.
72      *
73      * @return <code>true</code> if the thing is, or is configured as a wireless device
74      */
75     protected boolean isWirelessDevice() {
76         return getThingConfig().isWireless();
77     }
78
79     @Override
80     protected int getStateBitNbr(StateType stateType) {
81         int bitNbr = getThingConfig().getId() - 1;
82         if (stateType instanceof TroubleState) {
83             // for wireless devices we need to correct bit number
84             switch ((TroubleState) stateType) {
85                 case DEVICE_LOBATT1:
86                 case DEVICE_NOCOMM1:
87                 case OUTPUT_NOCOMM1:
88                     bitNbr -= 120;
89                     // pass through
90                 case DEVICE_LOBATT:
91                 case DEVICE_NOCOMM:
92                 case OUTPUT_NOCOMM:
93                     bitNbr -= getBridgeHandler().getIntegraType().getOnMainboard();
94                     break;
95                 default:
96                     // other states are either not supported or don't need correction
97                     break;
98             }
99         }
100         return bitNbr;
101     }
102
103     @Override
104     protected StateType getStateType(String channelId) {
105         String stateName = channelId.toUpperCase();
106         if (TroubleState.DEVICE_LOBATT.name().equals(stateName)
107                 || TroubleState.DEVICE_NOCOMM.name().equals(stateName)) {
108             if (getThingConfig().getId() - getBridgeHandler().getIntegraType().getOnMainboard() > 120) {
109                 // last 120 ACU-100 devices in INTEGRA 256 PLUS
110                 stateName += "1";
111             }
112             return TroubleState.valueOf(stateName);
113         } else {
114             return StateType.NONE;
115         }
116     }
117
118     /**
119      * Returns channel UID for given state type.
120      *
121      * @param stateType state type to get channel UID for
122      * @return channel UID object
123      */
124     private ChannelUID getChannelUID(StateType stateType) {
125         String channelId = stateType.toString().toLowerCase();
126         return new ChannelUID(getThing().getUID(), channelId);
127     }
128 }