]> git.basschouten.com Git - openhab-addons.git/blob
0eb71a32cad8d5df1679c11e31b3e4523e2e4914
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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             String hexString = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
46
47             return HexUtils.hexToBytes(hexString, " ");
48         } catch (IOException e) {
49             throw new AssertionError("IOException reading message data: ", e);
50         }
51     }
52
53     /**
54      * Reads a message given the file relative to this package and returns the object.
55      *
56      * @param messageName name of the message file to read
57      * @return a CaddxMessage object
58      */
59     public static CaddxMessage readCaddxMessage(String messageName) {
60         byte[] bytes = readRawMessage(messageName);
61         return new CaddxMessage(bytes, true);
62     }
63 }