]> git.basschouten.com Git - openhab-addons.git/blob
6badfa157485d052f1b0fb9b2718c12f2c5b3c31
[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.homematic.internal.communicator.virtual;
14
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.VIRTUAL_DATAPOINT_NAME_DELETE_DEVICE_MODE;
16
17 import java.io.IOException;
18
19 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
20 import org.openhab.binding.homematic.internal.model.HmDatapoint;
21 import org.openhab.binding.homematic.internal.model.HmDatapointConfig;
22 import org.openhab.binding.homematic.internal.model.HmDevice;
23 import org.openhab.binding.homematic.internal.model.HmInterface;
24 import org.openhab.binding.homematic.internal.model.HmValueType;
25
26 /**
27  * A virtual enum datapoint which holds the delete flag for deleting a device with the DELETE_DEVICE virtual datapoint.
28  * Falls back to LOCKED after 30 seconds to prevent accidental deletion.
29  *
30  * @author Gerhard Riegler - Initial contribution
31  */
32 public class DeleteDeviceModeVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
33     protected static final String MODE_LOCKED = "LOCKED";
34     protected static final String MODE_RESET = "RESET";
35     protected static final String MODE_FORCE = "FORCE";
36     protected static final String MODE_DEFER = "DEFER";
37     private static final int DELETE_MODE_DURATION = 30;
38
39     @Override
40     public String getName() {
41         return VIRTUAL_DATAPOINT_NAME_DELETE_DEVICE_MODE;
42     }
43
44     @Override
45     public void initialize(HmDevice device) {
46         if (!device.isGatewayExtras() && !(device.getHmInterface() == HmInterface.CUXD)) {
47             HmDatapoint dp = addDatapoint(device, 0, getName(), HmValueType.ENUM, 0, false);
48             dp.setOptions(new String[] { MODE_LOCKED, MODE_RESET, MODE_FORCE, MODE_DEFER });
49             dp.setMinValue(0);
50             dp.setMaxValue(dp.getOptions().length - 1);
51         }
52     }
53
54     @Override
55     public boolean canHandleCommand(HmDatapoint dp, Object value) {
56         return getName().equals(dp.getName());
57     }
58
59     @Override
60     public void handleCommand(VirtualGateway gateway, HmDatapoint dp, HmDatapointConfig dpConfig, Object value)
61             throws IOException, HomematicClientException {
62         dp.setValue(value);
63         if (!MODE_LOCKED.equals(dp.getOptionValue())) {
64             gateway.disableDatapoint(dp, DELETE_MODE_DURATION);
65         }
66     }
67 }