]> git.basschouten.com Git - openhab-addons.git/blob
6018f6e1cff318670daf3c39b9982a3438b41103
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.imperihome.internal.action;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.openhab.core.events.EventPublisher;
17 import org.openhab.core.items.Item;
18 import org.openhab.core.items.events.ItemCommandEvent;
19 import org.openhab.core.items.events.ItemEventFactory;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.io.imperihome.internal.model.device.AbstractDevice;
22 import org.openhab.io.imperihome.internal.model.device.DeviceType;
23 import org.openhab.io.imperihome.internal.processor.DeviceRegistry;
24 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
25
26 /**
27  * Action to stop a shutter. Actually just sends an ON command to a linked switch device, to be handled by rules.
28  *
29  * @author Pepijn de Geus - Initial contribution
30  */
31 public class StopShutterAction extends Action {
32
33     private final DeviceRegistry deviceRegistry;
34
35     public StopShutterAction(EventPublisher eventPublisher, DeviceRegistry deviceRegistry) {
36         super(eventPublisher);
37         this.deviceRegistry = deviceRegistry;
38     }
39
40     @Override
41     public boolean supports(AbstractDevice device, Item item) {
42         return device.getType() == DeviceType.SHUTTER && StringUtils.isNotBlank(device.getLinks().get("stopper"));
43     }
44
45     @Override
46     public void perform(AbstractDevice device, Item item, String value) {
47         String modeDeviceName = device.getLinks().get("stopper");
48         AbstractDevice modeDevice = deviceRegistry.getDevice(ItemProcessor.getDeviceId(modeDeviceName));
49         if (modeDevice == null) {
50             logger.error("Couldn't resolve linked Stopper device '{}', make sure the Item has iss tags",
51                     modeDeviceName);
52             return;
53         }
54
55         Item modeItem = modeDevice.getItem();
56
57         ItemCommandEvent event = ItemEventFactory.createCommandEvent(modeItem.getName(), OnOffType.ON, COMMAND_SOURCE);
58         eventPublisher.post(event);
59     }
60 }