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