]> git.basschouten.com Git - openhab-addons.git/blob
016b175739385591defff450e8353cb43417623d
[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.nikobus.internal.protocol;
14
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.atomic.AtomicBoolean;
17 import java.util.function.Consumer;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * The {@link NikobusCommand} class holds a command that can be send to Nikobus installation.
26  *
27  * @author Boris Krivonog - Initial contribution
28  */
29 @NonNullByDefault
30 public class NikobusCommand {
31     public static class Result {
32         private final Callable<String> callable;
33
34         private Result(String result) {
35             callable = () -> result;
36         }
37
38         private Result(Exception exception) {
39             callable = () -> {
40                 throw exception;
41             };
42         }
43
44         public String get() throws Exception {
45             return callable.call();
46         }
47     }
48
49     public static class ResponseHandler {
50         private final Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
51         private final Consumer<Result> resultConsumer;
52         private final int responseLength;
53         private final int addressStart;
54         private final String responseCode;
55         private final AtomicBoolean isCompleted = new AtomicBoolean();
56
57         private ResponseHandler(int responseLength, int addressStart, String responseCode,
58                 Consumer<Result> resultConsumer) {
59             this.responseLength = responseLength;
60             this.addressStart = addressStart;
61             this.responseCode = responseCode;
62             this.resultConsumer = resultConsumer;
63         }
64
65         public boolean isCompleted() {
66             return isCompleted.get();
67         }
68
69         public boolean complete(String result) {
70             return complete(new Result(result));
71         }
72
73         public boolean completeExceptionally(Exception exception) {
74             return complete(new Result(exception));
75         }
76
77         private boolean complete(Result result) {
78             if (isCompleted.getAndSet(true)) {
79                 return false;
80             }
81
82             try {
83                 resultConsumer.accept(result);
84             } catch (RuntimeException e) {
85                 logger.warn("Processing result {} failed with {}", result, e.getMessage(), e);
86             }
87
88             return true;
89         }
90
91         public int getResponseLength() {
92             return responseLength;
93         }
94
95         public int getAddressStart() {
96             return addressStart;
97         }
98
99         public String getResponseCode() {
100             return responseCode;
101         }
102     }
103
104     private final String payload;
105     private final @Nullable ResponseHandler responseHandler;
106
107     public NikobusCommand(String payload) {
108         this.payload = payload + '\r';
109         this.responseHandler = null;
110     }
111
112     public NikobusCommand(String payload, int responseLength, int addressStart, String responseCode,
113             Consumer<Result> resultConsumer) {
114         this.payload = payload + '\r';
115         this.responseHandler = new ResponseHandler(responseLength, addressStart, responseCode, resultConsumer);
116     }
117
118     public String getPayload() {
119         return payload;
120     }
121
122     public @Nullable ResponseHandler getResponseHandler() {
123         return responseHandler;
124     }
125 }