]> git.basschouten.com Git - openhab-addons.git/blob
1bbd274d6fad0331f143391fe5f13b0639d19663
[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.teleinfo.internal.reader.io.serialport;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.io.BufferedReader;
18 import java.io.FileInputStream;
19 import java.io.InputStreamReader;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.teleinfo.internal.reader.io.TeleinfoInputStream;
24 import org.openhab.binding.teleinfo.internal.serial.TeleinfoTicMode;
25 import org.openhab.binding.teleinfo.util.TestUtils;
26
27 /**
28  *
29  * @author Olivier MARCEAU - Initial contribution
30  */
31 @NonNullByDefault
32 public class FrameUtilTest {
33
34     @Test
35     public void testComputeGroupLineChecksumThreePhaseProd() throws Exception {
36         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
37                 new FileInputStream(TestUtils.getTestFile("linky-tic-mode-standard-three-phase-prod.raw"))));
38         String groupLine;
39         int i = 0;
40         while ((groupLine = bufferedReader.readLine()) != null) {
41             if (i >= 1 && !TeleinfoInputStream.isHeaderFrame(groupLine)) {
42                 char expected = groupLine.charAt(groupLine.length() - 1);
43                 char actual = FrameUtil.computeGroupLineChecksum(groupLine.substring(0, groupLine.length() - 2),
44                         TeleinfoTicMode.STANDARD);
45                 assertEquals(expected, actual, i + " " + groupLine + " " + (int) expected + " " + (int) actual);
46             }
47             i++;
48         }
49     }
50
51     @Test
52     public void testComputeGroupLineChecksumSinglePhaseProd() throws Exception {
53         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
54                 new FileInputStream(TestUtils.getTestFile("linky-tic-mode-standard-single-phase-prod.raw"))));
55         String groupLine;
56         int i = 0;
57         while ((groupLine = bufferedReader.readLine()) != null) {
58             if (i >= 1 && !TeleinfoInputStream.isHeaderFrame(groupLine)) {
59                 char expected = groupLine.charAt(groupLine.length() - 1);
60                 char actual = FrameUtil.computeGroupLineChecksum(groupLine.substring(0, groupLine.length() - 2),
61                         TeleinfoTicMode.STANDARD);
62                 assertEquals(expected, actual, i + " " + groupLine + " " + (int) expected + " " + (int) actual);
63             }
64             i++;
65         }
66     }
67
68     @Test
69     public void testComputeRelaisStates() {
70         assertArrayEquals(new boolean[] { true, false, false, false, false, false, false, false },
71                 FrameUtil.parseRelaisStates("001"));
72         assertArrayEquals(new boolean[] { false, false, true, true, false, false, false, true },
73                 FrameUtil.parseRelaisStates("140"));
74     }
75 }