]> git.basschouten.com Git - openhab-addons.git/blob
d6dc139d1bb90543494e3e81cba9ee4e41e79ff6
[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.Arrays;
16 import java.util.Collection;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.lcn.internal.LcnBindingConstants;
24 import org.openhab.binding.lcn.internal.LcnModuleHandler;
25 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
26 import org.openhab.binding.lcn.internal.connection.ModInfo;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.Units;
29
30 /**
31  * Handles Commands and State changes of operating hours counters of an LCN module.
32  *
33  * @author Fabian Wolter - Initial contribution
34  */
35 @NonNullByDefault
36 public class LcnModuleOperatingHoursCounterSubHandler extends AbstractLcnModuleSubHandler {
37     private static final Pattern PATTERN = Pattern.compile("\\$" + LcnBindingConstants.ADDRESS_WITHOUT_PREFIX + //
38             "(?<type>[" + Type.createPattern() + "])(?<number>\\d)(?<durationSec>\\d+)");
39
40     private enum Type {
41         OUTPUT("A", "output"),
42         RELAY("R", "relay"),
43         BINARY_INPUT("B", "binarysensor"),
44         OUTPUT_RELATIVE_WORK("I", "outputrelativework");
45
46         String pattern;
47         String id;
48
49         private Type(String pattern, String id) {
50             this.pattern = pattern;
51             this.id = id;
52         }
53
54         public static String getId(String pattern) {
55             return Stream.of(values()).filter(t -> t.pattern.equals(pattern)).findAny().get().id;
56         }
57
58         public static String createPattern() {
59             return Stream.of(values()).map(t -> t.pattern).collect(Collectors.joining("|"));
60         }
61     }
62
63     public LcnModuleOperatingHoursCounterSubHandler(LcnModuleHandler handler, ModInfo info) {
64         super(handler, info);
65     }
66
67     @Override
68     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
69         // nothing
70     }
71
72     @Override
73     public Collection<Pattern> getPckStatusMessagePatterns() {
74         return Arrays.asList(PATTERN);
75     }
76
77     @Override
78     public void handleStatusMessage(Matcher matcher) {
79         String number = matcher.group("number");
80         String type = matcher.group("type");
81         long durationSec = Long.parseLong(matcher.group("durationSec"));
82
83         handler.updateChannel(LcnChannelGroup.OPERATINGHOURS, Type.getId(type) + number,
84                 QuantityType.valueOf(durationSec, Units.SECOND));
85     }
86 }