]> git.basschouten.com Git - openhab-addons.git/blob
f17f716a1cd32d5032485e0fac39abe528fbf686
[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.omnilink.internal.handler;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import java.math.BigInteger;
18 import java.util.LinkedList;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
24 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.binding.BaseThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
32 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
33 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
34
35 /**
36  * The {@link AbstractOmnilinkHandler} defines some methods that can be used across
37  * the many different things exposed by the OmniLink protocol
38  *
39  * @author Brian O'Connell - Initial contribution
40  * @author Ethan Dye - openHAB3 rewrite
41  */
42 @NonNullByDefault
43 public abstract class AbstractOmnilinkHandler extends BaseThingHandler {
44     private final Logger logger = LoggerFactory.getLogger(AbstractOmnilinkHandler.class);
45
46     public AbstractOmnilinkHandler(Thing thing) {
47         super(thing);
48     }
49
50     public @Nullable OmnilinkBridgeHandler getOmnilinkBridgeHandler() {
51         Bridge bridge = getBridge();
52         if (bridge != null) {
53             return (OmnilinkBridgeHandler) bridge.getHandler();
54         } else {
55             return null;
56         }
57     }
58
59     protected void sendOmnilinkCommand(int message, int param1, int param2) {
60         try {
61             final OmnilinkBridgeHandler bridge = getOmnilinkBridgeHandler();
62             if (bridge != null) {
63                 bridge.sendOmnilinkCommand(message, param1, param2);
64             } else {
65                 logger.debug("Received null bridge while sending OmniLink command!");
66             }
67         } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
68             logger.debug("Could not send command to OmniLink Controller: {}", e.getMessage());
69         }
70     }
71
72     /**
73      * Calculate the area filter the a supplied area
74      *
75      * @param area Area to calculate filter for.
76      * @return Calculated Bit Filter for the supplied area. Bit 0 is area 1, bit 2 is area 2 and so on.
77      */
78     protected static int bitFilterForArea(AreaProperties areaProperties) {
79         return BigInteger.ZERO.setBit(areaProperties.getNumber() - 1).intValue();
80     }
81
82     protected @Nullable List<AreaProperties> getAreaProperties() {
83         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
84         List<AreaProperties> areas = new LinkedList<>();
85
86         if (bridgeHandler != null) {
87             ObjectPropertyRequest<AreaProperties> objectPropertyRequest = ObjectPropertyRequest
88                     .builder(bridgeHandler, ObjectPropertyRequests.AREA, 0, 1).build();
89
90             for (AreaProperties areaProperties : objectPropertyRequest) {
91                 String thingName = areaProperties.getName();
92                 if (areaProperties.getNumber() == 1 && "".equals(thingName)) {
93                     areas.add(areaProperties);
94                     break;
95                 } else if ("".equals(thingName)) {
96                     break;
97                 } else {
98                     areas.add(areaProperties);
99                 }
100             }
101         }
102         return areas;
103     }
104
105     /**
106      * Gets the configured number for a thing.
107      *
108      * @return Configured number for a thing.
109      */
110     protected int getThingNumber() {
111         return ((Number) getThing().getConfiguration().get(THING_PROPERTIES_NUMBER)).intValue();
112     }
113
114     /**
115      * Gets the configured area number for a thing.
116      *
117      * @return Configured area number for a thing.
118      */
119     protected int getAreaNumber() {
120         return ((Number) getThing().getConfiguration().get(THING_PROPERTIES_AREA)).intValue();
121     }
122 }