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