2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nikobus.internal.protocol;
15 import java.util.function.Consumer;
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;
22 * The {@link NikobusCommand} class defines factory functions to create commands that can be send to Nikobus
25 * @author Boris Krivonog - Initial contribution
28 public class SwitchModuleCommandFactory {
29 public static NikobusCommand createReadCommand(String address, SwitchModuleGroup group,
30 Consumer<Result> resultConsumer) {
31 checkAddress(address);
33 String commandPayload = CRCUtil.appendCRC2("$10" + CRCUtil.appendCRC(group.getStatusRequest() + address));
34 return new NikobusCommand(commandPayload, 27, 3, "$1C", resultConsumer);
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));
44 String payload = group.getStatusUpdate() + address + value + "FF";
45 return new NikobusCommand(CRCUtil.appendCRC2("$1E" + CRCUtil.appendCRC(payload)), 13, 5, "$0E", resultConsumer);
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));