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.mynice.internal.handler;
15 import static org.openhab.binding.mynice.internal.MyNiceBindingConstants.*;
16 import static org.openhab.core.thing.Thing.*;
18 import java.util.List;
20 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.mynice.internal.xml.dto.CommandType;
24 import org.openhab.binding.mynice.internal.xml.dto.Device;
25 import org.openhab.binding.mynice.internal.xml.dto.T4Command;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.binding.BaseThingHandler;
33 import org.openhab.core.thing.binding.BridgeHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
41 * @author Gaƫl L'hopital - Initial contribution
44 public class GateHandler extends BaseThingHandler implements MyNiceDataListener {
45 private static final String OPENING = "opening";
46 private static final String CLOSING = "closing";
48 private final Logger logger = LoggerFactory.getLogger(GateHandler.class);
50 private String id = "";
52 public GateHandler(Thing thing) {
57 public void initialize() {
58 id = (String) getConfig().get(DEVICE_ID);
59 getBridgeHandler().ifPresent(h -> h.registerDataListener(this));
63 public void dispose() {
64 getBridgeHandler().ifPresent(h -> h.unregisterDataListener(this));
67 private Optional<It4WifiHandler> getBridgeHandler() {
68 Bridge bridge = getBridge();
70 BridgeHandler handler = bridge.getHandler();
71 if (handler instanceof It4WifiHandler it4Handler) {
72 return Optional.of(it4Handler);
75 return Optional.empty();
79 public void handleCommand(ChannelUID channelUID, Command command) {
80 if (command instanceof RefreshType) {
83 handleCommand(channelUID.getId(), command.toString());
87 private void handleCommand(String channelId, String command) {
88 if (DOOR_COMMAND.equals(channelId)) {
89 getBridgeHandler().ifPresent(handler -> handler.sendCommand(id, command));
90 } else if (DOOR_T4_COMMAND.equals(channelId)) {
91 String allowed = thing.getProperties().get(ALLOWED_T4);
92 if (allowed != null && allowed.contains(command)) {
93 getBridgeHandler().ifPresent(handler -> {
95 T4Command t4 = T4Command.fromCode(command);
96 handler.sendCommand(id, t4);
97 } catch (IllegalArgumentException e) {
98 logger.warn("{} is not a valid T4 command", command);
102 logger.warn("This thing does not accept the T4 command '{}'", command);
108 public void onDataFetched(List<Device> devices) {
109 devices.stream().filter(d -> id.equals(d.id)).findFirst().map(device -> {
110 updateStatus(ThingStatus.ONLINE);
111 if (thing.getProperties().isEmpty()) {
112 int value = Integer.parseInt(device.properties.t4allowed.values, 16);
113 List<String> t4Allowed = T4Command.fromBitmask(value).stream().map(Enum::name).toList();
114 updateProperties(Map.of(PROPERTY_VENDOR, device.manuf, PROPERTY_MODEL_ID, device.prod,
115 PROPERTY_SERIAL_NUMBER, device.serialNr, PROPERTY_HARDWARE_VERSION, device.versionHW,
116 PROPERTY_FIRMWARE_VERSION, device.versionFW, ALLOWED_T4, String.join(",", t4Allowed)));
118 if (device.prod != null) {
119 getBridgeHandler().ifPresent(h -> h.sendCommand(CommandType.STATUS));
121 String status = device.properties.doorStatus;
122 updateState(DOOR_STATUS, new StringType(status));
123 updateState(DOOR_OBSTRUCTED, OnOffType.from("1".equals(device.properties.obstruct)));
124 updateState(DOOR_MOVING, OnOffType.from(status.equals(CLOSING) || status.equals(OPENING)));