2 * Copyright (c) 2010-2023 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.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;
32 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
33 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
34 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
37 * The {@link AbstractOmnilinkHandler} defines some methods that can be used across
38 * the many different things exposed by the OmniLink protocol
40 * @author Brian O'Connell - Initial contribution
41 * @author Ethan Dye - openHAB3 rewrite
44 public abstract class AbstractOmnilinkHandler extends BaseThingHandler {
45 private final Logger logger = LoggerFactory.getLogger(AbstractOmnilinkHandler.class);
47 public AbstractOmnilinkHandler(Thing thing) {
51 public @Nullable OmnilinkBridgeHandler getOmnilinkBridgeHandler() {
52 Bridge bridge = getBridge();
54 return (OmnilinkBridgeHandler) bridge.getHandler();
60 protected void sendOmnilinkCommand(int message, int param1, int param2) {
62 final OmnilinkBridgeHandler bridge = getOmnilinkBridgeHandler();
64 bridge.sendOmnilinkCommand(message, param1, param2);
66 logger.debug("Received null bridge while sending OmniLink command!");
68 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
69 logger.debug("Could not send command to OmniLink Controller: {}", e.getMessage());
74 * Calculate the area filter the a supplied area
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.
79 protected static int bitFilterForArea(AreaProperties areaProperties) {
80 return BigInteger.ZERO.setBit(areaProperties.getNumber() - 1).intValue();
83 protected @Nullable List<AreaProperties> getAreaProperties() {
84 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
85 List<AreaProperties> areas = new LinkedList<>();
87 if (bridgeHandler != null) {
88 ObjectPropertyRequest<AreaProperties> objectPropertyRequest = ObjectPropertyRequest
89 .builder(bridgeHandler, ObjectPropertyRequests.AREA, 0, 1).build();
91 for (AreaProperties areaProperties : objectPropertyRequest) {
92 String thingName = areaProperties.getName();
93 if (areaProperties.getNumber() == 1 && "".equals(thingName)) {
94 areas.add(areaProperties);
96 } else if ("".equals(thingName)) {
99 areas.add(areaProperties);
107 * Gets the configured number for a thing.
109 * @return Configured number for a thing.
111 protected int getThingNumber() {
112 return ((Number) getThing().getConfiguration().get(THING_PROPERTIES_NUMBER)).intValue();
116 * Gets the configured area number for a thing.
118 * @return Configured area number for a thing.
120 protected int getAreaNumber() {
121 String areaNumber = getThing().getProperties().get(THING_PROPERTIES_AREA);
122 if (areaNumber != null) {
123 return Integer.valueOf(areaNumber);