]> git.basschouten.com Git - openhab-addons.git/blob
f51a081b737a8b295f5942b827184a1587bc4b56
[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.bluetooth.am43.internal.command;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.bluetooth.am43.internal.data.MotorSettings;
17
18 /**
19  * The {@link SetSettingsCommand} sets the motor settings in bulk. There isn't a way to set them individually so before
20  * you can use this command you need to retrieve the existing commands with a {@link GetAllCommand}
21  *
22  * @author Connor Petty - Initial contribution
23  */
24 @NonNullByDefault
25 public class SetSettingsCommand extends AM43Command {
26
27     private static final byte COMMAND = (byte) 0x11;
28
29     public SetSettingsCommand(MotorSettings settings) {
30         super(COMMAND, createContent(settings));
31     }
32
33     private static byte[] createContent(MotorSettings motorSettings) {
34         @SuppressWarnings("null")
35         int direction = motorSettings.getDirection().toByte();
36         @SuppressWarnings("null")
37         int operationMode = motorSettings.getOperationMode().toByte();
38         int deviceType = motorSettings.getType();
39         int deviceLength = motorSettings.getLength();
40         int deviceSpeed = motorSettings.getSpeed();
41         int deviceDiameter = motorSettings.getDiameter();
42
43         int dataHead = ((direction & 1) << 1) | ((operationMode & 1) << 2) | (deviceType << 4);
44
45         return new byte[] { (byte) dataHead, (byte) deviceSpeed, 0, (byte) ((deviceLength & 0xFF00) >> 8),
46                 (byte) (deviceLength & 0xFF), (byte) deviceDiameter };
47     }
48 }