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.io.IOException;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.LinkedList;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24 import java.util.stream.Collectors;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.enocean.internal.EnOceanConfigStatusMessage;
29 import org.openhab.binding.enocean.internal.config.EnOceanBaseConfig;
30 import org.openhab.binding.enocean.internal.config.EnOceanBridgeConfig;
31 import org.openhab.binding.enocean.internal.config.EnOceanBridgeConfig.ESPVersion;
32 import org.openhab.binding.enocean.internal.messages.BasePacket;
33 import org.openhab.binding.enocean.internal.messages.ESP3PacketFactory;
34 import org.openhab.binding.enocean.internal.messages.Response;
35 import org.openhab.binding.enocean.internal.messages.Response.ResponseType;
36 import org.openhab.binding.enocean.internal.messages.responses.BaseResponse;
37 import org.openhab.binding.enocean.internal.messages.responses.RDBaseIdResponse;
38 import org.openhab.binding.enocean.internal.messages.responses.RDLearnedClientsResponse;
39 import org.openhab.binding.enocean.internal.messages.responses.RDLearnedClientsResponse.LearnedClient;
40 import org.openhab.binding.enocean.internal.messages.responses.RDRepeaterResponse;
41 import org.openhab.binding.enocean.internal.messages.responses.RDVersionResponse;
42 import org.openhab.binding.enocean.internal.transceiver.EnOceanESP2Transceiver;
43 import org.openhab.binding.enocean.internal.transceiver.EnOceanESP3Transceiver;
44 import org.openhab.binding.enocean.internal.transceiver.EnOceanTransceiver;
45 import org.openhab.binding.enocean.internal.transceiver.PacketListener;
46 import org.openhab.binding.enocean.internal.transceiver.ResponseListener;
47 import org.openhab.binding.enocean.internal.transceiver.ResponseListenerIgnoringTimeouts;
48 import org.openhab.binding.enocean.internal.transceiver.TeachInListener;
49 import org.openhab.binding.enocean.internal.transceiver.TransceiverErrorListener;
50 import org.openhab.core.config.core.Configuration;
51 import org.openhab.core.config.core.status.ConfigStatusMessage;
52 import org.openhab.core.io.transport.serial.PortInUseException;
53 import org.openhab.core.io.transport.serial.SerialPortManager;
54 import org.openhab.core.library.types.StringType;
55 import org.openhab.core.thing.Bridge;
56 import org.openhab.core.thing.ChannelUID;
57 import org.openhab.core.thing.Thing;
58 import org.openhab.core.thing.ThingStatus;
59 import org.openhab.core.thing.ThingStatusDetail;
60 import org.openhab.core.thing.ThingTypeUID;
61 import org.openhab.core.thing.binding.ConfigStatusBridgeHandler;
62 import org.openhab.core.types.Command;
63 import org.openhab.core.types.RefreshType;
64 import org.openhab.core.util.HexUtils;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
69 * The {@link EnOceanBridgeHandler} is responsible for sending ESP3Packages build by {@link EnOceanActuatorHandler} and
70 * transferring received ESP3Packages to {@link EnOceanSensorHandler}.
72 * @author Daniel Weber - Initial contribution
75 public class EnOceanBridgeHandler extends ConfigStatusBridgeHandler implements TransceiverErrorListener {
77 private Logger logger = LoggerFactory.getLogger(EnOceanBridgeHandler.class);
79 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_BRIDGE);
81 private @Nullable EnOceanTransceiver transceiver; // holds connection to serial/tcp port and sends/receives messages
82 private @Nullable ScheduledFuture<?> connectorTask; // is used for reconnection if something goes wrong
84 private byte[] baseId = new byte[0];
85 private Thing[] sendingThings = new Thing[128];
87 private SerialPortManager serialPortManager;
89 private boolean smackAvailable = false;
90 private boolean sendTeachOuts = true;
91 private Set<String> smackClients = Set.of();
93 public EnOceanBridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
95 this.serialPortManager = serialPortManager;
99 public void handleCommand(ChannelUID channelUID, Command command) {
100 if (transceiver == null) {
101 updateStatus(ThingStatus.OFFLINE);
105 switch (channelUID.getId()) {
106 case CHANNEL_REPEATERMODE:
107 if (command instanceof RefreshType) {
108 sendMessage(ESP3PacketFactory.CO_RD_REPEATER,
109 new ResponseListenerIgnoringTimeouts<RDRepeaterResponse>() {
111 public void responseReceived(RDRepeaterResponse response) {
112 if (response.isValid() && response.isOK()) {
113 updateState(channelUID, response.getRepeaterLevel());
115 updateState(channelUID, new StringType(REPEATERMODE_OFF));
119 } else if (command instanceof StringType) {
120 sendMessage(ESP3PacketFactory.CO_WR_REPEATER((StringType) command),
121 new ResponseListenerIgnoringTimeouts<BaseResponse>() {
123 public void responseReceived(BaseResponse response) {
124 if (response.isOK()) {
125 updateState(channelUID, (StringType) command);
132 case CHANNEL_SETBASEID:
133 if (command instanceof StringType) {
135 byte[] id = HexUtils.hexToBytes(((StringType) command).toFullString());
137 sendMessage(ESP3PacketFactory.CO_WR_IDBASE(id),
138 new ResponseListenerIgnoringTimeouts<BaseResponse>() {
140 public void responseReceived(BaseResponse response) {
141 if (response.isOK()) {
142 updateState(channelUID, new StringType("New Id successfully set"));
143 } else if (response.getResponseType() == ResponseType.RET_FLASH_HW_ERROR) {
144 updateState(channelUID,
145 new StringType("The write/erase/verify process failed"));
146 } else if (response.getResponseType() == ResponseType.RET_BASEID_OUT_OF_RANGE) {
147 updateState(channelUID, new StringType("Base id out of range"));
148 } else if (response.getResponseType() == ResponseType.RET_BASEID_MAX_REACHED) {
149 updateState(channelUID, new StringType("No more change possible"));
153 } catch (IllegalArgumentException e) {
154 updateState(channelUID, new StringType("BaseId could not be parsed"));
165 public void initialize() {
166 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "trying to connect to gateway...");
168 connectorTask = scheduler.scheduleWithFixedDelay(new Runnable() {
171 if (thing.getStatus() != ThingStatus.ONLINE) {
175 }, 0, 60, TimeUnit.SECONDS);
178 private synchronized void initTransceiver() {
180 EnOceanBridgeConfig c = getThing().getConfiguration().as(EnOceanBridgeConfig.class);
181 EnOceanTransceiver localTransceiver = transceiver;
182 if (localTransceiver != null) {
183 localTransceiver.shutDown();
186 switch (c.getESPVersion()) {
188 transceiver = new EnOceanESP2Transceiver(c.path, this, scheduler, serialPortManager);
189 smackAvailable = false;
190 sendTeachOuts = false;
193 transceiver = new EnOceanESP3Transceiver(c.path, this, scheduler, serialPortManager);
194 sendTeachOuts = c.sendTeachOuts;
200 localTransceiver = transceiver;
201 if (localTransceiver == null) {
202 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
203 "Failed to initialize EnOceanTransceiver");
207 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "opening serial port...");
208 localTransceiver.initialize();
210 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "starting rx thread...");
211 localTransceiver.startReceiving(scheduler);
212 logger.info("EnOceanSerialTransceiver RX thread up and running");
215 if (!c.rs485BaseId.isEmpty()) {
216 baseId = HexUtils.hexToBytes(c.rs485BaseId);
217 if (baseId.length != 4) {
218 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
219 "RS485 BaseId has the wrong format. It is expected to be an 8 digit hex code, for example 01000000");
222 baseId = new byte[4];
225 updateProperty(PROPERTY_BASE_ID, HexUtils.bytesToHex(baseId));
226 updateStatus(ThingStatus.ONLINE);
228 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
229 "trying to get bridge base id...");
231 logger.debug("request base id");
232 localTransceiver.sendBasePacket(ESP3PacketFactory.CO_RD_IDBASE,
233 new ResponseListenerIgnoringTimeouts<RDBaseIdResponse>() {
236 public void responseReceived(RDBaseIdResponse response) {
237 logger.debug("received response for base id");
238 if (response.isValid() && response.isOK()) {
239 baseId = response.getBaseId().clone();
240 updateProperty(PROPERTY_BASE_ID, HexUtils.bytesToHex(response.getBaseId()));
241 updateProperty(PROPERTY_REMAINING_WRITE_CYCLES_BASE_ID,
242 Integer.toString(response.getRemainingWriteCycles()));
243 EnOceanTransceiver localTransceiver = transceiver;
244 if (localTransceiver != null) {
245 localTransceiver.setFilteredDeviceId(baseId);
247 updateStatus(ThingStatus.ONLINE);
249 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
250 "Could not get BaseId");
255 if (c.getESPVersion() == ESPVersion.ESP3) {
256 logger.debug("set postmaster mailboxes");
257 localTransceiver.sendBasePacket(ESP3PacketFactory.SA_WR_POSTMASTER((byte) (c.enableSmack ? 20 : 0)),
258 new ResponseListenerIgnoringTimeouts<BaseResponse>() {
261 public void responseReceived(BaseResponse response) {
262 logger.debug("received response for postmaster mailboxes");
263 if (response.isOK()) {
264 updateProperty("Postmaster mailboxes:",
265 Integer.toString(c.enableSmack ? 20 : 0));
266 smackAvailable = c.enableSmack;
269 updateProperty("Postmaster mailboxes:", "Not supported");
270 smackAvailable = false;
277 logger.debug("request version info");
278 localTransceiver.sendBasePacket(ESP3PacketFactory.CO_RD_VERSION,
279 new ResponseListenerIgnoringTimeouts<RDVersionResponse>() {
282 public void responseReceived(RDVersionResponse response) {
283 if (response.isValid() && response.isOK()) {
284 updateProperty(PROPERTY_APP_VERSION, response.getAPPVersion());
285 updateProperty(PROPERTY_API_VERSION, response.getAPIVersion());
286 updateProperty(PROPERTY_CHIP_ID, response.getChipID());
287 updateProperty(PROPERTY_DESCRIPTION, response.getDescription());
291 } catch (IOException e) {
292 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Port could not be found");
293 } catch (PortInUseException e) {
294 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Port already in use");
295 } catch (Exception e) {
296 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Port could not be initialized");
302 public synchronized void dispose() {
303 EnOceanTransceiver transceiver = this.transceiver;
304 if (transceiver != null) {
305 transceiver.shutDown();
306 this.transceiver = null;
309 ScheduledFuture<?> connectorTask = this.connectorTask;
310 if (connectorTask != null) {
311 connectorTask.cancel(true);
312 this.connectorTask = null;
319 public Collection<ConfigStatusMessage> getConfigStatus() {
320 Collection<ConfigStatusMessage> configStatusMessages = new LinkedList<>();
322 // The serial port must be provided
323 String path = getThing().getConfiguration().as(EnOceanBridgeConfig.class).path;
324 if (path.isEmpty()) {
325 ConfigStatusMessage statusMessage = ConfigStatusMessage.Builder.error(PATH)
326 .withMessageKeySuffix(EnOceanConfigStatusMessage.PORT_MISSING.getMessageKey()).withArguments(PATH)
328 if (statusMessage != null) {
329 configStatusMessages.add(statusMessage);
333 return configStatusMessages;
336 public byte[] getBaseId() {
337 return baseId.clone();
340 public boolean isSmackClient(Thing sender) {
341 return smackClients.contains(sender.getConfiguration().as(EnOceanBaseConfig.class).enoceanId);
344 public @Nullable Integer getNextSenderId(Thing sender) {
345 return getNextSenderId(sender.getConfiguration().as(EnOceanBaseConfig.class).enoceanId);
348 public @Nullable Integer getNextSenderId(String enoceanId) {
349 EnOceanBridgeConfig config = getConfigAs(EnOceanBridgeConfig.class);
350 Integer senderId = config.nextSenderId;
351 if (senderId == null) {
354 if (sendingThings[senderId] == null) {
355 Configuration c = this.editConfiguration();
356 c.put(PARAMETER_NEXT_SENDERID, null);
357 updateConfiguration(c);
362 for (int i = 1; i < sendingThings.length; i++) {
363 if (sendingThings[i] == null || sendingThings[i].getConfiguration().as(EnOceanBaseConfig.class).enoceanId
364 .equalsIgnoreCase(enoceanId)) {
372 public boolean existsSender(int id, Thing sender) {
373 return sendingThings[id] != null && !sendingThings[id].getConfiguration().as(EnOceanBaseConfig.class).enoceanId
374 .equalsIgnoreCase(sender.getConfiguration().as(EnOceanBaseConfig.class).enoceanId);
377 public void addSender(int id, Thing thing) {
378 sendingThings[id] = thing;
381 public void removeSender(int id) {
382 sendingThings[id] = null;
385 public <T extends @Nullable Response> void sendMessage(BasePacket message,
386 @Nullable ResponseListener<T> responseListener) {
388 EnOceanTransceiver localTransceiver = transceiver;
389 if (localTransceiver == null) {
390 throw new IOException("EnOceanTransceiver has state null");
392 localTransceiver.sendBasePacket(message, responseListener);
393 } catch (IOException e) {
394 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
398 public void addPacketListener(PacketListener listener) {
399 addPacketListener(listener, listener.getEnOceanIdToListenTo());
402 public void addPacketListener(PacketListener listener, long senderIdToListenTo) {
403 EnOceanTransceiver localTransceiver = transceiver;
404 if (localTransceiver != null) {
405 localTransceiver.addPacketListener(listener, senderIdToListenTo);
409 public void removePacketListener(PacketListener listener) {
410 removePacketListener(listener, listener.getEnOceanIdToListenTo());
413 public void removePacketListener(PacketListener listener, long senderIdToListenTo) {
414 EnOceanTransceiver localTransceiver = transceiver;
415 if (localTransceiver != null) {
416 localTransceiver.removePacketListener(listener, senderIdToListenTo);
420 public void startDiscovery(TeachInListener teachInListener) {
421 EnOceanTransceiver localTransceiver = transceiver;
422 if (localTransceiver != null) {
423 localTransceiver.startDiscovery(teachInListener);
426 if (smackAvailable) {
427 // activate smack teach in
428 logger.debug("activate smack teach in");
430 if (localTransceiver == null) {
431 throw new IOException("EnOceanTransceiver has state null");
433 localTransceiver.sendBasePacket(ESP3PacketFactory.SA_WR_LEARNMODE(true),
434 new ResponseListenerIgnoringTimeouts<BaseResponse>() {
436 public void responseReceived(BaseResponse response) {
437 if (response.isOK()) {
438 logger.debug("Smack teach in activated");
442 } catch (IOException e) {
443 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
444 "Smack packet could not be send: " + e.getMessage());
449 public void stopDiscovery() {
450 EnOceanTransceiver localTransceiver = transceiver;
451 if (localTransceiver != null) {
452 localTransceiver.stopDiscovery();
456 if (localTransceiver == null) {
457 throw new IOException("EnOceanTransceiver has state null");
459 localTransceiver.sendBasePacket(ESP3PacketFactory.SA_WR_LEARNMODE(false), null);
461 } catch (IOException e) {
462 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
463 "Smack packet could not be send: " + e.getMessage());
467 private void refreshProperties() {
468 if (getThing().getStatus() == ThingStatus.ONLINE && smackAvailable) {
469 logger.debug("request learned smack clients");
471 EnOceanTransceiver localTransceiver = transceiver;
472 if (localTransceiver != null) {
473 localTransceiver.sendBasePacket(ESP3PacketFactory.SA_RD_LEARNEDCLIENTS,
474 new ResponseListenerIgnoringTimeouts<RDLearnedClientsResponse>() {
476 public void responseReceived(RDLearnedClientsResponse response) {
477 logger.debug("received response for learned smack clients");
478 if (response.isValid() && response.isOK()) {
479 LearnedClient[] clients = response.getLearnedClients();
480 updateProperty("Learned smart ack clients", Integer.toString(clients.length));
481 updateProperty("Smart ack clients",
482 Arrays.stream(clients)
483 .map(x -> String.format("%s (MB Idx: %d)",
484 HexUtils.bytesToHex(x.clientId), x.mailboxIndex))
485 .collect(Collectors.joining(", ")));
486 smackClients = Arrays.stream(clients).map(x -> HexUtils.bytesToHex(x.clientId))
487 .collect(Collectors.toSet());
492 } catch (IOException e) {
493 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
494 "Smack packet could not be send: " + e.getMessage());
500 public void errorOccured(Throwable exception) {
501 EnOceanTransceiver localTransceiver = transceiver;
502 if (localTransceiver != null) {
503 localTransceiver.shutDown();
506 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, exception.getMessage());
509 public boolean sendTeachOuts() {
510 return sendTeachOuts;