]> git.basschouten.com Git - openhab-addons.git/blob
18586d0f61851b67517c4559aca9f26b7b4e8756
[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.mqtt.handler;
14
15 import java.util.concurrent.Semaphore;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
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 @NonNullByDefault
28 public class MqttConnectionObserverEx implements MqttConnectionObserver {
29     public int counter = 0;
30     public Semaphore semaphore = new Semaphore(1);
31
32     public MqttConnectionObserverEx() throws InterruptedException {
33         semaphore.acquire();
34     }
35
36     @Override
37     public void connectionStateChanged(MqttConnectionState state, @Nullable Throwable error) {
38         // First we expect a CONNECTING state and then a DISCONNECTED state change
39         if (counter == 0 && state == MqttConnectionState.CONNECTING) {
40             counter = 1;
41         } else if (counter == 1 && state == MqttConnectionState.CONNECTED) {
42             counter = 2;
43             semaphore.release();
44         } else if (counter == 1 && state == MqttConnectionState.DISCONNECTED) {
45             counter = 2;
46         }
47     }
48 }