2 * Copyright (c) 2010-2023 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.rfxcom.internal.config;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidParameterException;
20 * Configuration class for Raw RFXCOM device.
22 * @author James Hewitt-Thomas - Initial contribution
25 public class RFXComRawDeviceConfiguration extends RFXComGenericDeviceConfiguration {
26 public static final String REPEAT_LABEL = "repeat";
29 public static final String ON_PULSES_LABEL = "onPulses";
30 public static final String OFF_PULSES_LABEL = "offPulses";
32 public String onPulses;
34 public String offPulses;
35 public short @Nullable [] onPulsesArray;
36 public short @Nullable [] offPulsesArray;
38 public static final String OPEN_PULSES_LABEL = "openPulses";
39 public static final String CLOSED_PULSES_LABEL = "closedPulses";
41 public String openPulses;
43 public String closedPulses;
44 public short @Nullable [] openPulsesArray;
45 public short @Nullable [] closedPulsesArray;
48 public void parseAndValidate() throws RFXComInvalidParameterException {
49 super.parseAndValidate();
51 onPulsesArray = parseAndValidatePulses("onPulses", onPulses);
52 offPulsesArray = parseAndValidatePulses("offPulses", offPulses);
53 openPulsesArray = parseAndValidatePulses("openPulses", openPulses);
54 closedPulsesArray = parseAndValidatePulses("closedPulses", closedPulses);
57 private static short @Nullable [] parseAndValidatePulses(String parameter, @Nullable String pulses)
58 throws RFXComInvalidParameterException {
60 return parseAndValidatePulsesNonNull(parameter, pulses);
66 private static short[] parseAndValidatePulsesNonNull(String parameter, String pulses)
67 throws RFXComInvalidParameterException {
68 String[] strings = pulses.trim().split("\\s+");
70 if (strings.length > 124) {
71 throw new RFXComInvalidParameterException(parameter, pulses, "Cannot have more than 124 pulses");
74 if (strings.length % 2 != 0) {
75 throw new RFXComInvalidParameterException(parameter, pulses, "Pulses must be in pairs");
79 short[] shorts = new short[strings.length];
80 for (int i = 0; i < strings.length; i++) {
81 int pulse = Integer.parseInt(strings[i]);
83 throw new RFXComInvalidParameterException(parameter, pulses, "Cannot have pulse above 65535 usec");
84 } else if (pulse < 0) {
85 throw new RFXComInvalidParameterException(parameter, pulses, "Cannot have negative pulse");
86 } else if (pulse == 0) {
87 // The user guide suggests that received pulses of size 0 should be
88 // replaced with something above 8000, as they represent gaps.
91 shorts[i] = (short) pulse;
95 } catch (NumberFormatException e) {
96 throw new RFXComInvalidParameterException(parameter, pulses, e.getMessage(), e);