]> git.basschouten.com Git - openhab-addons.git/blob
29efd8bd0a3f7299207d3097408a625a5c8df7da
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.teleinfo.internal.serial.TeleinfoTicMode;
17
18 /**
19  * The {@link FrameUtil} class defines a utility class for
20  * {@link org.openhab.binding.teleinfo.internal.data.FrameType#CBETM_LONG_BASE}.
21  *
22  * @author Nicolas SIBERIL - Initial contribution
23  */
24 @NonNullByDefault
25 public class FrameUtil {
26
27     private FrameUtil() {
28         // private constructor (utility class)
29     }
30
31     /**
32      * Compute the checksum of the given group line.
33      *
34      * @param groupLine group line {@literal ("etiquette" <SPACE> "valeur")}.
35      *            Note: the SPACE before the checksum of the group line
36      *            must not include in checksum computation.
37      * @return the checksum of the given group line.
38      */
39     public static char computeGroupLineChecksum(final String groupLine, TeleinfoTicMode ticMode) {
40         int sum = 0;
41         for (int i = 0; i < groupLine.length(); i++) {
42             sum += groupLine.codePointAt(i);
43         }
44         if (ticMode == TeleinfoTicMode.STANDARD) {
45             sum += 0x09;
46         }
47         sum = (sum & 0x3F) + 0x20;
48         return (char) sum;
49     }
50
51     /**
52      * Parse relais states.
53      *
54      * @param relais integer string
55      * @return State of each relais
56      */
57     public static boolean[] parseRelaisStates(String relais) {
58         boolean[] relaisState = new boolean[8];
59         int value = Integer.parseInt(relais);
60         for (int i = 0; i <= 7; i++) {
61             relaisState[i] = (value & 1) == 1;
62             value >>= 1;
63         }
64         return relaisState;
65     }
66 }