]> git.basschouten.com Git - openhab-addons.git/blob
9733310591339b34b515b9c65a1fbb2c79b0f19b
[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
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.connection.ModInfo;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Handle Acks received from an LCN module.
30  *
31  * @author Fabian Wolter - Initial contribution
32  */
33 @NonNullByDefault
34 public class LcnModuleMetaAckSubHandler extends AbstractLcnModuleSubHandler {
35     private final Logger logger = LoggerFactory.getLogger(LcnModuleMetaAckSubHandler.class);
36     /** The pattern for the Ack PCK message */
37     public static final Pattern PATTERN_POS = Pattern.compile("-M(?<segId>\\d{3})(?<modId>\\d{3})!");
38     private static final Pattern PATTERN_NEG = Pattern.compile("-M(?<segId>\\d{3})(?<modId>\\d{3})(?<code>\\d+)");
39
40     public LcnModuleMetaAckSubHandler(LcnModuleHandler handler, ModInfo info) {
41         super(handler, info);
42     }
43
44     @Override
45     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
46         // nothing
47     }
48
49     @Override
50     public void handleStatusMessage(Matcher matcher) {
51         if (matcher.pattern() == PATTERN_POS) {
52             handler.onAckRceived();
53         } else if (matcher.pattern() == PATTERN_NEG) {
54             logger.warn("{}: NACK received: {}", handler.getStatusMessageAddress(),
55                     codeToString(Integer.parseInt(matcher.group("code"))));
56         }
57     }
58
59     private String codeToString(int code) {
60         switch (code) {
61             case LcnBindingConstants.CODE_ACK:
62                 return "ACK";
63             case 5:
64                 return "Unknown command";
65             case 6:
66                 return "Invalid parameter count";
67             case 7:
68                 return "Invalid parameter";
69             case 8:
70                 return "Command not allowed (e.g. output locked)";
71             case 9:
72                 return "Command not allowed by module's configuration";
73             case 10:
74                 return "Module not capable";
75             case 11:
76                 return "Periphery missing";
77             case 12:
78                 return "Programming mode necessary";
79             case 14:
80                 return "Mains fuse blown";
81             default:
82                 return "Unknown";
83         }
84     }
85
86     @Override
87     public Collection<Pattern> getPckStatusMessagePatterns() {
88         return Arrays.asList(PATTERN_POS, PATTERN_NEG);
89     }
90 }