]> git.basschouten.com Git - openhab-addons.git/blob
bdb1a15969a16f21e051b8dc01676ebb8f5612dc
[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.binding.satel.internal.command;
14
15 import java.util.BitSet;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18
19 import org.apache.commons.lang.ArrayUtils;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.satel.internal.event.EventDispatcher;
22 import org.openhab.binding.satel.internal.event.NewStatesEvent;
23 import org.openhab.binding.satel.internal.types.ControlType;
24
25 /**
26  * Command class for commands that control (change) state of Integra objects,
27  * like partitions (arm, disarm), zones (bypass, unbypass) outputs (on, off,
28  * switch), etc.
29  *
30  * @author Krzysztof Goworek - Initial contribution
31  */
32 @NonNullByDefault
33 public class ControlObjectCommand extends ControlCommand {
34
35     private static final long REFRESH_DELAY = 1000;
36
37     private ControlType controlType;
38     private ScheduledExecutorService scheduler;
39
40     /**
41      * Creates new command class instance for specified type of control.
42      *
43      * @param controlType type of controlled objects
44      * @param objects bits that represents objects to control
45      * @param userCode code of the user on behalf the control is made
46      * @param scheduler scheduler object for scheduling refreshes
47      */
48     public ControlObjectCommand(ControlType controlType, byte[] objects, String userCode,
49             ScheduledExecutorService scheduler) {
50         super(controlType.getControlCommand(), ArrayUtils.addAll(userCodeToBytes(userCode), objects));
51         this.controlType = controlType;
52         this.scheduler = scheduler;
53     }
54
55     @Override
56     protected void handleResponseInternal(final EventDispatcher eventDispatcher) {
57         // force refresh states that might have changed
58         final BitSet newStates = controlType.getControlledStates();
59         if (!newStates.isEmpty()) {
60             // add delay to give a chance to process sent command
61             scheduler.schedule(() -> eventDispatcher.dispatchEvent(new NewStatesEvent(newStates)), REFRESH_DELAY,
62                     TimeUnit.MILLISECONDS);
63         }
64     }
65 }