]> git.basschouten.com Git - openhab-addons.git/blob
fd7f6871e170f568309a926abcd26a86cda7dc72
[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.pjlinkdevice.internal.device.command.lampstatus;
14
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.pjlinkdevice.internal.device.command.PrefixedResponse;
23 import org.openhab.binding.pjlinkdevice.internal.device.command.ResponseException;
24
25 /**
26  * The response part of {@link LampStatesCommand}
27  *
28  * @author Nils Schnabel - Initial contribution
29  */
30 @NonNullByDefault
31 public class LampStatesResponse extends PrefixedResponse<List<LampStatesResponse.LampState>> {
32     static final Pattern RESPONSE_VALIDATION_PATTERN = Pattern.compile("^((\\d+) ([01]))( (\\d+) ([01]))*$");
33     static final Pattern RESPONSE_PARSING_PATTERN = Pattern.compile("(?<hours>\\d+) (?<active>[01])");
34
35     @NonNullByDefault
36     public class LampState {
37         private boolean active;
38         private int lampHours;
39
40         public LampState(boolean active, int lampHours) {
41             this.active = active;
42             this.lampHours = lampHours;
43         }
44
45         public int getLampHours() {
46             return lampHours;
47         }
48
49         public boolean isActive() {
50             return active;
51         }
52     }
53
54     public LampStatesResponse(String response) throws ResponseException {
55         super("LAMP=", response);
56     }
57
58     @Override
59     protected List<LampStatesResponse.LampState> parseResponseWithoutPrefix(String responseWithoutPrefix)
60             throws ResponseException {
61         // validate if response fully matches specification
62         if (!RESPONSE_VALIDATION_PATTERN.matcher(responseWithoutPrefix).matches()) {
63             throw new ResponseException(
64                     MessageFormat.format("Lamp status response could not be parsed: ''{0}''", responseWithoutPrefix));
65         }
66
67         // go through individual matches for each lamp
68         List<LampStatesResponse.LampState> result = new ArrayList<>();
69         Matcher matcher = RESPONSE_PARSING_PATTERN.matcher(responseWithoutPrefix);
70         while (matcher.find()) {
71             int lampHours = Integer.parseInt(matcher.group("hours"));
72             boolean active = matcher.group("active").equals("1");
73             result.add(new LampState(active, lampHours));
74         }
75         return result;
76     }
77 }