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.enocean.internal.handler;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.Collection;
18 import java.util.Collections;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.openhab.binding.enocean.internal.config.EnOceanActuatorConfig;
26 import org.openhab.binding.enocean.internal.eep.EEP;
27 import org.openhab.binding.enocean.internal.eep.EEPFactory;
28 import org.openhab.binding.enocean.internal.eep.EEPType;
29 import org.openhab.binding.enocean.internal.messages.BasePacket;
30 import org.openhab.core.config.core.Configuration;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
37 import org.openhab.core.thing.type.ChannelTypeUID;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.RefreshType;
40 import org.openhab.core.util.HexUtils;
44 * @author Daniel Weber - Initial contribution
45 * This class defines base functionality for sending eep messages. This class extends EnOceanBaseSensorHandler
46 * class as most actuator things send status or response messages, too.
48 public class EnOceanBaseActuatorHandler extends EnOceanBaseSensorHandler {
50 // List of thing types which support sending of eep messages
51 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_CENTRALCOMMAND,
52 THING_TYPE_MEASUREMENTSWITCH, THING_TYPE_GENERICTHING, THING_TYPE_ROLLERSHUTTER, THING_TYPE_THERMOSTAT,
53 THING_TYPE_HEATRECOVERYVENTILATION);
55 protected byte[] senderId; // base id of bridge + senderIdOffset, used for sending msg
56 protected byte[] destinationId; // in case of broadcast FFFFFFFF otherwise the enocean id of the device
58 protected EEPType sendingEEPType = null;
60 private ScheduledFuture<?> refreshJob; // used for polling current status of thing
62 public EnOceanBaseActuatorHandler(Thing thing, ItemChannelLinkRegistry itemChannelLinkRegistry) {
63 super(thing, itemChannelLinkRegistry);
68 * @param senderIdOffset to be validated
69 * @return true if senderIdOffset is between ]0;128[ and is not used yet
71 private boolean validateSenderIdOffset(int senderIdOffset) {
72 if (senderIdOffset == -1) {
76 if (senderIdOffset > 0 && senderIdOffset < 128) {
77 EnOceanBridgeHandler bridgeHandler = getBridgeHandler();
78 if (bridgeHandler != null) {
79 return !bridgeHandler.existsSender(senderIdOffset, this.thing);
87 void initializeConfig() {
88 config = getConfigAs(EnOceanActuatorConfig.class);
91 protected EnOceanActuatorConfig getConfiguration() {
92 return (EnOceanActuatorConfig) config;
96 Collection<EEPType> getEEPTypes() {
97 Collection<EEPType> r = super.getEEPTypes();
98 if (sendingEEPType == null) {
102 return Collections.unmodifiableCollection(Stream
103 .concat(r.stream(), Collections.singletonList(sendingEEPType).stream()).collect(Collectors.toList()));
107 boolean validateConfig() {
108 EnOceanActuatorConfig config = getConfiguration();
109 if (config == null) {
110 configurationErrorDescription = "Configuration is not valid";
114 if (config.sendingEEPId == null || config.sendingEEPId.isEmpty()) {
115 configurationErrorDescription = "Sending EEP must be provided";
120 sendingEEPType = EEPType.getType(getConfiguration().sendingEEPId);
121 } catch (IllegalArgumentException e) {
122 configurationErrorDescription = "Sending EEP is not supported";
126 if (super.validateConfig()) {
128 if (sendingEEPType.getSupportsRefresh()) {
129 if (getConfiguration().pollingInterval > 0) {
130 refreshJob = scheduler.scheduleWithFixedDelay(() -> {
133 } catch (Exception e) {
135 }, 30, getConfiguration().pollingInterval, TimeUnit.SECONDS);
139 if (getConfiguration().broadcastMessages) {
140 destinationId = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
142 destinationId = HexUtils.hexToBytes(config.enoceanId);
144 } catch (Exception e) {
145 configurationErrorDescription = "Configuration is not valid";
149 if (validateSenderIdOffset(getConfiguration().senderIdOffset)) {
150 return initializeIdForSending();
152 configurationErrorDescription = "Sender Id is not valid for bridge";
159 private boolean initializeIdForSending() {
160 // Generic things are treated as actuator things, however to support also generic sensors one can define a
161 // senderIdOffset of -1
162 // TODO: seperate generic actuators from generic sensors?
163 String thingTypeId = this.getThing().getThingTypeUID().getId();
164 String genericThingTypeId = THING_TYPE_GENERICTHING.getId();
166 if (getConfiguration().senderIdOffset == -1 && thingTypeId.equals(genericThingTypeId)) {
170 EnOceanBridgeHandler bridgeHandler = getBridgeHandler();
171 if (bridgeHandler == null) {
175 // if senderIdOffset is not set (=> defaults to -1) or set to -1, the next free senderIdOffset is determined
176 if (getConfiguration().senderIdOffset == -1) {
177 Configuration updateConfig = editConfiguration();
178 getConfiguration().senderIdOffset = bridgeHandler.getNextSenderId(thing);
179 if (getConfiguration().senderIdOffset == -1) {
180 configurationErrorDescription = "Could not get a free sender Id from Bridge";
183 updateConfig.put(PARAMETER_SENDERIDOFFSET, getConfiguration().senderIdOffset);
184 updateConfiguration(updateConfig);
187 byte[] baseId = bridgeHandler.getBaseId();
188 baseId[3] = (byte) ((baseId[3] & 0xFF) + getConfiguration().senderIdOffset);
189 this.senderId = baseId;
191 this.updateProperty(PROPERTY_ENOCEAN_ID, HexUtils.bytesToHex(this.senderId));
192 bridgeHandler.addSender(getConfiguration().senderIdOffset, thing);
197 private void refreshStates() {
198 logger.debug("polling channels");
199 if (thing.getStatus().equals(ThingStatus.ONLINE)) {
200 for (Channel channel : this.getThing().getChannels()) {
201 handleCommand(channel.getUID(), RefreshType.REFRESH);
207 public void handleCommand(ChannelUID channelUID, Command command) {
208 // We must have a valid sendingEEPType and sender id to send commands
209 if (sendingEEPType == null || senderId == null) {
213 // check if the channel is linked otherwise do nothing
214 String channelId = channelUID.getId();
215 Channel channel = getThing().getChannel(channelUID);
216 if (channel == null || !isLinked(channelUID)) {
220 ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
221 String channelTypeId = (channelTypeUID != null) ? channelTypeUID.getId() : "";
223 // check if we do support refreshs
224 if (command == RefreshType.REFRESH) {
225 if (!sendingEEPType.getSupportsRefresh()) {
229 // receiving status cannot be refreshed
230 switch (channelTypeId) {
232 case CHANNEL_REPEATCOUNT:
233 case CHANNEL_LASTRECEIVED:
239 Configuration channelConfig = channel.getConfiguration();
241 EEP eep = EEPFactory.createEEP(sendingEEPType);
242 if (eep.convertFromCommand(channelId, channelTypeId, command, id -> getCurrentState(id), channelConfig)
244 BasePacket msg = eep.setSenderId(senderId).setDestinationId(destinationId)
245 .setSuppressRepeating(getConfiguration().suppressRepeating).getERP1Message();
247 getBridgeHandler().sendMessage(msg, null);
250 } catch (IllegalArgumentException e) {
251 logger.warn("Exception while sending telegram!", e);
256 public void handleRemoval() {
257 if (getConfiguration().senderIdOffset > 0) {
258 EnOceanBridgeHandler bridgeHandler = getBridgeHandler();
259 if (bridgeHandler != null) {
260 bridgeHandler.removeSender(getConfiguration().senderIdOffset);
264 super.handleRemoval();
268 public void dispose() {
269 if (refreshJob != null && !refreshJob.isCancelled()) {
270 refreshJob.cancel(true);