2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.satel.internal.command;
15 import java.util.BitSet;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
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;
26 * Command class for commands that control (change) state of Integra objects,
27 * like partitions (arm, disarm), zones (bypass, unbypass) outputs (on, off,
30 * @author Krzysztof Goworek - Initial contribution
33 public class ControlObjectCommand extends ControlCommand {
35 private static final long REFRESH_DELAY = 1000;
37 private ControlType controlType;
38 private ScheduledExecutorService scheduler;
41 * Creates new command class instance for specified type of control.
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
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;
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);