]> git.basschouten.com Git - openhab-addons.git/blob
53838b5d94b86e64b0f5711ddbcf5c4a53d3efb5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mqtt.handler;
14
15 import java.util.concurrent.Semaphore;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
20 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
21
22 /**
23  * We need an extended MqttConnectionObserverEx for testing if the state changes are coming in the right order.
24  *
25  * @author David Graeff - Initial contribution
26  */
27 public class MqttConnectionObserverEx implements MqttConnectionObserver {
28     public int counter = 0;
29     public Semaphore semaphore = new Semaphore(1);
30
31     public MqttConnectionObserverEx() throws InterruptedException {
32         semaphore.acquire();
33     }
34
35     @Override
36     public void connectionStateChanged(@NonNull MqttConnectionState state, @Nullable Throwable error) {
37         // First we expect a CONNECTING state and then a DISCONNECTED state change
38         if (counter == 0 && state == MqttConnectionState.CONNECTING) {
39             counter = 1;
40         } else if (counter == 1 && state == MqttConnectionState.CONNECTED) {
41             counter = 2;
42             semaphore.release();
43         } else if (counter == 1 && state == MqttConnectionState.DISCONNECTED) {
44             counter = 2;
45         }
46     }
47 }