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