2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.omnilink.internal.handler;
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
17 import java.math.BigInteger;
18 import java.util.LinkedList;
19 import java.util.List;
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;
31 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
32 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
33 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
36 * The {@link AbstractOmnilinkHandler} defines some methods that can be used across
37 * the many different things exposed by the OmniLink protocol
39 * @author Brian O'Connell - Initial contribution
40 * @author Ethan Dye - openHAB3 rewrite
43 public abstract class AbstractOmnilinkHandler extends BaseThingHandler {
44 private final Logger logger = LoggerFactory.getLogger(AbstractOmnilinkHandler.class);
46 public AbstractOmnilinkHandler(Thing thing) {
50 public @Nullable OmnilinkBridgeHandler getOmnilinkBridgeHandler() {
51 Bridge bridge = getBridge();
53 return (OmnilinkBridgeHandler) bridge.getHandler();
59 protected void sendOmnilinkCommand(int message, int param1, int param2) {
61 final OmnilinkBridgeHandler bridge = getOmnilinkBridgeHandler();
63 bridge.sendOmnilinkCommand(message, param1, param2);
65 logger.debug("Received null bridge while sending OmniLink command!");
67 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
68 logger.debug("Could not send command to OmniLink Controller: {}", e.getMessage());
73 * Calculate the area filter the a supplied area
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.
78 protected static int bitFilterForArea(AreaProperties areaProperties) {
79 return BigInteger.ZERO.setBit(areaProperties.getNumber() - 1).intValue();
82 protected @Nullable List<AreaProperties> getAreaProperties() {
83 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
84 List<AreaProperties> areas = new LinkedList<>();
86 if (bridgeHandler != null) {
87 ObjectPropertyRequest<AreaProperties> objectPropertyRequest = ObjectPropertyRequest
88 .builder(bridgeHandler, ObjectPropertyRequests.AREA, 0, 1).build();
90 for (AreaProperties areaProperties : objectPropertyRequest) {
91 String thingName = areaProperties.getName();
92 if (areaProperties.getNumber() == 1 && "".equals(thingName)) {
93 areas.add(areaProperties);
95 } else if ("".equals(thingName)) {
98 areas.add(areaProperties);
106 * Gets the configured number for a thing.
108 * @return Configured number for a thing.
110 protected int getThingNumber() {
111 return ((Number) getThing().getConfiguration().get(THING_PROPERTIES_NUMBER)).intValue();
115 * Gets the configured area number for a thing.
117 * @return Configured area number for a thing.
119 protected int getAreaNumber() {
120 return ((Number) getThing().getConfiguration().get(THING_PROPERTIES_AREA)).intValue();