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.enocean.internal.handler;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.Hashtable;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24 import java.util.function.Predicate;
26 import org.openhab.binding.enocean.internal.config.EnOceanBaseConfig;
27 import org.openhab.binding.enocean.internal.eep.EEP;
28 import org.openhab.binding.enocean.internal.eep.EEPFactory;
29 import org.openhab.binding.enocean.internal.eep.EEPType;
30 import org.openhab.binding.enocean.internal.messages.BasePacket;
31 import org.openhab.binding.enocean.internal.messages.ERP1Message;
32 import org.openhab.binding.enocean.internal.messages.ERP1Message.RORG;
33 import org.openhab.binding.enocean.internal.transceiver.PacketListener;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.thing.Channel;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
40 import org.openhab.core.thing.type.ChannelKind;
41 import org.openhab.core.thing.type.ChannelTypeUID;
42 import org.openhab.core.types.Command;
43 import org.openhab.core.types.State;
44 import org.openhab.core.types.UnDefType;
45 import org.openhab.core.util.HexUtils;
48 * @author Daniel Weber - Initial contribution
49 * This class defines base functionality for receiving eep messages.
51 public class EnOceanBaseSensorHandler extends EnOceanBaseThingHandler implements PacketListener {
53 // List of all thing types which support receiving of eep messages
54 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ROOMOPERATINGPANEL,
55 THING_TYPE_MECHANICALHANDLE, THING_TYPE_CONTACT, THING_TYPE_TEMPERATURESENSOR,
56 THING_TYPE_TEMPERATUREHUMIDITYSENSOR, THING_TYPE_ROCKERSWITCH, THING_TYPE_OCCUPANCYSENSOR,
57 THING_TYPE_LIGHTTEMPERATUREOCCUPANCYSENSOR, THING_TYPE_LIGHTSENSOR, THING_TYPE_PUSHBUTTON,
58 THING_TYPE_AUTOMATEDMETERSENSOR, THING_TYPE_ENVIRONMENTALSENSOR, THING_TYPE_MULTFUNCTIONSMOKEDETECTOR,
59 THING_TYPE_WINDOWSASHHANDLESENSOR);
61 protected final Hashtable<RORG, EEPType> receivingEEPTypes = new Hashtable<>();
63 protected ScheduledFuture<?> responseFuture = null;
65 public EnOceanBaseSensorHandler(Thing thing, ItemChannelLinkRegistry itemChannelLinkRegistry) {
66 super(thing, itemChannelLinkRegistry);
70 void initializeConfig() {
71 config = getConfigAs(EnOceanBaseConfig.class);
75 Collection<EEPType> getEEPTypes() {
76 return Collections.unmodifiableCollection(receivingEEPTypes.values());
80 boolean validateConfig() {
81 receivingEEPTypes.clear();
83 config.receivingEEPId.forEach(receivingEEP -> {
84 EEPType receivingEEPType = EEPType.getType(receivingEEP);
85 if (receivingEEPTypes.putIfAbsent(receivingEEPType.getRORG(), receivingEEPType) != null) {
86 throw new IllegalArgumentException("Receiving more than one EEP of the same RORG is not supported");
89 if (config.receivingSIGEEP) {
90 receivingEEPTypes.put(EEPType.SigBatteryStatus.getRORG(), EEPType.SigBatteryStatus);
92 } catch (IllegalArgumentException e) {
93 configurationErrorDescription = e.getMessage();
99 if (!validateEnoceanId(config.enoceanId)) {
100 configurationErrorDescription = "EnOceanId is not a valid EnOceanId";
104 if (!config.enoceanId.equals(EMPTYENOCEANID)) {
105 getBridgeHandler().addPacketListener(this);
112 public long getEnOceanIdToListenTo() {
113 return Long.parseLong(config.enoceanId, 16);
117 public void handleRemoval() {
118 if (getBridgeHandler() != null) {
119 getBridgeHandler().removePacketListener(this);
121 super.handleRemoval();
125 public void handleCommand(ChannelUID channelUID, Command command) {
126 // sensor things cannot send any messages, hence they are not allowed to handle any command
127 // The only possible command would be "Refresh"
130 protected Predicate<Channel> channelFilter(EEPType eepType, byte[] senderId) {
133 boolean result = eepType.isChannelSupported(c);
134 return (isLinked(c.getUID()) || c.getKind() == ChannelKind.TRIGGER) && result;
138 protected void sendRequestResponse() {
139 throw new UnsupportedOperationException("Sensor cannot send responses");
143 public void packetReceived(BasePacket packet) {
144 ERP1Message msg = (ERP1Message) packet;
145 EEPType receivingEEPType = receivingEEPTypes.get(msg.getRORG());
146 if (receivingEEPType == null) {
150 EEP eep = EEPFactory.buildEEP(receivingEEPType, (ERP1Message) packet);
151 logger.debug("ESP Packet payload {} for {} received", HexUtils.bytesToHex(packet.getPayload()),
152 HexUtils.bytesToHex(msg.getSenderId()));
155 byte[] senderId = msg.getSenderId();
157 // try to interpret received message for all linked or trigger channels
158 getThing().getChannels().stream().filter(channelFilter(receivingEEPType, senderId))
159 .sorted(Comparator.comparing(Channel::getKind)) // handle state channels first
160 .forEachOrdered(channel -> {
162 ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
163 String channelTypeId = (channelTypeUID != null) ? channelTypeUID.getId() : "";
165 String channelId = channel.getUID().getId();
166 Configuration channelConfig = channel.getConfiguration();
168 switch (channel.getKind()) {
170 State result = eep.convertToState(channelId, channelTypeId, channelConfig,
171 this::getCurrentState);
173 // if message can be interpreted (result != UnDefType.UNDEF) => update item state
174 if (result != null && result != UnDefType.UNDEF) {
175 updateState(channelId, result);
179 String lastEvent = lastEvents.get(channelId);
180 String event = eep.convertToEvent(channelId, channelTypeId, lastEvent, channelConfig);
182 triggerChannel(channel.getUID(), event);
183 lastEvents.put(channelId, event);
189 if (receivingEEPType.getRequstesResponse()) {
190 // fire trigger for receive
191 triggerChannel(prepareAnswer, "requestAnswer");
192 // Send response after 100ms
193 if (responseFuture == null || responseFuture.isDone()) {
194 responseFuture = scheduler.schedule(this::sendRequestResponse, 100, TimeUnit.MILLISECONDS);