]> git.basschouten.com Git - openhab-addons.git/blob
494c82fd319ecca927f96647adcfef00ef8af76c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.lcn.internal.LcnBindingConstants;
22 import org.openhab.binding.lcn.internal.LcnModuleHandler;
23 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
24 import org.openhab.binding.lcn.internal.common.LcnDefs;
25 import org.openhab.binding.lcn.internal.common.LcnException;
26 import org.openhab.binding.lcn.internal.common.PckGenerator;
27 import org.openhab.binding.lcn.internal.common.Variable;
28 import org.openhab.binding.lcn.internal.common.VariableValue;
29 import org.openhab.binding.lcn.internal.connection.ModInfo;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.OnOffType;
32
33 /**
34  * Handles Commands and State changes of regulator setpoints of an LCN module.
35  *
36  * @author Fabian Wolter - Initial contribution
37  */
38 @NonNullByDefault
39 public class LcnModuleRvarSetpointSubHandler extends AbstractLcnModuleVariableSubHandler {
40     private static final Pattern PATTERN = Pattern
41             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.S(?<id>\\d)(?<value>\\d+)");
42
43     public LcnModuleRvarSetpointSubHandler(LcnModuleHandler handler, ModInfo info) {
44         super(handler, info);
45     }
46
47     @Override
48     public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
49             throws LcnException {
50         Variable variable = getVariable(channelGroup, number);
51
52         if (info.hasExtendedMeasurementProcessing()) {
53             handler.sendPck(PckGenerator.setSetpointAbsolute(number, command.intValue()));
54         } else {
55             try {
56                 int relativeVariableChange = getRelativeChange(command, variable);
57                 handler.sendPck(
58                         PckGenerator.setSetpointRelative(number, LcnDefs.RelVarRef.CURRENT, relativeVariableChange));
59             } catch (LcnException e) {
60                 // current value unknown for some reason, refresh it in case we come again here
61                 info.refreshVariable(variable);
62                 throw e;
63             }
64         }
65     }
66
67     @Override
68     public void handleStatusMessage(Matcher matcher) throws LcnException {
69         Variable variable = Variable.setPointIdToVar(Integer.parseInt(matcher.group("id")) - 1);
70         VariableValue value = fireUpdateAndReset(matcher, "", variable);
71
72         fireUpdate(LcnChannelGroup.RVARLOCK, variable.getNumber(), OnOffType.from(value.isRegulatorLocked()));
73     }
74
75     @Override
76     public Collection<Pattern> getPckStatusMessagePatterns() {
77         return Collections.singleton(PATTERN);
78     }
79 }