]> git.basschouten.com Git - openhab-addons.git/blob
8f5e9fb3b719c8c69f27dc67d2d7755e57132dbf
[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.mynice.internal.handler;
14
15 import static org.openhab.binding.mynice.internal.MyNiceBindingConstants.*;
16 import static org.openhab.core.thing.Thing.*;
17
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21
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;
38
39 /**
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @NonNullByDefault
44 public class GateHandler extends BaseThingHandler implements MyNiceDataListener {
45     private static final String OPENING = "opening";
46     private static final String CLOSING = "closing";
47
48     private final Logger logger = LoggerFactory.getLogger(GateHandler.class);
49
50     private String id = "";
51
52     public GateHandler(Thing thing) {
53         super(thing);
54     }
55
56     @Override
57     public void initialize() {
58         id = (String) getConfig().get(DEVICE_ID);
59         getBridgeHandler().ifPresent(h -> h.registerDataListener(this));
60     }
61
62     @Override
63     public void dispose() {
64         getBridgeHandler().ifPresent(h -> h.unregisterDataListener(this));
65     }
66
67     private Optional<It4WifiHandler> getBridgeHandler() {
68         Bridge bridge = getBridge();
69         if (bridge != null) {
70             BridgeHandler handler = bridge.getHandler();
71             if (handler instanceof It4WifiHandler it4Handler) {
72                 return Optional.of(it4Handler);
73             }
74         }
75         return Optional.empty();
76     }
77
78     @Override
79     public void handleCommand(ChannelUID channelUID, Command command) {
80         if (command instanceof RefreshType) {
81             return;
82         } else {
83             handleCommand(channelUID.getId(), command.toString());
84         }
85     }
86
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 -> {
94                     try {
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);
99                     }
100                 });
101             } else {
102                 logger.warn("This thing does not accept the T4 command '{}'", command);
103             }
104         }
105     }
106
107     @Override
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)));
117             }
118             if (device.prod != null) {
119                 getBridgeHandler().ifPresent(h -> h.sendCommand(CommandType.STATUS));
120             } else {
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)));
125             }
126             return true;
127         });
128     }
129 }