]> git.basschouten.com Git - openhab-addons.git/blob
437371cb88f7c330e9fe5ffe0a2307c4c51e5e56
[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     public DSMRTelegramListener() {
47         parser = new P1TelegramParser(this);
48     }
49
50     /**
51      * Constructs {@link DSMRTelegramListener} with a Smarty decryptor to first decrypt incoming messages.
52      *
53      * @param decryptionKey Smarty decryption key
54      * @param additionalKey Additional optional descryption key
55      */
56     public DSMRTelegramListener(final String decryptionKey, final String additionalKey) {
57         parser = new SmartyDecrypter(new P1TelegramParser(this), this, decryptionKey, additionalKey);
58     }
59
60     /**
61      * Set the P1 Telegram listener.
62      *
63      * @param p1TelegramListener the listener to set
64      */
65     public void setP1TelegramListener(final P1TelegramListener p1TelegramListener) {
66         this.p1TelegramListener = p1TelegramListener;
67     }
68
69     // Handle calls from the Connector
70
71     @Override
72     public void handleData(final byte[] data, final int length) {
73         parser.parse(data, length);
74     }
75
76     @Override
77     public void handleError(final DSMRErrorStatus portEvent, final String message) {
78         onError(portEvent, message);
79         parser.reset();
80     }
81
82     // Handle calls from the Parser
83
84     /**
85      * Handler for cosemObjects received in a P1 telegram
86      *
87      * @param telegram the received telegram.
88      */
89     @Override
90     public void telegramReceived(final P1Telegram telegram) {
91         final List<CosemObject> cosemObjects = telegram.getCosemObjects();
92
93         if (logger.isTraceEnabled()) {
94             logger.trace("Received {} Cosem Objects", cosemObjects.size());
95         }
96         if (cosemObjects.isEmpty()) {
97             onError(DSMRErrorStatus.TELEGRAM_NO_DATA, "");
98         } else {
99             p1TelegramListener.telegramReceived(telegram);
100         }
101     }
102
103     @Override
104     public void onError(final DSMRErrorStatus state, final String message) {
105         p1TelegramListener.onError(state, message);
106     }
107
108     /**
109      * @param lenientMode the lenientMode to set
110      */
111     public void setLenientMode(final boolean lenientMode) {
112         parser.setLenientMode(lenientMode);
113     }
114 }