]> git.basschouten.com Git - openhab-addons.git/blob
c84dca2472be5fad9dc0d43bf2c29b0bd077b9c9
[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;
14
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertNull;
17 import static org.junit.jupiter.api.Assertions.fail;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.dsmr.internal.device.connector.DSMRErrorStatus;
25 import org.openhab.binding.dsmr.internal.device.p1telegram.P1Telegram;
26 import org.openhab.binding.dsmr.internal.device.p1telegram.P1TelegramListener;
27 import org.openhab.binding.dsmr.internal.device.p1telegram.P1TelegramParser;
28
29 /**
30  * Util class to read test input telegrams.
31  *
32  * @author Hilbrand Bouwkamp - Initial contribution
33  */
34 @NonNullByDefault
35 public final class TelegramReaderUtil {
36     private static final String TELEGRAM_EXT = ".telegram";
37
38     private TelegramReaderUtil() {
39         // Util class
40     }
41
42     /**
43      * Reads the raw bytes of the telegram given the file relative to this package and returns the objects.
44      *
45      * @param telegramName name of the telegram file to read
46      * @return The raw bytes of a telegram
47      */
48     public static byte[] readRawTelegram(final String telegramName) {
49         try (InputStream is = TelegramReaderUtil.class.getResourceAsStream(telegramName + TELEGRAM_EXT)) {
50             if (is == null) {
51                 fail("Could not find telegram file with name:" + telegramName + TELEGRAM_EXT);
52             }
53             return is.readAllBytes();
54         } catch (final IOException e) {
55             throw new AssertionError("IOException reading telegram data: ", e);
56         }
57     }
58
59     /**
60      * Reads a telegram given the file relative to this package and returns the objects.
61      *
62      * @param telegramName name of the telegram file to read
63      * @param expectedTelegramState expected state of the telegram read
64      * @return a P1Telegram object
65      */
66     public static P1Telegram readTelegram(final String telegramName) {
67         final byte[] telegram = readRawTelegram(telegramName);
68         final P1TelegramListenerImpl listener = new P1TelegramListenerImpl();
69         final P1TelegramParser parser = new P1TelegramParser(listener, true);
70
71         parser.setLenientMode(true);
72         parser.parse(telegram, telegram.length);
73         final P1Telegram p1Telegram = listener.telegram;
74
75         assertNotNull(p1Telegram, "Telegram state should have been set. (Missing newline at end of message?)");
76         assertNull(listener.state, "Expected TelegramState should not be set");
77         return p1Telegram;
78     }
79
80     public static class P1TelegramListenerImpl implements P1TelegramListener {
81         public @Nullable P1Telegram telegram;
82         public @Nullable DSMRErrorStatus state;
83
84         @Override
85         public void telegramReceived(final P1Telegram telegram) {
86             this.telegram = telegram;
87         }
88
89         @Override
90         public void onError(final DSMRErrorStatus state, final String error) {
91             this.state = state;
92         }
93     }
94 }