]> git.basschouten.com Git - openhab-addons.git/blob
a902b7b29f651b23073578ae27292d6d3500a156
[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.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.satel.internal.event.EventDispatcher;
19 import org.openhab.binding.satel.internal.event.IntegraStateEvent;
20 import org.openhab.binding.satel.internal.protocol.SatelMessage;
21 import org.openhab.binding.satel.internal.types.StateType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Command class for commands that return state of Integra objects, like
27  * partitions (armed, alarm, entry time), zones (violation, tamper, alarm),
28  * outputs and doors (opened, opened long).
29  *
30  * @author Krzysztof Goworek - Initial contribution
31  */
32 @NonNullByDefault
33 public class IntegraStateCommand extends SatelCommandBase {
34
35     private final Logger logger = LoggerFactory.getLogger(IntegraStateCommand.class);
36
37     private StateType stateType;
38
39     /**
40      * Constructs new command instance for specified type of state.
41      *
42      * @param stateType type of state
43      * @param extended if <code>true</code> command will be sent as extended (256 zones or outputs)
44      */
45     public IntegraStateCommand(StateType stateType, boolean extended) {
46         super(stateType.getRefreshCommand(), extended);
47         this.stateType = stateType;
48     }
49
50     /**
51      * @return <code>true</code> if current command is extended (256 zones/outputs)
52      */
53     public boolean isExtended() {
54         return Arrays.equals(EXTENDED_CMD_PAYLOAD, this.getPayload());
55     }
56
57     @Override
58     protected boolean isResponseValid(SatelMessage response) {
59         // validate response
60         if (response.getPayload().length != this.stateType.getPayloadLength(isExtended())) {
61             logger.debug("Invalid payload length for state type {}: {}", this.stateType, response.getPayload().length);
62             return false;
63         }
64         return true;
65     }
66
67     @Override
68     protected void handleResponseInternal(final EventDispatcher eventDispatcher) {
69         // dispatch event
70         eventDispatcher.dispatchEvent(
71                 new IntegraStateEvent(getResponse().getCommand(), getResponse().getPayload(), isExtended()));
72     }
73 }