2 * Copyright (c) 2010-2022 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.lcn.internal.subhandler;
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;
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;
31 * Handles Commands and State changes of operating hours counters of an LCN module.
33 * @author Fabian Wolter - Initial contribution
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+)");
41 OUTPUT("A", "output"),
43 BINARY_INPUT("B", "binarysensor"),
44 OUTPUT_RELATIVE_WORK("I", "outputrelativework");
49 private Type(String pattern, String id) {
50 this.pattern = pattern;
54 public static String getId(String pattern) {
55 return Stream.of(values()).filter(t -> t.pattern.equals(pattern)).findAny().get().id;
58 public static String createPattern() {
59 return Stream.of(values()).map(t -> t.pattern).collect(Collectors.joining("|"));
63 public LcnModuleOperatingHoursCounterSubHandler(LcnModuleHandler handler, ModInfo info) {
68 public void handleRefresh(LcnChannelGroup channelGroup, int number) {
73 public Collection<Pattern> getPckStatusMessagePatterns() {
74 return Arrays.asList(PATTERN);
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"));
83 handler.updateChannel(LcnChannelGroup.OPERATINGHOURS, Type.getId(type) + number,
84 QuantityType.valueOf(durationSec, Units.SECOND));