]> git.basschouten.com Git - openhab-addons.git/blob
c094b4affa81cebacf3953fa81e533f983c3807c
[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.caddx.internal;
14
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.core.util.HexUtils;
23
24 /**
25  * Util class to read test input messages.
26  *
27  * @author Georgios Moutsos - Initial contribution
28  */
29 @NonNullByDefault
30 public final class CaddxMessageReaderUtil {
31     private static final String MESSAGE_EXT = ".msg";
32
33     private CaddxMessageReaderUtil() {
34         // Util class
35     }
36
37     /**
38      * Reads the raw bytes of the message given the file relative to this package and returns the objects.
39      *
40      * @param messageName name of the telegram file to read
41      * @return The raw bytes of a telegram
42      */
43     public static byte[] readRawMessage(String messageName) {
44         try (InputStream is = CaddxMessageReaderUtil.class.getResourceAsStream(messageName + MESSAGE_EXT);
45                 InputStreamReader isr = new InputStreamReader(is);
46                 BufferedReader br = new BufferedReader(isr)) {
47             String hexString = br.lines().collect(Collectors.joining("\n"));
48
49             return HexUtils.hexToBytes(hexString, " ");
50         } catch (IOException e) {
51             throw new AssertionError("IOException reading message data: ", e);
52         }
53     }
54
55     /**
56      * Reads a message given the file relative to this package and returns the object.
57      *
58      * @param messageName name of the message file to read
59      * @return a CaddxMessage object
60      */
61     public static CaddxMessage readCaddxMessage(String messageName) {
62         byte[] bytes = readRawMessage(messageName);
63         return new CaddxMessage(CaddxMessageContext.NONE, bytes, true);
64     }
65 }