]> git.basschouten.com Git - openhab-addons.git/blob
25d151fa30f86d8f7a56af1ea10f5b234aa8433d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.gce.internal.model;
14
15 import java.util.regex.Pattern;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.gce.internal.handler.Ipx800DeviceConnector;
20 import org.openhab.binding.gce.internal.handler.Ipx800EventListener;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * This class handles message translation to and from the IPX.
26  *
27  * @author GaĆ«l L'hopital - Initial contribution
28  */
29 @NonNullByDefault
30 public class M2MMessageParser {
31     private static final String IO_DESCRIPTOR = "(\\d{32})";
32     private static final Pattern IO_PATTERN = Pattern.compile(IO_DESCRIPTOR);
33     private static final Pattern VALIDATION_PATTERN = Pattern
34             .compile("I=" + IO_DESCRIPTOR + "&O=" + IO_DESCRIPTOR + "&([AC]\\d{1,2}=\\d+&)*[^I]*");
35
36     private final Logger logger = LoggerFactory.getLogger(M2MMessageParser.class);
37     private final Ipx800DeviceConnector connector;
38     private final @Nullable Ipx800EventListener listener;
39
40     private String expectedResponse = "";
41
42     public M2MMessageParser(Ipx800DeviceConnector connector, @Nullable Ipx800EventListener listener) {
43         this.connector = connector;
44         this.listener = listener;
45         connector.setParser(this);
46     }
47
48     /**
49      *
50      * @param data
51      */
52     public void unsolicitedUpdate(String data) {
53         if (IO_PATTERN.matcher(data).matches()) {
54             PortDefinition portDefinition = PortDefinition.fromM2MCommand(expectedResponse);
55             decodeDataLine(portDefinition, data);
56         } else if (VALIDATION_PATTERN.matcher(data).matches()) {
57             for (String status : data.split("&")) {
58                 String statusPart[] = status.split("=");
59                 int portNumShift = 1;
60                 PortDefinition portDefinition = PortDefinition.fromPortName(statusPart[0].substring(0, 1));
61                 switch (portDefinition) {
62                     case CONTACT:
63                     case RELAY: {
64                         decodeDataLine(portDefinition, statusPart[1]);
65                         break;
66                     }
67                     case COUNTER:
68                         portNumShift = 0; // Align counters on 1 based array
69                     case ANALOG: {
70                         int portNumber = Integer.parseInt(statusPart[0].substring(1)) + portNumShift;
71                         setStatus(portDefinition.getPortName() + portNumber, Double.parseDouble(statusPart[1]));
72                     }
73                 }
74             }
75         } else if (!expectedResponse.isEmpty()) {
76             setStatus(expectedResponse, Double.parseDouble(data));
77         }
78
79         expectedResponse = "";
80     }
81
82     private void decodeDataLine(PortDefinition portDefinition, String data) {
83         for (int count = 0; count < data.length(); count++) {
84             setStatus(portDefinition.getPortName() + (count + 1), (double) data.charAt(count) - '0');
85         }
86     }
87
88     private void setStatus(String port, double value) {
89         logger.debug("Received {} : {}", port, value);
90         if (listener != null) {
91             listener.dataReceived(port, value);
92         }
93     }
94
95     public void setExpectedResponse(String expectedResponse) {
96         if (expectedResponse.endsWith("s")) { // GetInputs or GetOutputs
97             this.expectedResponse = expectedResponse;
98         } else { // GetAnx or GetCountx
99             PortDefinition portType = PortDefinition.fromM2MCommand(expectedResponse);
100             this.expectedResponse = expectedResponse.replaceAll(portType.getM2mCommand(), portType.getPortName());
101         }
102     }
103
104     /**
105      * Set output of the device sending the corresponding command
106      *
107      * @param targetPort
108      * @param targetValue
109      */
110     public void setOutput(String targetPort, int targetValue, boolean pulse) {
111         logger.debug("Sending {} to {}", targetValue, targetPort);
112         String command = String.format("Set%02d%s%s", Integer.parseInt(targetPort), targetValue, pulse ? "p" : "");
113         connector.send(command);
114     }
115
116     /**
117      * Resets the counter value to 0
118      *
119      * @param targetCounter
120      */
121     public void resetCounter(int targetCounter) {
122         logger.debug("Resetting counter {} to 0", targetCounter);
123         connector.send(String.format("ResetCount%d", targetCounter));
124     }
125
126     public void errorOccurred(Exception e) {
127         logger.warn("Error received from connector : {}", e.getMessage());
128         if (listener != null) {
129             listener.errorOccurred(e);
130         }
131     }
132
133     public void resetPLC() {
134         connector.send("Reset");
135     }
136 }