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.dsmr.internal.device;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.dsmr.internal.device.connector.DSMRConnectorListener;
19 import org.openhab.binding.dsmr.internal.device.connector.DSMRErrorStatus;
20 import org.openhab.binding.dsmr.internal.device.cosem.CosemObject;
21 import org.openhab.binding.dsmr.internal.device.p1telegram.P1Telegram;
22 import org.openhab.binding.dsmr.internal.device.p1telegram.P1TelegramListener;
23 import org.openhab.binding.dsmr.internal.device.p1telegram.P1TelegramParser;
24 import org.openhab.binding.dsmr.internal.device.p1telegram.TelegramParser;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Helper listener to receive telegram data from the connector, send it to the parser and forward data or errors from
30 * the parser to the DSMR Device.
32 * @author M. Volaart - Initial contribution
33 * @author Hilbrand Bouwkamp - Moved this code out of the DSMRPort class, fixed some issues and reduced code
36 public class DSMRTelegramListener implements P1TelegramListener, DSMRConnectorListener {
38 private final Logger logger = LoggerFactory.getLogger(DSMRTelegramListener.class);
39 private final TelegramParser parser;
41 private @NonNullByDefault({}) P1TelegramListener p1TelegramListener;
46 * @param eventListener listener to send received errors or messages to
48 public DSMRTelegramListener() {
49 parser = new P1TelegramParser(this);
53 * Constructs {@link DSMRTelegramListener} with a Smarty decryptor to first decrypt incoming messages.
55 * @param decryptionKey Smarty decryption key
56 * @param additionalKey Additional optional descryption key
58 public DSMRTelegramListener(final String decryptionKey, final String additionalKey) {
59 parser = new SmartyDecrypter(new P1TelegramParser(this), this, decryptionKey, additionalKey);
63 * Set the P1 Telegram listener.
65 * @param p1TelegramListener the listener to set
67 public void setP1TelegramListener(final P1TelegramListener p1TelegramListener) {
68 this.p1TelegramListener = p1TelegramListener;
71 // Handle calls from the Connector
74 public void handleData(final byte[] data, final int length) {
75 parser.parse(data, length);
79 public void handleError(final DSMRErrorStatus portEvent, final String message) {
80 onError(portEvent, message);
84 // Handle calls from the Parser
87 * Handler for cosemObjects received in a P1 telegram
89 * @param telegram the received telegram.
92 public void telegramReceived(final P1Telegram telegram) {
93 final List<CosemObject> cosemObjects = telegram.getCosemObjects();
95 if (logger.isTraceEnabled()) {
96 logger.trace("Received {} Cosem Objects", cosemObjects.size());
98 if (cosemObjects.isEmpty()) {
99 onError(DSMRErrorStatus.TELEGRAM_NO_DATA, "");
101 p1TelegramListener.telegramReceived(telegram);
106 public void onError(final DSMRErrorStatus state, final String message) {
107 p1TelegramListener.onError(state, message);
111 * @param lenientMode the lenientMode to set
113 public void setLenientMode(final boolean lenientMode) {
114 parser.setLenientMode(lenientMode);