]> git.basschouten.com Git - openhab-addons.git/blob
ebbd84ed97c570b6db30fc4cc4f1f616ed2078ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.nikobus.internal.protocol;
14
15 import java.util.function.Consumer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.nikobus.internal.protocol.NikobusCommand.Result;
19 import org.openhab.binding.nikobus.internal.utils.CRCUtil;
20
21 /**
22  * The {@link NikobusCommand} class defines factory functions to create commands that can be send to Nikobus
23  * installation.
24  *
25  * @author Boris Krivonog - Initial contribution
26  */
27 @NonNullByDefault
28 public class SwitchModuleCommandFactory {
29     public static NikobusCommand createReadCommand(String address, SwitchModuleGroup group,
30             Consumer<Result> resultConsumer) {
31         checkAddress(address);
32
33         String commandPayload = CRCUtil.appendCRC2("$10" + CRCUtil.appendCRC(group.getStatusRequest() + address));
34         return new NikobusCommand(commandPayload, 27, 3, "$1C", resultConsumer);
35     }
36
37     public static NikobusCommand createWriteCommand(String address, SwitchModuleGroup group, String value,
38             Consumer<Result> resultConsumer) {
39         checkAddress(address);
40         if (value.length() != 12) {
41             throw new IllegalArgumentException(String.format("Value must have 12 chars but got '%s'", value));
42         }
43
44         String payload = group.getStatusUpdate() + address + value + "FF";
45         return new NikobusCommand(CRCUtil.appendCRC2("$1E" + CRCUtil.appendCRC(payload)), 13, 5, "$0E", resultConsumer);
46     }
47
48     private static void checkAddress(String address) {
49         if (address.length() != 4) {
50             throw new IllegalArgumentException(String.format("Address must have 4 chars but got '%s'", address));
51         }
52     }
53 }