]> git.basschouten.com Git - openhab-addons.git/blob
02bd0a7893442f82d052db894e6a2b4816226697
[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.rfxcom.internal.config;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidParameterException;
18
19 /**
20  * Configuration class for Raw RFXCOM device.
21  *
22  * @author James Hewitt-Thomas - Initial contribution
23  */
24 @NonNullByDefault
25 public class RFXComRawDeviceConfiguration extends RFXComGenericDeviceConfiguration {
26     public static final String REPEAT_LABEL = "repeat";
27     public int repeat;
28
29     public static final String ON_PULSES_LABEL = "onPulses";
30     public static final String OFF_PULSES_LABEL = "offPulses";
31     @Nullable
32     public String onPulses;
33     @Nullable
34     public String offPulses;
35     public short @Nullable [] onPulsesArray;
36     public short @Nullable [] offPulsesArray;
37
38     public static final String OPEN_PULSES_LABEL = "openPulses";
39     public static final String CLOSED_PULSES_LABEL = "closedPulses";
40     @Nullable
41     public String openPulses;
42     @Nullable
43     public String closedPulses;
44     public short @Nullable [] openPulsesArray;
45     public short @Nullable [] closedPulsesArray;
46
47     @Override
48     public void parseAndValidate() throws RFXComInvalidParameterException {
49         super.parseAndValidate();
50
51         onPulsesArray = parseAndValidatePulses("onPulses", onPulses);
52         offPulsesArray = parseAndValidatePulses("offPulses", offPulses);
53         openPulsesArray = parseAndValidatePulses("openPulses", openPulses);
54         closedPulsesArray = parseAndValidatePulses("closedPulses", closedPulses);
55     }
56
57     private static short @Nullable [] parseAndValidatePulses(String parameter, @Nullable String pulses)
58             throws RFXComInvalidParameterException {
59         if (pulses != null) {
60             return parseAndValidatePulsesNonNull(parameter, pulses);
61         } else {
62             return null;
63         }
64     }
65
66     private static short[] parseAndValidatePulsesNonNull(String parameter, String pulses)
67             throws RFXComInvalidParameterException {
68         String[] strings = pulses.trim().split("\\s+");
69
70         if (strings.length > 124) {
71             throw new RFXComInvalidParameterException(parameter, pulses, "Cannot have more than 124 pulses");
72         }
73
74         if (strings.length % 2 != 0) {
75             throw new RFXComInvalidParameterException(parameter, pulses, "Pulses must be in pairs");
76         }
77
78         try {
79             short[] shorts = new short[strings.length];
80             for (int i = 0; i < strings.length; i++) {
81                 int pulse = Integer.parseInt(strings[i]);
82                 if (pulse > 65535) {
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.
89                     shorts[i] = 10000;
90                 } else {
91                     shorts[i] = (short) pulse;
92                 }
93             }
94             return shorts;
95         } catch (NumberFormatException e) {
96             throw new RFXComInvalidParameterException(parameter, pulses, e.getMessage(), e);
97         }
98     }
99 }