]> git.basschouten.com Git - openhab-addons.git/blob
25b41dd91f04816651ad8c75c68f3cd6d58672ae
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.smartmeter;
14
15 import java.io.IOException;
16 import java.util.function.Supplier;
17
18 import org.openhab.binding.smartmeter.connectors.ConnectorBase;
19
20 /**
21  *
22  * @author Matthias Steigenberger - Initial contribution
23  *
24  */
25 public class MockMeterReaderConnector extends ConnectorBase<Object> {
26
27     private boolean applyRetry;
28     private Supplier<Object> readNextSupplier;
29
30     protected MockMeterReaderConnector(String portName, boolean applyRetry, Supplier<Object> readNextSupplier) {
31         super(portName);
32         this.applyRetry = applyRetry;
33         this.readNextSupplier = readNextSupplier;
34     }
35
36     @Override
37     public void openConnection() throws IOException {
38     }
39
40     @Override
41     public void closeConnection() {
42     }
43
44     @Override
45     protected Object readNext(byte[] initMessage) throws IOException {
46         try {
47             return readNextSupplier.get();
48         } catch (RuntimeException e) {
49             if (e.getCause() instanceof IOException) {
50                 throw (IOException) e.getCause();
51             }
52             throw e;
53         }
54     }
55
56     @Override
57     protected boolean applyRetryHandling() {
58         return this.applyRetry;
59     }
60
61     @Override
62     protected boolean applyPeriod() {
63         return true;
64     }
65
66     @Override
67     protected void retryHook(int retryCount) {
68         super.retryHook(retryCount);
69     }
70 }