]> git.basschouten.com Git - openhab-addons.git/blob
8d46a391d9690bc154b266e1cb70b1bf7a1be5c3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.homematic.internal.communicator.virtual;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.io.IOException;
19
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
23 import org.openhab.binding.homematic.internal.misc.HomematicConstants;
24 import org.openhab.binding.homematic.internal.misc.MiscUtils;
25 import org.openhab.binding.homematic.internal.model.HmChannel;
26 import org.openhab.binding.homematic.internal.model.HmDatapoint;
27 import org.openhab.binding.homematic.internal.model.HmDevice;
28 import org.openhab.binding.homematic.internal.model.HmInterface;
29 import org.openhab.binding.homematic.internal.model.HmParamsetType;
30 import org.openhab.binding.homematic.internal.model.HmValueType;
31 import org.openhab.core.test.java.JavaTest;
32 import org.openhab.core.thing.CommonTriggerEvents;
33
34 /**
35  * Tests for {@link ButtonVirtualDatapointHandler}.
36  *
37  * @author Michael Reitler - Initial Contribution
38  *
39  */
40 public class ButtonDatapointTest extends JavaTest {
41
42     private static final int DISABLE_DATAPOINT_DELAY = 50;
43
44     private MockEventReceiver mockEventReceiver;
45     private final ButtonVirtualDatapointHandler bvdpHandler = new ButtonVirtualDatapointHandler();
46
47     @BeforeEach
48     public void setup() throws IOException {
49         this.mockEventReceiver = new MockEventReceiver();
50     }
51
52     @Test
53     public void testShortPress() throws IOException, HomematicClientException {
54         HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
55         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
56
57         mockEventReceiver.eventReceived(shortPressDp);
58
59         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
60     }
61
62     @Test
63     public void testLongPress() throws IOException, HomematicClientException {
64         HmDatapoint longPressDp = createPressDatapoint("PRESS_LONG", Boolean.TRUE);
65         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(longPressDp);
66
67         mockEventReceiver.eventReceived(longPressDp);
68
69         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.LONG_PRESSED));
70     }
71
72     @Test
73     public void testUnsupportedEvents() throws IOException, HomematicClientException {
74         HmDatapoint contPressDp = createPressDatapoint("PRESS_CONT", Boolean.TRUE);
75         HmDatapoint contButtonVirtualDatapoint = getButtonVirtualDatapoint(contPressDp);
76
77         mockEventReceiver.eventReceived(contPressDp);
78
79         HmDatapoint releaseDp = createPressDatapoint("PRESS_LONG_RELEASE", Boolean.TRUE);
80         HmDatapoint releaseButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
81
82         mockEventReceiver.eventReceived(releaseDp);
83
84         HmDatapoint crapDp = createPressDatapoint("CRAP", Boolean.TRUE);
85         HmDatapoint crapButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
86
87         mockEventReceiver.eventReceived(crapDp);
88
89         assertThat(contButtonVirtualDatapoint.getValue(), nullValue());
90         assertThat(releaseButtonVirtualDatapoint.getValue(), nullValue());
91         assertThat(crapButtonVirtualDatapoint.getValue(), nullValue());
92     }
93
94     @Test
95     public void testDoublePress() throws IOException, HomematicClientException, InterruptedException {
96         HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
97         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
98
99         mockEventReceiver.eventReceived(shortPressDp);
100         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
101
102         Thread.sleep(DISABLE_DATAPOINT_DELAY / 2);
103
104         shortPressDp.setValue(Boolean.TRUE);
105         mockEventReceiver.eventReceived(shortPressDp);
106         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.DOUBLE_PRESSED));
107
108         Thread.sleep(DISABLE_DATAPOINT_DELAY * 2);
109
110         shortPressDp.setValue(Boolean.TRUE);
111         mockEventReceiver.eventReceived(shortPressDp);
112         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
113     }
114
115     private HmDatapoint createPressDatapoint(String channelName, Object value) {
116         HmDatapoint pressDp = new HmDatapoint(channelName, "", HmValueType.ACTION, value, true, HmParamsetType.VALUES);
117         HmChannel hmChannel = new HmChannel(channelName, 1);
118         HmDevice device = new HmDevice("ABC12345", HmInterface.RF, "HM-MOCK", "mockid", "mockid", "mockfw");
119         hmChannel.setDevice(device);
120         device.addChannel(hmChannel);
121         hmChannel.addDatapoint(pressDp);
122         pressDp.setChannel(hmChannel);
123         bvdpHandler.initialize(device);
124
125         return pressDp;
126     }
127
128     private HmDatapoint getButtonVirtualDatapoint(HmDatapoint originalDatapoint) {
129         return originalDatapoint.getChannel().getDatapoints().stream()
130                 .filter(dp -> HomematicConstants.VIRTUAL_DATAPOINT_NAME_BUTTON.equals(dp.getName())).findFirst()
131                 .orElse(null);
132     }
133
134     /**
135      * Mock parts of {@linkplain org.openhab.binding.homematic.internal.communicator.AbstractHomematicGateway}
136      */
137     private class MockEventReceiver {
138
139         public void eventReceived(HmDatapoint dp) throws IOException, HomematicClientException {
140             if (bvdpHandler.canHandleEvent(dp)) {
141                 bvdpHandler.handleEvent(null, dp);
142             }
143             if (dp.isPressDatapoint() && MiscUtils.isTrueValue(dp.getValue())) {
144                 disableDatapoint(dp);
145             }
146         }
147
148         private void disableDatapoint(HmDatapoint dp) {
149             new Thread(() -> {
150                 try {
151                     Thread.sleep(DISABLE_DATAPOINT_DELAY);
152                     dp.setValue(Boolean.FALSE);
153                 } catch (InterruptedException e) {
154                 }
155             }).start();
156         }
157     }
158 }