]> git.basschouten.com Git - openhab-addons.git/blob
16a3acc4816c7341ab8ff14d84ec6c3c67f257c3
[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.dsmr.internal.device;
14
15 import java.util.List;
16
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;
27
28 /**
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.
31  *
32  * @author M. Volaart - Initial contribution
33  * @author Hilbrand Bouwkamp - Moved this code out of the DSMRPort class, fixed some issues and reduced code
34  */
35 @NonNullByDefault
36 public class DSMRTelegramListener implements P1TelegramListener, DSMRConnectorListener {
37
38     private final Logger logger = LoggerFactory.getLogger(DSMRTelegramListener.class);
39     private final TelegramParser parser;
40
41     private @NonNullByDefault({}) P1TelegramListener p1TelegramListener;
42
43     /**
44      * Constructor.
45      *
46      * @param eventListener listener to send received errors or messages to
47      */
48     public DSMRTelegramListener() {
49         parser = new P1TelegramParser(this);
50     }
51
52     /**
53      * Constructs {@link DSMRTelegramListener} with a Smarty decryptor to first decrypt incoming messages.
54      *
55      * @param decryptionKey Smarty decryption key
56      * @param additionalKey Additional optional descryption key
57      */
58     public DSMRTelegramListener(final String decryptionKey, final String additionalKey) {
59         parser = new SmartyDecrypter(new P1TelegramParser(this), this, decryptionKey, additionalKey);
60     }
61
62     /**
63      * Set the P1 Telegram listener.
64      *
65      * @param p1TelegramListener the listener to set
66      */
67     public void setP1TelegramListener(final P1TelegramListener p1TelegramListener) {
68         this.p1TelegramListener = p1TelegramListener;
69     }
70
71     // Handle calls from the Connector
72
73     @Override
74     public void handleData(final byte[] data, final int length) {
75         parser.parse(data, length);
76     }
77
78     @Override
79     public void handleError(final DSMRErrorStatus portEvent, final String message) {
80         onError(portEvent, message);
81         parser.reset();
82     }
83
84     // Handle calls from the Parser
85
86     /**
87      * Handler for cosemObjects received in a P1 telegram
88      *
89      * @param telegram the received telegram.
90      */
91     @Override
92     public void telegramReceived(final P1Telegram telegram) {
93         final List<CosemObject> cosemObjects = telegram.getCosemObjects();
94
95         if (logger.isTraceEnabled()) {
96             logger.trace("Received {} Cosem Objects", cosemObjects.size());
97         }
98         if (cosemObjects.isEmpty()) {
99             onError(DSMRErrorStatus.TELEGRAM_NO_DATA, "");
100         } else {
101             p1TelegramListener.telegramReceived(telegram);
102         }
103     }
104
105     @Override
106     public void onError(final DSMRErrorStatus state, final String message) {
107         p1TelegramListener.onError(state, message);
108     }
109
110     /**
111      * @param lenientMode the lenientMode to set
112      */
113     public void setLenientMode(final boolean lenientMode) {
114         parser.setLenientMode(lenientMode);
115     }
116 }