]> git.basschouten.com Git - openhab-addons.git/blob
9b9adb724aa5d1a146107f02a2ccdf4ff89588f2
[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.lcn.internal.subhandler;
14
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.util.stream.IntStream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.lcn.internal.LcnBindingConstants;
23 import org.openhab.binding.lcn.internal.LcnModuleHandler;
24 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
25 import org.openhab.binding.lcn.internal.common.LcnDefs;
26 import org.openhab.binding.lcn.internal.connection.ModInfo;
27 import org.openhab.core.library.types.OpenClosedType;
28
29 /**
30  * Handles State changes of binary sensors of an LCN module.
31  *
32  * @author Fabian Wolter - Initial contribution
33  */
34 @NonNullByDefault
35 public class LcnModuleBinarySensorSubHandler extends AbstractLcnModuleSubHandler {
36     private static final Pattern PATTERN = Pattern.compile(LcnBindingConstants.ADDRESS_REGEX + "Bx(?<byteValue>\\d+)");
37
38     public LcnModuleBinarySensorSubHandler(LcnModuleHandler handler, ModInfo info) {
39         super(handler, info);
40     }
41
42     @Override
43     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
44         info.refreshBinarySensors();
45     }
46
47     @Override
48     public void handleStatusMessage(Matcher matcher) {
49         info.onBinarySensorsResponseReceived();
50
51         boolean[] states = LcnDefs.getBooleanValue(Integer.parseInt(matcher.group("byteValue")));
52
53         IntStream.range(0, LcnChannelGroup.BINARYSENSOR.getCount())
54                 .forEach(i -> fireUpdate(LcnChannelGroup.BINARYSENSOR, i,
55                         states[i] ? OpenClosedType.OPEN : OpenClosedType.CLOSED));
56     }
57
58     @Override
59     public Collection<Pattern> getPckStatusMessagePatterns() {
60         return Collections.singleton(PATTERN);
61     }
62 }