]> git.basschouten.com Git - openhab-addons.git/blob
e27adb385e35014ffcbdf3d8fd1f7d0b69ff373b
[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.velux.internal.bridge;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.velux.internal.bridge.common.GetDeviceStatus;
17 import org.openhab.binding.velux.internal.bridge.common.RunProductDiscovery;
18 import org.openhab.binding.velux.internal.things.VeluxGwState;
19 import org.openhab.binding.velux.internal.things.VeluxGwState.VeluxGatewaySubState;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link VeluxBridgeDetectProducts} represents a complete set of transactions
25  * for temporary activation of device detection mode on the <B>Velux</B> bridge.
26  * <P>
27  * It therefore provides a method:
28  * <UL>
29  * <LI>{@link VeluxBridgeDetectProducts#detectProducts} for starting the detection.
30  * </UL>
31  *
32  * @see VeluxBridgeProvider
33  *
34  * @author Guenther Schreiner - Initial contribution
35  */
36 @NonNullByDefault
37 public class VeluxBridgeDetectProducts {
38     private final Logger logger = LoggerFactory.getLogger(VeluxBridgeDetectProducts.class);
39
40     // Class access methods
41
42     /**
43      * Login into bridge, start process to detect (new) products, loop until bridge is idle again and logout from bridge
44      * based on a well-prepared environment of a {@link VeluxBridgeProvider}.
45      *
46      * @param bridge Initialized Velux bridge handler.
47      * @return <b>success</b>
48      *         of type boolean describing the overall result of this interaction.
49      */
50     public boolean detectProducts(VeluxBridge bridge) {
51         logger.trace("detectProducts() called.");
52         boolean success = false;
53
54         logger.trace("detectProducts() About to activate detection.");
55         RunProductDiscovery bcp1 = bridge.bridgeAPI().runProductDiscovery();
56         if (!(bridge.bridgeCommunicate(bcp1)) || (bcp1.isCommunicationSuccessful())) {
57             while (true) {
58                 logger.trace("detectProducts() About to query detection status.");
59                 GetDeviceStatus bcp = bridge.bridgeAPI().getDeviceStatus();
60                 if (!(bridge.bridgeCommunicate(bcp)) || (bcp.isCommunicationSuccessful())) {
61                     logger.trace("detectProducts() finished with failure.");
62                     break;
63                 }
64                 VeluxGwState deviceStatus = bcp.getState();
65                 if (deviceStatus.getSubState() == (byte) VeluxGatewaySubState.GW_SS_P1.getStateValue()) {
66                     logger.trace("detectProducts() bridge is still busy.");
67                 } else if (deviceStatus.getSubState() == (byte) VeluxGatewaySubState.GW_SS_IDLE.getStateValue()) {
68                     logger.trace("detectProducts() bridge is idle again, now.");
69                     success = true;
70                     break;
71                 } else {
72                     logger.info("detectProducts() unknown devicestatus ({}) received.", deviceStatus);
73                 }
74             }
75         } else {
76             logger.trace("detectProducts() activate detection finished with failure.");
77         }
78
79         logger.debug("detectProducts() finished {}.", success ? "successfully" : "with failure");
80         return success;
81     }
82 }